From 7a96aa8169492edb76a9c3f29c3279c8e18dd0ac Mon Sep 17 00:00:00 2001 From: Jonathan Niehof Date: Fri, 23 Oct 2026 16:10:09 -0600 Subject: [PATCH 1/4] Fix ruff checks for tests --- tests/codice/l3/lo/test_codice_lo_processor.py | 17 +++++++++++++---- tests/glows/l3a/test_utils.py | 2 +- tests/glows/test_glows_processor.py | 6 +++--- tests/hi/test_hi_processor.py | 2 +- tests/hit/l3/pha/test_pha_event_reader.py | 1 - tests/hit/l3/test_hit_processor.py | 2 +- tests/integration/integration_test_helpers.py | 4 ++-- tests/maps/test_map_combination.py | 2 +- .../test_rectangular_survival_probability.py | 2 +- tests/maps/test_spectral_fit.py | 3 --- .../swe/l3/science/test_moments_calculation.py | 2 -- tests/swe/l3/science/test_pitch_calculations.py | 1 - .../3S5_l1b_ram_maps/crap/new_patch_arc2.py | 4 ++-- .../3S5_l1b_ram_maps/csv2yaml.py | 2 +- .../3S5_l1b_ram_maps/new_patch_arc2.py | 4 ++-- .../3S5_l1b_ram_maps/new_patch_arc2_linear.py | 4 ++-- .../3S5_l1b_ram_maps/old/csv2yaml.py | 2 +- tests/ultra/test_ultra_processor.py | 1 + 18 files changed, 32 insertions(+), 29 deletions(-) diff --git a/tests/codice/l3/lo/test_codice_lo_processor.py b/tests/codice/l3/lo/test_codice_lo_processor.py index b35aee30..a3a162d6 100644 --- a/tests/codice/l3/lo/test_codice_lo_processor.py +++ b/tests/codice/l3/lo/test_codice_lo_processor.py @@ -1478,6 +1478,15 @@ def test_process_l3a_direct_events_handles_masked_priority_counts( dependencies=input_collection, input_metadata=input_metadata ) result = processor.process_l3a_direct_event_data_product(dependencies) + self.assertIsInstance(result, CodiceLoL3aDirectEventDataProduct) + self.assertEqual( + result.tof.shape, + ( + num_direct_event_epochs, + num_priorities, + priority_l1_spin_sectors, + ), + ) self.assertEqual(1, mock_calculate_normalization_factor.call_count) actual_stacked_priorities = mock_calculate_normalization_factor.call_args.args[ @@ -1664,10 +1673,10 @@ def test_process_l3a_direct_events_aligns_passthrough_vars_to_direct_event_epoch result_var[3], ) - self.assertTrue(np.all(result_var.mask[0] == False)) - self.assertTrue(np.all(result_var.mask[1] == True)) - self.assertTrue(np.all(result_var.mask[2] == False)) - self.assertTrue(np.all(result_var.mask[3] == False)) + self.assertFalse(np.any(result_var.mask[0])) + self.assertTrue(np.all(result_var.mask[1])) + self.assertFalse(np.any(result_var.mask[2])) + self.assertFalse(np.any(result_var.mask[3])) @patch(f"{MODULE}.rebin_3d_distribution_azimuth_to_elevation") @patch(f"{MODULE}.combine_priorities_for_species_and_calculate_count_uncertainty") diff --git a/tests/glows/l3a/test_utils.py b/tests/glows/l3a/test_utils.py index 5447384e..40819541 100644 --- a/tests/glows/l3a/test_utils.py +++ b/tests/glows/l3a/test_utils.py @@ -171,7 +171,7 @@ def test_fails_if_cdf_has_more_than_one_histogram(self): ) with self.assertRaises(AssertionError) as cm: - glows_l2_data: GlowsL2Data = read_l2_glows_data(cdf) + _ = read_l2_glows_data(cdf) self.assertEqual( ("Level 2 file should have only one histogram",), cm.exception.args ) diff --git a/tests/glows/test_glows_processor.py b/tests/glows/test_glows_processor.py index fc3de6c1..6aacbf94 100644 --- a/tests/glows/test_glows_processor.py +++ b/tests/glows/test_glows_processor.py @@ -1654,9 +1654,9 @@ def test_process_glows_l3d_drift(self, mock_save_data, _): actual_input_file_line = next( ( - l - for l in l3d_text_path.read_text().split("\n") - if l.startswith("# input files:") + L + for L in l3d_text_path.read_text().split("\n") + if L.startswith("# input files:") ), None, ) diff --git a/tests/hi/test_hi_processor.py b/tests/hi/test_hi_processor.py index 84d3d87e..db14915c 100644 --- a/tests/hi/test_hi_processor.py +++ b/tests/hi/test_hi_processor.py @@ -686,7 +686,7 @@ def test_raises_error_for_currently_unimplemented_maps(self): processor = HiProcessor(ProcessingInputCollection(), input_metadata) with self.assertRaises(NotImplementedError): - product = processor.process() + _ = processor.process() @patch( "imap_l3_processing.hi.hi_processor.HiSpectralIndexDependencies.fetch_dependencies" diff --git a/tests/hit/l3/pha/test_pha_event_reader.py b/tests/hit/l3/pha/test_pha_event_reader.py index 491b774a..2c006658 100644 --- a/tests/hit/l3/pha/test_pha_event_reader.py +++ b/tests/hit/l3/pha/test_pha_event_reader.py @@ -269,7 +269,6 @@ def test_read_all_pha_events_stops_reading_events_when_it_finds_all_zeros(self): event2 = create_raw_pha_event() event3 = create_raw_pha_event() - number_of_events = 33715 event_record_buffer = self._create_pha_event_binary(event1) event_record_buffer += self._create_pha_event_binary(event2) event_record_buffer += self._create_pha_event_binary(event3) diff --git a/tests/hit/l3/test_hit_processor.py b/tests/hit/l3/test_hit_processor.py index 3ae2e147..c406a28e 100644 --- a/tests/hit/l3/test_hit_processor.py +++ b/tests/hit/l3/test_hit_processor.py @@ -1057,7 +1057,7 @@ def test_process_direct_event_product( np.all(direct_event_product.pha_value[mask] == UNSIGNED_INT2_FILL_VALUE) ) self.assertTrue(np.all(np.isnan(direct_event_product.energy_at_detector[mask]))) - self.assertTrue(np.all(direct_event_product.is_low_gain[mask] == False)) + self.assertFalse(np.any(direct_event_product.is_low_gain[mask])) np.testing.assert_array_equal( direct_event_product.detector_flags, diff --git a/tests/integration/integration_test_helpers.py b/tests/integration/integration_test_helpers.py index a69a974e..137f236d 100644 --- a/tests/integration/integration_test_helpers.py +++ b/tests/integration/integration_test_helpers.py @@ -192,7 +192,7 @@ def __init__( valid_files.append(input_file) if isinstance(imap_input_file_path, SPICEFilePath): spice_file_paths.append(input_file) - except: + except Exception: continue self.input_files = valid_files @@ -220,7 +220,7 @@ def __enter__(self): for file_path in self.input_files: try: stage_input_file(file_path) - except: + except Exception: pass def __exit__(self, exc_type, exc_val, exc_tb): diff --git a/tests/maps/test_map_combination.py b/tests/maps/test_map_combination.py index ef06ad53..7a0dd1db 100644 --- a/tests/maps/test_map_combination.py +++ b/tests/maps/test_map_combination.py @@ -159,7 +159,7 @@ def _check_maps_match(self, combination: CombinationStrategy): else: try: combination._check_maps_match([map_1, map_with_difference]) - except: + except Exception: self.fail( f"Differences in other fields should be alright: {field.name}" ) diff --git a/tests/maps/test_rectangular_survival_probability.py b/tests/maps/test_rectangular_survival_probability.py index 1bc43d87..12321f32 100644 --- a/tests/maps/test_rectangular_survival_probability.py +++ b/tests/maps/test_rectangular_survival_probability.py @@ -541,7 +541,7 @@ def test_exposure_weighting_with_interpolated_survival_probabilities(self): * self.l1c_hi_dataset.exposure_times ) - sensor, expected_skygrid_elevation_index = Sensor.Hi90, 901 + sensor = Sensor.Hi90 pointing_set = RectangularSurvivalProbabilityPointingSet( self.l1c_hi_dataset, sensor, diff --git a/tests/maps/test_spectral_fit.py b/tests/maps/test_spectral_fit.py index 0494c9f2..d1e33823 100644 --- a/tests/maps/test_spectral_fit.py +++ b/tests/maps/test_spectral_fit.py @@ -136,9 +136,6 @@ def test_handles_small_fluxes(self): result_gamma_error, result_chi_square, ) = fit_arrays_to_power_law(flux, uncertainty, energies) - fitted_flux = result_a * np.power( - energies.reshape((-1, *spacial_dimension_shape)), -result_gamma - ) self.assertGreater(result_a, 0) def test_spectral_fit_map(self): diff --git a/tests/swe/l3/science/test_moments_calculation.py b/tests/swe/l3/science/test_moments_calculation.py index 8cb08ab1..822743b9 100644 --- a/tests/swe/l3/science/test_moments_calculation.py +++ b/tests/swe/l3/science/test_moments_calculation.py @@ -475,8 +475,6 @@ def test_fit_moments_retrying_on_failure_should_stop_retrying_with_few_energies_ ): density_history = np.array([100, 89, 72]) - high_density_value = 200 - calculated_moments = create_dataclass_mock(Moments, density=None) mock_calculate_fit_temp_dens_velocity.side_effect = [ calculated_moments, diff --git a/tests/swe/l3/science/test_pitch_calculations.py b/tests/swe/l3/science/test_pitch_calculations.py index b22ce6c6..82597593 100644 --- a/tests/swe/l3/science/test_pitch_calculations.py +++ b/tests/swe/l3/science/test_pitch_calculations.py @@ -76,7 +76,6 @@ def test_average_over_look_directions_with_zeroes(self): def test_look_direction(self): inst_az = np.array([[0, 90], [180, 270]]) inst_el = np.array([-90, 0, 90]) - shape = (2, 2, 3, 3) expected_look_direction = np.array( [ [ diff --git a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/crap/new_patch_arc2.py b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/crap/new_patch_arc2.py index e49c2ca8..d54b632d 100755 --- a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/crap/new_patch_arc2.py +++ b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/crap/new_patch_arc2.py @@ -194,8 +194,8 @@ def make_imap_lo_map( ny, nx = data.shape # Original grid centers - lats = np.linspace(-87, 87, ny) - lons = np.linspace(3, 357, nx) + lats = np.linspace(-87, 87, ny) # noqa: F841 + lons = np.linspace(3, 357, nx) # noqa: F841 # Build corner grid for pcolormesh lat_edges = np.linspace(-90, 90, ny + 1) diff --git a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/csv2yaml.py b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/csv2yaml.py index 7d9a653d..5ac0ff47 100755 --- a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/csv2yaml.py +++ b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/csv2yaml.py @@ -60,7 +60,7 @@ def csv_to_map_yaml( with open(csv_file, newline="") as f: reader = csv.reader(f) - header = next(reader) # 10–59, not used + header = next(reader) # 10–59, not used # noqa: F841 for row in reader: formatted_row = [ f"{float(x):.3E}" if float(x) != 0 else "0.000E+00" for x in row diff --git a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2.py b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2.py index 778de8fd..9a438088 100755 --- a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2.py +++ b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2.py @@ -203,8 +203,8 @@ def make_imap_lo_map( ny, nx = data.shape # Original grid centers - lats = np.linspace(-87, 87, ny) - lons = np.linspace(3, 357, nx) + lats = np.linspace(-87, 87, ny) # noqa: F841 + lons = np.linspace(3, 357, nx) # noqa: F841 # Build corner grid for pcolormesh lat_edges = np.linspace(-90, 90, ny + 1) diff --git a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2_linear.py b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2_linear.py index c27edae7..b163a0d1 100755 --- a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2_linear.py +++ b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/new_patch_arc2_linear.py @@ -202,8 +202,8 @@ def make_imap_lo_map( ny, nx = data.shape # Original grid centers - lats = np.linspace(-87, 87, ny) - lons = np.linspace(3, 357, nx) + lats = np.linspace(-87, 87, ny) # noqa: F841 + lons = np.linspace(3, 357, nx) # noqa: F841 # Build corner grid for pcolormesh lat_edges = np.linspace(-90, 90, ny + 1) diff --git a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/old/csv2yaml.py b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/old/csv2yaml.py index 8d8bb6ef..791b1fd8 100755 --- a/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/old/csv2yaml.py +++ b/tests/test_data/lo/lo_txt_pipeline/3S5_l1b_ram_maps/old/csv2yaml.py @@ -55,7 +55,7 @@ def csv_to_map_yaml( with open(csv_file, newline="") as f: reader = csv.reader(f) - header = next(reader) # 10–59, not used + header = next(reader) # 10–59, not used # noqa: F841 for row in reader: formatted_row = [ f"{float(x):.3E}" if float(x) != 0 else "0.000E+00" for x in row diff --git a/tests/ultra/test_ultra_processor.py b/tests/ultra/test_ultra_processor.py index 0a880473..447d4b6e 100644 --- a/tests/ultra/test_ultra_processor.py +++ b/tests/ultra/test_ultra_processor.py @@ -1198,6 +1198,7 @@ def test_process_spectral_index_with_custom_energy_bin_range( processor = UltraProcessor(input_deps, input_metadata) result = processor.process() + self.assertEqual(result, [mock_save_data.return_value]) mock_slice_energy_range_by_bin.assert_called_once_with( dependencies.map_data.intensity_map_data, 5, 10 From 917a1e048067d1826121a73cb31a8d17ba1462b1 Mon Sep 17 00:00:00 2001 From: Jonathan Niehof Date: Fri, 23 Oct 2026 17:10:43 -0600 Subject: [PATCH 2/4] Fix ruff checks for scripts --- .../codice/create_more_accurate_l3a_direct_event_input.py | 6 +++--- scripts/glows/download_l2_prod_data.py | 2 +- scripts/glows/download_validation_data.py | 2 +- scripts/hi/create_all_maps_from_inputs.py | 2 +- scripts/swapi/generate_reference_integrals.py | 8 +++++--- scripts/swapi/sample_wind_solar_wind.py | 2 +- .../swapi_alpha_sw_speed_temperature_density_demo.py | 2 +- scripts/swe/compare_outputs.py | 4 ++-- scripts/swe/create_fake_l2_cdf.py | 4 ++-- scripts/swe/read_ace.py | 4 ++-- .../ultra/create_example_ultra_l2_map_with_power_law.py | 1 - scripts/ultra/validation/create_l3_validation_maps.py | 2 +- 12 files changed, 20 insertions(+), 19 deletions(-) diff --git a/scripts/codice/create_more_accurate_l3a_direct_event_input.py b/scripts/codice/create_more_accurate_l3a_direct_event_input.py index e0f90e9d..d389e9ca 100644 --- a/scripts/codice/create_more_accurate_l3a_direct_event_input.py +++ b/scripts/codice/create_more_accurate_l3a_direct_event_input.py @@ -39,9 +39,9 @@ def extend_priority_counts_to_24_spin_angles( rgfo_half_spin = rng.integers( 0, 33, size=template_cdf["rgfo_half_spin"].shape ) - randomly_fill_value = rng.choice( - [False, True], size=template_cdf["rgfo_half_spin"].shape - ) + # randomly_fill_value = rng.choice( + # [False, True], size=template_cdf["rgfo_half_spin"].shape + # ) cdf["rgfo_half_spin"] = rgfo_half_spin # cdf["rgfo_half_spin"] = np.where(randomly_fill_value, cdf["rgfo_half_spin"].attrs["FILLVAL"], diff --git a/scripts/glows/download_l2_prod_data.py b/scripts/glows/download_l2_prod_data.py index 590de2e8..b626e2a3 100644 --- a/scripts/glows/download_l2_prod_data.py +++ b/scripts/glows/download_l2_prod_data.py @@ -9,7 +9,7 @@ dotenv.load_dotenv(".env.prod") -from tests.test_helpers import get_run_local_data_path +from tests.test_helpers import get_run_local_data_path # noqa: E402 GLOWS_L3E_REQUIRED_SPICE_KERNELS: list[SpiceKernelTypes] = [ SpiceKernelTypes.ScienceFrames, diff --git a/scripts/glows/download_validation_data.py b/scripts/glows/download_validation_data.py index 53effeb5..a68af4f7 100644 --- a/scripts/glows/download_validation_data.py +++ b/scripts/glows/download_validation_data.py @@ -9,7 +9,7 @@ Path(__file__).parent.parent.parent / "glows_l3_validation_data" ) -import imap_data_access +import imap_data_access # noqa: E402 ancillary_descriptors = [ ("calibration-data", "20100101"), diff --git a/scripts/hi/create_all_maps_from_inputs.py b/scripts/hi/create_all_maps_from_inputs.py index f24ed757..2d298ada 100644 --- a/scripts/hi/create_all_maps_from_inputs.py +++ b/scripts/hi/create_all_maps_from_inputs.py @@ -50,7 +50,7 @@ def run_all_maps( output_dir = Path(f"{datetime.now().strftime('%Y%m%d')}_hi_validation_output") try: input_dir = Path(sys.argv[1]) - except: + except Exception: input_dir = Path("hi_validation_input") print( f"Could not open input file, using default input path of {input_dir} instead" diff --git a/scripts/swapi/generate_reference_integrals.py b/scripts/swapi/generate_reference_integrals.py index 33c89fff..b8ee14eb 100644 --- a/scripts/swapi/generate_reference_integrals.py +++ b/scripts/swapi/generate_reference_integrals.py @@ -26,13 +26,14 @@ import sys from pathlib import Path +import numpy as np +import pandas as pd + _REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(_REPO_ROOT)) sys.path.insert(0, str(_REPO_ROOT / "docs" / "swapi" / "figure_src")) -import numpy as np -import pandas as pd - +# ruff: disable[E402] from imap_l3_processing.constants import ( METERS_PER_KILOMETER, PROTON_CHARGE_COULOMBS, @@ -44,6 +45,7 @@ from imap_l3_processing.swapi.response.swapi_response import SwapiResponse from scripts.swapi.reference_integral import reference_integrals_batch from figure_utils import ANCHOR_ROTATION_SWAPI_TO_RTN +# ruff: enable[E402] _INSTRUMENT_DATA = _REPO_ROOT / "instrument_team_data" / "swapi" _WIND_SAMPLES_PATH = ( diff --git a/scripts/swapi/sample_wind_solar_wind.py b/scripts/swapi/sample_wind_solar_wind.py index 83fa042b..23aea951 100644 --- a/scripts/swapi/sample_wind_solar_wind.py +++ b/scripts/swapi/sample_wind_solar_wind.py @@ -38,7 +38,7 @@ _REPO_ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(_REPO_ROOT)) -from imap_l3_processing.constants import ( +from imap_l3_processing.constants import ( # noqa: E402 BOLTZMANN_CONSTANT_JOULES_PER_KELVIN, PROTON_MASS_KG, ) diff --git a/scripts/swapi/swapi_alpha_sw_speed_temperature_density_demo.py b/scripts/swapi/swapi_alpha_sw_speed_temperature_density_demo.py index 6189f358..24f6f7ee 100644 --- a/scripts/swapi/swapi_alpha_sw_speed_temperature_density_demo.py +++ b/scripts/swapi/swapi_alpha_sw_speed_temperature_density_demo.py @@ -181,5 +181,5 @@ def main(file_path): try: file_path = sys.argv[1] main(file_path) - except: + except Exception: main(os.path.abspath("../../instrument_team_data/swapi/swapi_test_data_v5.dat")) diff --git a/scripts/swe/compare_outputs.py b/scripts/swe/compare_outputs.py index b1251f36..a955beca 100644 --- a/scripts/swe/compare_outputs.py +++ b/scripts/swe/compare_outputs.py @@ -3,8 +3,8 @@ import matplotlib.pyplot as plt import numpy as np import pyhdf -from pyhdf.HDF import * -from pyhdf.VS import * +from pyhdf.HDF import HDF +from pyhdf.VS import VS from spacepy import pycdf from spacepy.pycdf import CDF diff --git a/scripts/swe/create_fake_l2_cdf.py b/scripts/swe/create_fake_l2_cdf.py index 3bdaf4c6..f940fd9e 100644 --- a/scripts/swe/create_fake_l2_cdf.py +++ b/scripts/swe/create_fake_l2_cdf.py @@ -5,8 +5,8 @@ import numpy as np import pyhdf import pyhdf.SD -from pyhdf.HDF import * -from pyhdf.VS import * +from pyhdf.HDF import HDF +from pyhdf.VS import VD from spacepy import pycdf from spacepy.pycdf import CDF diff --git a/scripts/swe/read_ace.py b/scripts/swe/read_ace.py index ced28fe0..0bf72886 100644 --- a/scripts/swe/read_ace.py +++ b/scripts/swe/read_ace.py @@ -14,11 +14,11 @@ import numpy as np import xarray as xr from matplotlib.colors import LogNorm -from pyhdf.HDF import * +from pyhdf.HDF import HDF from pyhdf.SD import SD, SDC # import pyhdf.SD -from pyhdf.VS import * +from pyhdf.VS import VS from spacepy.pycdf import CDF SD diff --git a/scripts/ultra/create_example_ultra_l2_map_with_power_law.py b/scripts/ultra/create_example_ultra_l2_map_with_power_law.py index 07d8378a..0dae3012 100644 --- a/scripts/ultra/create_example_ultra_l2_map_with_power_law.py +++ b/scripts/ultra/create_example_ultra_l2_map_with_power_law.py @@ -14,7 +14,6 @@ def _create_example_ultra_l2_map_with_power_law( ): out_path.parent.mkdir(parents=True, exist_ok=True) out_path.unlink(missing_ok=True) - nside = 2 num_lon = 60 lon = np.linspace(0, 360, 60) num_lat = 30 diff --git a/scripts/ultra/validation/create_l3_validation_maps.py b/scripts/ultra/validation/create_l3_validation_maps.py index 5fd7666d..c460dbb9 100644 --- a/scripts/ultra/validation/create_l3_validation_maps.py +++ b/scripts/ultra/validation/create_l3_validation_maps.py @@ -51,7 +51,7 @@ def run_all_maps( output_dir = Path(f"{datetime.now().strftime('%Y%m%d')}_ultra_validation_output") try: input_dir = Path(sys.argv[1]) - except: + except Exception: input_dir = Path(r"C:\Users\Harrison\AppData\Local\cava\cava\data") print( f"Could not open input file, using default input path of {input_dir} instead" From dd820b7c71ac683dc6a1cbf7f60407070f66a2df Mon Sep 17 00:00:00 2001 From: Jonathan Niehof Date: Mon, 26 Oct 2026 13:18:56 -0600 Subject: [PATCH 3/4] Fix ruff checks for docs --- docs/swapi/figure_src/figure_utils.py | 2 ++ docs/swapi/figure_src/plot_pui_cutoff_staircase.py | 2 ++ docs/swapi/figure_src/plot_pui_distribution.py | 2 ++ docs/swapi/figure_src/plot_pui_reference_comparison.py | 2 ++ 4 files changed, 8 insertions(+) diff --git a/docs/swapi/figure_src/figure_utils.py b/docs/swapi/figure_src/figure_utils.py index 15cd0e8c..9e92b4dd 100644 --- a/docs/swapi/figure_src/figure_utils.py +++ b/docs/swapi/figure_src/figure_utils.py @@ -12,6 +12,7 @@ REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.insert(0, str(REPO_ROOT)) +# ruff: disable[E402] from imap_l3_processing.constants import ( METERS_PER_KILOMETER, PROTON_CHARGE_COULOMBS, @@ -19,6 +20,7 @@ ) from imap_l3_processing.swapi.constants import SWAPI_K_FACTOR from imap_l3_processing.swapi.response.swapi_response import SwapiResponse +# ruff: enable[E402] FIGURES_DIR = REPO_ROOT / "docs" / "swapi" / "figures" diff --git a/docs/swapi/figure_src/plot_pui_cutoff_staircase.py b/docs/swapi/figure_src/plot_pui_cutoff_staircase.py index bf4a2021..7187749a 100644 --- a/docs/swapi/figure_src/plot_pui_cutoff_staircase.py +++ b/docs/swapi/figure_src/plot_pui_cutoff_staircase.py @@ -28,6 +28,7 @@ REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.insert(0, str(REPO_ROOT)) +# ruff: disable[E402] import matplotlib matplotlib.use("Agg") @@ -59,6 +60,7 @@ from imap_l3_processing.swapi.l3a.utils import ( velocity_components_to_angles_in_instrument_frame, ) +# ruff: enable[E402] _SW_SPEED_KMS = 450.0 _SW_AZIMUTH_DEG = 0.0 diff --git a/docs/swapi/figure_src/plot_pui_distribution.py b/docs/swapi/figure_src/plot_pui_distribution.py index 03a3fe52..5e1b3767 100644 --- a/docs/swapi/figure_src/plot_pui_distribution.py +++ b/docs/swapi/figure_src/plot_pui_distribution.py @@ -22,6 +22,7 @@ REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.insert(0, str(REPO_ROOT)) +# ruff: disable[E402] import matplotlib matplotlib.use("Agg") @@ -38,6 +39,7 @@ FittingParameters, VasyliunasSiscoeDistribution, ) +# ruff: enable[E402] _CUTOFF_SPEED_KMS = 450.0 _SW_SPEED_INERTIAL_KMS = 450.0 diff --git a/docs/swapi/figure_src/plot_pui_reference_comparison.py b/docs/swapi/figure_src/plot_pui_reference_comparison.py index c0546f37..900b2b97 100644 --- a/docs/swapi/figure_src/plot_pui_reference_comparison.py +++ b/docs/swapi/figure_src/plot_pui_reference_comparison.py @@ -21,6 +21,7 @@ REPO_ROOT = Path(__file__).resolve().parents[3] sys.path.insert(0, str(REPO_ROOT)) +# ruff: disable[E402] import matplotlib matplotlib.use("Agg") @@ -30,6 +31,7 @@ from figure_utils import FIGURES_DIR from imap_l3_processing.constants import HE_PUI_PARTICLE_MASS_PER_CHARGE_M_P_PER_E +# ruff: enable[E402] _REFERENCE_CSV_PATH = ( REPO_ROOT / "tests" / "test_data" / "swapi" / "pui_count_rate_reference.csv" From 4a9055c832eb69be997a03eb4f06ad704d8426aa Mon Sep 17 00:00:00 2001 From: Jonathan Niehof Date: Mon, 26 Oct 2026 14:12:24 -0600 Subject: [PATCH 4/4] Fix all other ruff checks --- .../l3a/glows_toolkit/l0_data_histogram.py | 2 +- .../glows_toolkit/l1b_data_direct_events.py | 4 +- .../l3a/glows_toolkit/l2_data_histogram.py | 6 +-- .../glows/l3a/glows_toolkit/l3a_data.py | 5 -- .../glows/l3bc/l3bc_toolkit/funcs.py | 2 +- .../glows/l3d/science/toolkit/funcs.py | 2 +- .../science/toolkit/l3d_SolarParamHistory.py | 1 - imap_l3_processing/maps/mpfit.py | 46 ++++++++----------- .../predicted_ephemeris_tracker.py | 7 ++- .../swe/l3/science/pitch_calculations.py | 6 +-- imap_l3_processing/utils.py | 1 - pyproject.toml | 1 + tests/glows/test_glows_processor.py | 6 +-- 13 files changed, 40 insertions(+), 49 deletions(-) diff --git a/imap_l3_processing/glows/l3a/glows_toolkit/l0_data_histogram.py b/imap_l3_processing/glows/l3a/glows_toolkit/l0_data_histogram.py index 933046ce..57f8eb36 100644 --- a/imap_l3_processing/glows/l3a/glows_toolkit/l0_data_histogram.py +++ b/imap_l3_processing/glows/l3a/glows_toolkit/l0_data_histogram.py @@ -67,7 +67,7 @@ def deserialize(self, reader): # additional padding when odd number of bins? if self.data["number_of_bins_per_histogram"] % 2 == 1: - aux = reader.read_uint8() + _ = reader.read_uint8() return self.data diff --git a/imap_l3_processing/glows/l3a/glows_toolkit/l1b_data_direct_events.py b/imap_l3_processing/glows/l3a/glows_toolkit/l1b_data_direct_events.py index af633c00..fc70c2c6 100644 --- a/imap_l3_processing/glows/l3a/glows_toolkit/l1b_data_direct_events.py +++ b/imap_l3_processing/glows/l3a/glows_toolkit/l1b_data_direct_events.py @@ -205,8 +205,8 @@ def _save_data_to_json_file(self): data = copy.deepcopy(self.data) for key in fmt["arrays"].keys(): - if ( - type(data[key]) == np.ndarray + if isinstance( + data[key], np.ndarray ): # check if we have more than one direct event data[key] = ( "[" diff --git a/imap_l3_processing/glows/l3a/glows_toolkit/l2_data_histogram.py b/imap_l3_processing/glows/l3a/glows_toolkit/l2_data_histogram.py index aa2e6e32..56209638 100644 --- a/imap_l3_processing/glows/l3a/glows_toolkit/l2_data_histogram.py +++ b/imap_l3_processing/glows/l3a/glows_toolkit/l2_data_histogram.py @@ -172,7 +172,7 @@ def _check_l1b_bad_time_flags(self, l1b_data): ): cmp_result = 1 - if l1b_data["flags"]["is_night"] == True: + if l1b_data["flags"]["is_night"]: print("(night", self.var["is_night_counter"], cmp_result, end=") ") if cmp_result != 0: @@ -351,7 +351,7 @@ def is_new_observational_day(self, l1b_data): ): # we are at the second (or later) l1b file if ( self.var["is_night_for_previous_l1b"] == 1 - and l1b_data["flags"]["is_night"] == False + and not l1b_data["flags"]["is_night"] ): # new observational day is_new_observ_day = True @@ -465,7 +465,7 @@ def process_l1b_data_files(self, l1b_data, pointing_counter): # increment good-time counter for L1b files self.var["l1b_file_counter_good_time"] += 1 - if l1b_data["flags"]["is_night"] == True: + if l1b_data["flags"]["is_night"]: # increment is-night-flag-raised counter for L1b files self.var["is_night_counter"] += 1 diff --git a/imap_l3_processing/glows/l3a/glows_toolkit/l3a_data.py b/imap_l3_processing/glows/l3a/glows_toolkit/l3a_data.py index a0d91a27..0c093546 100644 --- a/imap_l3_processing/glows/l3a/glows_toolkit/l3a_data.py +++ b/imap_l3_processing/glows/l3a/glows_toolkit/l3a_data.py @@ -140,11 +140,6 @@ def _compute_time_dependent_bckgrd(self, anc_input_from_instr_team): fh.close() # read L3a lightcurve start dates and array of corrections - identifiers = np.loadtxt( - anc_input_from_instr_team["time_dependent_bckgrd"], - usecols=[0], - dtype=str, - ) start_dates = np.loadtxt( anc_input_from_instr_team["time_dependent_bckgrd"], usecols=[1], diff --git a/imap_l3_processing/glows/l3bc/l3bc_toolkit/funcs.py b/imap_l3_processing/glows/l3bc/l3bc_toolkit/funcs.py index 09222822..43b924c9 100644 --- a/imap_l3_processing/glows/l3bc/l3bc_toolkit/funcs.py +++ b/imap_l3_processing/glows/l3bc/l3bc_toolkit/funcs.py @@ -520,7 +520,7 @@ def read_raw_OMNI_data(ext_dependencies, t_window=None): ext_dependencies["omni_raw_data"], usecols=(0, 1, 2, 5, 23, 24, 27, 30, 31, 34) ) omni_raw_date = time_from_yday(omni_raw[:, (0, 1, 2)]) - if t_window == None: + if t_window is None: out = omni_raw else: idx_ini = np.abs(omni_raw_date - t_window[0]).argmin() diff --git a/imap_l3_processing/glows/l3d/science/toolkit/funcs.py b/imap_l3_processing/glows/l3d/science/toolkit/funcs.py index 09222822..43b924c9 100644 --- a/imap_l3_processing/glows/l3d/science/toolkit/funcs.py +++ b/imap_l3_processing/glows/l3d/science/toolkit/funcs.py @@ -520,7 +520,7 @@ def read_raw_OMNI_data(ext_dependencies, t_window=None): ext_dependencies["omni_raw_data"], usecols=(0, 1, 2, 5, 23, 24, 27, 30, 31, 34) ) omni_raw_date = time_from_yday(omni_raw[:, (0, 1, 2)]) - if t_window == None: + if t_window is None: out = omni_raw else: idx_ini = np.abs(omni_raw_date - t_window[0]).argmin() diff --git a/imap_l3_processing/glows/l3d/science/toolkit/l3d_SolarParamHistory.py b/imap_l3_processing/glows/l3d/science/toolkit/l3d_SolarParamHistory.py index 8373d6cc..62b71e36 100644 --- a/imap_l3_processing/glows/l3d/science/toolkit/l3d_SolarParamHistory.py +++ b/imap_l3_processing/glows/l3d/science/toolkit/l3d_SolarParamHistory.py @@ -115,7 +115,6 @@ def __init__(self, anc_input_from_instr_team, ext_dependencies): self.ini_data = {} self.ini_data["CR_last"] = None self.ini_data["label"] = list(anc_input_from_instr_team["WawHelioIon"].keys()) - N = len(self.ini_data["label"]) self.ini_data["time"] = {} for k in anc_input_from_instr_team["WawHelioIon"]: self.ini_data["time"][k] = None diff --git a/imap_l3_processing/maps/mpfit.py b/imap_l3_processing/maps/mpfit.py index 73e58da6..c5b88e25 100644 --- a/imap_l3_processing/maps/mpfit.py +++ b/imap_l3_processing/maps/mpfit.py @@ -835,6 +835,9 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, If nocovar is set or MPFIT terminated abnormally, then .covar is set to a scalar with value None. + .catch_msg + String describing the last operation attempted (which may have failed). + .errmsg A string error or warning message is returned. @@ -869,12 +872,13 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, self.perror = None self.status = 0 # Invalid input flag set while we check inputs self.debug = debug + self.catch_msg = "" self.errmsg = "" self.nfev = 0 self.damp = damp self.dof = 0 - if fcn == None: + if fcn is None: self.errmsg = "Usage: parms = mpfit('myfunt', ... )" return @@ -1070,7 +1074,6 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, if (nprint > 0) and (iterfunct is not None): if ((self.niter - 1) % nprint) == 0: - mperr = 0 xnew0 = self.params.copy() dof = numpy.max([len(fvec) - len(x), 0]) @@ -1103,7 +1106,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, # Calculate the jacobian matrix self.status = 2 - catch_msg = "calling MPFIT_FDJAC2" + self.catch_msg = "calling MPFIT_FDJAC2" fjac = self.fdjac2( fcn, x, @@ -1125,7 +1128,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, # Determine if any of the parameters are pegged at the limits if qanylim: - catch_msg = "zeroing derivatives of pegged parameters" + self.catch_msg = "zeroing derivatives of pegged parameters" whlpeg = (numpy.nonzero(qllim & (x == llim)))[0] nlpeg = len(whlpeg) whupeg = (numpy.nonzero(qulim & (x == ulim)))[0] @@ -1149,7 +1152,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, # On the first iteration if "diag" is unspecified, scale # according to the norms of the columns of the initial jacobian - catch_msg = "rescaling diagonal elements" + self.catch_msg = "rescaling diagonal elements" if self.niter == 1: if (rescale == 0) or (len(diag) < n): diag = wa2.copy() @@ -1164,7 +1167,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, delta = factor # Form (q transpose)*fvec and store the first n components in qtf - catch_msg = "forming (q transpose)*fvec" + self.catch_msg = "forming (q transpose)*fvec" wa4 = fvec.copy() for j in range(n): lj = ipvt[j] @@ -1192,7 +1195,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, # if ct GT 0 then goto, FAIL_OVERFLOW # Compute the norm of the scaled gradient - catch_msg = "computing the scaled gradient" + self.catch_msg = "computing the scaled gradient" gnorm = 0.0 if self.fnorm != 0: for j in range(n): @@ -1216,7 +1219,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, # Beginning of the inner loop while 1: # Determine the levenberg-marquardt parameter - catch_msg = "calculating LM parameter (MPFIT_)" + self.catch_msg = "calculating LM parameter (MPFIT_)" [fjac, par, wa1, wa2] = self.lmpar( fjac, ipvt, diag, qtf, delta, wa1, wa2, par=par ) @@ -1236,7 +1239,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, if qanylim: # Do not allow any steps out of bounds - catch_msg = "checking for a step out of bounds" + self.catch_msg = "checking for a step out of bounds" if nlpeg > 0: wa1[whlpeg] = numpy.clip(wa1[whlpeg], 0.0, numpy.max(wa1)) if nupeg > 0: @@ -1299,8 +1302,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, self.params[ifree] = wa2 # Evaluate the function at x+p and calculate its norm - mperr = 0 - catch_msg = "calling " + str(fcn) + self.catch_msg = "calling " + str(fcn) [self.status, wa4] = self.call(fcn, self.params, functkw) if self.status < 0: self.errmsg = 'WARNING: premature termination by "' + fcn + '"' @@ -1308,7 +1310,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, fnorm1 = self.enorm(wa4) # Compute the scaled actual reduction - catch_msg = "computing convergence criteria" + self.catch_msg = "computing convergence criteria" actred = -1.0 if (0.1 * fnorm1) < self.fnorm: actred = -((fnorm1 / self.fnorm) ** 2) + 1.0 @@ -1399,7 +1401,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, if ~numpy.all( numpy.isfinite(wa1) & numpy.isfinite(wa2) & numpy.isfinite(x) ) or ~numpy.isfinite(ratio): - errmsg = """ERROR: parameter or function value(s) have become + self.errmsg = """ERROR: parameter or function value(s) have become 'infinite; check model function for over- 'and underflow""" self.status = -16 break @@ -1410,7 +1412,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, break # End of outer loop. - catch_msg = "in the termination phase" + self.catch_msg = "in the termination phase" # Termination, either normal or user imposed. if len(self.params) == 0: return @@ -1419,9 +1421,9 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, else: self.params[ifree] = x if (nprint > 0) and (self.status > 0): - catch_msg = "calling " + str(fcn) + self.catch_msg = "calling " + str(fcn) [status, fvec] = self.call(fcn, self.params, functkw) - catch_msg = "in the termination phase" + self.catch_msg = "in the termination phase" self.fnorm = self.enorm(fvec) if (self.fnorm is not None) and (fnorm1 is not None): @@ -1440,7 +1442,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, ): sz = fjac.shape if (n > 0) and (sz[0] >= n) and (sz[1] >= n) and (len(ipvt) >= n): - catch_msg = "computing the covariance matrix" + self.catch_msg = "computing the covariance matrix" cv = self.calc_covar(fjac[0:n, 0:n], ipvt[0:n]) cv.shape = [n, n] nn = len(xall) @@ -1452,7 +1454,7 @@ def iterfunct(myfunct, p, iter, fnorm, functkw=None, self.covar[ifree, ifree[i]] = cv[:, i] # Compute errors in parameters - catch_msg = "computing parameter errors" + self.catch_msg = "computing parameter errors" self.perror = numpy.zeros(nn, dtype=float) d = numpy.diagonal(self.covar) wh = (numpy.nonzero(d >= 0))[0] @@ -1464,7 +1466,6 @@ def __str__(self): return { "params": self.params, "niter": self.niter, - "params": self.params, "covar": self.covar, "perror": self.perror, "status": self.status, @@ -1559,9 +1560,6 @@ def parinfo(self, parinfo=None, key="a", default=None, n=0): values.append(default) # Convert to numeric arrays if possible - test = default - if type(default) is list: - test = default[0] values = numpy.asarray(values) return values @@ -1624,7 +1622,6 @@ def fdjac2( # Compute analytical derivative if requested if autoderivative == 0: - mperr = 0 fjac = numpy.zeros(nall, dtype=float) fjac[ifree] = 1.0 # Specify which parameters need derivatives [status, fp] = self.call(fcn, xall, functkw, fjac=fjac) @@ -1694,7 +1691,6 @@ def fdjac2( # COMPUTE THE TWO-SIDED DERIVATIVE xp[ifree[j]] = xall[ifree[j]] - h[j] - mperr = 0 [status, fm] = self.call(fcn, xp, functkw) if status < 0: return None @@ -2001,7 +1997,6 @@ def qrsolv(self, r, ipvt, diag, qtb, sdiag): if self.debug: print("Entering qrsolv...") sz = r.shape - m = sz[0] n = sz[1] # copy r and (q transpose)*b to preserve input and initialize s. @@ -2176,7 +2171,6 @@ def lmpar(self, r, ipvt, diag, qtb, delta, x, sdiag, par=None): dwarf = self.machar.minnum machep = self.machar.machep sz = r.shape - m = sz[0] n = sz[1] # Compute and store in x the gauss-newton direction. If the diff --git a/imap_l3_processing/predicted_ephemeris_tracker.py b/imap_l3_processing/predicted_ephemeris_tracker.py index 36cad005..90ed2017 100644 --- a/imap_l3_processing/predicted_ephemeris_tracker.py +++ b/imap_l3_processing/predicted_ephemeris_tracker.py @@ -1,4 +1,4 @@ -from typing import Callable +from typing import Callable, ParamSpec, TypeVar from imap_data_access import SPICEFilePath from spiceypy import spiceypy, KernelPool, SpiceyError @@ -25,7 +25,10 @@ def __init__(self): except SPICEFilePath.InvalidImapFileError: self.kernels_without_predict.append(kernel) - def run[T, **P](self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: + T = TypeVar("T") + P = ParamSpec("P") + + def run(self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T: if self.used_predict or not self.predict_kernel_available: result = func(*args, **kwargs) else: diff --git a/imap_l3_processing/swe/l3/science/pitch_calculations.py b/imap_l3_processing/swe/l3/science/pitch_calculations.py index 86ed3443..a83e347b 100644 --- a/imap_l3_processing/swe/l3/science/pitch_calculations.py +++ b/imap_l3_processing/swe/l3/science/pitch_calculations.py @@ -72,7 +72,7 @@ def line_model(params, x): sc_pot = spline_energies[peaks[0]] ch_break = spline_energies[peaks[1]] BACKUP_SPLINE_UNRESOLVED = SweL3Flags.NONE - except: + except Exception: # Spline peak finder did not work BACKUP_SPLINE_UNRESOLVED = SweL3Flags.BACKUP_SPLINE_UNRESOLVED sc_pot = np.nan @@ -160,7 +160,7 @@ def refine_breakpoint_value(energy, psd, breakpoint_value, num_points): ) # spline not used BACKUP_SPLINE_UNRESOLVED = SweL3Flags.NONE - except: + except Exception: # Fall back on Spline method # Fit did not converge BACKUP_SPLINE_UNRESOLVED = SweL3Flags.BACKUP_SPLINE_UNRESOLVED @@ -192,7 +192,7 @@ def refine_breakpoint_value(energy, psd, breakpoint_value, num_points): ch_break_output = refine_breakpoint_value( np.exp(log_energy_ch), log_psd_ch, z[2], 3 ) - except: + except Exception: # Fall back on Spline method # Fit did not converge BREAKPOINT_FIT_UNCONVERGED = SweL3Flags.BREAKPOINT_FIT_UNCONVERGED diff --git a/imap_l3_processing/utils.py b/imap_l3_processing/utils.py index a97e8fdf..9652b3df 100644 --- a/imap_l3_processing/utils.py +++ b/imap_l3_processing/utils.py @@ -8,7 +8,6 @@ from tempfile import TemporaryDirectory from typing import Optional, Union from urllib.parse import urlparse -from spiceypy import spiceypy import imap_data_access import requests diff --git a/pyproject.toml b/pyproject.toml index a529d166..7e851cea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,3 +58,4 @@ addopts = "-ra" [tool.ruff] target-version = "py310" +lint.ignore = ["E741"] diff --git a/tests/glows/test_glows_processor.py b/tests/glows/test_glows_processor.py index 6aacbf94..fc3de6c1 100644 --- a/tests/glows/test_glows_processor.py +++ b/tests/glows/test_glows_processor.py @@ -1654,9 +1654,9 @@ def test_process_glows_l3d_drift(self, mock_save_data, _): actual_input_file_line = next( ( - L - for L in l3d_text_path.read_text().split("\n") - if L.startswith("# input files:") + l + for l in l3d_text_path.read_text().split("\n") + if l.startswith("# input files:") ), None, )