Commit 617822eb authored by Matthijs's avatar Matthijs
Browse files

finishing up the tools move (previous commit)

parent ddd71f05
...@@ -65,6 +65,7 @@ def data_processor(config): ...@@ -65,6 +65,7 @@ def data_processor(config):
if config['dataprocessor_plotting']: if config['dataprocessor_plotting']:
XYZStackViewer(inp, cmap='viridis') XYZStackViewer(inp, cmap='viridis')
XYZStackViewer(shifted_fft(inp).real) XYZStackViewer(shifted_fft(inp).real)
# TODO: investigate why these viewers crash after a limited time
# Other settings # Other settings
config['fresnel_nr'] = 0 config['fresnel_nr'] = 0
...@@ -72,62 +73,5 @@ def data_processor(config): ...@@ -72,62 +73,5 @@ def data_processor(config):
config['use_farfield_formula'] = True config['use_farfield_formula'] = True
config['magn'] = 1 config['magn'] = 1
config['data_sq'] = abs(config['data']) ** 2 config['data_sq'] = abs(config['data']) ** 2
config['data_zeros'] = 1 # probably here I can put in the mask with NaNs (where we have no sensitivity
return config return config
def bin_array(arr: np.ndarray, new_shape: any, pad_zeros=True) -> np.ndarray:
"""
Reduce the size of an array by binning
:param arr: original
:param new_shape: tuple which must be an integer divisor of the original shape, or integer to bin by that factor
:return: new array
"""
# make tuple with new shape
if type(new_shape) == int: # binning factor is given
_shape = tuple([i // new_shape for i in arr.shape])
binfactor = tuple([new_shape for i in _shape])
else:
_shape = new_shape
binfactor = tuple([s // _shape[i] for i, s in enumerate(arr.shape)])
# determine if padding is needed
padding = tuple([(0, (binfactor[i] - s % binfactor[i]) % binfactor[i]) for i, s in enumerate(arr.shape)])
if pad_zeros and np.any(np.array(padding) != 0):
_arr = np.pad(arr, padding, mode='constant', constant_values=0) # pad array
_shape = tuple([s//binfactor[i] for i, s in enumerate(_arr.shape)]) # update binned size due to padding
else:
_arr = arr # expected to fail if padding has non-zeros
# send to 2d or 3d padding functions
try:
if len(arr.shape) == 2:
out = bin_2d_array(_arr, _shape)
elif len(arr.shape) == 3:
out = bin_3d_array(_arr, _shape)
else:
raise NotImplementedError('Cannot only bin 3d or 2d arrays')
return out
except ValueError:
raise ValueError("Cannot bin data with this shape. Try setting pad_zeros=True, or change the binning.")
def bin_3d_array(arr: np.ndarray, new_shape: tuple) -> np.ndarray:
""""
bins a 3D numpy array
Args:
arr: input array to be binned
new_shape: shape after binning, must be an integer divisor of the original shape
Returns:
binned np array
"""
shape = (new_shape[0], arr.shape[0] // new_shape[0],
new_shape[1], arr.shape[1] // new_shape[1],
new_shape[2], arr.shape[2] // new_shape[2])
if np.any(np.isnan(arr)):
binfactor = 1
for i, s in enumerate(arr.shape):
binfactor *= new_shape[i] / s
return np.nanmean(arr.reshape(shape), axis=(5, 3, 1)) * binfactor
else:
return np.sum(arr.reshape(shape), axis=(5, 3, 1))
...@@ -5,8 +5,6 @@ from proxtoolbox import Algorithms ...@@ -5,8 +5,6 @@ from proxtoolbox import Algorithms
from proxtoolbox import ProxOperators from proxtoolbox import ProxOperators
from proxtoolbox.ProxOperators.proxoperators import ProxOperator from proxtoolbox.ProxOperators.proxoperators import ProxOperator
from proxtoolbox.Problems.OrbitalTomog import Graphics from proxtoolbox.Problems.OrbitalTomog import Graphics
# from .proxoperators import ProxOperator
#from phase_retrieval.back_end_utilities import norm
from numpy.linalg import norm from numpy.linalg import norm
from numpy import square, sqrt from numpy import square, sqrt
...@@ -33,13 +31,6 @@ class Phase(Problem): ...@@ -33,13 +31,6 @@ class Phase(Problem):
data, data_norm, support, data, data_norm, support,
""" """
self.config = new_config self.config = new_config
#self.back_end = back_end
#call data processor, read data
# module = __import__(self.config['data_filename'])
# data_processor = getattr(module, self.config['data_filename'])
# data_processor(self.config)
if 'Nz' not in self.config: if 'Nz' not in self.config:
self.config['Nz'] = 1 self.config['Nz'] = 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment