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

WIP JP-3610: Memory usage with Detector1 pipeline #8588

Closed
wants to merge 87 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
994e729
running time and memory usage substantial improvement
penaguerrero Jun 21, 2024
a1c088b
added description of PR
penaguerrero Jun 21, 2024
048414b
updated docs
penaguerrero Jun 21, 2024
d53d1fa
removed unused code
penaguerrero Jun 21, 2024
1bf6096
addressing comments in PR
penaguerrero Jun 21, 2024
0828bc3
Merge branch 'master' into speedupemicorr
penaguerrero Jun 21, 2024
c59656b
adding a missing comma
penaguerrero Jun 21, 2024
2c899be
Merge branch 'master' into speedupemicorr
penaguerrero Jun 25, 2024
db29e55
Merge branch 'master' into speedupemicorr
penaguerrero Jun 25, 2024
083705c
removing copies and adding optional model class as input
penaguerrero Jun 25, 2024
3266e39
Merge branch 'master' into speedupemicorr
penaguerrero Jun 25, 2024
0b16f54
silly mistake
penaguerrero Jun 25, 2024
c3a6084
Merge branch 'master' into speedupemicorr
penaguerrero Jun 26, 2024
84ea302
Merge branch 'master' into speedupemicorr
penaguerrero Jun 26, 2024
3a3ccf2
Merge branch 'master' into speedupemicorr
penaguerrero Jun 26, 2024
5b42065
returning some code to prevent input to be modified in-place
penaguerrero Jun 26, 2024
fdefa18
Merge branch 'master' into speedupemicorr
penaguerrero Jun 26, 2024
4b5bee7
Merge branch 'master' into speedupemicorr
penaguerrero Jun 28, 2024
6922f4b
Merge branch 'master' into speedupemicorr
penaguerrero Jul 3, 2024
c1ed7c0
added step class attribute to avoid pile up of memory
penaguerrero Jul 3, 2024
c0a4a12
removed print statement
penaguerrero Jul 3, 2024
bc37ddb
fixed silly bug
penaguerrero Jul 3, 2024
87c6bd6
removed unused code
penaguerrero Jul 3, 2024
8cc1a01
removed unused code
penaguerrero Jul 3, 2024
b404403
Merge branch 'master' into speedupemicorr
penaguerrero Jul 3, 2024
3617d47
rename of variable
penaguerrero Jul 3, 2024
17dfc33
removed Step attribute modify_input
penaguerrero Jul 8, 2024
3905694
removed the close statement
penaguerrero Jul 8, 2024
3d19630
removed the close statement
penaguerrero Jul 8, 2024
0f1549c
fixing tests
penaguerrero Jul 8, 2024
1bdc7de
Merge branch 'master' into speedupemicorr
penaguerrero Jul 8, 2024
c65cbee
fixing tests
penaguerrero Jul 8, 2024
2f41a6a
removed the copy since this is now handled in ipc_step.py
penaguerrero Jul 8, 2024
0209356
run test on copy of datamodel
penaguerrero Jul 8, 2024
5633f90
fixed silly mistake
penaguerrero Jul 8, 2024
43deb58
now returning an open model
penaguerrero Jul 8, 2024
11c24df
removing with statement to open datamodel
penaguerrero Jul 9, 2024
1f6a54a
Merge branch 'master' into speedupemicorr
penaguerrero Jul 10, 2024
54083e1
moved change to unreleased notes
penaguerrero Jul 10, 2024
4223b90
Merge branch 'master' into speedupemicorr
penaguerrero Jul 11, 2024
4425d81
Merge branch 'master' into speedupemicorr
penaguerrero Jul 15, 2024
3956bd4
Merge branch 'master' into speedupemicorr
penaguerrero Jul 15, 2024
dedc920
Merge branch 'master' into speedupemicorr
penaguerrero Jul 15, 2024
344f5dd
Merge branch 'master' into speedupemicorr
penaguerrero Jul 16, 2024
4c9aada
Merge branch 'master' into speedupemicorr
penaguerrero Jul 17, 2024
111ee46
addressing PR comments
penaguerrero Jul 17, 2024
c2a8f1d
fixing emicorr test
penaguerrero Jul 17, 2024
f484e0f
Merge branch 'master' into speedupemicorr
penaguerrero Jul 17, 2024
372a8e3
fixing tests
penaguerrero Jul 17, 2024
82e6b9d
Merge branch 'master' into speedupemicorr
penaguerrero Jul 17, 2024
f739847
Merge branch 'master' into speedupemicorr
penaguerrero Jul 17, 2024
a3386fd
Merge branch 'master' into speedupemicorr
penaguerrero Jul 18, 2024
f40ddbe
Merge branch 'master' into speedupemicorr
penaguerrero Jul 18, 2024
f75e8e6
Merge branch 'master' into speedupemicorr
penaguerrero Jul 18, 2024
5f8f7c0
Merge branch 'master' into speedupemicorr
penaguerrero Jul 24, 2024
22c3721
Merge branch 'master' into speedupemicorr
penaguerrero Jul 25, 2024
284f12a
making sure both types are float
penaguerrero Jul 25, 2024
2c21155
Merge branch 'master' into speedupemicorr
penaguerrero Jul 29, 2024
b7d8fc2
Merge branch 'master' into speedupemicorr
penaguerrero Sep 3, 2024
1089563
Merge branch 'master' into speedupemicorr
penaguerrero Sep 6, 2024
71e515c
Merge branch 'master' into speedupemicorr
penaguerrero Sep 9, 2024
e0e1c45
Merge branch 'master' into speedupemicorr
penaguerrero Sep 11, 2024
5fe7be2
Merge branch 'master' into speedupemicorr
penaguerrero Sep 16, 2024
ca6daea
Merge branch 'master' into speedupemicorr
penaguerrero Sep 17, 2024
65c43ab
Merge branch 'master' into speedupemicorr
penaguerrero Sep 18, 2024
2bbefef
fixing conflict
penaguerrero Sep 18, 2024
9c76724
Merge branch 'master' into speedupemicorr
penaguerrero Sep 20, 2024
0075370
Merge branch 'master' into speedupemicorr
penaguerrero Sep 20, 2024
580e754
reverting removing important copy
penaguerrero Sep 20, 2024
33c46d1
fixing silly mistake
penaguerrero Sep 20, 2024
921323c
trying with statement
penaguerrero Sep 23, 2024
0abea5c
trying with statement
penaguerrero Sep 23, 2024
ea24a87
trying with statement
penaguerrero Sep 23, 2024
8c4e9fe
Merge branch 'master' into speedupemicorr
penaguerrero Sep 23, 2024
d6ccdf5
fixing rebase of with statement
penaguerrero Sep 23, 2024
df9b17b
removing unused import
penaguerrero Sep 23, 2024
90596b2
Merge branch 'master' into speedupemicorr
penaguerrero Sep 23, 2024
c1479ae
fixing rebase
penaguerrero Sep 23, 2024
4ec43b8
adding towncirer changes
penaguerrero Sep 23, 2024
b39de6e
move change log entry to towncrier fragment
zacharyburnett Sep 23, 2024
efd3c85
minor changes
penaguerrero Sep 23, 2024
680cd6f
Merge branch 'main' into speedupemicorr
penaguerrero Sep 23, 2024
b44e57b
Merge branch 'main' into speedupemicorr
penaguerrero Sep 24, 2024
e317fac
Merge branch 'main' into speedupemicorr
penaguerrero Sep 24, 2024
bb338ee
improoving garbage collect and mem usage
penaguerrero Sep 24, 2024
1ff1d0d
fix potential type error for test
penaguerrero Sep 24, 2024
efa083c
Merge branch 'main' into speedupemicorr
penaguerrero Sep 25, 2024
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
1 change: 1 addition & 0 deletions changes/8588.general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Created two functions to workaround the memory increase due to datamodel.open. The new functions in the basic_utils script are use_datamodels and copy_datamodel. The first function determines if it is necessary to open a datamodel and returns it or simply returns the datamodel given as input. The second function makes a copy of the datamodel depending on whether the step is being run as part of a pipeline or not. Both functions are now called in all steps of the Detector1 pipeline.
2 changes: 2 additions & 0 deletions jwst/charge_migration/charge_migration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Module for charge migration
#
import gc
import logging
import numpy as np

Expand Down Expand Up @@ -45,6 +46,7 @@ def charge_migration(output_model, signal_threshold):
# Save the flags in the output GROUPDQ array
output_model.groupdq = gdq_new

gc.collect()
return output_model


Expand Down
18 changes: 10 additions & 8 deletions jwst/charge_migration/charge_migration_step.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#! /usr/bin/env python
import gc
import logging

from stdatamodels.jwst import datamodels
from ..stpipe import Step

from . import charge_migration
from jwst.lib.basic_utils import use_datamodel, copy_datamodel

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
Expand All @@ -24,24 +26,24 @@
skip = boolean(default=True)
"""

def process(self, step_input):
def process(self, input_model):

# Open the input data model
with datamodels.RampModel(step_input) as input_model:
with use_datamodel(input_model, model_class=datamodels.RampModel) as input_model:

if (input_model.data.shape[1] < 3): # skip step if only 1 or 2 groups/integration
log.info('Too few groups per integration; skipping charge_migration')
result, input_model = copy_datamodel(input_model, self.parent)

input_model.meta.cal_step.charge_migration = 'SKIPPED'
return input_model
if (result.data.shape[1] < 3): # skip step if only 1 or 2 groups/integration
log.info('Too few groups per integration; skipping charge_migration')

# Work on a copy
result = input_model.copy()
result.meta.cal_step.charge_migration = 'SKIPPED'
return result

# Retrieve the parameter value(s)
signal_threshold = self.signal_threshold

result = charge_migration.charge_migration(result, signal_threshold)
result.meta.cal_step.charge_migration = 'COMPLETE'

gc.collect()

Check warning on line 48 in jwst/charge_migration/charge_migration_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/charge_migration/charge_migration_step.py#L48

Added line #L48 was not covered by tests
return result
21 changes: 12 additions & 9 deletions jwst/dark_current/dark_current_step.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import gc
from stdatamodels.jwst import datamodels

from ..stpipe import Step
from stcal.dark_current import dark_sub
from jwst.lib.basic_utils import use_datamodel, copy_datamodel
import numpy as np


Expand All @@ -23,24 +25,24 @@

reference_file_types = ['dark']

def process(self, step_input):
def process(self, input_model):

# Open the input data model
with datamodels.RampModel(step_input) as input_model:
with use_datamodel(input_model, model_class=datamodels.RampModel) as input_model:

result, input_model = copy_datamodel(input_model, self.parent)

# Get the name of the dark reference file to use
self.dark_name = self.get_reference_file(input_model, 'dark')
self.dark_name = self.get_reference_file(result, 'dark')
self.log.info('Using DARK reference file %s', self.dark_name)

# Check for a valid reference file
if self.dark_name == 'N/A':
self.log.warning('No DARK reference file found')
self.log.warning('Dark current step will be skipped')
input_model.meta.cal_step.dark = 'SKIPPED'
return input_model

# Work on a copy
result = input_model.copy()
result.meta.cal_step.dark = 'SKIPPED'
gc.collect()
return result

Check warning on line 45 in jwst/dark_current/dark_current_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/dark_current/dark_current_step.py#L43-L45

Added lines #L43 - L45 were not covered by tests

# Create name for the intermediate dark, if desired.
dark_output = self.dark_output
Expand Down Expand Up @@ -77,7 +79,8 @@
# Cleanup
del dark_model
del result


gc.collect()
return out_ramp

def set_average_dark_current(self, input_model, dark_model):
Expand Down
18 changes: 10 additions & 8 deletions jwst/dq_init/dq_init_step.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#! /usr/bin/env python
import gc
from stdatamodels.jwst import datamodels

from ..stpipe import Step
from . import dq_initialization
from jwst.lib.basic_utils import use_datamodel, copy_datamodel


__all__ = ["DQInitStep"]
Expand Down Expand Up @@ -41,7 +43,7 @@

# Try to open the input as a regular RampModel
try:
input_model = datamodels.RampModel(step_input)
input_model = use_datamodel(step_input, model_class=datamodels.RampModel)
# Check to see if it's Guider raw data
if input_model.meta.exposure.type in dq_initialization.guider_list:
# Reopen as a GuiderRawModel
Expand All @@ -61,19 +63,18 @@
self.log.error("Can't open input")
raise

result, input_model = copy_datamodel(input_model, self.parent)

# Retrieve the mask reference file name
self.mask_filename = self.get_reference_file(input_model, 'mask')
self.mask_filename = self.get_reference_file(result, 'mask')
self.log.info('Using MASK reference file %s', self.mask_filename)

# Check for a valid reference file
if self.mask_filename == 'N/A':
self.log.warning('No MASK reference file found')
self.log.warning('DQ initialization step will be skipped')
input_model.meta.cal_step.dq_init = 'SKIPPED'
return input_model

# Work on a copy
result = input_model.copy()
result.meta.cal_step.dq_init = 'SKIPPED'
return result

Check warning on line 77 in jwst/dq_init/dq_init_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/dq_init/dq_init_step.py#L76-L77

Added lines #L76 - L77 were not covered by tests

# Load the reference file
mask_model = datamodels.MaskModel(self.mask_filename)
Expand All @@ -82,7 +83,8 @@
result = dq_initialization.correct_model(result, mask_model)

# Cleanup
del step_input
del mask_model
del input_model
gc.collect()

return result
2 changes: 2 additions & 0 deletions jwst/dq_init/dq_initialization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import gc

import numpy as np

Expand Down Expand Up @@ -81,6 +82,7 @@ def do_dqinit(output_model, mask_model):

output_model.meta.cal_step.dq_init = 'COMPLETE'

gc.collect()
return output_model


Expand Down
21 changes: 11 additions & 10 deletions jwst/emicorr/emicorr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import logging
import gc
from astropy.stats import sigma_clipped_stats as scs
from stdatamodels.jwst import datamodels

Expand Down Expand Up @@ -176,6 +177,7 @@ def apply_emicorr(output_model, emicorr_model,
readpatt = output_model.meta.exposure.readpatt
xsize = output_model.meta.subarray.xsize # SUBSIZE1 keyword
xstart = output_model.meta.subarray.xstart # SUBSTRT1 keyword

# get the number of samples, 10us sample times per pixel (1 for fastmode, 9 for slowmode)
nsamples = output_model.meta.exposure.nsamples

Expand Down Expand Up @@ -438,25 +440,24 @@ def apply_emicorr(output_model, emicorr_model,
# This is the phase matched noise model to subtract from each pixel of the input image
dd_noise = lut[(phaseall * period_in_pixels).astype(int)]

# Interleave (straight copy) into 4 amps
noise = np.zeros((nints, ngroups, ny, nx)) # same size as input data
noise_x = np.arange(nx4) * 4
for k in range(4):
noise[:, :, :, noise_x + k] = dd_noise

# Safety catch; anywhere the noise value is not finite, set it to zero
noise[~np.isfinite(noise)] = 0.0
dd_noise[~np.isfinite(dd_noise)] = 0.0

# Subtract EMI noise from the input data
log.info('Subtracting EMI noise from data')
output_model.data = output_model.data - noise

# Interleave (straight copy) into 4 amps
noise_x = np.arange(nx4) * 4
for k in range(4):
output_model.data[..., noise_x + k] = output_model.data[..., noise_x + k] - dd_noise

# clean up
del data
del dd_all
del times_this_int
del phaseall
del noise
del dd_noise
gc.collect()

if save_intermediate_results and save_onthefly_reffile is not None:
if 'FAST' in readpatt:
Expand All @@ -471,7 +472,7 @@ def apply_emicorr(output_model, emicorr_model,
}
freq_pa_dict['subarray_cases'] = on_the_fly_subarr_case
mk_reffile(freq_pa_dict, save_onthefly_reffile)

return output_model


Expand Down
24 changes: 13 additions & 11 deletions jwst/emicorr/emicorr_step.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#! /usr/bin/env python

import gc
from stdatamodels.jwst import datamodels
from ..stpipe import Step
from . import emicorr
from jwst.lib.basic_utils import use_datamodel, copy_datamodel


__all__ = ["EmiCorrStep"]
Expand All @@ -28,27 +30,26 @@

reference_file_types = ['emicorr']

def process(self, step_input):
def process(self, input_model):

# Open the input data model
with datamodels.open(step_input) as input_model:
with use_datamodel(input_model, model_class=datamodels.RampModel) as input_model:

Check warning on line 36 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L36

Added line #L36 was not covered by tests

result, input_model = copy_datamodel(input_model, self.parent)

Check warning on line 38 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L38

Added line #L38 was not covered by tests

# Catch the cases to skip
instrument = input_model.meta.instrument.name
instrument = result.meta.instrument.name

Check warning on line 41 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L41

Added line #L41 was not covered by tests
if instrument != 'MIRI':
self.log.warning('EMI correction not implemented for instrument: {}'.format(instrument))
input_model.meta.cal_step.emicorr = 'SKIPPED'
return input_model
result.meta.cal_step.emicorr = 'SKIPPED'
return result

Check warning on line 45 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L44-L45

Added lines #L44 - L45 were not covered by tests

readpatt = input_model.meta.exposure.readpatt
readpatt = result.meta.exposure.readpatt

Check warning on line 47 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L47

Added line #L47 was not covered by tests
allowed_readpatts = ['FAST', 'FASTR1', 'SLOW', 'SLOWR1']
if readpatt.upper() not in allowed_readpatts:
self.log.warning('EMI correction not implemented for read pattern: {}'.format(readpatt))
input_model.meta.cal_step.emicorr = 'SKIPPED'
return input_model

# Work on a copy
result = input_model.copy()
result.meta.cal_step.emicorr = 'SKIPPED'
return result

Check warning on line 52 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L51-L52

Added lines #L51 - L52 were not covered by tests

# Setup parameters
pars = {
Expand Down Expand Up @@ -103,4 +104,5 @@
# Cleanup
del emicorr_model

gc.collect()

Check warning on line 107 in jwst/emicorr/emicorr_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/emicorr/emicorr_step.py#L107

Added line #L107 was not covered by tests
return result
26 changes: 13 additions & 13 deletions jwst/firstframe/firstframe_step.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gc
from ..stpipe import Step
from . import firstframe_sub
from stdatamodels.jwst import datamodels
from jwst.lib.basic_utils import use_datamodel, copy_datamodel


__all__ = ["FirstFrameStep"]
Expand All @@ -18,23 +19,22 @@ class FirstFrameStep(Step):
spec = """
"""

def process(self, step_input):
def process(self, input_model):

# Open the input data model
with datamodels.open(step_input) as input_model:
with use_datamodel(input_model) as input_model:

result, input_model = copy_datamodel(input_model, self.parent)

# check the data is MIRI data
detector = input_model.meta.instrument.detector.upper()
if detector[:3] != 'MIR':
detector = result.meta.instrument.detector.upper()
if detector[:3] == 'MIR':
# Do the firstframe correction subtraction
result = firstframe_sub.do_correction(result)
else:
self.log.warning('First Frame Correction is only for MIRI data')
self.log.warning('First frame step will be skipped')
input_model.meta.cal_step.firstframe = 'SKIPPED'
return input_model

# Cork on a copy
result = input_model.copy()

# Do the firstframe correction subtraction
result = firstframe_sub.do_correction(result)
result.meta.cal_step.firstframe = 'SKIPPED'

gc.collect()
return result
2 changes: 2 additions & 0 deletions jwst/firstframe/firstframe_sub.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#
# Module for the firstframe correction for MIRI science data sets
#
import gc
import numpy as np
import logging

Expand Down Expand Up @@ -45,4 +46,5 @@ def do_correction(output):
log.warning("Step will be skipped")
output.meta.cal_step.firstframe = 'SKIPPED'

gc.collect()
return output
2 changes: 2 additions & 0 deletions jwst/gain_scale/gain_scale.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
import numpy as np
import logging

Expand Down Expand Up @@ -44,4 +45,5 @@ def do_correction(output_model, gain_factor):
output_model.meta.exposure.gain_factor = gain_factor
output_model.meta.cal_step.gain_scale = 'COMPLETE'

gc.collect()
return output_model
Loading
Loading