-
Notifications
You must be signed in to change notification settings - Fork 12
HDU -> FITS #1107
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
Merged
Merged
HDU -> FITS #1107
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
bd80d3e
extending database model to support hdulists rather than just primary…
rhayes777 fd6e604
add migration steps
rhayes777 44f1571
update scraper and search outputs to use fits
rhayes777 c9ad10c
remove code and fix
rhayes777 53542a8
Merge branch 'main' into feature/aggregate_hdu
rhayes777 b3281a5
slow test
rhayes777 1b35670
fits files for testing
rhayes777 242b6be
refactor
rhayes777 f1ebd10
conftest
rhayes777 cae6880
refactor
rhayes777 6023d84
extract function to obtain filename
rhayes777 9b1b7b2
fixes
rhayes777 dd82148
basic summarisation of fits
rhayes777 8d50c5d
improve API clarity
rhayes777 6355e11
output fits to file
rhayes777 31fccc3
docs
rhayes777 641bf1c
fix
rhayes777 55eb731
write to using file not name
rhayes777 5cfb486
generalise to enum
rhayes777 56721db
extra asssertion
rhayes777 885bba7
Merge branch 'main' into feature/aggregate_hdu
rhayes777 b182f21
fixes
rhayes777 1707c45
Merge branch 'feature/agg_image_fix' into feature/aggregate_hdu
rhayes777 922b18f
test custom enums work
rhayes777 a04623c
raise a ValueError for an empty aggregator
rhayes777 9ca692d
Merge branch 'feature/agg_image_fix' into feature/aggregate_hdu
rhayes777 0328093
add assertion
rhayes777 d200948
include fits when getting value from fit
rhayes777 1a69546
try bumping actions/cache version
rhayes777 f61f3e9
remove default + fixes
rhayes777 a27af17
optional list for naming aggregated images
rhayes777 09a8825
also for fits
rhayes777 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import re | ||
| from enum import Enum | ||
| from typing import List, Union | ||
|
|
||
| from astropy.io import fits | ||
| from pathlib import Path | ||
|
|
||
| from autofit.aggregator.search_output import SearchOutput | ||
| from autofit.aggregator import Aggregator | ||
|
|
||
|
|
||
| def subplot_filename(subplot: Enum) -> str: | ||
| subplot_type = subplot.__class__ | ||
| return ( | ||
| re.sub( | ||
| r"([A-Z])", | ||
| r"_\1", | ||
| subplot_type.__name__.replace("FITS", ""), | ||
| ) | ||
| .lower() | ||
| .lstrip("_") | ||
| ) | ||
|
|
||
|
|
||
| class FitFITS(Enum): | ||
| """ | ||
| The HDUs that can be extracted from the fit.fits file. | ||
| """ | ||
|
|
||
| ModelImage = "MODEL_IMAGE" | ||
| ResidualMap = "RESIDUAL_MAP" | ||
| NormalizedResidualMap = "NORMALIZED_RESIDUAL_MAP" | ||
| ChiSquaredMap = "CHI_SQUARED_MAP" | ||
|
|
||
|
|
||
| class AggregateFITS: | ||
| def __init__(self, aggregator: Aggregator): | ||
| """ | ||
| A class for extracting fits files from the aggregator. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| aggregator | ||
| The aggregator containing the fits files. | ||
| """ | ||
| if len(aggregator) == 0: | ||
| raise ValueError("The aggregator is empty.") | ||
|
|
||
| self.aggregator = aggregator | ||
|
|
||
| @staticmethod | ||
| def _hdus( | ||
| result: SearchOutput, | ||
| *hdus: Enum, | ||
| ) -> List[fits.ImageHDU]: | ||
| """ | ||
| Extract the HDUs from a given fits for a given search. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| result | ||
| The search output. | ||
| hdus | ||
| The HDUs to extract. | ||
|
|
||
| Returns | ||
| ------- | ||
| The extracted HDUs. | ||
| """ | ||
| row = [] | ||
| for hdu in hdus: | ||
| source = result.value(subplot_filename(hdu)) | ||
| source_hdu = source[source.index_of(hdu.value)] | ||
| row.append( | ||
| fits.ImageHDU( | ||
| data=source_hdu.data, | ||
| header=source_hdu.header, | ||
| ) | ||
| ) | ||
| return row | ||
|
|
||
| def extract_fits(self, *hdus: Enum) -> List[fits.HDUList]: | ||
| """ | ||
| Extract the HDUs from the fits files for every search in the aggregator. | ||
|
|
||
| Return the result as a list of HDULists. The first HDU in each list is an empty PrimaryHDU. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| hdus | ||
| The HDUs to extract. | ||
|
|
||
| Returns | ||
| ------- | ||
| The extracted HDUs. | ||
| """ | ||
| output = [fits.PrimaryHDU()] | ||
| for result in self.aggregator: | ||
| output.extend(self._hdus(result, *hdus)) | ||
|
|
||
| return fits.HDUList(output) | ||
|
|
||
| def output_to_folder( | ||
| self, | ||
| folder: Path, | ||
| *hdus: Enum, | ||
| name: Union[str, List[str]], | ||
| ): | ||
| """ | ||
| Output the fits files for every search in the aggregator to a folder. | ||
|
|
||
| Only include HDUs specific in the hdus argument. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| folder | ||
| The folder to output the fits files to. | ||
| hdus | ||
| The HDUs to output. | ||
| name | ||
| The name of the fits file. This is the attribute of the search output that is used to name the file. | ||
| OR a list of names for each HDU. | ||
| """ | ||
| folder.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| for i, result in enumerate(self.aggregator): | ||
| if isinstance(name, str): | ||
| output_name = getattr(result, name) | ||
| else: | ||
| output_name = name[i] | ||
|
|
||
| hdu_list = fits.HDUList( | ||
| [fits.PrimaryHDU()] | ||
| + self._hdus( | ||
| result, | ||
| *hdus, | ||
| ) | ||
| ) | ||
| with open(folder / f"{output_name}.fits", "wb") as file: | ||
| hdu_list.writeto(file) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need to be implemented for other fits files. For example, galaxy_images.fits can be included by defining GalaxyImagesFITS