Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: validate vector columns created by external tool #352

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gentestfiles: testdata/vector_columns.fits

testdata/vector_columns.fits: filegen/vector_columns.py
python $< -o $@
33 changes: 33 additions & 0 deletions filegen/vector_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python

"""
Generate a file with vector columns, to validate https://github.com/simonrw/rust-fitsio/pull/330
"""

import argparse
from pathlib import Path

import numpy as np
from astropy.io import fits


def gen_file() -> fits.BinTableHDU:
# https://docs.astropy.org/en/stable/io/fits/index.html#creating-a-new-table-file
a1 = np.array(["NGC1001", "NGC1002", "NGC1003"])
a2 = np.array([11.1, 12.3, 15.2])
a3 = np.array([[1, 2], [3, 4], [5, 6]])
col1 = fits.Column(name="target", format="20A", array=a1)
col2 = fits.Column(name="V_mag", format="E", array=a2)
col3 = fits.Column(name="index", format="2K", array=a3)
cols = fits.ColDefs([col1, col2, col3])
hdu = fits.BinTableHDU.from_columns(cols, name="info")
return hdu


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", required=True, type=Path)
args = parser.parse_args()

file = gen_file()
file.writeto(str(args.output), overwrite=True)
13 changes: 13 additions & 0 deletions fitsio/tests/test_vector_datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,16 @@ make_test!(
ColumnDataType::Double,
3.1415926535879323
);

// integration test using file generated by `filegen/vector_columns.py`
#[test]
fn read_vector_data_from_example_file() {
let mut f = FitsFile::open("../testdata/vector_columns.fits").unwrap();
let table = f.hdu("info").unwrap();

let string_data: Vec<String> = table.read_col(&mut f, "target").unwrap();
assert_eq!(string_data, &["NGC1001", "NGC1002", "NGC1003"]);

let vector_data: Vec<i32> = table.read_col(&mut f, "index").unwrap();
assert_eq!(vector_data, &[1, 2, 3, 4, 5, 6]);
}
9 changes: 8 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};

test-python = pkgs.python3.withPackages (ps: with ps; [
numpy
astropy
ipython
]);
in
{
devShells.default = pkgs.mkShell rec {
Expand All @@ -23,7 +29,8 @@
pkgs.cargo-nextest
pkgs.bacon
# for bin/test
pkgs.python3
# test-python
test-python
] ++ pkgs.lib.optionals pkgs.stdenv.isLinux [
pkgs.cargo-tarpaulin
];
Expand Down
Binary file added testdata/vector_columns.fits
Binary file not shown.
Loading