Remove the various deprication warnings related to converting h5py datasets to arrays with np.array()

This commit is contained in:
allevitan
2024-10-14 08:30:15 -04:00
parent e9e2fd4bee
commit 40651eaf54
4 changed files with 29 additions and 27 deletions
+1 -2
View File
@@ -352,8 +352,7 @@ def synthesize_reconstructions(probes, objects, use_probe=False, obj_slice=None,
else:
shift = ip.find_shift(synth_obj[obj_slice],obj[obj_slice], resolution=50)
obj = ip.sinc_subpixel_shift(obj,np.array(shift))
obj = ip.sinc_subpixel_shift(obj, shift)
if len(probe.shape) == 3:
probe = t.stack([ip.sinc_subpixel_shift(p,tuple(shift))
+12 -11
View File
@@ -134,18 +134,18 @@ def get_sample_info(cxi_file):
metadata[attr] = np.float32(s1[attr][()])
if 'unit_cell' in s1:
metadata['unit_cell'] = np.array(s1['unit_cell']).astype(np.float32)
metadata['unit_cell'] = s1['unit_cell'][()].astype(np.float32)
if 'geometry_1/orientation' in s1:
orient = np.array(s1['geometry_1/orientation']).astype(np.float32)
orient = s1['geometry_1/orientation'][()].astype(np.float32)
xvec = orient[:3] / np.linalg.norm(orient[:3])
yvec = orient[3:] / np.linalg.norm(orient[3:])
metadata['orientation'] = np.array([xvec,yvec,
np.cross(xvec,yvec)])
if 'geometry_1/surface_normal' in s1:
snorm = np.array(s1['geometry_1/surface_normal']).astype(np.float32)
snorm = s1['geometry_1/surface_normal'][()].astype(np.float32)
xvec = np.cross(np.array([0.,1.,0.]), snorm)
xvec /= np.linalg.norm(xvec)
yvec = np.cross(snorm, xvec)
@@ -218,7 +218,7 @@ def get_detector_geometry(cxi_file):
d1 = i1['detector_1']
if 'detector_1/basis_vectors' in i1:
basis_vectors = np.array(d1['basis_vectors'])
basis_vectors = d1['basis_vectors'][()]
if basis_vectors.shape == (2,3):
basis_vectors = basis_vectors.T
else:
@@ -244,11 +244,11 @@ def get_detector_geometry(cxi_file):
[-x_pixel_size,0,0]]).transpose()
try:
distance = np.float32(d1['distance'])
distance = np.float32(d1['distance'][()])
except:
distance = None
try:
corner_position = np.array(d1['corner_position'])
corner_position = d1['corner_position'][()]
except:
corner_position = None
@@ -292,7 +292,7 @@ def get_mask(cxi_file):
i1 = cxi_file['entry_1/instrument_1']
if 'detector_1/mask' in i1:
mask = np.array(i1['detector_1/mask']).astype(np.uint32)
mask = i1['detector_1/mask'][()].astype(np.uint32)
mask_on = np.equal(mask,np.uint32(0))
mask_has_signal = np.equal(mask,np.uint32(0x00001000))
return np.logical_or(mask_on,mask_has_signal).astype(bool)
@@ -324,7 +324,7 @@ def get_dark(cxi_file):
i1 = cxi_file['entry_1/instrument_1']
if 'detector_1/data_dark' in i1:
darks = np.array(i1['detector_1/data_dark'])
darks = i1['detector_1/data_dark'][()]
dims = tuple(range(len(darks.shape) - 2))
darks = np.nanmean(darks,axis=dims)
else:
@@ -426,7 +426,7 @@ def get_shot_to_shot_info(cxi_file, field_name):
else:
raise KeyError('Data is not defined within cxi file')
return np.array(cxi_file[pull_from]).astype(np.float32)
return cxi_file[pull_from][()].astype(np.float32)
def get_ptycho_translations(cxi_file):
@@ -851,11 +851,12 @@ def h5_to_nested_dict(h5_file):
for key in h5_file.keys():
value = h5_file[key]
if isinstance(value, h5py.Dataset):
arr = np.array(value)
arr = value[()]
if arr.dtype == object:
d[key] = arr.ravel()[0].decode('utf-8')
elif arr.ndim == 0:
d[key] = arr.ravel()[0]
# TODO is this needed with arr = value[()]?
d[key] = arr.ravel()[0]
else:
d[key] = arr
+15 -13
View File
@@ -182,11 +182,11 @@ def test_add_detector(tmp_path):
# Check this directly since we want to make sure it saved
# the pixel sizes
d1 = f['entry_1/instrument_1/detector_1']
read_basis = np.array(d1['basis_vectors'])
read_x_pix = np.float32(d1['x_pixel_size'])
read_y_pix = np.float32(d1['y_pixel_size'])
read_distance = np.float32(d1['distance'])
read_corner = np.array(d1['corner_position'])
read_basis = d1['basis_vectors'][()]
read_x_pix = d1['x_pixel_size'][()]
read_y_pix = d1['y_pixel_size'][()]
read_distance = d1['distance'][()]
read_corner = d1['corner_position'][()]
assert np.isclose(distance, read_distance)
assert np.allclose(basis, read_basis)
@@ -232,8 +232,8 @@ def test_add_data(tmp_path):
with h5py.File(tmp_path / 'test_add_data.cxi','r') as f:
# Check this directly since we want to make sure it saved
# it in all the places it should have
read_data_1 = np.array(f['entry_1/data_1/data'])
read_data_2 = np.array(f['entry_1/instrument_1/detector_1/data'])
read_data_1 = f['entry_1/data_1/data'][()]
read_data_2 = f['entry_1/instrument_1/detector_1/data'][()]
read_axes = str(f['entry_1/instrument_1/detector_1/data'].attrs['axes'].decode())
assert np.allclose(fake_data, read_data_1)
@@ -261,9 +261,10 @@ def test_add_shot_to_shot_info(tmp_path):
with h5py.File(tmp_path / 'test_add_shot_to_shot_info.cxi') as f:
# Check this directly since we want to make sure it saved
# it in all the places it should have
read_analyzer_1 = np.array(f['entry_1/data_1/analyzer_angle'])
read_analyzer_2 = np.array(f['entry_1/instrument_1/detector_1/analyzer_angle'])
read_analyzer_3 = np.array(f['entry_1/sample_1/geometry_1/analyzer_angle'])
read_analyzer_1 = f['entry_1/data_1/analyzer_angle'][()]
read_analyzer_2 = \
f['entry_1/instrument_1/detector_1/analyzer_angle'][()]
read_analyzer_3 = f['entry_1/sample_1/geometry_1/analyzer_angle'][()]
assert np.allclose(analyzer, read_analyzer_1)
assert np.allclose(analyzer, read_analyzer_2)
@@ -280,9 +281,10 @@ def test_add_ptycho_translations(tmp_path):
with h5py.File(tmp_path / 'test_add_ptycho_translations.cxi','r') as f:
# Check this directly since we want to make sure it saved
# it in all the places it should have
read_translations_1 = np.array(f['entry_1/data_1/translation'])
read_translations_2 = np.array(f['entry_1/instrument_1/detector_1/translation'])
read_translations_3 = np.array(f['entry_1/sample_1/geometry_1/translation'])
read_translations_1 = f['entry_1/data_1/translation'][()]
read_translations_2 = \
f['entry_1/instrument_1/detector_1/translation'][()]
read_translations_3 = f['entry_1/sample_1/geometry_1/translation'][()]
assert np.allclose(-translations, read_translations_1)
assert np.allclose(-translations, read_translations_2)
+1 -1
View File
@@ -6,7 +6,7 @@ import numpy as np
def test_intensity():
wavefields = t.rand((5,10,10)) + 1j * t.rand((5,10,10))
epsilon=1e-6
np_result = np.abs(t.as_tensor(wavefields))**2 + epsilon
np_result = np.abs(wavefields.numpy())**2 + epsilon
assert t.allclose(measurements.intensity(wavefields,epsilon=epsilon),
t.as_tensor(np_result))