-
Notifications
You must be signed in to change notification settings - Fork 0
fix flatfield results, return correction matrix #4
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
906019f
fix flatfield results, return correction matrix
mikelangmayr ed105fc
update naming and README to be consitent
mikelangmayr 777efc2
add parameter to save corrected flat fits file
mikelangmayr 0cffce4
update README, add max_workers config property
mikelangmayr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -176,4 +176,4 @@ output/* | |
|
|
||
| .python-version | ||
|
|
||
| .DS_Store | ||
| .DS_Store | ||
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
14 changes: 9 additions & 5 deletions
14
src/keck_primitives/save_corrected.py → src/keck_primitives/save_correction.py
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 |
|---|---|---|
| @@ -1,18 +1,22 @@ | ||
| from keckdrpframework.models.arguments import Arguments | ||
| from keckdrpframework.primitives.base_primitive import BasePrimitive | ||
| from core.flat import save_corrected_fits | ||
| from core.flat import save_flat_fits | ||
|
|
||
|
|
||
| class SaveCorrectedFits(BasePrimitive): | ||
| class SaveFlatFits(BasePrimitive): | ||
| def __init__(self, action, context): | ||
| super().__init__(action, context) | ||
|
|
||
| def _perform(self, input_args: Arguments, config: dict) -> dict: | ||
| """Apply the flat correction to the original data and save as a new FITS file.""" | ||
| original_data = input_args["original_data"] | ||
| """Save flat field results to a FITS file. | ||
|
|
||
| By default saves the correction matrix. If original_data is provided, | ||
| saves the corrected image instead. | ||
| """ | ||
| correction = input_args["correction"] | ||
| header = input_args["header"] | ||
| output_path = input_args["output_path"] | ||
| original_data = input_args["original_data"] if "original_data" in input_args else None | ||
|
|
||
| result_path = save_corrected_fits(original_data, correction, header, output_path) | ||
| result_path = save_flat_fits(correction, header, output_path, original_data) | ||
| return {"output_path": result_path} |
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 |
|---|---|---|
|
|
@@ -4,30 +4,31 @@ | |
| from workflows.prefect_tasks.load_flat import load_flat_frame_task | ||
| from workflows.prefect_tasks.create_master_flat import create_master_flat_task | ||
| from workflows.prefect_tasks.trace_slits import trace_slits_task | ||
| from workflows.prefect_tasks.save_corrected import save_corrected_fits_task | ||
| from workflows.prefect_tasks.save_correction import save_flat_fits_task | ||
| from workflows.prefect_tasks.save_trace import save_trace_solution_task | ||
| from workflows.prefect_tasks.qa_plot import generate_qa_plot_task | ||
|
|
||
|
|
||
| @task(name="Process Single Flat Frame") | ||
| def process_single_flat_frame(flat_fits_path: str, output_dir: str): | ||
| def process_single_flat_frame(flat_fits_path: str, output_dir: str, save_corrected_flat: bool = False): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there is a use case to correct the flat image itself by the master_flat and save the corrected flat. As far as I know it is not needed for any of the DRP steps. We just need the master flat. |
||
| """ | ||
| Process a single LRIS2 flat FITS file through all DRP steps. | ||
|
|
||
| Args: | ||
| flat_fits_path: Path to input FITS file | ||
| output_dir: Output directory for results | ||
| save_corrected_flat: If True, also save the corrected flat image | ||
| """ | ||
| logger = get_run_logger() | ||
| filename = os.path.splitext(os.path.basename(flat_fits_path))[0] | ||
|
|
||
| # Construct output paths | ||
| corrected_output = os.path.join(output_dir, filename, "flat_corrected.fits") | ||
| correction_output = os.path.join(output_dir, filename, "flat_correction.fits") | ||
| trace_output = os.path.join(output_dir, filename, "slit_trace.txt") | ||
| qa_output = os.path.join(output_dir, filename, "flat_norm_qa.png") | ||
|
|
||
| # Ensure output dirs | ||
| os.makedirs(os.path.dirname(corrected_output), exist_ok=True) | ||
| os.makedirs(os.path.dirname(correction_output), exist_ok=True) | ||
|
|
||
| # Load FITS | ||
| logger.info(f"Loading {flat_fits_path}") | ||
|
|
@@ -50,35 +51,44 @@ def process_single_flat_frame(flat_fits_path: str, output_dir: str): | |
|
|
||
| # Save outputs | ||
| logger.info("Saving results") | ||
| save_corrected_fits_task(data, correction, header, corrected_output) | ||
| save_flat_fits_task(correction, header, correction_output) | ||
| if save_corrected_flat: | ||
| corrected_output = os.path.join(output_dir, filename, "flat_corrected.fits") | ||
| save_flat_fits_task(correction, header, corrected_output, original_data=data) | ||
| save_trace_solution_task(slit_positions, trace_output) | ||
| generate_qa_plot_task(correction, qa_output) | ||
|
|
||
| logger.info(f"Finished processing {flat_fits_path}") | ||
|
|
||
| @flow( | ||
| name="Batch Process LRIS2 Flats", | ||
| description="Process all flat frames using spectroscopic flat fielding", | ||
| task_runner=ConcurrentTaskRunner(max_workers=2), # You can adjust this | ||
| ) | ||
| def batch_process_all_flats(input_dir: str, output_dir: str): | ||
| def batch_process_all_flats(input_dir: str, output_dir: str, save_corrected_flat: bool = False, max_workers: int = 2): | ||
| """ | ||
| Process all FITS files in a directory using spectroscopic flat fielding. | ||
|
|
||
| Args: | ||
| input_dir: Directory containing input FITS files | ||
| output_dir: Directory for output files | ||
| save_corrected_flat: If True, also save the corrected flat image | ||
| max_workers: Number of files to process in parallel | ||
| """ | ||
| logger = get_run_logger() | ||
| @flow( | ||
| name="Batch Process LRIS2 Flats", | ||
| description="Process all flat frames using spectroscopic flat fielding", | ||
| task_runner=ConcurrentTaskRunner(max_workers=max_workers), | ||
| ) | ||
| def _batch_process_all_flats(input_dir: str, output_dir: str, save_corrected_flat: bool, max_workers: int): | ||
| logger = get_run_logger() | ||
|
|
||
| fits_files = [ | ||
| os.path.join(input_dir, f) | ||
| for f in os.listdir(input_dir) | ||
| if f.lower().endswith(".fits") | ||
| ] | ||
| logger.info(f"Found {len(fits_files)} FITS files in {input_dir}.") | ||
| logger.info(f"Settings: save_corrected_flat={save_corrected_flat}, max_workers={max_workers}") | ||
|
|
||
| fits_files = [ | ||
| os.path.join(input_dir, f) | ||
| for f in os.listdir(input_dir) | ||
| if f.lower().endswith(".fits") | ||
| ] | ||
| logger.info(f"Found {len(fits_files)} FITS files in {input_dir}.") | ||
| futures = [process_single_flat_frame.submit(fp, output_dir, save_corrected_flat) for fp in fits_files] | ||
|
|
||
| futures = [process_single_flat_frame.submit(fp, output_dir) for fp in fits_files] | ||
| for future in futures: | ||
| future.result() | ||
|
|
||
| for future in futures: | ||
| future.result() | ||
| return _batch_process_all_flats(input_dir, output_dir, save_corrected_flat, max_workers) | ||
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 was deleted.
Oops, something went wrong.
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,25 @@ | ||
| from prefect import task | ||
| from keckdrpframework.models.arguments import Arguments | ||
| from keck_primitives.save_correction import SaveFlatFits | ||
| from keck_primitives.utils import DummyAction, DummyContext | ||
|
|
||
|
|
||
| @task(name="Save Flat FITS") | ||
| def save_flat_fits_task(correction, header, output_path: str, original_data=None): | ||
| """Save flat field results to a FITS file. | ||
|
|
||
| By default saves the correction matrix. If original_data is provided, | ||
| saves the corrected image instead. | ||
| """ | ||
| args = Arguments() | ||
| args["correction"] = correction | ||
| args["header"] = header | ||
| args["output_path"] = output_path | ||
| if original_data is not None: | ||
| args["original_data"] = original_data | ||
|
|
||
| action = DummyAction(args=args) | ||
| context = DummyContext() | ||
|
|
||
| result = SaveFlatFits(action, context)._perform(args, config={}) | ||
| return result["output_path"] |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.