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

move some functions from create_derived_agasc_h5.py #161

Merged
merged 15 commits into from
Jan 5, 2024
Merged

Conversation

javierggt
Copy link
Contributor

@javierggt javierggt commented Dec 13, 2023

Move some functions from create_derived_agasc_h5.py into the agasc module.

The objective is to be able to call agasc.write_agasc from my AGASC-Gaia scripts.

Description

Fixes #

Interface impacts

Testing

Unit tests

  • No unit tests
  • Mac
  • Linux
  • Windows

Independent check of unit tests by [REVIEWER NAME]

  • [PLATFORM]:

Functional tests

from agasc import write_agasc
from pathlib import Path
h5 = tables.open_file(Path(os.environ['SKA']) / 'data' / 'agasc' / 'agasc_1p7.h5')
t = h5.root.data[:1000]
write_agasc('test.h5', stars=t, version='1p9')

@javierggt javierggt requested a review from taldcroft December 13, 2023 20:59
@javierggt
Copy link
Contributor Author

I should have marked this as WIP. I just added another commit.

One question that is left is whether this is the right place for this. The stuff I'm adding might be better in a separate utils submodule.

@javierggt javierggt force-pushed the write-agasc branch 2 times, most recently from 17dae1c to 8c5e834 Compare December 14, 2023 17:41
Copy link
Member

@taldcroft taldcroft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks generally good. In additional to the inline comments, all the public functions need full doc strings. The non-public ones should at least have a single-line docstring with a simple description of what it is doing.

agasc/agasc.py Outdated
h5.root.healpix_index.attrs["nside"] = nside


DTYPE = np.dtype(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this more descriptive like TABLE_DTYPE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

agasc/agasc.py Outdated
)


class Order(Enum):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More descriptive, like TableOrder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

agasc/agasc.py Outdated
if stars.dtype != DTYPE:
cols = DTYPE.names
missing_keys = set(cols) - set(stars.dtype.names)
assert not missing_keys, f"Missing keys in stars: {missing_keys}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No assert in non-test modules. Do a test and raise a ValueError instead. FWIW running Python with the -OO flag strips out assert statements. Not that we do that, but I've just been reviewing a monster astropy PR that makes astropy work with -OO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Note: there was a trivial assert right after this, and I just removed it. My tendency would have been to keep this second assert, and let it be stripped by the -OO option.

agasc/agasc.py Outdated
write_healpix_index_table(filename, healpix_index, nside)


def write_agasc_(filename, stars, version):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Private methods have a leading underscore, not trailing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

from agasc import write_agasc

with (
tempfile.TemporaryDirectory() as tempdir,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Path(os.environ["SKA"]) / "data" / "agasc" / "agasc_1p7.h5"
) as h5_in,
):
t = Table(h5_in.root.data[:1000])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only line that needs the h5_in context, so out-dent all the rest to take them out of the context manager.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

agasc/agasc.py Outdated
Comment on lines 931 to 939
if order == Order.DEC:
logger.info("Sorting on DEC column")
idx_sort = np.argsort(stars["DEC"])
elif order == Order.HEALPIX:
logger.info(
f"Creating healpix_index table for nside={nside} "
"and sorting by healpix index"
)
healpix_index, idx_sort = get_healpix_index_table(stars, nside)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an opportunity to use the new switch statement in Python 3.10!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I understand it, the switch statement gains us nothing in terms of performance, and it actually makes the code slightly more verbose, so I'm not sure we want to use it.

Anyway, I changed it.

@javierggt javierggt requested a review from taldcroft January 4, 2024 16:23
Copy link
Member

@taldcroft taldcroft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice!

@javierggt javierggt merged commit b457c13 into master Jan 5, 2024
1 check passed
from agasc import write_agasc

with tables.open_file(
Path(os.environ["SKA"]) / "data" / "agasc" / "agasc_1p7.h5"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now failing on HEAD because the file in /proj/sot/ska/data/agasc is agasc1p7.h5 (no _). I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I happen to have a link with the _. I don't know why.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I couldn't remember the filename history to know if we wanted to make the link you have or update the test to match what we've got in /proj/sot/ska.

@javierggt javierggt mentioned this pull request Jan 11, 2024
@javierggt javierggt mentioned this pull request Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants