Skip to content

Commit

Permalink
Updating style checks using ruff.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmacdonald-stsci committed Jan 10, 2025
1 parent 74821de commit 8fd4bca
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
51 changes: 27 additions & 24 deletions src/stcal/jump/jump.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# jump.py - detect cosmic ray jumps and their side effects like
# snowballs and showers.

import logging
import multiprocessing
import time
Expand All @@ -19,7 +22,8 @@

def detect_jumps_data(jump_data):
"""
This is the high-level controlling routine for the jump detection process.
Detect jumps and their side effects, such as showers and snowballs.
It loads and sets the various input data and parameters needed by each of
the individual detection methods and then calls the detection methods in
turn.
Expand Down Expand Up @@ -125,7 +129,7 @@ def detect_jumps_data(jump_data):

def twopoint_diff_multi(jump_data, twopt_params, data, gdq, readnoise_2d, n_slices):
"""
Implements multiprocessing for jump detection.
Split data for jump detection multiprocessing.
Parameters
----------
Expand Down Expand Up @@ -322,7 +326,7 @@ def slice_data(twopt_params, data, gdq, readnoise_2d, n_slices):

def setup_pdq(jump_data):
"""
Prepares the pixel DQ array for procesing, removing invalid data.
Prepare the pixel DQ array for procesing, removing invalid data.
Paramter
--------
Expand All @@ -343,8 +347,7 @@ def setup_pdq(jump_data):

def flag_large_events(gdq, jump_flag, sat_flag, jump_data):
"""
This routine controls the creation of expanded regions that are flagged as
jumps.
Control the creation of expanded regions that are flagged as jumps.
These events are called snowballs for the NIR. While they are most commonly
circular, there are elliptical ones. This routine does not handle the
Expand Down Expand Up @@ -424,7 +427,7 @@ def flag_large_events(gdq, jump_flag, sat_flag, jump_data):

def extend_saturation(cube, grp, sat_ellipses, jump_data, persist_jumps):
"""
Extend the saturated ellipses that are larger than the min_sat_radius
Extend the saturated ellipses that are larger than the min_sat_radius.
Parameter
---------
Expand Down Expand Up @@ -702,9 +705,10 @@ def point_inside_ellipse(point, ellipse):

def near_edge(jump, low_threshold, high_threshold):
"""
This routing tests whether the center of a jump is close to the edge of
the detector. Jumps that are within the threshold will not require a
saturated core since this may be off the detector
Test whether the center of a jump is close to the edge of the detector.
Jumps that are within the threshold will not require a saturated core
since this may be off the detector
Parameter
---------
Expand Down Expand Up @@ -734,7 +738,7 @@ def near_edge(jump, low_threshold, high_threshold):
def find_faint_extended(
indata, ingdq, pdq, readnoise_2d, jump_data, min_diffs_for_shower=10):
"""
Flagging showers.
Flag groups based on showers detected.
Parameters
----------
Expand Down Expand Up @@ -846,7 +850,7 @@ def find_faint_extended(

def max_flux_showers(jump_data, nints, indata, ingdq, gdq):
"""
Ensure that flagging showers didn't change final fluxes by more than the allowed amount
Ensure that flagging showers didn't change final fluxes by more than allowed.
Parameter
---------
Expand Down Expand Up @@ -935,7 +939,7 @@ def count_dnu_groups(gdq, jump_data):

def process_ellipses(ellipses, image, expand_by_ratio, expansion, jump_data):
"""
Draw ellipses onto an image
Draw ellipses onto an image.
Parameters
----------
Expand Down Expand Up @@ -971,12 +975,13 @@ def process_ellipses(ellipses, image, expand_by_ratio, expansion, jump_data):


def compute_axes(expand_by_ratio, ellipse, expansion, jump_data):
'''
Expand the ellipse by the expansion factor. The number of pixels added to
both axes is the number of pixels added to the minor axis. This prevents
very large flagged ellipses with high axis ratio ellipses. The major and
minor axis are not always the same index. Therefore, we have to test to
find which is actually the minor axis.
"""
Expand the ellipse by the expansion factor.
The number of pixels added to both axes is the number of pixels added
to the minor axis. This prevents very large flagged ellipses with high
axis ratio ellipses. The major and minor axis are not always the same
index. Therefore, we have to test to find which is actually the minor axis.
Parameters
----------
Expand All @@ -996,7 +1001,7 @@ def compute_axes(expand_by_ratio, ellipse, expansion, jump_data):
______
axes : tuple
Expanded and rounded ellipse axes.
'''
"""
if expand_by_ratio:
if ellipse[1][1] < ellipse[1][0]:
axis1 = ellipse[1][0] + (expansion - 1.0) * ellipse[1][1]
Expand Down Expand Up @@ -1080,7 +1085,7 @@ def get_bigcontours(ratio, intg, grp, gdq, pdq, jump_data, ring_2D_kernel):

def diff_meddiff_int(intg, median_diffs, sigma, first_diffs_masked):
"""
Computes the SNR ratio of each difference.
Compute the SNR ratio of each difference.
Parameters
----------
Expand Down Expand Up @@ -1194,7 +1199,7 @@ def nan_invalid_data(data, gdq, jump_data):

def find_first_good_group(int_gdq, do_not_use):
"""
Find first good group
Find first good group.
Parameter
---------
Expand Down Expand Up @@ -1224,9 +1229,7 @@ def find_first_good_group(int_gdq, do_not_use):

def calc_num_slices(n_rows, max_cores, max_available):
"""
Compute the number of data slices needed for multiprocessesing, based
on requested number of processes, available number of processes, and
the number of rows (the data is sliced by row).
Compute the number of data slices needed for multiprocessesing.
Parameter
---------
Expand Down
29 changes: 23 additions & 6 deletions src/stcal/jump/jump_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
DELIM = "-" * 80

class JumpData:
def __init__(
self, jump_model=None, gain2d=None, rnoise2d=None, dqflags=None
):
"""Contains data needed for detecting jumps."""

def __init__(self, jump_model=None, gain2d=None, rnoise2d=None, dqflags=None):
"""
Initialize JumpData instance.
jump_model : model
Input data model, assumed to be of type RampModel.
Expand Down Expand Up @@ -196,7 +198,7 @@ def __init__(

def init_arrays_from_model(self, jump_model):
"""
Sets arrays from a data model.
Set arrays from a data model.
jump_model : datamodel
A datamodel with certain expected parameters.
Expand All @@ -207,7 +209,7 @@ def init_arrays_from_model(self, jump_model):

def init_arrays_from_arrays(self, data, gdq, pdq):
"""
Sets arrays from a numpy arrays.
Set arrays from a numpy arrays.
Parameters
----------
Expand All @@ -226,7 +228,7 @@ def init_arrays_from_arrays(self, data, gdq, pdq):

def set_detection_settings(self, rej, _3grp, _4grp, mx, mn, _4flag):
"""
Set class instance detection settings
Set class instance detection settings.
Parameters
----------
Expand Down Expand Up @@ -402,6 +404,7 @@ def set_sigma_clipping_info(self, mingrps, minsig, useints):
# Print methods, primarily used for debugging.

def print_jump_data(self, fd=None):
"""Print jump_data information."""
self.print_jump_data_arrays(fd=fd)
self.print_jump_data_options(fd=fd)
self.print_jump_data_dqflags(fd=fd)
Expand All @@ -411,13 +414,15 @@ def print_jump_data(self, fd=None):
self.print_jump_data_internal_state(fd=fd)

Check warning on line 414 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L408-L414

Added lines #L408 - L414 were not covered by tests

def print_jump_data_arrays(self, pix_list=None, fd=None):
"""Print jump_data arrays."""
if fd is None:
print(self.get_jump_data_arrays())

Check warning on line 419 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L418-L419

Added lines #L418 - L419 were not covered by tests
else:
print(self.get_jump_data_arrays(), file=fd)

Check warning on line 421 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L421

Added line #L421 was not covered by tests


def get_jump_data_arrays(self):
"""Return string of jump_data information."""
oline = f"{DELIM}\n"
oline += "JumpData Arrays\n"
oline += f"Data shape = {self.data.shape}\n"
Expand All @@ -433,12 +438,14 @@ def get_jump_data_arrays(self):
return oline

Check warning on line 438 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L437-L438

Added lines #L437 - L438 were not covered by tests

def print_jump_data_options(self, fd=None):
"""Print jump_data options."""
if fd is None:
print(self.get_jump_data_options())

Check warning on line 443 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L442-L443

Added lines #L442 - L443 were not covered by tests
else:
print(self.get_jump_data_options(), file=fd)

Check warning on line 445 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L445

Added line #L445 was not covered by tests

def get_jump_data_options(self):
"""Return string of jump_data option."""
oline = f"{DELIM}\n"
oline += f"JumpData Options\n"
oline += f"nframes = {self.nframes}\n"
Expand All @@ -463,12 +470,14 @@ def get_jump_data_options(self):
return oline

Check warning on line 470 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L467-L470

Added lines #L467 - L470 were not covered by tests

def print_jump_data_dqflags(self, fd=None):
"""Print string of jump_data dqflags."""
if fd is None:
print(self.get_jump_data_dqflags())

Check warning on line 475 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L474-L475

Added lines #L474 - L475 were not covered by tests
else:
print(self.get_jump_data_dqflags(), file=fd)

Check warning on line 477 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L477

Added line #L477 was not covered by tests

def get_jump_data_dqflags(self):
"""Return string of jump_data dqflags."""
oline = f"{DELIM}\n"
oline += "DQ Flags\n"
oline += f"{INDENT}fl_good = {self.fl_good}\n"
Expand All @@ -481,12 +490,14 @@ def get_jump_data_dqflags(self):
return oline

Check warning on line 490 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L481-L490

Added lines #L481 - L490 were not covered by tests

def print_jump_data_snowball(self, fd=None):
"""Print string of jump_data snowball data."""
if fd is None:
print(self.get_jump_data_snowball())

Check warning on line 495 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L494-L495

Added lines #L494 - L495 were not covered by tests
else:
print(self.get_jump_data_snowball(), file=fd)

Check warning on line 497 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L497

Added line #L497 was not covered by tests

def get_jump_data_snowball(self):
"""Return string of jump_data snowball data."""
oline = f"{DELIM}\n"
oline += "Snowball Information\n"
oline += f"expand_large_events = {self.expand_large_events}\n"
Expand All @@ -502,12 +513,14 @@ def get_jump_data_snowball(self):
return oline

Check warning on line 513 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L501-L513

Added lines #L501 - L513 were not covered by tests

def print_jump_data_shower(self, fd=None):
"""Print string of jump_data shower data."""
if fd is None:
print(self.get_jump_data_shower())

Check warning on line 518 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L517-L518

Added lines #L517 - L518 were not covered by tests
else:
print(self.get_jump_data_shower(), file=fd)

Check warning on line 520 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L520

Added line #L520 was not covered by tests

def get_jump_data_shower(self):
"""Return string of jump_data shower data."""
oline = f"{DELIM}\n"
oline += "Shower Information\n"
oline += f"find_showers = {self.find_showers}\n"
Expand All @@ -522,12 +535,14 @@ def get_jump_data_shower(self):
return oline

Check warning on line 535 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L524-L535

Added lines #L524 - L535 were not covered by tests

def print_jump_data_sigma_clipping(self, fd=None):
"""Print string of jump_data sigma clipping data."""
if fd is None:
print(self.get_jump_data_sigma_clipping())

Check warning on line 540 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L539-L540

Added lines #L539 - L540 were not covered by tests
else:
print(self.get_jump_data_sigma_clipping(), file=fd)

Check warning on line 542 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L542

Added line #L542 was not covered by tests

def get_jump_data_sigma_clipping(self):
"""Return string of jump_data sigma clipping data."""
oline = f"{DELIM}\n"
oline += "Sigma Clipping\n"
oline += f"minimum_groups = {self.minimum_groups}\n"
Expand All @@ -537,12 +552,14 @@ def get_jump_data_sigma_clipping(self):
return oline

Check warning on line 552 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L546-L552

Added lines #L546 - L552 were not covered by tests

def print_jump_data_internal_state(self, fd=None):
"""Print string of jump_data internal state."""
if fd is None:
print(self.get_jump_data_internal_state())

Check warning on line 557 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L556-L557

Added lines #L556 - L557 were not covered by tests
else:
print(self.get_jump_data_internal_state(), file=fd)

Check warning on line 559 in src/stcal/jump/jump_class.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/jump/jump_class.py#L559

Added line #L559 was not covered by tests

def get_jump_data_internal_state(self):
"""Return string of jump_data internal state."""
oline = f"{DELIM}\n"
oline += "Internal State\n"
oline += f"grps_masked_after_shower = {self.grps_masked_after_shower}\n"
Expand Down
15 changes: 10 additions & 5 deletions src/stcal/jump/twopoint_difference.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

def find_crs(dataa, group_dq, read_noise, twopt_p):
"""
Detect jump due to cosmic rays using the two point difference method.
An interface between the detect_jumps_data function and the
find_crs_old function using the TwoPointParams class that makes
adding and removing parameters when using the two point
Expand Down Expand Up @@ -99,11 +101,12 @@ def find_crs_old(
min_diffs_single_pass=10,
):
"""
Find CRs/Jumps in each integration within the input data array. The input
data array is assumed to be in units of electrons, i.e. already multiplied
by the gain. We also assume that the read noise is in units of electrons.
We also assume that there are at least three groups in the integrations.
This was checked by jump_step before this routine is called.
Find CRs/Jumps in each integration within the input data array.
The input data array is assumed to be in units of electrons, i.e. already
multiplied by the gain. We also assume that the read noise is in units of
electrons. We also assume that there are at least three groups in the
integrations. This was checked by jump_step before this routine is called.
Parameters
----------
Expand Down Expand Up @@ -183,6 +186,7 @@ def find_crs_old(
min_diffs_single_pass: integer
The minimum number of groups to switch from the iterative flagging of
cosmic rays to just finding all the outliers at once.
Returns
-------
gdq : int, 4D array
Expand Down Expand Up @@ -526,6 +530,7 @@ def calc_med_first_diffs(in_first_diffs):
those three groups will be returned without any clipping. Finally, if
there are two usable groups, the group with the smallest absolute
difference will be returned.
Parameters
----------
in_first_diffs : array, float
Expand Down
14 changes: 14 additions & 0 deletions src/stcal/jump/twopoint_difference_class.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
class TwoPointParams:
"""Contains data needed for computing two point difference."""

def __init__(self, jump_data=None, copy_arrs=False):
"""
Initialize TwoPointParams instance.
Parameters
----------
jump_data : JumpData
Class containing parameters and methods to detect jumps.
copy_arrs : bool
Should the arrays be copied.
"""
if jump_data is not None:
self.normal_rej_thresh = jump_data.rejection_thresh

Expand Down Expand Up @@ -58,6 +71,7 @@ def __init__(self, jump_data=None, copy_arrs=False):
self.copy_arrs = copy_arrs

def __repr__(self):
"""Create __repr__ string."""
delim = "-" * 60
ostr = f"{delim}\n"
ostr += f"normal_rej_thresh = {self.normal_rej_thresh}\n"
Expand Down

0 comments on commit 8fd4bca

Please sign in to comment.