From 3f7d5ae3dc8ad760e4dd97121edf38549c661bde Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Tue, 6 Aug 2024 14:00:10 +0100 Subject: [PATCH 1/3] Add test data generation scripts --- Makefile | 4 ++++ filegen/vector_columns.py | 33 +++++++++++++++++++++++++++++++++ testdata/vector_columns.fits | Bin 0 -> 8640 bytes 3 files changed, 37 insertions(+) create mode 100644 Makefile create mode 100755 filegen/vector_columns.py create mode 100644 testdata/vector_columns.fits diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..4983640d --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +gentestfiles: testdata/vector_columns.fits + +testdata/vector_columns.fits: filegen/vector_columns.py + python $< -o $@ diff --git a/filegen/vector_columns.py b/filegen/vector_columns.py new file mode 100755 index 00000000..9a14e622 --- /dev/null +++ b/filegen/vector_columns.py @@ -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) diff --git a/testdata/vector_columns.fits b/testdata/vector_columns.fits new file mode 100644 index 0000000000000000000000000000000000000000..587cb4631f2a6eddb1df7c475dd93fa74c5ee90a GIT binary patch literal 8640 zcmeH~-)`D46vn$h-E*9~TtY%GcEd#|p|S=6E#AbgCbJk8i8#n{rSd54GqvwDI|+)B z9K=*qsnYQUah#7YC%;(HcM>vNOe26HI0l6t{3*n`ka-EJfC-@ql!}Rz$<(3OOCv%T zWZB{ORr@h1*&b4+7^wY@yItDVI9!rMpE~R7Yd>+HuegL_eQ?flF3PMBWou&1q9u*u zafctZglA4Gvop0QLjp(u2_OL^fCP{L5_tavOq@)}JRX8KA~6j|)5x=)st2n~Fu4cC zR$C7ILDk{Zk{nOpG(QLbpP#Ed?z{8zjmhI%E;j0~$>ZrEiG$X6jmj@@`@x{YPwTtJ z;&%Qc);a!+eqkev`yK3=F8pPtEy<=J>JJ1-9%e96!G_3!j?f=UO*7)|G?9!gX`u a%aH(2kN^@u0!RP}AOR$R1dzZxA@C2*S2{re literal 0 HcmV?d00001 From 5fa0b833236640c39430581130168084780e4889 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Tue, 6 Aug 2024 14:00:40 +0100 Subject: [PATCH 2/3] add python environment for filegen code --- flake.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index caabaf32..17b48516 100644 --- a/flake.nix +++ b/flake.nix @@ -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 { @@ -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 ]; From 04a70ae64860d69a4b613dd4ce4ce42ca8e286aa Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Tue, 6 Aug 2024 14:01:33 +0100 Subject: [PATCH 3/3] Add test for reading vector columns --- fitsio/tests/test_vector_datatypes.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fitsio/tests/test_vector_datatypes.rs b/fitsio/tests/test_vector_datatypes.rs index f6125752..b4e2fde9 100644 --- a/fitsio/tests/test_vector_datatypes.rs +++ b/fitsio/tests/test_vector_datatypes.rs @@ -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 = table.read_col(&mut f, "target").unwrap(); + assert_eq!(string_data, &["NGC1001", "NGC1002", "NGC1003"]); + + let vector_data: Vec = table.read_col(&mut f, "index").unwrap(); + assert_eq!(vector_data, &[1, 2, 3, 4, 5, 6]); +}