Commit 93581134 authored by Matthijs's avatar Matthijs
Browse files

configured import *

and added pad_to_square from my_tools
parent 747e8f3f
from .array_tools import *
from .binning import *
from .interpolation import *
from .padding import *
......@@ -2,6 +2,8 @@ from numpy import roll, ndarray, floor, iscomplexobj, round
from scipy.ndimage.measurements import maximum_position, center_of_mass
from scipy.fftpack import fftn, fftshift, ifftn, ifftshift
__all__ = ["shift_array", 'roll_to_pos', 'shifted_ifft', 'shifted_fft']
def shift_array(arr: ndarray, dy: int, dx: int):
"""
......
import numpy as np
__all__ = ['bin_array']
def bin_array(arr: np.ndarray, new_shape: any, pad_zeros=True) -> np.ndarray:
"""
......@@ -20,7 +22,7 @@ def bin_array(arr: np.ndarray, new_shape: any, pad_zeros=True) -> np.ndarray:
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
_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
......
import numpy as np
from .array_tools import shifted_fft, shifted_ifft, roll_to_pos
__all__ = ['fourier_interpolate']
def fourier_interpolate(arr: np.ndarray, factor: any = 2., **kwargs) -> np.ndarray:
"""
......
from numpy import min, max, all, array, subtract, pad
__all__ = ['pad_to_square']
def pad_to_square(arr: array, new_shape: tuple = None, **kwargs) -> array:
"""
Given a numpy array of unequal dimensions,
pad with zeros until it all dimensions have equal length
:param arr: numpy array
:param new_shape: desired shape
:param kwargs: are passed on to np.pad
:return:
"""
old_shape = arr.shape
if max(old_shape) == min(old_shape) and new_shape is None:
return arr
if new_shape is None:
new_shape = tuple([max(old_shape) for dim in old_shape])
elif type(new_shape) == int:
new_shape = tuple(new_shape for dim in old_shape)
assert all(array(new_shape) >= array(old_shape)), 'New dimensions must be larger than old dimensions'
padding = subtract(new_shape, old_shape)
left_pad = padding // 2
right_pad = padding - left_pad
cv = kwargs.pop('constant_values', 0)
padmode = kwargs.pop('mode', 'constant')
return pad(arr, tuple([(left_pad[i], right_pad[i]) for i in range(len(left_pad))]),
mode=padmode, constant_values=cv, **kwargs)
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