lint conftest.py

This commit is contained in:
gnzng committed 2025-07-07 13:38:52 -07:00
1 parent 93f5d1969d
commit 38be347a18
1 file changed
+66 -66
+66 -66
View File
@@ -47,6 +47,7 @@ def pytest_collection_modifyitems(config, items):
if "slow" in item.keywords:
item.add_marker(skip_slow)
@pytest.fixture
def reconstruction_device(request):
return request.config.getoption("--reconstruction_device")
@@ -57,7 +58,6 @@ def show_plot(request):
return request.config.getoption("--plot")
@pytest.fixture(scope='module')
def ptycho_cxi_1():
"""Creates an example file for CXI ptychography. This file is defined
@@ -67,11 +67,11 @@ def ptycho_cxi_1():
"""
expected = {}
f = h5py.File('ptycho_cxi_1','w',driver='core',backing_store=False)
f = h5py.File('ptycho_cxi_1', 'w', driver='core', backing_store=False)
# Start by defining the basic structure
f.create_dataset('cxi_version', data=150)
f.create_dataset('number_of_entries',data=1)
f.create_dataset('number_of_entries', data=1)
# Then define a bunch of metadata for entry_1
e1f = f.create_group('entry_1')
@@ -104,19 +104,19 @@ def ptycho_cxi_1():
s1f['concentration'] = s1e['concentration']
s1e['mass'] = np.float32(np.random.rand())
s1f['mass'] = s1e['mass']
s1e['temperature'] = np.float32(np.random.rand()*100)
s1e['temperature'] = np.float32(np.random.rand() * 100)
s1f['temperature'] = s1e['temperature']
s1e['thickness'] = np.float32(np.random.rand()*1e-7)
s1e['thickness'] = np.float32(np.random.rand() * 1e-7)
s1f['thickness'] = s1e['thickness']
s1e['unit_cell_volume'] = np.float32(np.random.rand() * 1e-27)
s1f['unit_cell_volume'] = s1e['unit_cell_volume']
s1e['unit_cell'] = np.array([1,1,1,90,90,90]).astype(np.float32)
s1f.create_dataset('unit_cell',data = s1e['unit_cell'])
s1e['unit_cell'] = np.array([1, 1, 1, 90, 90, 90]).astype(np.float32)
s1f.create_dataset('unit_cell', data=s1e['unit_cell'])
i1f = e1f.create_group('instrument_1')
source1f = i1f.create_group('source_1')
energy = np.float32(1.3618e-16) #Joules, = 850 eV
energy = np.float32(1.3618e-16) # Joules, = 850 eV
source1f['energy'] = energy
expected['wavelength'] = np.float32(1.9864459e-25) / energy
source1f['wavelength'] = expected['wavelength']
@@ -126,48 +126,48 @@ def ptycho_cxi_1():
d1e = expected['detector']
d1e['distance'] = np.float32(0.3)
d1f['distance'] = d1e['distance']
d1e['basis'] = np.array([[0,-30e-6,0],
[-20e-6,0,0]]).astype(np.float32).transpose()
d1f.create_dataset('basis_vectors',data=d1e['basis'])
d1e['basis'] = np.array([[0, -30e-6, 0],
[-20e-6, 0, 0]]).astype(np.float32).transpose()
d1f.create_dataset('basis_vectors', data=d1e['basis'])
d1f['x_pixel_size'] = np.float32(20e-6)
d1f['y_pixel_size'] = np.float32(30e-6)
d1e['corner'] = np.array((2550e-6,3825e-6,0.3)).astype(np.float32)
d1e['corner'] = np.array((2550e-6, 3825e-6, 0.3)).astype(np.float32)
d1f.create_dataset('corner_position', data=d1e['corner'])
# Remember the format for the CXI file differs from the format used
# internally
mask = np.zeros((256,256)).astype(np.int32)
mask[5,8] = 1
expected['mask'] = np.ones((256,256)).astype(bool)
expected['mask'][5,8] = 0
d1f.create_dataset('mask',data=mask)
mask = np.zeros((256, 256)).astype(np.int32)
mask[5, 8] = 1
expected['mask'] = np.ones((256, 256)).astype(bool)
expected['mask'][5, 8] = 0
d1f.create_dataset('mask', data=mask)
# There is no specification for this in the CXI file format :(
qe_mask = np.ones((256,256)).astype(np.float32)
qe_mask = np.ones((256, 256)).astype(np.float32)
expected['qe_mask'] = qe_mask
d1f.create_dataset('qe_mask',data=qe_mask)
d1f.create_dataset('qe_mask', data=qe_mask)
# Create an initial background
dark = np.ones((256,256)) * 0.01
dark = np.ones((256, 256)) * 0.01
expected['dark'] = dark
d1f.create_dataset('data_dark', data=dark)
data1f = e1f.create_group('data_1')
data = np.random.rand(100,256,256).astype(np.float32)
data = np.random.rand(100, 256, 256).astype(np.float32)
expected['data'] = data
d1f.create_dataset('data',data=data)
d1f.create_dataset('data', data=data)
data1f['data'] = h5py.SoftLink('/entry_1/instrument_1/detector_1/data')
d1f['data'].attrs['axes'] = np.bytes_('translation:y:x')
expected['axes'] = ['translation','y','x']
expected['axes'] = ['translation', 'y', 'x']
g1f = s1f.create_group('geometry_1')
orientation = np.array([1.,0,0,0,1,0])
orientation = np.array([1., 0, 0, 0, 1, 0])
g1f.create_dataset('orientation', data=orientation)
s1e['orientation'] = np.array([[1.,0,0],[0,1,0],[0,0,1]])
translations = np.arange(300).reshape((100,3)).astype(np.float32)
g1f.create_dataset('translation',data=translations)
s1e['orientation'] = np.array([[1., 0, 0], [0, 1, 0], [0, 0, 1]])
translations = np.arange(300).reshape((100, 3)).astype(np.float32)
g1f.create_dataset('translation', data=translations)
data1f['translation'] = h5py.SoftLink('/entry_1/sample_1/geometry_1/translation')
d1f['translation'] = h5py.SoftLink('/entry_1/sample_1/geometry_1/translation')
expected['translations'] = -translations
@@ -193,11 +193,11 @@ def ptycho_cxi_2():
"""
expected = {}
f = h5py.File('ptycho_cxi_2','w',driver='core',backing_store=False)
f = h5py.File('ptycho_cxi_2', 'w', driver='core', backing_store=False)
# Start by defining the basic structure
f.create_dataset('cxi_version', data=150)
f.create_dataset('number_of_entries',data=1)
f.create_dataset('number_of_entries', data=1)
# Then define a bunch of metadata for entry_1
e1f = f.create_group('entry_1')
@@ -210,13 +210,13 @@ def ptycho_cxi_2():
s1f = e1f.create_group('sample_1')
expected['sample info'] = {}
s1e = expected['sample info']
s1e['temperature'] = np.float32(np.random.rand()*100)
s1e['temperature'] = np.float32(np.random.rand() * 100)
s1f['temperature'] = s1e['temperature']
i1f = e1f.create_group('instrument_1')
source1f = i1f.create_group('source_1')
energy = np.float32(1.3618e-16) #Joules, = 850 eV
energy = np.float32(1.3618e-16) # Joules, = 850 eV
expected['wavelength'] = np.float32(1.9864459e-25) / energy
source1f['wavelength'] = expected['wavelength']
@@ -224,11 +224,11 @@ def ptycho_cxi_2():
expected['detector'] = {}
d1e = expected['detector']
d1e['distance'] = np.float32(0.3)
d1e['basis'] = np.array([[0,-30e-6,0],
[-20e-6,0,0]]).astype(np.float32).transpose()
d1e['basis'] = np.array([[0, -30e-6, 0],
[-20e-6, 0, 0]]).astype(np.float32).transpose()
d1f['x_pixel_size'] = np.float32(20e-6)
d1f['y_pixel_size'] = np.float32(30e-6)
d1e['corner'] = np.array((2550e-6,3825e-6,0.3)).astype(np.float32)
d1e['corner'] = np.array((2550e-6, 3825e-6, 0.3)).astype(np.float32)
d1f.create_dataset('corner_position', data=d1e['corner'])
# Remember the format for the CXI file differs from the format used
@@ -236,24 +236,23 @@ def ptycho_cxi_2():
expected['mask'] = None
expected['qe_mask'] = None
# Test with a set of dark images
dark = np.ones((10,256,256)) * 0.01
expected['dark'] = np.nanmean(dark,axis=0)
dark = np.ones((10, 256, 256)) * 0.01
expected['dark'] = np.nanmean(dark, axis=0)
d1f.create_dataset('data_dark', data=dark)
e1f.create_group('data_1')
data1f = e1f.create_group('data_1')
data = np.random.rand(100,256,256).astype(np.float32)
data = np.random.rand(100, 256, 256).astype(np.float32)
expected['data'] = data
d1f.create_dataset('data',data=data)
d1f.create_dataset('data', data=data)
expected['axes'] = None
g1f = s1f.create_group('geometry_1')
translations = np.arange(300).reshape((100,3)).astype(np.float32)
g1f.create_dataset('translation',data=translations)
translations = np.arange(300).reshape((100, 3)).astype(np.float32)
g1f.create_dataset('translation', data=translations)
expected['translations'] = -translations
yield f, expected
@@ -276,11 +275,11 @@ def ptycho_cxi_3():
"""
expected = {}
f = h5py.File('ptycho_cxi_3','w',driver='core',backing_store=False)
f = h5py.File('ptycho_cxi_3', 'w', driver='core', backing_store=False)
# Start by defining the basic structure
f.create_dataset('cxi_version', data=150)
f.create_dataset('number_of_entries',data=1)
f.create_dataset('number_of_entries', data=1)
# Then define a bunch of metadata for entry_1
e1f = f.create_group('entry_1')
@@ -297,7 +296,7 @@ def ptycho_cxi_3():
i1f = e1f.create_group('instrument_1')
source1f = i1f.create_group('source_1')
energy = np.float32(1.3618e-16) #Joules, = 850 eV
energy = np.float32(1.3618e-16) # Joules, = 850 eV
source1f['energy'] = energy
expected['wavelength'] = np.float32(1.9864459e-25) / energy
@@ -306,41 +305,41 @@ def ptycho_cxi_3():
d1e = expected['detector']
d1e['distance'] = np.float32(0.3)
d1f['distance'] = d1e['distance']
d1e['basis'] = np.array([[0,-30e-6,0],
[-20e-6,0,0]]).astype(np.float32).transpose()
d1f.create_dataset('basis_vectors',data=d1e['basis'])
d1e['basis'] = np.array([[0, -30e-6, 0],
[-20e-6, 0, 0]]).astype(np.float32).transpose()
d1f.create_dataset('basis_vectors', data=d1e['basis'])
d1e['corner'] = None
# Remember the format for the CXI file differs from the format used
# internally
mask = np.ones((256,256)).astype(np.uint32) * 0x00001000
mask[15,47] = 38
expected['mask'] = np.ones((256,256)).astype(bool)
expected['mask'][15,47] = 0
d1f.create_dataset('mask',data=mask)
mask = np.ones((256, 256)).astype(np.uint32) * 0x00001000
mask[15, 47] = 38
expected['mask'] = np.ones((256, 256)).astype(bool)
expected['mask'][15, 47] = 0
d1f.create_dataset('mask', data=mask)
expected['qe_mask'] = None
expected['dark'] = None
data1f = e1f.create_group('data_1')
data = np.random.rand(100,256,256).astype(np.float32)
data = np.random.rand(100, 256, 256).astype(np.float32)
expected['data'] = data
data1f.create_dataset('data',data=data)
data1f.create_dataset('data', data=data)
data1f['data'].attrs['axes'] = np.bytes_('translation:y:x')
expected['axes'] = ['translation','y','x']
expected['axes'] = ['translation', 'y', 'x']
translations = np.arange(300).reshape((100,3)).astype(np.float32)
data1f.create_dataset('translation',data=translations)
translations = np.arange(300).reshape((100, 3)).astype(np.float32)
data1f.create_dataset('translation', data=translations)
expected['translations'] = -translations
yield f, expected
f.close()
@pytest.fixture(scope='module')
def polarized_ptycho_cxi(ptycho_cxi_1):
f, expected = ptycho_cxi_1
@@ -352,7 +351,7 @@ def polarized_ptycho_cxi(ptycho_cxi_1):
data1f.create_dataset('polarizer_angle', data=expected['polarizer_angle'])
yield f, expected
# As specific issues start to crop up with loading CXI files from different
# beamlines, put a fixture here that replicates the issue so that we can
@@ -374,6 +373,7 @@ def gold_ball_cxi(pytestconfig):
return str(pytestconfig.rootpath) + \
'/examples/example_data/AuBalls_700ms_30nmStep_3_6SS_filter.cxi'
@pytest.fixture(scope='module')
def lab_ptycho_cxi(pytestconfig):
return str(pytestconfig.rootpath) + \
@@ -382,8 +382,8 @@ def lab_ptycho_cxi(pytestconfig):
@pytest.fixture(scope='module')
def example_nested_dicts(pytestconfig):
example_tensor = t.as_tensor(np.array([1,4.5,7]))
example_array = np.ones([10,20,30])
example_tensor = t.as_tensor(np.array([1, 4.5, 7]))
example_array = np.ones([10, 20, 30])
example_scalar = 4.5
example_single_element_array = np.array([0.3])
example_string = 'testing'