Skip to content

Commit

Permalink
ruff auto-fix and format imports (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
BradleySappington committed May 13, 2024
1 parent bd42089 commit 1baa46d
Show file tree
Hide file tree
Showing 26 changed files with 169 additions and 224 deletions.
15 changes: 7 additions & 8 deletions dev_utils/compute_psf_library.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import sys
import multiprocessing
import datetime
import logging
import logging.handlers
import datetime
import time
from os.path import abspath, sep, join, exists, isdir, split
import multiprocessing
import os
from itertools import product, chain
import sys
from itertools import chain, product
from os.path import abspath, exists, isdir, join, split

import matplotlib

matplotlib.use('Agg')
Expand All @@ -16,8 +16,7 @@

if not os.environ.get('WEBBPSF_PATH'):
os.environ['WEBBPSF_PATH'] = '/grp/jwst/ote/webbpsf-data'
import webbpsf

import webbpsf # noqa

N_PROCESSES = 16

Expand Down
14 changes: 7 additions & 7 deletions dev_utils/field_dependence/read_codev_dat_fits.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import datetime

import astropy.io.fits as fits
import basis
import numpy as np
import pint

units = pint.UnitRegistry()
import astropy.io.fits as fits
import basis
import matplotlib.pyplot as plt
from datetime import datetime

# Script that reads in a set of CodeV Pupil map OPD files across fields and instruments and fits Zernikes to the OPD
# distribution at each field point and then Legendres to the variation of each Zernike coefficient across field. The
Expand All @@ -19,9 +19,9 @@ def read_codeV_data_file(filename):
for i, line in enumerate(fh):
if i is headerlines:
break
if i is 6:
if i == 6:
x_field_deg = float(str.split(line)[3])
if i is 7:
if i == 7:
y_field_deg = float(str.split(line)[3])
data = np.loadtxt(filename, skiprows=headerlines)
data_defined = data != -99999
Expand All @@ -36,7 +36,7 @@ def main():
num_wf_pts_x = 256
num_wf_pts_y = 256

instruments = [
instruments = [
'fgs',
'nircam',
'miri',
Expand Down
2 changes: 0 additions & 2 deletions dev_utils/make-minimal-datafiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
# This is used to make a stripped-down version of the data files for use on GitHub Actions

import os, sys
import astropy.io.fits as fits
import subprocess
import glob

try:
inputfile = sys.argv[1]
Expand Down
16 changes: 8 additions & 8 deletions dev_utils/wfe_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
webbpsf.webbpsf_core.poppy.conf.use_fftw = False
webbpsf.webbpsf_core.poppy.conf.use_multiprocessing = True
ncores = 8
webbpsf.webbpsf_core.poppy.conf.n_processes = ncores
webbpsf.webbpsf_core.poppy.conf.n_processes = ncores

inst = webbpsf.NIRCam()
inst.filter = "F430M"
inst.detector_position = (1024,1024)

# Baseline test: No SI WFE, no distortion
inst.include_si_wfe = False
%timeit psf = inst.calc_psf(add_distortion=False, monochromatic=4.3e-6)
%timeit psf = inst.calc_psf(add_distortion=False, monochromatic=4.3e-6) # noqa
# Result: 911 ms ± 5.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit psf = inst.calc_psf(add_distortion=False, nlambda=ncores)
%timeit psf = inst.calc_psf(add_distortion=False, nlambda=ncores) # noqa
# Result: 5.62 s ± 177 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# Turn on SI WFE at center of detector (will use interpolation)
inst.include_si_wfe = True
%timeit psf = inst.calc_psf(add_distortion=False, monochromatic=4.3e-6)
%timeit psf = inst.calc_psf(add_distortion=False, monochromatic=4.3e-6) # noqa
# Result: 1.41 s ± 12.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit psf = inst.calc_psf(add_distortion=False, nlambda=ncores)
%timeit psf = inst.calc_psf(add_distortion=False, nlambda=ncores) # noqa
# Result: 6.1 s ± 96.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

# Use pixel (0,0) to force extrapolation algorithm
inst.detector_position = (0,0)
%timeit psf = inst.calc_psf(add_distortion=False, monochromatic=4.3e-6)
%timeit psf = inst.calc_psf(add_distortion=False, monochromatic=4.3e-6) # noqa
# Result: 1.8 s ± 12.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit psf = inst.calc_psf(add_distortion=False, nlambda=ncores)
# Result: 6.53 s ± 85.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit psf = inst.calc_psf(add_distortion=False, nlambda=ncores) # noqa
# Result: 6.53 s ± 85.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
3 changes: 1 addition & 2 deletions docs/exts/numfig.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from docutils.nodes import figure, caption, Text, reference, raw, SkipNode, Element
from docutils.nodes import SkipNode, Text, caption, figure, raw, reference
from sphinx.roles import XRefRole


# Element classes

class page_ref(reference):
Expand Down
17 changes: 8 additions & 9 deletions webbpsf/jupyter_gui.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import logging
import matplotlib
import astropy.io.fits as fits

import astropy.io.fits as fits
import poppy
from . import webbpsf_core
from . import roman
from . import utils

from . import roman, webbpsf_core

_log = logging.getLogger('webbpsf')

Expand Down Expand Up @@ -35,16 +32,17 @@ def show_notebook_interface_wfi(instrument):
# (Currently not a hard dependency for the full webbpsf package, so we import
# within the function.)
import ipywidgets as widgets
from IPython.display import display, clear_output
from IPython.display import clear_output, display
from matplotlib import pyplot as plt

try:
import synphot
except ImportError:
raise ImportError(f'For now, synphot must be installed to use the notebook interface')
raise ImportError('For now, synphot must be installed to use the notebook interface')

# Clean up some warnings we know about so as not to scare the users
import warnings

from matplotlib.cbook import MatplotlibDeprecationWarning

warnings.simplefilter('ignore', MatplotlibDeprecationWarning)
Expand Down Expand Up @@ -219,7 +217,7 @@ def show_notebook_interface_jwst(instrument):
# (Currently not a hard dependency for the full webbpsf package, so we import
# within the function.)
import ipywidgets as widgets
from IPython.display import display, clear_output
from IPython.display import clear_output, display
from matplotlib import pyplot as plt

if isinstance(instrument, str):
Expand All @@ -228,10 +226,11 @@ def show_notebook_interface_jwst(instrument):
try:
import synphot
except ImportError:
raise ImportError(f'For now, synphot must be installed to use the notebook interface')
raise ImportError('For now, synphot must be installed to use the notebook interface')

# Clean up some warnings we know about so as not to scare the users
import warnings

from matplotlib.cbook import MatplotlibDeprecationWarning

warnings.simplefilter('ignore', MatplotlibDeprecationWarning)
Expand Down
8 changes: 4 additions & 4 deletions webbpsf/mast_wss.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Functions for interacting with the MAST archive and JWST measured data


import os
import functools
import os

import astropy
import astropy.io.fits as fits
import astropy.time
import astropy.units as u
import numpy as np
from astropy.time import Time, TimeDelta

from astroquery.mast import Mast, Observations

import webbpsf.utils
Expand Down Expand Up @@ -285,7 +284,7 @@ def import_wss_opd(filename, npix_out=1024, verbose=False):
# initial coarse resolution on the segment gaps
mask = inputOPD != 0
paddedOPD = webbpsf.utils.border_extrapolate_pad(inputOPD, mask)
wasopd[0].header.add_history(f' Dilated OPD values to fill adjacent invalid pixels (i.e. fill in gaps)')
wasopd[0].header.add_history(' Dilated OPD values to fill adjacent invalid pixels (i.e. fill in gaps)')

# interpolate to larger size
newopd = webbpsf.utils.rescale_interpolate_opd(paddedOPD, npix_out)
Expand Down Expand Up @@ -753,9 +752,10 @@ def get_visit_nrc_ta_image(visitid, verbose=True, kind='cal'):
except urllib.error.HTTPError as err:
if err.code == 401: # Unauthorized
# Use MAST API to allow retrieval of exclusive access data, if relevant
import astroquery
import tempfile

import astroquery

mast_api_token = os.environ.get('MAST_API_TOKEN', None)
mast_obs = astroquery.mast.ObservationsClass(mast_api_token)
uri = f'mast:JWST/product/{filename}'
Expand Down
7 changes: 2 additions & 5 deletions webbpsf/match_data.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
## Functions to match or fit PSFs to observed JWST data
import astropy
import astropy.units as u
import astropy.io.fits as fits

import scipy.optimize
import pysiaf

import webbpsf
import pysiaf


def setup_sim_to_match_file(filename_or_HDUList, verbose=True, plot=False, choice='closest'):
Expand All @@ -30,7 +27,7 @@ def setup_sim_to_match_file(filename_or_HDUList, verbose=True, plot=False, choic
else:
header = filename_or_HDUList[0].header
if verbose:
print(f'Setting up sim to match provided FITS HDUList object')
print('Setting up sim to match provided FITS HDUList object')

inst = webbpsf.instrument(header['INSTRUME'])

Expand Down
33 changes: 15 additions & 18 deletions webbpsf/opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,29 @@
# prescription, as published for instance in Lightsey et al. 2012 Opt. Eng.
#
###############################################################################
import copy
import functools
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import scipy.special as sp
import scipy
import astropy.table
import astropy.io.fits as fits
import astropy.units as u
import logging
from collections import OrderedDict
import os
import warnings
from packaging.version import Version
import copy
from collections import OrderedDict

import astropy.io.fits as fits
import astropy.table
import astropy.units as u
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import poppy
import poppy.zernike as zernike
import scipy
import scipy.special as sp
from packaging.version import Version

import pysiaf

from . import constants
from . import surs
from . import utils
import webbpsf

from . import constants, surs, utils

_log = logging.getLogger('webbpsf')

__doc__ = """
Expand Down Expand Up @@ -1651,7 +1648,7 @@ def _get_field_dependence_secondary_mirror(self, v2v3):
wfe_sign = 1

for i in range(3, 9):
self.opd_header[f'SMIF_H{i}'] = (z_coeffs[i], f'Hexike coeff from S.M. influence fn model')
self.opd_header[f'SMIF_H{i}'] = (z_coeffs[i], 'Hexike coeff from S.M. influence fn model')
self.opd_header['HISTORY'] = 'Field point (x,y): ({})'.format(v2v3)
self.opd_header['HISTORY'] = 'Control point: {} ({})'.format(
self.control_point_fieldpoint.upper(), self.ote_control_point
Expand Down

0 comments on commit 1baa46d

Please sign in to comment.