KerrArray¶
- class Stoner.Image.KerrArray(*args)[source]¶
Bases:
Stoner.Image.core.ImageArray
A subclass for Kerr microscopy specific image functions.
Attributes Summary
Clone the image and then rotate the imaage 90 degrees counter clockwise.
Clone the image and then rotate the imaage 90 degrees clockwise.
The transposed array.
Return the aspect ratio (width/height) of the image.
Base object if memory is from some other object.
Class of the underlying data (read-only).
Return the coordinates of the centre of the image.
Duplicate the ImageFile and return the copy.
An object to simplify the interaction of the array with the ctypes module.
Returns the underlying data, as a view of the masked array.
Access the DrawProxy opbject for accessing the skimage draw sub module.
Data-type of the array's elements.
The filling value of the masked array is a scalar.
Information about the memory layout of the array.
Return the numpy.ndarray.flat rather than a MaskedIterator.
Clone the image and then mirror the image horizontally.
Clone the image and then mirror the image vertically.
Hardness of the mask
The imaginary part of the masked array.
Length of one array element in bytes.
Current mask.
Return the maximum coordinate extent (xmin,xmax,ymin,ymax).
Read the metadata dictionary.
Total bytes consumed by the elements of the array.
Number of array dimensions.
The real part of the masked array.
Get or set the mask of the array if it has no named fields.
Tuple of array dimensions.
Share status of the mask (read-only).
Number of elements in the array.
Tuple of bytes to step in each dimension when traversing an array.
Do a test call to tesseract to see if it is there and cache the result.
Get a title for this image.
Methods Summary
Rescale the intensity of the image.
Stoner__Image__imagefuncs__align
(ref[, method])Use one of a variety of algroithms to align two images.
Provide a consistent way to get at the underlying array data in both ImageArray and ImageFile objects.
Return the image converted to floating point type.
Stoner__Image__imagefuncs__asint
([dtype])Convert the image to unsigned integer format.
Clip intensity outside the range -1,1 or 0,1.
Clip negative pixels to 0.
Stoner__Image__imagefuncs__convert
(dtype[, ...])Convert an image to the requested data-type.
Align images to correct for image drift.
Stoner__Image__imagefuncs__crop
(*args, **kargs)Crop the image according to a box.
Stoner__Image__imagefuncs__denoise
([weight])Rename the skimage restore function.
Nulop function for testing the integration into ImageArray.
Return intensity limits, i.e. (min, max) tuple, of the image's dtype.
Stoner__Image__imagefuncs__fft
([shift, ...])Perform a 2d fft of the image and shift the result to get zero frequency in the centre.
Alias for skimage.filters.gaussian.
Use
scipy.interpolate.griddata()
to shift the image to a regular grid of co-ordinates.Stoner__Image__imagefuncs__hist
(*args, **kargs)Pass through to
matplotlib.pyplot.hist()
function.Stoner__Image__imagefuncs__imshow
(**kwargs)Quickly plot of image.
Subtract a polynomial background from image.
Norm alise the data to a fixed scale.
Plot the histogram and cumulative distribution for the image.
Wrap sckit-image method of the same name to get a line_profile.
Quantise the image data into fixed levels given by a mapping.
Rerurn a map of the radial co-ordinates of an image from a given centre, with adjustments for pixel size.
Extract a radial profile line from an image.
Find values of the data that are beyond a percentile of the overall distribution and replace them.
Stoner__Image__imagefuncs__rotate
(angle[, ...])Rotate image by a certain angle around its center.
Stoner__Image__imagefuncs__save
([filename])Save the image into the file 'filename'.
Stoner__Image__imagefuncs__save_npy
(filename)Save the ImageArray as a numpy array.
Stoner__Image__imagefuncs__save_png
(filename)Save the ImageArray with metadata in a png file.
Stoner__Image__imagefuncs__save_tiff
(filename)Save the ImageArray as a tiff image with metadata.
Implements a 2D Savitsky Golay Filter for a 2D array (e.g.
Return the minimum and maximum values in the image.
Subtract a background image from the ImageArray.
Return a boolean array which is thresholded between threshmin and threshmax.
Stoner__Image__imagefuncs__translate
(translation)Translate the image.
Find the limits of an image after a translation.
Crop the bottom text area from a standard Kermit image.
Try to create a boolean array which is a mask for typical defects found in Image images.
Create a mask array for a typical subtract Image image.
Convert image to float and crop_text.
Get the length in pixels of the image scale bar.
Use image recognition to try to pull the metadata numbers off the image.
Reduce the metadata down to a few useful pieces and do a bit of processing.
Stoner__Image__util__im_scale
(n, m, ...[, copy])Scaleunsigned/positive integers from n to m bits.
Stoner__Image__util__prec_loss
(dtypeobj)Warn over precision loss when converting image.
Stoner__Image__util__sign_loss
(dtypeobj)Warn over loss of sign information when converting image.
Stoner__compat__get_filedialog
(**opts)Wrap around Tk file dialog to mange creating file dialogs in a cross platform way.
Stoner__core__exceptions__assertion
([message])Raise an error when condition is false.
Resale the font size of a matplotlib text object to fit within a box.
Mark a function as one that changes the size of the ImageArray.
Mark a function as one that Should not be converted from an array to an ImageFile.
Stoner__tools__decorators__make_Data
(**kargs)Return an instance of Stoner.Data passig through constructor arguments.
Stoner__tools__null__null
(**kargs)A null function to be documented.
Stoner__tools__tests__isTuple
(*args[, strict])Determine if obj is a tuple of a certain signature.
Chack to see if a value is iterable.
adjust_contrast
([lims, percent])Rescale the intensity of the image.
align
(ref[, method])Use one of a variety of algroithms to align two images.
all
([axis, out, keepdims])Returns True if all elements evaluate to True.
anom
([axis, dtype])Compute the anomalies (deviations from the arithmetic mean) along the given axis.
any
([axis, out, keepdims])Returns True if any of the elements of a evaluate to True.
argmax
([axis, fill_value, out])Returns array of indices of the maximum values along the given axis.
argmin
([axis, fill_value, out])Return array of indices to the minimum values along the given axis.
argpartition
(kth[, axis, kind, order])Returns the indices that would partition this array.
argsort
([axis, kind, order, endwith, fill_value])Return an ndarray of indices that sort the array along the specified axis.
asarray
()Provide a consistent way to get at the underlying array data in both ImageArray and ImageFile objects.
asfloat
([normalise, clip, clip_negative])Return the image converted to floating point type.
asint
([dtype])Convert the image to unsigned integer format.
assertion
([message])Raise an error when condition is false.
astype
(dtype[, order, casting, subok, copy])Copy of the array, cast to a specified type.
auto_fit_fontsize
(width, height[, ...])Resale the font size of a matplotlib text object to fit within a box.
byteswap
([inplace])Swap the bytes of the array elements
Mark a function as one that changes the size of the ImageArray.
choose
(choices[, out, mode])Use an index array to construct a new array from a set of choices.
clear
()clip
([min, max, out])Return an array whose values are limited to
[min, max]
.clip_intensity
([clip_negative, limits])Clip intensity outside the range -1,1 or 0,1.
clip_neg
()Clip negative pixels to 0.
compress
(condition[, axis, out])Return a where condition is
True
.Return all the non-masked data as a 1-D array.
conj
()Complex-conjugate all elements.
Return the complex conjugate, element-wise.
convert
(dtype[, force_copy, uniform, normalise])Convert an image to the requested data-type.
copy
([order])Return a copy of the array.
correct_drift
(ref, **kargs)Align images to correct for image drift.
count
([axis, keepdims])Count the non-masked elements of the array along the given axis.
crop
(*args, **kargs)Crop the image according to a box.
crop_text
([copy])Crop the bottom text area from a standard Kermit image.
cumprod
([axis, dtype, out])Return the cumulative product of the array elements over the given axis.
cumsum
([axis, dtype, out])Return the cumulative sum of the array elements over the given axis.
defect_mask
([thresh, corner_thresh, radius, ...])Try to create a boolean array which is a mask for typical defects found in Image images.
defect_mask_subtract_image
([threshmin, ...])Create a mask array for a typical subtract Image image.
denoise
([weight])Rename the skimage restore function.
diagonal
([offset, axis1, axis2])Return specified diagonals.
Nulop function for testing the integration into ImageArray.
dot
(b[, out])Masked dot product of two arrays.
dtype_limits
([clip_negative])Return intensity limits, i.e. (min, max) tuple, of the image's dtype.
dump
(file)Dump a pickle of the array to the specified file.
dumps
()Returns the pickle of the array as a string.
fft
([shift, phase, remove_dc, gaussian, window])Perform a 2d fft of the image and shift the result to get zero frequency in the centre.
fill
(value)Fill the array with a scalar value.
filled
([fill_value])Return a copy of self, with masked values filled with a given value.
filter_image
([sigma])Alias for skimage.filters.gaussian.
flatten
([order])Return a copy of the array collapsed into one dimension.
Convert image to float and crop_text.
gaussian_filter
(sigma[, order, output, ...])Multidimensional Gaussian filter.
get
(k[,d])get_filedialog
(**opts)Wrap around Tk file dialog to mange creating file dialogs in a cross platform way.
The filling value of the masked array is a scalar.
get_imag
()The imaginary part of the masked array.
get_real
()The real part of the masked array.
Get the length in pixels of the image scale bar.
getfield
(dtype[, offset])Returns a field of the given array as a certain type.
griddata
(values, xi[, method, fill_value, ...])Interpolate unstructured D-D data.
gridimage
([points, xi, method, fill_value, ...])Use
scipy.interpolate.griddata()
to shift the image to a regular grid of co-ordinates.Force the mask to hard.
hist
(*args, **kargs)Pass through to
matplotlib.pyplot.hist()
function.ids
()Return the addresses of the data and mask areas.
im_scale
(n, m, dtypeobj_in, dtypeobj[, copy])Scaleunsigned/positive integers from n to m bits.
imshow
(**kwargs)Quickly plot of image.
isTuple
(*args[, strict])Determine if obj is a tuple of a certain signature.
Return a boolean indicating whether the data is contiguous.
Chack to see if a value is iterable.
item
(*args)Copy an element of an array to a standard Python scalar and return it.
items
()Make sure we implement an items that doesn't just iterate over self.
itemset
(*args)Insert scalar into an array (scalar is cast to array's dtype, if possible)
Mark a function as one that Should not be converted from an array to an ImageFile.
keys
()Return the keys of the metadata dictionary.
level_image
([poly_vert, poly_horiz, box, ...])Subtract a polynomial background from image.
make_Data
(**kargs)Return an instance of Stoner.Data passig through constructor arguments.
max
([axis, out, fill_value, keepdims])Return the maximum along a given axis.
mean
([axis, dtype, out, keepdims])Returns the average of the array elements along given axis.
min
([axis, out, fill_value, keepdims])Return the minimum along a given axis.
mini
([axis])Return the array minimum along the specified axis.
newbyteorder
([new_order])Return the array with the same data viewed with a different byte order.
nonzero
()Return the indices of unmasked elements that are not zero.
normalise
([scale, sample, limits, scale_masked])Norm alise the data to a fixed scale.
null
(**kargs)A null function to be documented.
ocr_metadata
([field_only])Use image recognition to try to pull the metadata numbers off the image.
partition
(kth[, axis, kind, order])Rearranges the elements in the array in such a way that the value of the element in kth position is in the position it would be in a sorted array.
plot_histogram
([bins])Plot the histogram and cumulative distribution for the image.
pop
(k[,d])If key is not found, d is returned if given, otherwise KeyError is raised.
popitem
()as a 2-tuple; but raise KeyError if D is empty.
prec_loss
(dtypeobj)Warn over precision loss when converting image.
prod
([axis, dtype, out, keepdims])Return the product of the array elements over the given axis.
product
([axis, dtype, out, keepdims])Return the product of the array elements over the given axis.
profile_line
([src, dst, linewidth, order, ...])Wrap sckit-image method of the same name to get a line_profile.
ptp
([axis, out, fill_value, keepdims])Return (maximum - minimum) along the given dimension (i.e.
put
(indices, values[, mode])Set storage-indexed locations to corresponding values.
quantize
(output[, levels])Quantise the image data into fixed levels given by a mapping.
radial_coordinates
([centre, pixel_size, angle])Rerurn a map of the radial co-ordinates of an image from a given centre, with adjustments for pixel size.
radial_profile
([angle, r, centre, pixel_size])Extract a radial profile line from an image.
ravel
([order])Returns a 1D version of self, as a view.
Reduce the metadata down to a few useful pieces and do a bit of processing.
remove_outliers
([percentiles, replace])Find values of the data that are beyond a percentile of the overall distribution and replace them.
repeat
(repeats[, axis])Repeat elements of an array.
reshape
(*s, **kwargs)Give a new shape to the array without changing its data.
resize
(newshape[, refcheck, order])rotate
(angle[, resize, center, order, mode, ...])Rotate image by a certain angle around its center.
round
([decimals, out])Return each element rounded to the given number of decimals.
save
([filename])Save the image into the file 'filename'.
save_npy
(filename)Save the ImageArray as a numpy array.
save_png
(filename)Save the ImageArray with metadata in a png file.
save_tiff
(filename[, forcetype])Save the ImageArray as a tiff image with metadata.
Interpolate unstructured D-D data.
Multidimensional Gaussian filter.
searchsorted
(v[, side, sorter])Find indices where elements of v should be inserted in a to maintain order.
set_fill_value
([value])setdefault
(k[,d])setfield
(val, dtype[, offset])Put a value into a specified place in a field defined by a data-type.
setflags
([write, align, uic])Set array flags WRITEABLE, ALIGNED, (WRITEBACKIFCOPY and UPDATEIFCOPY), respectively.
sgolay2d
([points, poly, derivative])Implements a 2D Savitsky Golay Filter for a 2D array (e.g.
Reduce a mask to nomask when possible.
sign_loss
(dtypeobj)Warn over loss of sign information when converting image.
Force the mask to soft.
sort
([axis, kind, order, endwith, fill_value])Sort the array, in-place
span
()Return the minimum and maximum values in the image.
squeeze
([axis])Remove axes of length one from a.
std
([axis, dtype, out, ddof, keepdims])Returns the standard deviation of the array elements along given axis.
subtract_image
(background[, contrast, clip, ...])Subtract a background image from the ImageArray.
sum
([axis, dtype, out, keepdims])Return the sum of the array elements over the given axis.
swapaxes
(axis1, axis2)Return a view of the array with axis1 and axis2 interchanged.
take
(indices[, axis, out, mode])threshold_minmax
([threshmin, threshmax])Return a boolean array which is thresholded between threshmin and threshmax.
tobytes
([fill_value, order])Return the array data as a string containing the raw bytes in the array.
tofile
(fid[, sep, format])Save a masked array to a file in binary format.
toflex
()Transforms a masked array into a flexible-type array.
tolist
([fill_value])Return the data portion of the masked array as a hierarchical Python list.
Transforms a masked array into a flexible-type array.
tostring
([fill_value, order])A compatibility alias for tobytes, with exactly the same behavior.
trace
([offset, axis1, axis2, dtype, out])Return the sum along diagonals of the array.
translate
(translation[, add_metadata, ...])Translate the image.
translate_limits
(translation[, reverse])Find the limits of an image after a translation.
transpose
(*axes)Returns a view of the array with axes transposed.
Copy the mask and set the sharedmask flag to False.
update
([E, ]**F)If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values
()Return the values of the metadata dictionary.
var
([axis, dtype, out, ddof, keepdims])Compute the variance along the specified axis.
view
([dtype, type, fill_value])Return a view of the MaskedArray data.
Attributes Documentation
- CCW¶
Clone the image and then rotate the imaage 90 degrees counter clockwise.
- CW¶
Clone the image and then rotate the imaage 90 degrees clockwise.
- LineSelect = <function LineSelect>¶
- StonerAssertionError = <function StonerAssertionError>¶
- Stoner__Image__widgets__LineSelect = <function LineSelect>¶
- Stoner__core__base__metadataObject = <function metadataObject>¶
- Stoner__core__base__typeHintedDict = <function typeHintedDict>¶
- Stoner__core__exceptions__StonerAssertionError = <function StonerAssertionError>¶
- T¶
- aspect¶
Return the aspect ratio (width/height) of the image.
- base¶
Base object if memory is from some other object.
The base of an array that owns its memory is None:
>>> x = np.array([1,2,3,4]) >>> x.base is None True
Slicing creates a view, whose memory is shared with x:
>>> y = x[2:] >>> y.base is x True
- baseclass¶
Class of the underlying data (read-only).
- centre¶
Return the coordinates of the centre of the image.
- clone¶
Duplicate the ImageFile and return the copy.
Using .clone allows further methods to modify the clone, allowing the original immage to be unmodified.
- ctypes¶
An object to simplify the interaction of the array with the ctypes module.
This attribute creates an object that makes it easier to use arrays when calling shared libraries with the ctypes module. The returned object has, among others, data, shape, and strides attributes (see Notes below) which themselves return ctypes objects that can be used as arguments to a shared library.
None
- cPython object
Possessing attributes data, shape, strides, etc.
numpy.ctypeslib
Below are the public attributes of this object which were documented in “Guide to NumPy” (we have omitted undocumented public attributes, as well as documented private attributes):
- _ctypes.data
A pointer to the memory area of the array as a Python integer. This memory area may contain data that is not aligned, or not in correct byte-order. The memory area may not even be writeable. The array flags and data-type of this array should be respected when passing this attribute to arbitrary C-code to avoid trouble that can include Python crashing. User Beware! The value of this attribute is exactly the same as
self._array_interface_['data'][0]
.Note that unlike
data_as
, a reference will not be kept to the array: code likectypes.c_void_p((a + b).ctypes.data)
will result in a pointer to a deallocated array, and should be spelt(a + b).ctypes.data_as(ctypes.c_void_p)
- _ctypes.shape
A ctypes array of length self.ndim where the basetype is the C-integer corresponding to
dtype('p')
on this platform. This base-type could be ctypes.c_int, ctypes.c_long, or ctypes.c_longlong depending on the platform. The c_intp type is defined accordingly in numpy.ctypeslib. The ctypes array contains the shape of the underlying array.- Type
(c_intp*self.ndim)
- _ctypes.strides
A ctypes array of length self.ndim where the basetype is the same as for the shape attribute. This ctypes array contains the strides information from the underlying array. This strides information is important for showing how many bytes must be jumped to get to the next element in the array.
- Type
(c_intp*self.ndim)
- _ctypes.data_as(obj)
Return the data pointer cast to a particular c-types object. For example, calling
self._as_parameter_
is equivalent toself.data_as(ctypes.c_void_p)
. Perhaps you want to use the data as a pointer to a ctypes array of floating-point data:self.data_as(ctypes.POINTER(ctypes.c_double))
.The returned pointer will keep a reference to the array.
- _ctypes.shape_as(obj)
Return the shape tuple as an array of some other c-types type. For example:
self.shape_as(ctypes.c_short)
.
- _ctypes.strides_as(obj)
Return the strides tuple as an array of some other c-types type. For example:
self.strides_as(ctypes.c_longlong)
.
If the ctypes module is not available, then the ctypes attribute of array objects still returns something useful, but ctypes objects are not returned and errors may be raised instead. In particular, the object will still have the
as_parameter
attribute which will return an integer equal to the data attribute.>>> import ctypes >>> x = np.array([[0, 1], [2, 3]], dtype=np.int32) >>> x array([[0, 1], [2, 3]], dtype=int32) >>> x.ctypes.data 31962608 # may vary >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) <__main__.LP_c_uint object at 0x7ff2fc1fc200> # may vary >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)).contents c_uint(0) >>> x.ctypes.data_as(ctypes.POINTER(ctypes.c_uint64)).contents c_ulong(4294967296) >>> x.ctypes.shape <numpy.core._internal.c_long_Array_2 object at 0x7ff2fc1fce60> # may vary >>> x.ctypes.strides <numpy.core._internal.c_long_Array_2 object at 0x7ff2fc1ff320> # may vary
- data¶
Returns the underlying data, as a view of the masked array.
If the underlying data is a subclass of
numpy.ndarray
, it is returned as such.>>> x = np.ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]]) >>> x.data matrix([[1, 2], [3, 4]])
The type of the data can be accessed through the
baseclass
attribute.
- debug = False¶
- draw¶
Access the DrawProxy opbject for accessing the skimage draw sub module.
- dtype¶
- filename = ''¶
- fill_value¶
The filling value of the masked array is a scalar. When setting, None will set to a default based on the data type.
>>> for dt in [np.int32, np.int64, np.float64, np.complex128]: ... np.ma.array([0, 1], dtype=dt).get_fill_value() ... 999999 999999 1e+20 (1e+20+0j)
>>> x = np.ma.array([0, 1.], fill_value=-np.inf) >>> x.fill_value -inf >>> x.fill_value = np.pi >>> x.fill_value 3.1415926535897931 # may vary
Reset to default:
>>> x.fill_value = None >>> x.fill_value 1e+20
- flags¶
Information about the memory layout of the array.
- C_CONTIGUOUS (C)
The data is in a single, C-style contiguous segment.
- F_CONTIGUOUS (F)
The data is in a single, Fortran-style contiguous segment.
- OWNDATA (O)
The array owns the memory it uses or borrows it from another object.
- WRITEABLE (W)
The data area can be written to. Setting this to False locks the data, making it read-only. A view (slice, etc.) inherits WRITEABLE from its base array at creation time, but a view of a writeable array may be subsequently locked while the base array remains writeable. (The opposite is not true, in that a view of a locked array may not be made writeable. However, currently, locking a base object does not lock any views that already reference it, so under that circumstance it is possible to alter the contents of a locked array via a previously created writeable view onto it.) Attempting to change a non-writeable array raises a RuntimeError exception.
- ALIGNED (A)
The data and all elements are aligned appropriately for the hardware.
- WRITEBACKIFCOPY (X)
This array is a copy of some other array. The C-API function PyArray_ResolveWritebackIfCopy must be called before deallocating to the base array will be updated with the contents of this array.
- UPDATEIFCOPY (U)
(Deprecated, use WRITEBACKIFCOPY) This array is a copy of some other array. When this array is deallocated, the base array will be updated with the contents of this array.
- FNC
F_CONTIGUOUS and not C_CONTIGUOUS.
- FORC
F_CONTIGUOUS or C_CONTIGUOUS (one-segment test).
- BEHAVED (B)
ALIGNED and WRITEABLE.
- CARRAY (CA)
BEHAVED and C_CONTIGUOUS.
- FARRAY (FA)
BEHAVED and F_CONTIGUOUS and not C_CONTIGUOUS.
The flags object can be accessed dictionary-like (as in
a.flags['WRITEABLE']
), or by using lowercased attribute names (as ina.flags.writeable
). Short flag names are only supported in dictionary access.Only the WRITEBACKIFCOPY, UPDATEIFCOPY, WRITEABLE, and ALIGNED flags can be changed by the user, via direct assignment to the attribute or dictionary entry, or by calling ndarray.setflags.
The array flags cannot be set arbitrarily:
UPDATEIFCOPY can only be set
False
.WRITEBACKIFCOPY can only be set
False
.ALIGNED can only be set
True
if the data is truly aligned.WRITEABLE can only be set
True
if the array owns its own memory or the ultimate owner of the memory exposes a writeable buffer interface or is a string.
Arrays can be both C-style and Fortran-style contiguous simultaneously. This is clear for 1-dimensional arrays, but can also be true for higher dimensional arrays.
Even for contiguous arrays a stride for a given dimension
arr.strides[dim]
may be arbitrary ifarr.shape[dim] == 1
or the array has no elements. It does not generally hold thatself.strides[-1] == self.itemsize
for C-style contiguous arrays orself.strides[0] == self.itemsize
for Fortran-style contiguous arrays is true.
- flat¶
Return the numpy.ndarray.flat rather than a MaskedIterator.
- flip_h¶
Clone the image and then mirror the image horizontally.
- flip_v¶
Clone the image and then mirror the image vertically.
- fmts = ['png', 'npy', 'tiff', 'tif']¶
- hardmask¶
Hardness of the mask
- imag¶
The imaginary part of the masked array.
This property is a view on the imaginary part of this MaskedArray.
real
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) >>> x.imag masked_array(data=[1.0, --, 1.6], mask=[False, True, False], fill_value=1e+20)
- itemsize¶
Length of one array element in bytes.
>>> x = np.array([1,2,3], dtype=np.float64) >>> x.itemsize 8 >>> x = np.array([1,2,3], dtype=np.complex128) >>> x.itemsize 16
- mask¶
Current mask.
- max_box¶
Return the maximum coordinate extent (xmin,xmax,ymin,ymax).
- metadata¶
Read the metadata dictionary.
- metadataObject = <function metadataObject>¶
- nbytes¶
Total bytes consumed by the elements of the array.
Does not include memory consumed by non-element attributes of the array object.
>>> x = np.zeros((3,5,2), dtype=np.complex128) >>> x.nbytes 480 >>> np.prod(x.shape) * x.itemsize 480
- ndim¶
Number of array dimensions.
>>> x = np.array([1, 2, 3]) >>> x.ndim 1 >>> y = np.zeros((2, 3, 4)) >>> y.ndim 3
- real¶
The real part of the masked array.
This property is a view on the real part of this MaskedArray.
imag
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) >>> x.real masked_array(data=[1.0, --, 3.45], mask=[False, True, False], fill_value=1e+20)
- recordmask¶
Get or set the mask of the array if it has no named fields. For structured arrays, returns a ndarray of booleans where entries are
True
if all the fields are masked,False
otherwise:>>> x = np.ma.array([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], ... mask=[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], ... dtype=[('a', int), ('b', int)]) >>> x.recordmask array([False, False, True, False, False])
- shape¶
Share status of the mask (read-only).
- size¶
Number of elements in the array.
Equal to
np.prod(a.shape)
, i.e., the product of the array’s dimensions.a.size returns a standard arbitrary precision Python integer. This may not be the case with other methods of obtaining the same value (like the suggested
np.prod(a.shape)
, which returns an instance ofnp.int_
), and may be relevant if the value is used further in calculations that may overflow a fixed size integer type.>>> x = np.zeros((3, 5, 2), dtype=np.complex128) >>> x.size 30 >>> np.prod(x.shape) 30
- strides¶
Tuple of bytes to step in each dimension when traversing an array.
The byte offset of element
(i[0], i[1], ..., i[n])
in an array a is:offset = sum(np.array(i) * a.strides)
A more detailed explanation of strides can be found in the “ndarray.rst” file in the NumPy reference guide.
Imagine an array of 32-bit integers (each 4 bytes):
x = np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]], dtype=np.int32)
This array is stored in memory as 40 bytes, one after the other (known as a contiguous block of memory). The strides of an array tell us how many bytes we have to skip in memory to move to the next position along a certain axis. For example, we have to skip 4 bytes (1 value) to move to the next column, but 20 bytes (5 values) to get to the same position in the next row. As such, the strides for the array x will be
(20, 4)
.numpy.lib.stride_tricks.as_strided
>>> y = np.reshape(np.arange(2*3*4), (2,3,4)) >>> y array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> y.strides (48, 16, 4) >>> y[1,1,1] 17 >>> offset=sum(y.strides * np.array((1,1,1))) >>> offset/y.itemsize 17
>>> x = np.reshape(np.arange(5*6*7*8), (5,6,7,8)).transpose(2,3,1,0) >>> x.strides (32, 4, 224, 1344) >>> i = np.array([3,5,2,2]) >>> offset = sum(i * x.strides) >>> x[3,5,2,2] 813 >>> offset / x.itemsize 813
- tesseractable¶
Do a test call to tesseract to see if it is there and cache the result.
- title¶
Get a title for this image.
- typeHintedDict = <function typeHintedDict>¶
Methods Documentation
- Stoner__Image__imagefuncs__adjust_contrast(lims=(0.1, 0.9), percent=True)¶
Rescale the intensity of the image.
Mostly a call through to skimage.exposure.rescale_intensity. The absolute limits of contrast are added to the metadata as ‘adjust_contrast’
- lims: 2-tuple
limits of rescaling the intensity
- percent: bool
if True then lims are the give the percentile of the image intensity histogram, otherwise lims are absolute
- image: ImageArray
rescaled image
- Stoner__Image__imagefuncs__align(ref, method='scharr', **kargs)¶
Use one of a variety of algroithms to align two images.
- Parameters
im (ndarray)
ref (ndarray)
- Keyword Arguments
method (str or None) – If given specifies which module to try and use. Options: ‘scharr’, ‘chi2_shift’, ‘imreg_dft’, ‘cv2’
_box (integer, float, tuple of images or floats) – Used with ImageArray.crop to select a subset of the image to use for the aligning process.
scale (int) – Rescale the image and reference image by constant factor before finding the translation vector.
prefilter (callable) – A method to apply to the image before carrying out the translation to the align to the reference.
**kargs (various) – All other keyword arguments are passed to the specific algorithm.
- Returns
(ImageArray or ndarray) aligned image
Notes
- Currently three algorithms are supported:
image_registration module’s chi^2 shift: This uses a dft with an automatic up-sampling of the fourier transform for sub-pixel alignment. The metadata key chi2_shift contains the translation vector and errors.
imreg_dft module’s similarity function. This implements a full scale, rotation, translation algorithm (by default cosntrained for just translation). It’s unclear how much sub-pixel translation is accomodated.
cv2 module based affine transform on a gray scale image. from: http://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/
- Stoner__Image__imagefuncs__asarray()¶
Provide a consistent way to get at the underlying array data in both ImageArray and ImageFile objects.
- Stoner__Image__imagefuncs__asfloat(normalise=True, clip=False, clip_negative=False)¶
Return the image converted to floating point type.
If currently an int type and normalise then floats will be normalised to the maximum allowed value of the int type. If currently a float type then no change occurs. If clip then clip values outside the range -1,1 If clip_negative then further clip values to range 0,1
- Stoner__Image__imagefuncs__asint(dtype=<class 'numpy.uint16'>)¶
Convert the image to unsigned integer format.
May raise warnings about loss of precision.
- Stoner__Image__imagefuncs__clip_intensity(clip_negative=False, limits=None)¶
Clip intensity outside the range -1,1 or 0,1.
- Keyword Arguments
clip_negative (bool) – if True clip to range 0,1 else range -1,1
limits (low,high) – Clip the intensity between low and high rather than zero and 1.
Ensure data range is -1 to 1 or 0 to 1 if clip_negative is True.
- Stoner__Image__imagefuncs__clip_neg()¶
Clip negative pixels to 0.
Most useful for float where pixels above 1 are reduced to 1.0 and -ve pixels are changed to 0.
- Stoner__Image__imagefuncs__convert(dtype, force_copy=False, uniform=False, normalise=True)¶
Convert an image to the requested data-type.
Warnings are issued in case of precision loss, or when negative values are clipped during conversion to unsigned integer types (sign loss).
Floating point values are expected to be normalized and will be clipped to the range [0.0, 1.0] or [-1.0, 1.0] when converting to unsigned or signed integers respectively.
Numbers are not shifted to the negative side when converting from unsigned to signed integer types. Negative values will be clipped when converting to unsigned integers.
- imagendarray
Input image.
- dtypedtype
Target data-type.
- force_copybool
Force a copy of the data, irrespective of its current dtype.
- uniformbool
Uniformly quantize the floating point range to the integer range. By default (uniform=False) floating point values are scaled and rounded to the nearest integers, which minimizes back and forth conversion errors.
- normalisebool
When converting from int types to float normalise the resulting array by the maximum allowed value of the int type.
DirectX data conversion rules. http://msdn.microsoft.com/en-us/library/windows/desktop/dd607323%28v=vs.85%29.aspx
Data Conversions. In “OpenGL ES 2.0 Specification v2.0.25”, pp 7-8. Khronos Group, 2010.
Proper treatment of pixels as integers. A.W. Paeth. In “Graphics Gems I”, pp 249-256. Morgan Kaufmann, 1990.
Dirty Pixels. J. Blinn. In “Jim Blinn’s corner: Dirty Pixels”, pp 47-57. Morgan Kaufmann, 1998.
- Stoner__Image__imagefuncs__correct_drift(ref, **kargs)¶
Align images to correct for image drift.
- Parameters
ref (ImageArray) – Reference image with assumed zero drift
- Keyword Arguments
threshold (float) – threshold for detecting imperfections in images (see skimage.feature.corner_fast for details)
upsample_factor (float) – the resolution for the shift 1/upsample_factor pixels registered. see skimage.feature.register_translation for more details
box (sequence of 4 ints) – defines a region of the image to use for identifyign the drift defaults to the whol image. Use this to avoid drift calculations being confused by the scale bar/annotation region.
do_shift (bool) – Shift the image, or just calculate the drift and store in metadata (default True, shit)
- Returns
A shifted iamge with the image shift added to the metadata as ‘correct drift’.
Detects common features on the images and tracks them moving. Adds ‘drift_shift’ to the metadata as the (x,y) vector that translated the image back to it’s origin.
- Stoner__Image__imagefuncs__crop(*args, **kargs)¶
Crop the image according to a box.
- Parameters
box (tuple) – (xmin,xmax,ymin,ymax) If None image will be shown and user will be asked to select a box (bit experimental)
- Keyword Arguments
copy (bool) – If True return a copy of ImageFile with the cropped image
- Returns
(ImageArray) – view or copy of array asked for
Notes
This is essentially like taking a view onto the array but uses image x,y coords (x,y –> col,row) Returns a view according to the coords given. If box is None it will allow the user to select a rectangle. If a tuple is given with None included then max extent is used for that coord (analagous to slice). If copy then return a copy of self with the cropped image.
- The box can be specified in a number of ways:
(int): A border around all sides of the given number pixels is ignored.
(float 0.0-1.0): A border of the given fraction of the images height and width is ignored
(string): A correspoinding item of metadata is located and used to specify the box
(tuple of 4 ints or floats): For each item in the tuple it is interpreted as foloows:
(int): A pixel co-ordinate in either the x or y direction
(float 0.0-1.0): A fraction of the width or height in from the left, right, top, bottom sides
(float > 1.0): Is rounded to the nearest integer and used a pixel cordinate.
None: The extent of the image is used.
Example
a=ImageFile(np.arange(12).reshape(3,4))
a.crop(1,3,None,None)
- Stoner__Image__imagefuncs__denoise(weight=0.1)¶
Rename the skimage restore function.
- Stoner__Image__imagefuncs__do_nothing()¶
Nulop function for testing the integration into ImageArray.
- Stoner__Image__imagefuncs__dtype_limits(clip_negative=True)¶
Return intensity limits, i.e. (min, max) tuple, of the image’s dtype.
- Parameters
image (ndarray) – Input image.
clip_negative (bool) – If True, clip the negative range (i.e. return 0 for min intensity) even if the image dtype allows negative values.
- Returns
(imin, imax – tuple): Lower and upper intensity limits.
- Stoner__Image__imagefuncs__fft(shift=True, phase=False, remove_dc=False, gaussian=None, window=None)¶
Perform a 2d fft of the image and shift the result to get zero frequency in the centre.
- Keyword Arguments
shift (bool) – Shift the fft so that zero order is in the centre of the image. Default True
phase (bool, None) – If true, return the phase angle rather than the magnitude if False. If None, return the raw fft. Default False - return magnitude of fft.
remove_dc (bool) – Replace the points around the dc offset with the mean of the fft to avoid dc offset artefacts. Default False
gaussian (None or float) – Apply a gaussian blur to the fft where this parameter is the width of the blue in px. Default None for off.
window (None or str) – If not None (default) the image is multiplied by the given window function before the fft is calculated. This avpoids leaking some signal into the higher frequency bands due to discontinuities at the image edges.
- Returns
fft of the image, preserving metadata.
- Stoner__Image__imagefuncs__filter_image(sigma=2)¶
Alias for skimage.filters.gaussian.
- Stoner__Image__imagefuncs__gridimage(points=None, xi=None, method='linear', fill_value=None, rescale=False)¶
Use
scipy.interpolate.griddata()
to shift the image to a regular grid of co-ordinates.- Parameters
points (tuple of (x-co-ords,yco-ordsa)) – The actual sampled data co-ordinates
xi (tupe of (2D array,2D array)) – The regular grid co-ordinates (as generated by e.g.
np.meshgrid()
)
- Keyword Arguments
method ("linear","cubic","nearest") – how to interpolate, default is linear
fill_value (folat, Callable, None) – What to put when the co-ordinates go out of range (default is None). May be a callable in which case the initial image is presented as the only argument. If None, use the mean value.
rescale (bool) – If the x and y co-ordinates are very different in scale, set this to True.
- Returns
A copy of the modified image. The image data is interpolated and metadata kets “actual_x”,”actual_y”,”sample_ x”,”samp[le_y” are set to give co-ordinates of new grid.
Notes
If points and or xi are missed out then we try to construct them from the metadata. For points, the metadata keys “actual-x” and “actual_y” are looked for and for xi, the metadata keys “sample_x” and “sample_y” are used. These are set, for example, by the
Stoner.HDF5.SXTMImage
loader if the interformeter stage data was found in the file.The metadata used in this case is then adjusted as well to ensure that repeated application of this method doesn’t change the image after it has been corrected once.
- Stoner__Image__imagefuncs__hist(*args, **kargs)¶
Pass through to
matplotlib.pyplot.hist()
function.
- Stoner__Image__imagefuncs__imshow(**kwargs)¶
Quickly plot of image.
- Keyword Arguments
figure (int, str or matplotlib.figure) – if int then use figure number given, if figure is ‘new’ then create a new figure, if None then use whatever default figure is available
show_axis (bool) – If True, show the axis otherwise don’t (default)’
title (str,None,False) – Title for plot - defaults to False (no title). None will take the title from the filename if present
title_args (dict) – Arguments to pass to the title function to control formatting.
cmap (str,matplotlib.cmap) – Colour scheme for plot, defaults to gray
Any masked areas are set to NaN which stops them being plotted at all.
- Stoner__Image__imagefuncs__level_image(poly_vert=1, poly_horiz=1, box=None, poly=None, mode='clip')¶
Subtract a polynomial background from image.
- Keword Arguments:
- poly_vert (int): fit a polynomial in the vertical direction for the image of order
given. If 0 do not fit or subtract in the vertical direction
- poly_horiz (int): fit a polynomial of order poly_horiz to the image. If 0 given
do not subtract
- box (array, list or tuple of int): [xmin,xmax,ymin,ymax] define region for fitting. IF None use entire
image
- poly (list or None): [pvert, phoriz] pvert and phoriz are arrays of polynomial coefficients
(highest power first) to subtract in the horizontal and vertical directions. If None function defaults to fitting its own polynomial.
- mode (str): Either ‘clip’ or ‘norm’ - specifies how to handle intensitry values that end up being outside
of the accepted range for the image.
- Returns
A new copy of the processed images.
Fit and subtract a background to the image. Fits a polynomial of order given in the horizontal and vertical directions and subtracts. If box is defined then level the entire image according to the gradient within the box. The polynomial subtracted is added to the metadata as ‘poly_vert_subtract’ and ‘poly_horiz_subtract’
- Stoner__Image__imagefuncs__normalise(scale=None, sample=False, limits=(0.0, 1.0), scale_masked=False)¶
Norm alise the data to a fixed scale.
- Keyword Arguements:
- scale (2-tuple):
The range to scale the image to, defaults to -1 to 1.
- saple (box):
Only use a section of the input image to calculate the new scale over. Default is False - whole image
- limits (low,high):
Take the input range from the high and low fraction of the input when sorted.
- scale_masked (bool):
If True then the masked region is also scaled, otherwise any masked region is ignored. Default, False.
- Returns
A scaled version of the data. The ndarray min and max methods are used to allow masked images to be operated on only on the unmasked areas.
Notes
The sample keyword controls the area in which the range of input values is calculated, the actual scaling is done on the whole image.
The limits parameter is used to set the input scale being normalised from - if an image has a few outliers then this setting can be used to clip the input range before normalising. The parameters in the limit are the values at the low and high fractions of the cumulative distribution functions.
- Stoner__Image__imagefuncs__plot_histogram(bins=256)¶
Plot the histogram and cumulative distribution for the image.
- Stoner__Image__imagefuncs__profile_line(src=None, dst=None, linewidth=1, order=1, mode='constant', cval=0.0, constrain=True, **kargs)¶
Wrap sckit-image method of the same name to get a line_profile.
- Parameters
img (ImageArray) – Image data to take line section of
- Keyword Parameters:
- src, dst (2-tuple of int or float):
start and end of line profile. If the co-ordinates are given as intergers then they are assumed to be pxiel co-ordinates, floats are assumed to be real-space co-ordinates using the embedded metadata.
- linewidth (int):
the wideth of the profile to be taken.
- order (int 1-3):
Order of interpolation used to find image data when not aligned to a point
- mode (str):
How to handle data outside of the image.
- cval (float):
The constant value to assume for data outside of the image is mode is “constant”
- constrain (bool):
Ensure the src and dst are within the image (default True).
- no_scale (bool):
Do not attempt to scale values by the image scale and work in pixels throughout. (default False)
- Returns
A
Stoner.Data
object containing the line profile data and the metadata from the image.
- Stoner__Image__imagefuncs__quantize(output, levels=None)¶
Quantise the image data into fixed levels given by a mapping.
- Parameters
output (list,array,tuple) – Output levels to return.
- Keyword Arguments
levels (list, array or None) – The input band markers. If None is constructed from the data.
The number of levels should be one less than the number of output levels given.
Notes
The routine will ignore all masked pixels and will preserve the mask.
- Stoner__Image__imagefuncs__radial_coordinates(centre=(None, None), pixel_size=(1, 1), angle=False)¶
Rerurn a map of the radial co-ordinates of an image from a given centre, with adjustments for pixel size.
- Keyword Arguments
centre (2-tuple) – Co-ordinates of centre point in terms of the orginal pixels. Defaults to(None,None) for the middle of the image.
pixel_size (2-tuple) – The size of one pixel in (dx by dy) - defaults to 1,1
angle (bool, None) – Whether to return the angles (in radians, True), distances (False) o a complex number (None).
- Returns
An array of the same class as the input, but with values corresponding to the radial co-ordinates.
- Stoner__Image__imagefuncs__radial_profile(angle=None, r=None, centre=(None, None), pixel_size=(1, 1))¶
Extract a radial profile line from an image.
- Keyword Paramaters:
- angle (float, tuple, None):
- Select the radial angle to include:
float selects a single angle
tuple selects a range of angles
None integrates over all angles
- r (array, None):
Edges of the bins in the radual direction - will return r.size-1 points. Default is None which uses the minimum r value found on the edges of the image.
- centre (2-tuple):
Co-ordinates of centre point in terms of the orginal pixels. Defaults to(None,None) for the middle of the image.
- pixel_size (2-tuple):
The size of one pixel in (dx by dy) - defaults to 1,1
- Retunrs:
- (Data):
A py:class:Stoner.Data object with a column for r and columns for mean, std, and number of pixels.
- Stoner__Image__imagefuncs__remove_outliers(percentiles=(0.01, 0.99), replace=None)¶
Find values of the data that are beyond a percentile of the overall distribution and replace them.
- Keyword Parameters:
- percentile (2 tuple):
Fraction percentiles to consider to be outliers (default is (0.01,0.99) for 1% limits)
- replace (2 tuple or None):
Values to set outliers to. If None, then the pixel values at the percentile limits are used.
- Returns
(ndarray) – Tje modified array.
Use this method if you have an image with a small number of pixels with extreme values that are out of range.
- Stoner__Image__imagefuncs__rotate(angle, resize=False, center=None, order=1, mode='constant', cval=0, clip=True, preserve_range=False)¶
Rotate image by a certain angle around its center.
- Parameters
angle (float) – Rotation angle in radians in clockwise direction.
- Keyword Parameters:
- resize (bool):
Determine whether the shape of the output image will be automatically calculated, so the complete rotated image exactly fits. Default is False.
- center (iterable of length 2):
The rotation center. If
center=None
, the image is rotated around its center, i.e.center=(cols / 2 - 0.5, rows / 2 - 0.5)
. Please note that this parameter is (cols, rows), contrary to normal skimage ordering.- order (int):
The order of the spline interpolation, default is 1. The order has to be in the range 0-5. See skimage.transform.warp for detail.
- mode ({‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’}):
Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of numpy.pad.
- cval (float):
Used in conjunction with mode ‘constant’, the value outside the image boundaries.
- clip (bool):
Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range.
- preserve_range (bool):
Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of Stpomer.Image.ImageArray.as_float.
- Returns
(ImageFile/ImageArray) – Rotated image
Notes
(pass through to the skimage.transform.warps.rotate function)
- Stoner__Image__imagefuncs__save(filename=None, **kargs)¶
Save the image into the file ‘filename’.
- Parameters
filename (string, bool or None) – Filename to save data as, if this is None then the current filename for the object is used If this is not set, then then a file dialog is used. If filename is False then a file dialog is forced.
- Keyword Arguments
Notes
Metadata will be preserved in .png and .tif format.
fmt can be ‘png’, ‘npy’, ‘tif’, ‘tiff’ or a list of more than one of those. tif is recommended since metadata is lost in .npy format but data is converted to integer format for png so that definition cannot be saved.
Since Stoner.Image is meant to be a general 2d array often with negative and floating point data this poses a problem for saving images. Images are naturally saved as 8 or more bit unsigned integer values representing colour. The only obvious way to save an image and preserve negative data is to save as a float32 tif. This has the advantage over the npy data type which cannot be opened by external programs and will not save metadata.
- Stoner__Image__imagefuncs__save_npy(filename)¶
Save the ImageArray as a numpy array.
- Stoner__Image__imagefuncs__save_png(filename)¶
Save the ImageArray with metadata in a png file.
This can only save as 8bit unsigned integer so there is likely to be a loss of precision on floating point data
- Stoner__Image__imagefuncs__save_tiff(filename, forcetype=False)¶
Save the ImageArray as a tiff image with metadata.
- Parameters
filename (str) – Filename to save file as.
- Keyword Arguments
forcetype (bool) – (depricated) if forcetype then preserve data type as best as possible on save. Otherwise we let the underlying pillow library choose the best data type.
Note
PIL can save in modes “L” (8bit unsigned int), “I” (32bit signed int), or “F” (32bit signed float). In general max info is preserved for “F” type so if forcetype is not specified then this is the default. For boolean type data mode “L” will suffice and this is chosen in all cases. The type name is added as a string to the metadata before saving.
- Stoner__Image__imagefuncs__sgolay2d(points=15, poly=1, derivative=None)¶
Implements a 2D Savitsky Golay Filter for a 2D array (e.g. image).
- Parameters
img (ImageArray or ImageFile) – image to be filtered
- Keyword Arguments
points (int) – The number of points in the window aperture. Must be an odd number. (default 15)
poly (int) – Degree of polynomial to use in the filter. (defatult 1)
- Type of defivative to calculate. Can be:
None - smooth only (default) “x”,”y” - calculate dIntentity/dx or dIntensity/dy “both” - calculate the full derivative and return magnitud and angle.
- ReturnsL
- (imageArray or ImageFile):
filtered image.
- Raises
ValueError if points, order or derivative are incorrect. –
Notes
Adapted from code on the scipy cookbook : https://scipy-cookbook.readthedocs.io/items/SavitzkyGolay.html
- Stoner__Image__imagefuncs__span()¶
Return the minimum and maximum values in the image.
- Stoner__Image__imagefuncs__subtract_image(background, contrast=16, clip=True, offset=0.5)¶
Subtract a background image from the ImageArray.
Multiply the contrast by the contrast parameter. If clip is on then clip the intensity after for the maximum allowed data range.
- Stoner__Image__imagefuncs__threshold_minmax(threshmin=0.1, threshmax=0.9)¶
Return a boolean array which is thresholded between threshmin and threshmax.
(ie True if value is between threshmin and threshmax)
- Stoner__Image__imagefuncs__translate(translation, add_metadata=False, order=3, mode='wrap', cval=None)¶
Translate the image.
Areas lost by move are cropped, and areas gained are made black (0) The area not lost or cropped is added as a metadata parameter ‘translation_limits’
- Parameters
translate (2-tuple) – translation (x,y)
- Keyword Arguments
add_metadata (bool) – Record the shift in the image metadata order (int): Interpolation order (default, 3, bi-cubic)
mode (str) – How to handle points outside the original image. See
skimage.transform.warp()
. Defaults to “wrap”cval (float) – The value to fill with if mode is constant. If not speficied or None, defaults to the mean pixcel value.
- Returns
im (ImageArray) – translated image
- Stoner__Image__imagefuncs__translate_limits(translation, reverse=False)¶
Find the limits of an image after a translation.
After using ImageArray.translate some areas will be black, this finds the max area that still has original pixels in
- Parameters
translation – 2-tuple the (x,y) translation applied to the image
- Keyword Arguments
reverse (bool) – whether to reverse the translation vector (default False, no)
- Returns
limits –
- 4-tuple
(xmin,xmax,ymin,ymax) the maximum coordinates of the image with original information
- Stoner__Image__kerrfuncs__crop_text(copy=False)¶
Crop the bottom text area from a standard Kermit image.
- KeywordArguments:
- copy(bool):
Whether to return a copy of the data or the original data
Returns: (ImageArray):
cropped image
- Stoner__Image__kerrfuncs__defect_mask(thresh=0.6, corner_thresh=0.05, radius=1, return_extra=False)¶
Try to create a boolean array which is a mask for typical defects found in Image images.
Best for unprocessed raw images. (for subtract images see defect_mask_subtract_image) Looks for big bright things by thresholding and small and dark defects using skimage’s corner_fast algorithm
Parameters: thresh (float):
brighter stuff than this gets removed (after image levelling)
- corner_thresh (float):
see corner_fast (skimage):
- radius (float):
radius of pixels around corners that are added to mask
- return_extra (bool):
this returns a dictionary with some of the intermediate steps of the calculation
- Returns
totmask (ndarray of bool) – mask
- info (optional dict):
dictionary of intermediate calculation steps
- Stoner__Image__kerrfuncs__defect_mask_subtract_image(threshmin=0.25, threshmax=0.9, denoise_weight=0.1, return_extra=False)¶
Create a mask array for a typical subtract Image image.
Uses a denoise algorithm followed by simple thresholding.
- Returns
totmask (ndarray of bool) – the created mask info (optional dict):
the intermediate denoised image
- Stoner__Image__kerrfuncs__float_and_croptext()¶
Convert image to float and crop_text.
Just to group typical functions together
- Stoner__Image__kerrfuncs__get_scalebar()¶
Get the length in pixels of the image scale bar.
- Stoner__Image__kerrfuncs__ocr_metadata(field_only=False)¶
Use image recognition to try to pull the metadata numbers off the image.
- Requirements:
This function uses tesseract to recognise the image, therefore tesseract file1 file2 must be valid on your command line. Install tesseract from https://sourceforge.net/projects/tesseract-ocr-alt/files/?source=navbar
- KeywordArguments:
- field_only(bool):
only try to return a field value
- Returns
metadata –
- dict
updated metadata dictionary
- Stoner__Image__kerrfuncs__reduce_metadata()¶
Reduce the metadata down to a few useful pieces and do a bit of processing.
- Returns
(
typeHintedDict
) – the new metadata
- Stoner__Image__util__im_scale(n, m, dtypeobj_in, dtypeobj, copy=True)¶
Scaleunsigned/positive integers from n to m bits.
Numbers can be represented exactly only if m is a multiple of n Output array is of same kind as input.
- Stoner__Image__util__prec_loss(dtypeobj)¶
Warn over precision loss when converting image.
- Stoner__Image__util__sign_loss(dtypeobj)¶
Warn over loss of sign information when converting image.
- Stoner__compat__get_filedialog(**opts)¶
Wrap around Tk file dialog to mange creating file dialogs in a cross platform way.
- Parameters
what (str) – What sort of a dialog to create - options are ‘file’,’directory’,’save’,’files’
**opts (dict) – Arguments to pass through to the underlying dialog function.
- Returns
A file name or directory or list of files.
- Stoner__core__exceptions__assertion(message='Library Assertion Error set')¶
Raise an error when condition is false.
A utility functiuon to be used when assert might have been.
- Stoner__plot__utils__auto_fit_fontsize(width, height, scale_down=True, scale_up=False)¶
Resale the font size of a matplotlib text object to fit within a box.
- Parameters
text (matplotlib.text.Text) – Text object to be scaled in Figure units.
width,height (float) – Target width and height to scale to.
- Keyword Arguments
scale_up (scale_down,) – Whether to reduce the font size to fit (default True), or increase it to fit (default False)
- Returns
(float) – scaling factor applied.
- Stoner__tools__decorators__changes_size()¶
Mark a function as one that changes the size of the ImageArray.
- Stoner__tools__decorators__keep_return_type()¶
Mark a function as one that Should not be converted from an array to an ImageFile.
- Stoner__tools__decorators__make_Data(**kargs)¶
Return an instance of Stoner.Data passig through constructor arguments.
Calling make_Data(None) is a speical case to return the Data class ratther than an instance
- Stoner__tools__null__null(**kargs)¶
A null function to be documented.
- Stoner__tools__tests__isTuple(*args: type, strict: bool = True) bool ¶
Determine if obj is a tuple of a certain signature.
- Parameters
obj (object) – The object to check
*args (type) – Each of the suceeding arguments are used to determine the expected type of each element.
- Keywoprd Arguments:
- strict(bool):
Whether the elements of the tuple have to be exactly the type specified or just castable as the type
- Returns
(bool) – True if obj is a matching tuple.
- Stoner__tools__tests__isiterable() bool ¶
Chack to see if a value is iterable.
- Parameters
value – Entitiy to check if it is iterable
- Returns
(bool) – True if value is an instance of collections.Iterable.
- adjust_contrast(lims=(0.1, 0.9), percent=True)¶
Rescale the intensity of the image.
Mostly a call through to skimage.exposure.rescale_intensity. The absolute limits of contrast are added to the metadata as ‘adjust_contrast’
- lims: 2-tuple
limits of rescaling the intensity
- percent: bool
if True then lims are the give the percentile of the image intensity histogram, otherwise lims are absolute
- image: ImageArray
rescaled image
- align(ref, method='scharr', **kargs)¶
Use one of a variety of algroithms to align two images.
- Parameters
im (ndarray)
ref (ndarray)
- Keyword Arguments
method (str or None) – If given specifies which module to try and use. Options: ‘scharr’, ‘chi2_shift’, ‘imreg_dft’, ‘cv2’
_box (integer, float, tuple of images or floats) – Used with ImageArray.crop to select a subset of the image to use for the aligning process.
scale (int) – Rescale the image and reference image by constant factor before finding the translation vector.
prefilter (callable) – A method to apply to the image before carrying out the translation to the align to the reference.
**kargs (various) – All other keyword arguments are passed to the specific algorithm.
- Returns
(ImageArray or ndarray) aligned image
Notes
- Currently three algorithms are supported:
image_registration module’s chi^2 shift: This uses a dft with an automatic up-sampling of the fourier transform for sub-pixel alignment. The metadata key chi2_shift contains the translation vector and errors.
imreg_dft module’s similarity function. This implements a full scale, rotation, translation algorithm (by default cosntrained for just translation). It’s unclear how much sub-pixel translation is accomodated.
cv2 module based affine transform on a gray scale image. from: http://www.learnopencv.com/image-alignment-ecc-in-opencv-c-python/
- all(axis=None, out=None, keepdims=<no value>)¶
Returns True if all elements evaluate to True.
The output array is masked where all the values along the given axis are masked: if the output would have been a scalar and that all the values are masked, then the output is masked.
Refer to numpy.all for full documentation.
numpy.ndarray.all : corresponding function for ndarrays numpy.all : equivalent function
>>> np.ma.array([1,2,3]).all() True >>> a = np.ma.array([1,2,3], mask=True) >>> (a.all() is np.ma.masked) True
- anom(axis=None, dtype=None)¶
Compute the anomalies (deviations from the arithmetic mean) along the given axis.
Returns an array of anomalies, with the same shape as the input and where the arithmetic mean is computed along the given axis.
- axisint, optional
Axis over which the anomalies are taken. The default is to use the mean of the flattened array as reference.
- dtypedtype, optional
- Type to use in computing the variance. For arrays of integer type
the default is float32; for arrays of float types it is the same as the array type.
mean : Compute the mean of the array.
>>> a = np.ma.array([1,2,3]) >>> a.anom() masked_array(data=[-1., 0., 1.], mask=False, fill_value=1e+20)
- any(axis=None, out=None, keepdims=<no value>)¶
Returns True if any of the elements of a evaluate to True.
Masked values are considered as False during computation.
Refer to numpy.any for full documentation.
numpy.ndarray.any : corresponding function for ndarrays numpy.any : equivalent function
- argmax(axis=None, fill_value=None, out=None)¶
Returns array of indices of the maximum values along the given axis. Masked values are treated as if they had the value fill_value.
- axis{None, integer}
If None, the index is into the flattened array, otherwise along the specified axis
- fill_valuescalar or None, optional
Value used to fill in the masked values. If None, the output of maximum_fill_value(self._data) is used instead.
- out{None, array}, optional
Array into which the result can be placed. Its type is preserved and it must be of the right shape to hold the output.
index_array : {integer_array}
>>> a = np.arange(6).reshape(2,3) >>> a.argmax() 5 >>> a.argmax(0) array([1, 1, 1]) >>> a.argmax(1) array([2, 2])
- argmin(axis=None, fill_value=None, out=None)¶
Return array of indices to the minimum values along the given axis.
- axis{None, integer}
If None, the index is into the flattened array, otherwise along the specified axis
- fill_valuescalar or None, optional
Value used to fill in the masked values. If None, the output of minimum_fill_value(self._data) is used instead.
- out{None, array}, optional
Array into which the result can be placed. Its type is preserved and it must be of the right shape to hold the output.
- ndarray or scalar
If multi-dimension input, returns a new ndarray of indices to the minimum values along the given axis. Otherwise, returns a scalar of index to the minimum values along the given axis.
>>> x = np.ma.array(np.arange(4), mask=[1,1,0,0]) >>> x.shape = (2,2) >>> x masked_array( data=[[--, --], [2, 3]], mask=[[ True, True], [False, False]], fill_value=999999) >>> x.argmin(axis=0, fill_value=-1) array([0, 0]) >>> x.argmin(axis=0, fill_value=9) array([1, 1])
- argpartition(kth, axis=- 1, kind='introselect', order=None)¶
Returns the indices that would partition this array.
Refer to numpy.argpartition for full documentation.
New in version 1.8.0.
numpy.argpartition : equivalent function
- argsort(axis=<no value>, kind=None, order=None, endwith=True, fill_value=None)¶
Return an ndarray of indices that sort the array along the specified axis. Masked values are filled beforehand to fill_value.
- axisint, optional
Axis along which to sort. If None, the default, the flattened array is used.
Changed in version 1.13.0: Previously, the default was documented to be -1, but that was in error. At some future date, the default will change to -1, as originally intended. Until then, the axis should be given explicitly when
arr.ndim > 1
, to avoid a FutureWarning.- kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional
The sorting algorithm used.
- orderlist, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. Not all fields need be specified.
- endwith{True, False}, optional
Whether missing values (if any) should be treated as the largest values (True) or the smallest values (False) When the array contains unmasked values at the same extremes of the datatype, the ordering of these values and the masked values is undefined.
- fill_valuescalar or None, optional
Value used internally for the masked values. If
fill_value
is not None, it supersedesendwith
.
- index_arrayndarray, int
Array of indices that sort a along the specified axis. In other words,
a[index_array]
yields a sorted a.
ma.MaskedArray.sort : Describes sorting algorithms used. lexsort : Indirect stable sort with multiple keys. numpy.ndarray.sort : Inplace sort.
See sort for notes on the different sorting algorithms.
>>> a = np.ma.array([3,2,1], mask=[False, False, True]) >>> a masked_array(data=[3, 2, --], mask=[False, False, True], fill_value=999999) >>> a.argsort() array([1, 0, 2])
- asarray()¶
Provide a consistent way to get at the underlying array data in both ImageArray and ImageFile objects.
- asfloat(normalise=True, clip=False, clip_negative=False)¶
Return the image converted to floating point type.
If currently an int type and normalise then floats will be normalised to the maximum allowed value of the int type. If currently a float type then no change occurs. If clip then clip values outside the range -1,1 If clip_negative then further clip values to range 0,1
- asint(dtype=<class 'numpy.uint16'>)¶
Convert the image to unsigned integer format.
May raise warnings about loss of precision.
- assertion(message='Library Assertion Error set')¶
Raise an error when condition is false.
A utility functiuon to be used when assert might have been.
- astype(dtype, order='K', casting='unsafe', subok=True, copy=True)¶
Copy of the array, cast to a specified type.
- dtypestr or dtype
Typecode or data-type to which the array is cast.
- order{‘C’, ‘F’, ‘A’, ‘K’}, optional
Controls the memory layout order of the result. ‘C’ means C order, ‘F’ means Fortran order, ‘A’ means ‘F’ order if all the arrays are Fortran contiguous, ‘C’ order otherwise, and ‘K’ means as close to the order the array elements appear in memory as possible. Default is ‘K’.
- casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional
Controls what kind of data casting may occur. Defaults to ‘unsafe’ for backwards compatibility.
‘no’ means the data types should not be cast at all.
‘equiv’ means only byte-order changes are allowed.
‘safe’ means only casts which can preserve values are allowed.
‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.
‘unsafe’ means any data conversions may be done.
- subokbool, optional
If True, then sub-classes will be passed-through (default), otherwise the returned array will be forced to be a base-class array.
- copybool, optional
By default, astype always returns a newly allocated array. If this is set to false, and the dtype, order, and subok requirements are satisfied, the input array is returned instead of a copy.
- arr_tndarray
Unless copy is False and the other conditions for returning the input array are satisfied (see description for copy input parameter), arr_t is a new array of the same shape as the input array, with dtype, order given by dtype, order.
Changed in version 1.17.0: Casting between a simple data type and a structured one is possible only for “unsafe” casting. Casting to multiple fields is allowed, but casting from multiple fields is not.
Changed in version 1.9.0: Casting from numeric to string types in ‘safe’ casting mode requires that the string dtype length is long enough to store the max integer/float value converted.
- ComplexWarning
When casting from complex to float or int. To avoid this, one should use
a.real.astype(t)
.
>>> x = np.array([1, 2, 2.5]) >>> x array([1. , 2. , 2.5])
>>> x.astype(int) array([1, 2, 2])
- auto_fit_fontsize(width, height, scale_down=True, scale_up=False)¶
Resale the font size of a matplotlib text object to fit within a box.
- Parameters
text (matplotlib.text.Text) – Text object to be scaled in Figure units.
width,height (float) – Target width and height to scale to.
- Keyword Arguments
scale_up (scale_down,) – Whether to reduce the font size to fit (default True), or increase it to fit (default False)
- Returns
(float) – scaling factor applied.
- byteswap(inplace=False)¶
Swap the bytes of the array elements
Toggle between low-endian and big-endian data representation by returning a byteswapped array, optionally swapped in-place. Arrays of byte-strings are not swapped. The real and imaginary parts of a complex number are swapped individually.
- inplacebool, optional
If
True
, swap bytes in-place, default isFalse
.
- outndarray
The byteswapped array. If inplace is
True
, this is a view to self.
>>> A = np.array([1, 256, 8755], dtype=np.int16) >>> list(map(hex, A)) ['0x1', '0x100', '0x2233'] >>> A.byteswap(inplace=True) array([ 256, 1, 13090], dtype=int16) >>> list(map(hex, A)) ['0x100', '0x1', '0x3322']
Arrays of byte-strings are not swapped
>>> A = np.array([b'ceg', b'fac']) >>> A.byteswap() array([b'ceg', b'fac'], dtype='|S3')
A.newbyteorder().byteswap()
produces an array with the same valuesbut different representation in memory
>>> A = np.array([1, 2, 3]) >>> A.view(np.uint8) array([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0], dtype=uint8) >>> A.newbyteorder().byteswap(inplace=True) array([1, 2, 3]) >>> A.view(np.uint8) array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3], dtype=uint8)
- changes_size()¶
Mark a function as one that changes the size of the ImageArray.
- choose(choices, out=None, mode='raise')¶
Use an index array to construct a new array from a set of choices.
Refer to numpy.choose for full documentation.
numpy.choose : equivalent function
- clear() None. Remove all items from D. ¶
- clip(min=None, max=None, out=None, **kwargs)¶
Return an array whose values are limited to
[min, max]
. One of max or min must be given.Refer to numpy.clip for full documentation.
numpy.clip : equivalent function
- clip_intensity(clip_negative=False, limits=None)¶
Clip intensity outside the range -1,1 or 0,1.
- Keyword Arguments
clip_negative (bool) – if True clip to range 0,1 else range -1,1
limits (low,high) – Clip the intensity between low and high rather than zero and 1.
Ensure data range is -1 to 1 or 0 to 1 if clip_negative is True.
- clip_neg()¶
Clip negative pixels to 0.
Most useful for float where pixels above 1 are reduced to 1.0 and -ve pixels are changed to 0.
- compress(condition, axis=None, out=None)¶
Return a where condition is
True
.If condition is a ~ma.MaskedArray, missing values are considered as
False
.- conditionvar
Boolean 1-d array selecting which entries to return. If len(condition) is less than the size of a along the axis, then output is truncated to length of condition array.
- axis{None, int}, optional
Axis along which the operation must be performed.
- out{None, ndarray}, optional
Alternative output array in which to place the result. It must have the same shape as the expected output but the type will be cast if necessary.
- resultMaskedArray
A
MaskedArray
object.
Please note the difference with
compressed()
! The output ofcompress()
has a mask, the output ofcompressed()
does not.>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.compress([1, 0, 1]) masked_array(data=[1, 3], mask=[False, False], fill_value=999999)
>>> x.compress([1, 0, 1], axis=1) masked_array( data=[[1, 3], [--, --], [7, 9]], mask=[[False, False], [ True, True], [False, False]], fill_value=999999)
- compressed()¶
Return all the non-masked data as a 1-D array.
- datandarray
A new ndarray holding the non-masked data is returned.
The result is not a MaskedArray!
>>> x = np.ma.array(np.arange(5), mask=[0]*2 + [1]*3) >>> x.compressed() array([0, 1]) >>> type(x.compressed()) <class 'numpy.ndarray'>
- conj()¶
Complex-conjugate all elements.
Refer to numpy.conjugate for full documentation.
numpy.conjugate : equivalent function
- conjugate()¶
Return the complex conjugate, element-wise.
Refer to numpy.conjugate for full documentation.
numpy.conjugate : equivalent function
- convert(dtype, force_copy=False, uniform=False, normalise=True)¶
Convert an image to the requested data-type.
Warnings are issued in case of precision loss, or when negative values are clipped during conversion to unsigned integer types (sign loss).
Floating point values are expected to be normalized and will be clipped to the range [0.0, 1.0] or [-1.0, 1.0] when converting to unsigned or signed integers respectively.
Numbers are not shifted to the negative side when converting from unsigned to signed integer types. Negative values will be clipped when converting to unsigned integers.
- imagendarray
Input image.
- dtypedtype
Target data-type.
- force_copybool
Force a copy of the data, irrespective of its current dtype.
- uniformbool
Uniformly quantize the floating point range to the integer range. By default (uniform=False) floating point values are scaled and rounded to the nearest integers, which minimizes back and forth conversion errors.
- normalisebool
When converting from int types to float normalise the resulting array by the maximum allowed value of the int type.
DirectX data conversion rules. http://msdn.microsoft.com/en-us/library/windows/desktop/dd607323%28v=vs.85%29.aspx
Data Conversions. In “OpenGL ES 2.0 Specification v2.0.25”, pp 7-8. Khronos Group, 2010.
Proper treatment of pixels as integers. A.W. Paeth. In “Graphics Gems I”, pp 249-256. Morgan Kaufmann, 1990.
Dirty Pixels. J. Blinn. In “Jim Blinn’s corner: Dirty Pixels”, pp 47-57. Morgan Kaufmann, 1998.
- copy(order='C')¶
Return a copy of the array.
- order{‘C’, ‘F’, ‘A’, ‘K’}, optional
Controls the memory layout of the copy. ‘C’ means C-order, ‘F’ means F-order, ‘A’ means ‘F’ if a is Fortran contiguous, ‘C’ otherwise. ‘K’ means match the layout of a as closely as possible. (Note that this function and
numpy.copy()
are very similar but have different default values for their order= arguments, and this function always passes sub-classes through.)
numpy.copy : Similar function with different default behavior numpy.copyto
This function is the preferred method for creating an array copy. The function
numpy.copy()
is similar, but it defaults to using order ‘K’, and will not pass sub-classes through by default.>>> x = np.array([[1,2,3],[4,5,6]], order='F')
>>> y = x.copy()
>>> x.fill(0)
>>> x array([[0, 0, 0], [0, 0, 0]])
>>> y array([[1, 2, 3], [4, 5, 6]])
>>> y.flags['C_CONTIGUOUS'] True
- correct_drift(ref, **kargs)¶
Align images to correct for image drift.
- Parameters
ref (ImageArray) – Reference image with assumed zero drift
- Keyword Arguments
threshold (float) – threshold for detecting imperfections in images (see skimage.feature.corner_fast for details)
upsample_factor (float) – the resolution for the shift 1/upsample_factor pixels registered. see skimage.feature.register_translation for more details
box (sequence of 4 ints) – defines a region of the image to use for identifyign the drift defaults to the whol image. Use this to avoid drift calculations being confused by the scale bar/annotation region.
do_shift (bool) – Shift the image, or just calculate the drift and store in metadata (default True, shit)
- Returns
A shifted iamge with the image shift added to the metadata as ‘correct drift’.
Detects common features on the images and tracks them moving. Adds ‘drift_shift’ to the metadata as the (x,y) vector that translated the image back to it’s origin.
- count(axis=None, keepdims=<no value>)¶
Count the non-masked elements of the array along the given axis.
- axisNone or int or tuple of ints, optional
Axis or axes along which the count is performed. The default, None, performs the count over all the dimensions of the input array. axis may be negative, in which case it counts from the last to the first axis.
New in version 1.10.0.
If this is a tuple of ints, the count is performed on multiple axes, instead of a single axis or all the axes as before.
- keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
- resultndarray or scalar
An array with the same shape as the input array, with the specified axis removed. If the array is a 0-d array, or if axis is None, a scalar is returned.
ma.count_masked : Count masked elements in array or along a given axis.
>>> import numpy.ma as ma >>> a = ma.arange(6).reshape((2, 3)) >>> a[1, :] = ma.masked >>> a masked_array( data=[[0, 1, 2], [--, --, --]], mask=[[False, False, False], [ True, True, True]], fill_value=999999) >>> a.count() 3
When the axis keyword is specified an array of appropriate size is returned.
>>> a.count(axis=0) array([1, 1, 1]) >>> a.count(axis=1) array([3, 0])
- crop(*args, **kargs)¶
Crop the image according to a box.
- Parameters
box (tuple) – (xmin,xmax,ymin,ymax) If None image will be shown and user will be asked to select a box (bit experimental)
- Keyword Arguments
copy (bool) – If True return a copy of ImageFile with the cropped image
- Returns
(ImageArray) – view or copy of array asked for
Notes
This is essentially like taking a view onto the array but uses image x,y coords (x,y –> col,row) Returns a view according to the coords given. If box is None it will allow the user to select a rectangle. If a tuple is given with None included then max extent is used for that coord (analagous to slice). If copy then return a copy of self with the cropped image.
- The box can be specified in a number of ways:
(int): A border around all sides of the given number pixels is ignored.
(float 0.0-1.0): A border of the given fraction of the images height and width is ignored
(string): A correspoinding item of metadata is located and used to specify the box
(tuple of 4 ints or floats): For each item in the tuple it is interpreted as foloows:
(int): A pixel co-ordinate in either the x or y direction
(float 0.0-1.0): A fraction of the width or height in from the left, right, top, bottom sides
(float > 1.0): Is rounded to the nearest integer and used a pixel cordinate.
None: The extent of the image is used.
Example
a=ImageFile(np.arange(12).reshape(3,4))
a.crop(1,3,None,None)
- crop_text(copy=False)¶
Crop the bottom text area from a standard Kermit image.
- KeywordArguments:
- copy(bool):
Whether to return a copy of the data or the original data
Returns: (ImageArray):
cropped image
- cumprod(axis=None, dtype=None, out=None)¶
Return the cumulative product of the array elements over the given axis.
Masked values are set to 1 internally during the computation. However, their position is saved, and the result will be masked at the same locations.
Refer to numpy.cumprod for full documentation.
The mask is lost if out is not a valid MaskedArray !
Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.cumprod : corresponding function for ndarrays numpy.cumprod : equivalent function
- cumsum(axis=None, dtype=None, out=None)¶
Return the cumulative sum of the array elements over the given axis.
Masked values are set to 0 internally during the computation. However, their position is saved, and the result will be masked at the same locations.
Refer to numpy.cumsum for full documentation.
The mask is lost if out is not a valid
ma.MaskedArray
!Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.cumsum : corresponding function for ndarrays numpy.cumsum : equivalent function
>>> marr = np.ma.array(np.arange(10), mask=[0,0,0,1,1,1,0,0,0,0]) >>> marr.cumsum() masked_array(data=[0, 1, 3, --, --, --, 9, 16, 24, 33], mask=[False, False, False, True, True, True, False, False, False, False], fill_value=999999)
- defect_mask(thresh=0.6, corner_thresh=0.05, radius=1, return_extra=False)¶
Try to create a boolean array which is a mask for typical defects found in Image images.
Best for unprocessed raw images. (for subtract images see defect_mask_subtract_image) Looks for big bright things by thresholding and small and dark defects using skimage’s corner_fast algorithm
Parameters: thresh (float):
brighter stuff than this gets removed (after image levelling)
- corner_thresh (float):
see corner_fast (skimage):
- radius (float):
radius of pixels around corners that are added to mask
- return_extra (bool):
this returns a dictionary with some of the intermediate steps of the calculation
- Returns
totmask (ndarray of bool) – mask
- info (optional dict):
dictionary of intermediate calculation steps
- defect_mask_subtract_image(threshmin=0.25, threshmax=0.9, denoise_weight=0.1, return_extra=False)¶
Create a mask array for a typical subtract Image image.
Uses a denoise algorithm followed by simple thresholding.
- Returns
totmask (ndarray of bool) – the created mask info (optional dict):
the intermediate denoised image
- denoise(weight=0.1)¶
Rename the skimage restore function.
- diagonal(offset=0, axis1=0, axis2=1)¶
Return specified diagonals. In NumPy 1.9 the returned array is a read-only view instead of a copy as in previous NumPy versions. In a future version the read-only restriction will be removed.
Refer to
numpy.diagonal()
for full documentation.numpy.diagonal : equivalent function
- do_nothing()¶
Nulop function for testing the integration into ImageArray.
- dot(b, out=None)¶
Masked dot product of two arrays. Note that out and strict are located in different positions than in ma.dot. In order to maintain compatibility with the functional version, it is recommended that the optional arguments be treated as keyword only. At some point that may be mandatory.
New in version 1.10.0.
- bmasked_array_like
Inputs array.
- outmasked_array, optional
Output argument. This must have the exact kind that would be returned if it was not used. In particular, it must have the right type, must be C-contiguous, and its dtype must be the dtype that would be returned for ma.dot(a,b). This is a performance feature. Therefore, if these conditions are not met, an exception is raised, instead of attempting to be flexible.
- strictbool, optional
Whether masked data are propagated (True) or set to 0 (False) for the computation. Default is False. Propagating the mask means that if a masked value appears in a row or column, the whole row or column is considered masked.
New in version 1.10.2.
numpy.ma.dot : equivalent function
- dtype_limits(clip_negative=True)¶
Return intensity limits, i.e. (min, max) tuple, of the image’s dtype.
- Parameters
image (ndarray) – Input image.
clip_negative (bool) – If True, clip the negative range (i.e. return 0 for min intensity) even if the image dtype allows negative values.
- Returns
(imin, imax – tuple): Lower and upper intensity limits.
- dump(file)¶
Dump a pickle of the array to the specified file. The array can be read back with pickle.load or numpy.load.
- filestr or Path
A string naming the dump file.
Changed in version 1.17.0: pathlib.Path objects are now accepted.
- dumps()¶
Returns the pickle of the array as a string. pickle.loads or numpy.loads will convert the string back to an array.
None
- fft(shift=True, phase=False, remove_dc=False, gaussian=None, window=None)¶
Perform a 2d fft of the image and shift the result to get zero frequency in the centre.
- Keyword Arguments
shift (bool) – Shift the fft so that zero order is in the centre of the image. Default True
phase (bool, None) – If true, return the phase angle rather than the magnitude if False. If None, return the raw fft. Default False - return magnitude of fft.
remove_dc (bool) – Replace the points around the dc offset with the mean of the fft to avoid dc offset artefacts. Default False
gaussian (None or float) – Apply a gaussian blur to the fft where this parameter is the width of the blue in px. Default None for off.
window (None or str) – If not None (default) the image is multiplied by the given window function before the fft is calculated. This avpoids leaking some signal into the higher frequency bands due to discontinuities at the image edges.
- Returns
fft of the image, preserving metadata.
- fill(value)¶
Fill the array with a scalar value.
- valuescalar
All elements of a will be assigned this value.
>>> a = np.array([1, 2]) >>> a.fill(0) >>> a array([0, 0]) >>> a = np.empty(2) >>> a.fill(1) >>> a array([1., 1.])
- filled(fill_value=None)¶
Return a copy of self, with masked values filled with a given value. However, if there are no masked values to fill, self will be returned instead as an ndarray.
- fill_valuearray_like, optional
The value to use for invalid entries. Can be scalar or non-scalar. If non-scalar, the resulting ndarray must be broadcastable over input array. Default is None, in which case, the fill_value attribute of the array is used instead.
- filled_arrayndarray
A copy of
self
with invalid entries replaced by fill_value (be it the function argument or the attribute ofself
), orself
itself as an ndarray if there are no invalid entries to be replaced.
The result is not a MaskedArray!
>>> x = np.ma.array([1,2,3,4,5], mask=[0,0,1,0,1], fill_value=-999) >>> x.filled() array([ 1, 2, -999, 4, -999]) >>> x.filled(fill_value=1000) array([ 1, 2, 1000, 4, 1000]) >>> type(x.filled()) <class 'numpy.ndarray'>
Subclassing is preserved. This means that if, e.g., the data part of the masked array is a recarray, filled returns a recarray:
>>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray) >>> m = np.ma.array(x, mask=[(True, False), (False, True)]) >>> m.filled() rec.array([(999999, 2), ( -3, 999999)], dtype=[('f0', '<i8'), ('f1', '<i8')])
- filter_image(sigma=2)¶
Alias for skimage.filters.gaussian.
- flatten(order='C')¶
Return a copy of the array collapsed into one dimension.
- order{‘C’, ‘F’, ‘A’, ‘K’}, optional
‘C’ means to flatten in row-major (C-style) order. ‘F’ means to flatten in column-major (Fortran- style) order. ‘A’ means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K’ means to flatten a in the order the elements occur in memory. The default is ‘C’.
- yndarray
A copy of the input array, flattened to one dimension.
ravel : Return a flattened array. flat : A 1-D flat iterator over the array.
>>> a = np.array([[1,2], [3,4]]) >>> a.flatten() array([1, 2, 3, 4]) >>> a.flatten('F') array([1, 3, 2, 4])
- float_and_croptext()¶
Convert image to float and crop_text.
Just to group typical functions together
- gaussian_filter(sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0)¶
Multidimensional Gaussian filter.
- inputarray_like
The input array.
- sigmascalar or sequence of scalars
Standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.
- orderint or sequence of ints, optional
The order of the filter along each axis is given as a sequence of integers, or as a single number. An order of 0 corresponds to convolution with a Gaussian kernel. A positive order corresponds to convolution with that derivative of a Gaussian.
- outputarray or dtype, optional
The array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created.
- modestr or sequence, optional
The mode parameter determines how the input array is extended when the filter overlaps a border. By passing a sequence of modes with length equal to the number of dimensions of the input array, different modes can be specified along each axis. Default value is ‘reflect’. The valid values and their behavior is as follows:
- ‘reflect’ (d c b a | a b c d | d c b a)
The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.
- ‘constant’ (k k k k | a b c d | k k k k)
The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.
- ‘nearest’ (a a a a | a b c d | d d d d)
The input is extended by replicating the last pixel.
- ‘mirror’ (d c b | a b c d | c b a)
The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.
- ‘wrap’ (a b c d | a b c d | a b c d)
The input is extended by wrapping around to the opposite edge.
For consistency with the interpolation functions, the following mode names can also be used:
- ‘grid-constant’
This is a synonym for ‘constant’.
- ‘grid-mirror’
This is a synonym for ‘reflect’.
- ‘grid-wrap’
This is a synonym for ‘wrap’.
- cvalscalar, optional
Value to fill past edges of input if mode is ‘constant’. Default is 0.0.
- truncatefloat
Truncate the filter at this many standard deviations. Default is 4.0.
- gaussian_filterndarray
Returned array of same shape as input.
The multidimensional filter is implemented as a sequence of 1-D convolution filters. The intermediate arrays are stored in the same data type as the output. Therefore, for output types with a limited precision, the results may be imprecise because intermediate results may be stored with insufficient precision.
>>> from scipy.ndimage import gaussian_filter >>> a = np.arange(50, step=2).reshape((5,5)) >>> a array([[ 0, 2, 4, 6, 8], [10, 12, 14, 16, 18], [20, 22, 24, 26, 28], [30, 32, 34, 36, 38], [40, 42, 44, 46, 48]]) >>> gaussian_filter(a, sigma=1) array([[ 4, 6, 8, 9, 11], [10, 12, 14, 15, 17], [20, 22, 24, 25, 27], [29, 31, 33, 34, 36], [35, 37, 39, 40, 42]])
>>> from scipy import misc >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.gray() # show the filtered result in grayscale >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = misc.ascent() >>> result = gaussian_filter(ascent, sigma=5) >>> ax1.imshow(ascent) >>> ax2.imshow(result) >>> plt.show()
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- get_filedialog(**opts)¶
Wrap around Tk file dialog to mange creating file dialogs in a cross platform way.
- Parameters
what (str) – What sort of a dialog to create - options are ‘file’,’directory’,’save’,’files’
**opts (dict) – Arguments to pass through to the underlying dialog function.
- Returns
A file name or directory or list of files.
- get_fill_value()¶
The filling value of the masked array is a scalar. When setting, None will set to a default based on the data type.
>>> for dt in [np.int32, np.int64, np.float64, np.complex128]: ... np.ma.array([0, 1], dtype=dt).get_fill_value() ... 999999 999999 1e+20 (1e+20+0j)
>>> x = np.ma.array([0, 1.], fill_value=-np.inf) >>> x.fill_value -inf >>> x.fill_value = np.pi >>> x.fill_value 3.1415926535897931 # may vary
Reset to default:
>>> x.fill_value = None >>> x.fill_value 1e+20
- get_imag()¶
The imaginary part of the masked array.
This property is a view on the imaginary part of this MaskedArray.
real
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) >>> x.imag masked_array(data=[1.0, --, 1.6], mask=[False, True, False], fill_value=1e+20)
- get_real()¶
The real part of the masked array.
This property is a view on the real part of this MaskedArray.
imag
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) >>> x.real masked_array(data=[1.0, --, 3.45], mask=[False, True, False], fill_value=1e+20)
- get_scalebar()¶
Get the length in pixels of the image scale bar.
- getfield(dtype, offset=0)¶
Returns a field of the given array as a certain type.
A field is a view of the array data with a given data-type. The values in the view are determined by the given type and the offset into the current array in bytes. The offset needs to be such that the view dtype fits in the array dtype; for example an array of dtype complex128 has 16-byte elements. If taking a view with a 32-bit integer (4 bytes), the offset needs to be between 0 and 12 bytes.
- dtypestr or dtype
The data type of the view. The dtype size of the view can not be larger than that of the array itself.
- offsetint
Number of bytes to skip before beginning the element view.
>>> x = np.diag([1.+1.j]*2) >>> x[1, 1] = 2 + 4.j >>> x array([[1.+1.j, 0.+0.j], [0.+0.j, 2.+4.j]]) >>> x.getfield(np.float64) array([[1., 0.], [0., 2.]])
By choosing an offset of 8 bytes we can select the complex part of the array for our view:
>>> x.getfield(np.float64, offset=8) array([[1., 0.], [0., 4.]])
- griddata(values, xi, method='linear', fill_value=nan, rescale=False)¶
Interpolate unstructured D-D data.
- points2-D ndarray of floats with shape (n, D), or length D tuple of 1-D ndarrays with shape (n,).
Data point coordinates.
- valuesndarray of float or complex, shape (n,)
Data values.
- xi2-D ndarray of floats with shape (m, D), or length D tuple of ndarrays broadcastable to the same shape.
Points at which to interpolate data.
- method{‘linear’, ‘nearest’, ‘cubic’}, optional
Method of interpolation. One of
nearest
return the value at the data point closest to the point of interpolation. See NearestNDInterpolator for more details.
linear
tessellate the input point set to N-D simplices, and interpolate linearly on each simplex. See LinearNDInterpolator for more details.
cubic
(1-D)return the value determined from a cubic spline.
cubic
(2-D)return the value determined from a piecewise cubic, continuously differentiable (C1), and approximately curvature-minimizing polynomial surface. See CloughTocher2DInterpolator for more details.
- fill_valuefloat, optional
Value used to fill in for requested points outside of the convex hull of the input points. If not provided, then the default is
nan
. This option has no effect for the ‘nearest’ method.- rescalebool, optional
Rescale points to unit cube before performing interpolation. This is useful if some of the input dimensions have incommensurable units and differ by many orders of magnitude.
New in version 0.14.0.
- ndarray
Array of interpolated values.
New in version 0.9.
Suppose we want to interpolate the 2-D function
>>> def func(x, y): ... return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
on a grid in [0, 1]x[0, 1]
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
but we only know its values at 1000 data points:
>>> rng = np.random.default_rng() >>> points = rng.random((1000, 2)) >>> values = func(points[:,0], points[:,1])
This can be done with griddata – below we try out all of the interpolation methods:
>>> from scipy.interpolate import griddata >>> grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest') >>> grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear') >>> grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')
One can see that the exact result is reproduced by all of the methods to some degree, but for this smooth function the piecewise cubic interpolant gives the best results:
>>> import matplotlib.pyplot as plt >>> plt.subplot(221) >>> plt.imshow(func(grid_x, grid_y).T, extent=(0,1,0,1), origin='lower') >>> plt.plot(points[:,0], points[:,1], 'k.', ms=1) >>> plt.title('Original') >>> plt.subplot(222) >>> plt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower') >>> plt.title('Nearest') >>> plt.subplot(223) >>> plt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower') >>> plt.title('Linear') >>> plt.subplot(224) >>> plt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower') >>> plt.title('Cubic') >>> plt.gcf().set_size_inches(6, 6) >>> plt.show()
- LinearNDInterpolator :
Piecewise linear interpolant in N dimensions.
- NearestNDInterpolator :
Nearest-neighbor interpolation in N dimensions.
- CloughTocher2DInterpolator :
Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D.
- gridimage(points=None, xi=None, method='linear', fill_value=None, rescale=False)¶
Use
scipy.interpolate.griddata()
to shift the image to a regular grid of co-ordinates.- Parameters
points (tuple of (x-co-ords,yco-ordsa)) – The actual sampled data co-ordinates
xi (tupe of (2D array,2D array)) – The regular grid co-ordinates (as generated by e.g.
np.meshgrid()
)
- Keyword Arguments
method ("linear","cubic","nearest") – how to interpolate, default is linear
fill_value (folat, Callable, None) – What to put when the co-ordinates go out of range (default is None). May be a callable in which case the initial image is presented as the only argument. If None, use the mean value.
rescale (bool) – If the x and y co-ordinates are very different in scale, set this to True.
- Returns
A copy of the modified image. The image data is interpolated and metadata kets “actual_x”,”actual_y”,”sample_ x”,”samp[le_y” are set to give co-ordinates of new grid.
Notes
If points and or xi are missed out then we try to construct them from the metadata. For points, the metadata keys “actual-x” and “actual_y” are looked for and for xi, the metadata keys “sample_x” and “sample_y” are used. These are set, for example, by the
Stoner.HDF5.SXTMImage
loader if the interformeter stage data was found in the file.The metadata used in this case is then adjusted as well to ensure that repeated application of this method doesn’t change the image after it has been corrected once.
- harden_mask()¶
Force the mask to hard.
Whether the mask of a masked array is hard or soft is determined by its ~ma.MaskedArray.hardmask property. harden_mask sets ~ma.MaskedArray.hardmask to
True
.ma.MaskedArray.hardmask
- hist(*args, **kargs)¶
Pass through to
matplotlib.pyplot.hist()
function.
- ids()¶
Return the addresses of the data and mask areas.
None
>>> x = np.ma.array([1, 2, 3], mask=[0, 1, 1]) >>> x.ids() (166670640, 166659832) # may vary
If the array has no mask, the address of nomask is returned. This address is typically not close to the data in memory:
>>> x = np.ma.array([1, 2, 3]) >>> x.ids() (166691080, 3083169284) # may vary
- im_scale(n, m, dtypeobj_in, dtypeobj, copy=True)¶
Scaleunsigned/positive integers from n to m bits.
Numbers can be represented exactly only if m is a multiple of n Output array is of same kind as input.
- imshow(**kwargs)¶
Quickly plot of image.
- Keyword Arguments
figure (int, str or matplotlib.figure) – if int then use figure number given, if figure is ‘new’ then create a new figure, if None then use whatever default figure is available
show_axis (bool) – If True, show the axis otherwise don’t (default)’
title (str,None,False) – Title for plot - defaults to False (no title). None will take the title from the filename if present
title_args (dict) – Arguments to pass to the title function to control formatting.
cmap (str,matplotlib.cmap) – Colour scheme for plot, defaults to gray
Any masked areas are set to NaN which stops them being plotted at all.
- isTuple(*args: type, strict: bool = True) bool ¶
Determine if obj is a tuple of a certain signature.
- Parameters
obj (object) – The object to check
*args (type) – Each of the suceeding arguments are used to determine the expected type of each element.
- Keywoprd Arguments:
- strict(bool):
Whether the elements of the tuple have to be exactly the type specified or just castable as the type
- Returns
(bool) – True if obj is a matching tuple.
- iscontiguous()¶
Return a boolean indicating whether the data is contiguous.
None
>>> x = np.ma.array([1, 2, 3]) >>> x.iscontiguous() True
iscontiguous returns one of the flags of the masked array:
>>> x.flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : False WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
- isiterable() bool ¶
Chack to see if a value is iterable.
- Parameters
value – Entitiy to check if it is iterable
- Returns
(bool) – True if value is an instance of collections.Iterable.
- item(*args)¶
Copy an element of an array to a standard Python scalar and return it.
*args : Arguments (variable number and type)
none: in this case, the method only works for arrays with one element (a.size == 1), which element is copied into a standard Python scalar object and returned.
int_type: this argument is interpreted as a flat index into the array, specifying which element to copy and return.
tuple of int_types: functions as does a single int_type argument, except that the argument is interpreted as an nd-index into the array.
- zStandard Python scalar object
A copy of the specified element of the array as a suitable Python scalar
When the data type of a is longdouble or clongdouble, item() returns a scalar array object because there is no available Python scalar that would not lose information. Void arrays return a buffer object for item(), unless fields are defined, in which case a tuple is returned.
item is very similar to a[args], except, instead of an array scalar, a standard Python scalar is returned. This can be useful for speeding up access to elements of the array and doing arithmetic on elements of the array using Python’s optimized math.
>>> np.random.seed(123) >>> x = np.random.randint(9, size=(3, 3)) >>> x array([[2, 2, 6], [1, 3, 6], [1, 0, 1]]) >>> x.item(3) 1 >>> x.item(7) 0 >>> x.item((0, 1)) 2 >>> x.item((2, 2)) 1
- itemset(*args)¶
Insert scalar into an array (scalar is cast to array’s dtype, if possible)
There must be at least 1 argument, and define the last argument as item. Then,
a.itemset(*args)
is equivalent to but faster thana[args] = item
. The item should be a scalar value and args must select a single item in the array a.- *argsArguments
If one argument: a scalar, only used in case a is of size 1. If two arguments: the last argument is the value to be set and must be a scalar, the first argument specifies a single array element location. It is either an int or a tuple.
Compared to indexing syntax, itemset provides some speed increase for placing a scalar into a particular location in an ndarray, if you must do this. However, generally this is discouraged: among other problems, it complicates the appearance of the code. Also, when using itemset (and item) inside a loop, be sure to assign the methods to a local variable to avoid the attribute look-up at each loop iteration.
>>> np.random.seed(123) >>> x = np.random.randint(9, size=(3, 3)) >>> x array([[2, 2, 6], [1, 3, 6], [1, 0, 1]]) >>> x.itemset(4, 0) >>> x.itemset((2, 2), 9) >>> x array([[2, 2, 6], [1, 0, 6], [1, 0, 9]])
- keep_return_type()¶
Mark a function as one that Should not be converted from an array to an ImageFile.
- level_image(poly_vert=1, poly_horiz=1, box=None, poly=None, mode='clip')¶
Subtract a polynomial background from image.
- Keword Arguments:
- poly_vert (int): fit a polynomial in the vertical direction for the image of order
given. If 0 do not fit or subtract in the vertical direction
- poly_horiz (int): fit a polynomial of order poly_horiz to the image. If 0 given
do not subtract
- box (array, list or tuple of int): [xmin,xmax,ymin,ymax] define region for fitting. IF None use entire
image
- poly (list or None): [pvert, phoriz] pvert and phoriz are arrays of polynomial coefficients
(highest power first) to subtract in the horizontal and vertical directions. If None function defaults to fitting its own polynomial.
- mode (str): Either ‘clip’ or ‘norm’ - specifies how to handle intensitry values that end up being outside
of the accepted range for the image.
- Returns
A new copy of the processed images.
Fit and subtract a background to the image. Fits a polynomial of order given in the horizontal and vertical directions and subtracts. If box is defined then level the entire image according to the gradient within the box. The polynomial subtracted is added to the metadata as ‘poly_vert_subtract’ and ‘poly_horiz_subtract’
- make_Data(**kargs)¶
Return an instance of Stoner.Data passig through constructor arguments.
Calling make_Data(None) is a speical case to return the Data class ratther than an instance
- max(axis=None, out=None, fill_value=None, keepdims=<no value>)¶
Return the maximum along a given axis.
- axis{None, int}, optional
Axis along which to operate. By default,
axis
is None and the flattened input is used.- outarray_like, optional
Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output.
- fill_valuescalar or None, optional
Value used to fill in the masked values. If None, use the output of maximum_fill_value().
- keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
- amaxarray_like
New array holding the result. If
out
was specified,out
is returned.
- ma.maximum_fill_value
Returns the maximum filling value for a given datatype.
- mean(axis=None, dtype=None, out=None, keepdims=<no value>)¶
Returns the average of the array elements along given axis.
Masked entries are ignored, and result elements which are not finite will be masked.
Refer to numpy.mean for full documentation.
numpy.ndarray.mean : corresponding function for ndarrays numpy.mean : Equivalent function numpy.ma.average : Weighted average.
>>> a = np.ma.array([1,2,3], mask=[False, False, True]) >>> a masked_array(data=[1, 2, --], mask=[False, False, True], fill_value=999999) >>> a.mean() 1.5
- min(axis=None, out=None, fill_value=None, keepdims=<no value>)¶
Return the minimum along a given axis.
- axis{None, int}, optional
Axis along which to operate. By default,
axis
is None and the flattened input is used.- outarray_like, optional
Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output.
- fill_valuescalar or None, optional
Value used to fill in the masked values. If None, use the output of minimum_fill_value.
- keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
- aminarray_like
New array holding the result. If
out
was specified,out
is returned.
- ma.minimum_fill_value
Returns the minimum filling value for a given datatype.
- mini(axis=None)¶
Return the array minimum along the specified axis.
Deprecated since version 1.13.0: This function is identical to both:
self.min(keepdims=True, axis=axis).squeeze(axis=axis)
np.ma.minimum.reduce(self, axis=axis)
Typically though,
self.min(axis=axis)
is sufficient.- axisint, optional
The axis along which to find the minima. Default is None, in which case the minimum value in the whole array is returned.
- minscalar or MaskedArray
If axis is None, the result is a scalar. Otherwise, if axis is given and the array is at least 2-D, the result is a masked array with dimension one smaller than the array on which mini is called.
>>> x = np.ma.array(np.arange(6), mask=[0 ,1, 0, 0, 0 ,1]).reshape(3, 2) >>> x masked_array( data=[[0, --], [2, 3], [4, --]], mask=[[False, True], [False, False], [False, True]], fill_value=999999) >>> x.mini() masked_array(data=0, mask=False, fill_value=999999) >>> x.mini(axis=0) masked_array(data=[0, 3], mask=[False, False], fill_value=999999) >>> x.mini(axis=1) masked_array(data=[0, 2, 4], mask=[False, False, False], fill_value=999999)
There is a small difference between mini and min:
>>> x[:,1].mini(axis=0) masked_array(data=3, mask=False, fill_value=999999) >>> x[:,1].min(axis=0) 3
- newbyteorder(new_order='S', /)¶
Return the array with the same data viewed with a different byte order.
Equivalent to:
arr.view(arr.dtype.newbytorder(new_order))
Changes are also made in all fields and sub-arrays of the array data type.
- new_orderstring, optional
Byte order to force; a value from the byte order specifications below. new_order codes can be any of:
‘S’ - swap dtype from current to opposite endian
{‘<’, ‘little’} - little endian
{‘>’, ‘big’} - big endian
‘=’ - native order, equivalent to sys.byteorder
{‘|’, ‘I’} - ignore (no change to byte order)
The default value (‘S’) results in swapping the current byte order.
- new_arrarray
New array object with the dtype reflecting given change to the byte order.
- nonzero()¶
Return the indices of unmasked elements that are not zero.
Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:
a[a.nonzero()]
To group the indices by element, rather than dimension, use instead:
np.transpose(a.nonzero())
The result of this is always a 2d array, with a row for each non-zero element.
None
- tuple_of_arraystuple
Indices of elements that are non-zero.
- numpy.nonzero :
Function operating on ndarrays.
- flatnonzero :
Return indices that are non-zero in the flattened version of the input array.
- numpy.ndarray.nonzero :
Equivalent ndarray method.
- count_nonzero :
Counts the number of non-zero elements in the input array.
>>> import numpy.ma as ma >>> x = ma.array(np.eye(3)) >>> x masked_array( data=[[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], mask=False, fill_value=1e+20) >>> x.nonzero() (array([0, 1, 2]), array([0, 1, 2]))
Masked elements are ignored.
>>> x[1, 1] = ma.masked >>> x masked_array( data=[[1.0, 0.0, 0.0], [0.0, --, 0.0], [0.0, 0.0, 1.0]], mask=[[False, False, False], [False, True, False], [False, False, False]], fill_value=1e+20) >>> x.nonzero() (array([0, 2]), array([0, 2]))
Indices can also be grouped by element.
>>> np.transpose(x.nonzero()) array([[0, 0], [2, 2]])
A common use for
nonzero
is to find the indices of an array, where a condition is True. Given an array a, the condition a > 3 is a boolean array and since False is interpreted as 0, ma.nonzero(a > 3) yields the indices of the a where the condition is true.>>> a = ma.array([[1,2,3],[4,5,6],[7,8,9]]) >>> a > 3 masked_array( data=[[False, False, False], [ True, True, True], [ True, True, True]], mask=False, fill_value=True) >>> ma.nonzero(a > 3) (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
The
nonzero
method of the condition array can also be called.>>> (a > 3).nonzero() (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
- normalise(scale=None, sample=False, limits=(0.0, 1.0), scale_masked=False)¶
Norm alise the data to a fixed scale.
- Keyword Arguements:
- scale (2-tuple):
The range to scale the image to, defaults to -1 to 1.
- saple (box):
Only use a section of the input image to calculate the new scale over. Default is False - whole image
- limits (low,high):
Take the input range from the high and low fraction of the input when sorted.
- scale_masked (bool):
If True then the masked region is also scaled, otherwise any masked region is ignored. Default, False.
- Returns
A scaled version of the data. The ndarray min and max methods are used to allow masked images to be operated on only on the unmasked areas.
Notes
The sample keyword controls the area in which the range of input values is calculated, the actual scaling is done on the whole image.
The limits parameter is used to set the input scale being normalised from - if an image has a few outliers then this setting can be used to clip the input range before normalising. The parameters in the limit are the values at the low and high fractions of the cumulative distribution functions.
- null(**kargs)¶
A null function to be documented.
- ocr_metadata(field_only=False)¶
Use image recognition to try to pull the metadata numbers off the image.
- Requirements:
This function uses tesseract to recognise the image, therefore tesseract file1 file2 must be valid on your command line. Install tesseract from https://sourceforge.net/projects/tesseract-ocr-alt/files/?source=navbar
- KeywordArguments:
- field_only(bool):
only try to return a field value
- Returns
metadata –
- dict
updated metadata dictionary
- partition(kth, axis=- 1, kind='introselect', order=None)¶
Rearranges the elements in the array in such a way that the value of the element in kth position is in the position it would be in a sorted array. All elements smaller than the kth element are moved before this element and all equal or greater are moved behind it. The ordering of the elements in the two partitions is undefined.
New in version 1.8.0.
- kthint or sequence of ints
Element index to partition by. The kth element value will be in its final sorted position and all smaller elements will be moved before it and all equal or greater elements behind it. The order of all elements in the partitions is undefined. If provided with a sequence of kth it will partition all elements indexed by kth of them into their sorted position at once.
- axisint, optional
Axis along which to sort. Default is -1, which means sort along the last axis.
- kind{‘introselect’}, optional
Selection algorithm. Default is ‘introselect’.
- orderstr or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need to be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.
numpy.partition : Return a parititioned copy of an array. argpartition : Indirect partition. sort : Full sort.
See
np.partition
for notes on the different algorithms.>>> a = np.array([3, 4, 2, 1]) >>> a.partition(3) >>> a array([2, 1, 3, 4])
>>> a.partition((1, 3)) >>> a array([1, 2, 3, 4])
- plot_histogram(bins=256)¶
Plot the histogram and cumulative distribution for the image.
- pop(k[, d]) v, remove specified key and return the corresponding value. ¶
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair ¶
as a 2-tuple; but raise KeyError if D is empty.
- prec_loss(dtypeobj)¶
Warn over precision loss when converting image.
- prod(axis=None, dtype=None, out=None, keepdims=<no value>)¶
Return the product of the array elements over the given axis.
Masked elements are set to 1 internally for computation.
Refer to numpy.prod for full documentation.
Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.prod : corresponding function for ndarrays numpy.prod : equivalent function
- product(axis=None, dtype=None, out=None, keepdims=<no value>)¶
Return the product of the array elements over the given axis.
Masked elements are set to 1 internally for computation.
Refer to numpy.prod for full documentation.
Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.prod : corresponding function for ndarrays numpy.prod : equivalent function
- profile_line(src=None, dst=None, linewidth=1, order=1, mode='constant', cval=0.0, constrain=True, **kargs)¶
Wrap sckit-image method of the same name to get a line_profile.
- Parameters
img (ImageArray) – Image data to take line section of
- Keyword Parameters:
- src, dst (2-tuple of int or float):
start and end of line profile. If the co-ordinates are given as intergers then they are assumed to be pxiel co-ordinates, floats are assumed to be real-space co-ordinates using the embedded metadata.
- linewidth (int):
the wideth of the profile to be taken.
- order (int 1-3):
Order of interpolation used to find image data when not aligned to a point
- mode (str):
How to handle data outside of the image.
- cval (float):
The constant value to assume for data outside of the image is mode is “constant”
- constrain (bool):
Ensure the src and dst are within the image (default True).
- no_scale (bool):
Do not attempt to scale values by the image scale and work in pixels throughout. (default False)
- Returns
A
Stoner.Data
object containing the line profile data and the metadata from the image.
- ptp(axis=None, out=None, fill_value=None, keepdims=False)¶
Return (maximum - minimum) along the given dimension (i.e. peak-to-peak value).
Warning
ptp preserves the data type of the array. This means the return value for an input of signed integers with n bits (e.g. np.int8, np.int16, etc) is also a signed integer with n bits. In that case, peak-to-peak values greater than
2**(n-1)-1
will be returned as negative values. An example with a work-around is shown below.- axis{None, int}, optional
Axis along which to find the peaks. If None (default) the flattened array is used.
- out{None, array_like}, optional
Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type will be cast if necessary.
- fill_valuescalar or None, optional
Value used to fill in the masked values.
- keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the array.
- ptpndarray.
A new array holding the result, unless
out
was specified, in which case a reference toout
is returned.
>>> x = np.ma.MaskedArray([[4, 9, 2, 10], ... [6, 9, 7, 12]])
>>> x.ptp(axis=1) masked_array(data=[8, 6], mask=False, fill_value=999999)
>>> x.ptp(axis=0) masked_array(data=[2, 0, 5, 2], mask=False, fill_value=999999)
>>> x.ptp() 10
This example shows that a negative value can be returned when the input is an array of signed integers.
>>> y = np.ma.MaskedArray([[1, 127], ... [0, 127], ... [-1, 127], ... [-2, 127]], dtype=np.int8) >>> y.ptp(axis=1) masked_array(data=[ 126, 127, -128, -127], mask=False, fill_value=999999, dtype=int8)
A work-around is to use the view() method to view the result as unsigned integers with the same bit width:
>>> y.ptp(axis=1).view(np.uint8) masked_array(data=[126, 127, 128, 129], mask=False, fill_value=999999, dtype=uint8)
- put(indices, values, mode='raise')¶
Set storage-indexed locations to corresponding values.
Sets self._data.flat[n] = values[n] for each n in indices. If values is shorter than indices then it will repeat. If values has some masked values, the initial mask is updated in consequence, else the corresponding values are unmasked.
- indices1-D array_like
Target indices, interpreted as integers.
- valuesarray_like
Values to place in self._data copy at target indices.
- mode{‘raise’, ‘wrap’, ‘clip’}, optional
Specifies how out-of-bounds indices will behave. ‘raise’ : raise an error. ‘wrap’ : wrap around. ‘clip’ : clip to the range.
values can be a scalar or length 1 array.
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.put([0,4,8],[10,20,30]) >>> x masked_array( data=[[10, --, 3], [--, 20, --], [7, --, 30]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999)
>>> x.put(4,999) >>> x masked_array( data=[[10, --, 3], [--, 999, --], [7, --, 30]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999)
- quantize(output, levels=None)¶
Quantise the image data into fixed levels given by a mapping.
- Parameters
output (list,array,tuple) – Output levels to return.
- Keyword Arguments
levels (list, array or None) – The input band markers. If None is constructed from the data.
The number of levels should be one less than the number of output levels given.
Notes
The routine will ignore all masked pixels and will preserve the mask.
- radial_coordinates(centre=(None, None), pixel_size=(1, 1), angle=False)¶
Rerurn a map of the radial co-ordinates of an image from a given centre, with adjustments for pixel size.
- Keyword Arguments
centre (2-tuple) – Co-ordinates of centre point in terms of the orginal pixels. Defaults to(None,None) for the middle of the image.
pixel_size (2-tuple) – The size of one pixel in (dx by dy) - defaults to 1,1
angle (bool, None) – Whether to return the angles (in radians, True), distances (False) o a complex number (None).
- Returns
An array of the same class as the input, but with values corresponding to the radial co-ordinates.
- radial_profile(angle=None, r=None, centre=(None, None), pixel_size=(1, 1))¶
Extract a radial profile line from an image.
- Keyword Paramaters:
- angle (float, tuple, None):
- Select the radial angle to include:
float selects a single angle
tuple selects a range of angles
None integrates over all angles
- r (array, None):
Edges of the bins in the radual direction - will return r.size-1 points. Default is None which uses the minimum r value found on the edges of the image.
- centre (2-tuple):
Co-ordinates of centre point in terms of the orginal pixels. Defaults to(None,None) for the middle of the image.
- pixel_size (2-tuple):
The size of one pixel in (dx by dy) - defaults to 1,1
- Retunrs:
- (Data):
A py:class:Stoner.Data object with a column for r and columns for mean, std, and number of pixels.
- ravel(order='C')¶
Returns a 1D version of self, as a view.
- order{‘C’, ‘F’, ‘A’, ‘K’}, optional
The elements of a are read using this index order. ‘C’ means to index the elements in C-like order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to index the elements in Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of axis indexing. ‘A’ means to read the elements in Fortran-like index order if m is Fortran contiguous in memory, C-like order otherwise. ‘K’ means to read the elements in the order they occur in memory, except for reversing the data when strides are negative. By default, ‘C’ index order is used.
- MaskedArray
Output view is of shape
(self.size,)
(or(np.ma.product(self.shape),)
).
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.ravel() masked_array(data=[1, --, 3, --, 5, --, 7, --, 9], mask=[False, True, False, True, False, True, False, True, False], fill_value=999999)
- reduce_metadata()¶
Reduce the metadata down to a few useful pieces and do a bit of processing.
- Returns
(
typeHintedDict
) – the new metadata
- remove_outliers(percentiles=(0.01, 0.99), replace=None)¶
Find values of the data that are beyond a percentile of the overall distribution and replace them.
- Keyword Parameters:
- percentile (2 tuple):
Fraction percentiles to consider to be outliers (default is (0.01,0.99) for 1% limits)
- replace (2 tuple or None):
Values to set outliers to. If None, then the pixel values at the percentile limits are used.
- Returns
(ndarray) – Tje modified array.
Use this method if you have an image with a small number of pixels with extreme values that are out of range.
- repeat(repeats, axis=None)¶
Repeat elements of an array.
Refer to numpy.repeat for full documentation.
numpy.repeat : equivalent function
- reshape(*s, **kwargs)¶
Give a new shape to the array without changing its data.
Returns a masked array containing the same data, but with a new shape. The result is a view on the original array; if this is not possible, a ValueError is raised.
- shapeint or tuple of ints
The new shape should be compatible with the original shape. If an integer is supplied, then the result will be a 1-D array of that length.
- order{‘C’, ‘F’}, optional
Determines whether the array data should be viewed as in C (row-major) or FORTRAN (column-major) order.
- reshaped_arrayarray
A new view on the array.
reshape : Equivalent function in the masked array module. numpy.ndarray.reshape : Equivalent method on ndarray object. numpy.reshape : Equivalent function in the NumPy module.
The reshaping operation cannot guarantee that a copy will not be made, to modify the shape in place, use
a.shape = s
>>> x = np.ma.array([[1,2],[3,4]], mask=[1,0,0,1]) >>> x masked_array( data=[[--, 2], [3, --]], mask=[[ True, False], [False, True]], fill_value=999999) >>> x = x.reshape((4,1)) >>> x masked_array( data=[[--], [2], [3], [--]], mask=[[ True], [False], [False], [ True]], fill_value=999999)
- resize(newshape, refcheck=True, order=False)¶
Warning
This method does nothing, except raise a ValueError exception. A masked array does not own its data and therefore cannot safely be resized in place. Use the numpy.ma.resize function instead.
This method is difficult to implement safely and may be deprecated in future releases of NumPy.
- rotate(angle, resize=False, center=None, order=1, mode='constant', cval=0, clip=True, preserve_range=False)¶
Rotate image by a certain angle around its center.
- Parameters
angle (float) – Rotation angle in radians in clockwise direction.
- Keyword Parameters:
- resize (bool):
Determine whether the shape of the output image will be automatically calculated, so the complete rotated image exactly fits. Default is False.
- center (iterable of length 2):
The rotation center. If
center=None
, the image is rotated around its center, i.e.center=(cols / 2 - 0.5, rows / 2 - 0.5)
. Please note that this parameter is (cols, rows), contrary to normal skimage ordering.- order (int):
The order of the spline interpolation, default is 1. The order has to be in the range 0-5. See skimage.transform.warp for detail.
- mode ({‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’}):
Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of numpy.pad.
- cval (float):
Used in conjunction with mode ‘constant’, the value outside the image boundaries.
- clip (bool):
Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range.
- preserve_range (bool):
Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of Stpomer.Image.ImageArray.as_float.
- Returns
(ImageFile/ImageArray) – Rotated image
Notes
(pass through to the skimage.transform.warps.rotate function)
- round(decimals=0, out=None)¶
Return each element rounded to the given number of decimals.
Refer to numpy.around for full documentation.
numpy.ndarray.round : corresponding function for ndarrays numpy.around : equivalent function
- save(filename=None, **kargs)¶
Save the image into the file ‘filename’.
- Parameters
filename (string, bool or None) – Filename to save data as, if this is None then the current filename for the object is used If this is not set, then then a file dialog is used. If filename is False then a file dialog is forced.
- Keyword Arguments
Notes
Metadata will be preserved in .png and .tif format.
fmt can be ‘png’, ‘npy’, ‘tif’, ‘tiff’ or a list of more than one of those. tif is recommended since metadata is lost in .npy format but data is converted to integer format for png so that definition cannot be saved.
Since Stoner.Image is meant to be a general 2d array often with negative and floating point data this poses a problem for saving images. Images are naturally saved as 8 or more bit unsigned integer values representing colour. The only obvious way to save an image and preserve negative data is to save as a float32 tif. This has the advantage over the npy data type which cannot be opened by external programs and will not save metadata.
- save_npy(filename)¶
Save the ImageArray as a numpy array.
- save_png(filename)¶
Save the ImageArray with metadata in a png file.
This can only save as 8bit unsigned integer so there is likely to be a loss of precision on floating point data
- save_tiff(filename, forcetype=False)¶
Save the ImageArray as a tiff image with metadata.
- Parameters
filename (str) – Filename to save file as.
- Keyword Arguments
forcetype (bool) – (depricated) if forcetype then preserve data type as best as possible on save. Otherwise we let the underlying pillow library choose the best data type.
Note
PIL can save in modes “L” (8bit unsigned int), “I” (32bit signed int), or “F” (32bit signed float). In general max info is preserved for “F” type so if forcetype is not specified then this is the default. For boolean type data mode “L” will suffice and this is chosen in all cases. The type name is added as a string to the metadata before saving.
- scipy__interpolate__ndgriddata__griddata(values, xi, method='linear', fill_value=nan, rescale=False)¶
Interpolate unstructured D-D data.
- points2-D ndarray of floats with shape (n, D), or length D tuple of 1-D ndarrays with shape (n,).
Data point coordinates.
- valuesndarray of float or complex, shape (n,)
Data values.
- xi2-D ndarray of floats with shape (m, D), or length D tuple of ndarrays broadcastable to the same shape.
Points at which to interpolate data.
- method{‘linear’, ‘nearest’, ‘cubic’}, optional
Method of interpolation. One of
nearest
return the value at the data point closest to the point of interpolation. See NearestNDInterpolator for more details.
linear
tessellate the input point set to N-D simplices, and interpolate linearly on each simplex. See LinearNDInterpolator for more details.
cubic
(1-D)return the value determined from a cubic spline.
cubic
(2-D)return the value determined from a piecewise cubic, continuously differentiable (C1), and approximately curvature-minimizing polynomial surface. See CloughTocher2DInterpolator for more details.
- fill_valuefloat, optional
Value used to fill in for requested points outside of the convex hull of the input points. If not provided, then the default is
nan
. This option has no effect for the ‘nearest’ method.- rescalebool, optional
Rescale points to unit cube before performing interpolation. This is useful if some of the input dimensions have incommensurable units and differ by many orders of magnitude.
New in version 0.14.0.
- ndarray
Array of interpolated values.
New in version 0.9.
Suppose we want to interpolate the 2-D function
>>> def func(x, y): ... return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2
on a grid in [0, 1]x[0, 1]
>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]
but we only know its values at 1000 data points:
>>> rng = np.random.default_rng() >>> points = rng.random((1000, 2)) >>> values = func(points[:,0], points[:,1])
This can be done with griddata – below we try out all of the interpolation methods:
>>> from scipy.interpolate import griddata >>> grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest') >>> grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear') >>> grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')
One can see that the exact result is reproduced by all of the methods to some degree, but for this smooth function the piecewise cubic interpolant gives the best results:
>>> import matplotlib.pyplot as plt >>> plt.subplot(221) >>> plt.imshow(func(grid_x, grid_y).T, extent=(0,1,0,1), origin='lower') >>> plt.plot(points[:,0], points[:,1], 'k.', ms=1) >>> plt.title('Original') >>> plt.subplot(222) >>> plt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower') >>> plt.title('Nearest') >>> plt.subplot(223) >>> plt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower') >>> plt.title('Linear') >>> plt.subplot(224) >>> plt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower') >>> plt.title('Cubic') >>> plt.gcf().set_size_inches(6, 6) >>> plt.show()
- LinearNDInterpolator :
Piecewise linear interpolant in N dimensions.
- NearestNDInterpolator :
Nearest-neighbor interpolation in N dimensions.
- CloughTocher2DInterpolator :
Piecewise cubic, C1 smooth, curvature-minimizing interpolant in 2D.
- scipy__ndimage__filters__gaussian_filter(sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0)¶
Multidimensional Gaussian filter.
- inputarray_like
The input array.
- sigmascalar or sequence of scalars
Standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for all axes.
- orderint or sequence of ints, optional
The order of the filter along each axis is given as a sequence of integers, or as a single number. An order of 0 corresponds to convolution with a Gaussian kernel. A positive order corresponds to convolution with that derivative of a Gaussian.
- outputarray or dtype, optional
The array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created.
- modestr or sequence, optional
The mode parameter determines how the input array is extended when the filter overlaps a border. By passing a sequence of modes with length equal to the number of dimensions of the input array, different modes can be specified along each axis. Default value is ‘reflect’. The valid values and their behavior is as follows:
- ‘reflect’ (d c b a | a b c d | d c b a)
The input is extended by reflecting about the edge of the last pixel. This mode is also sometimes referred to as half-sample symmetric.
- ‘constant’ (k k k k | a b c d | k k k k)
The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.
- ‘nearest’ (a a a a | a b c d | d d d d)
The input is extended by replicating the last pixel.
- ‘mirror’ (d c b | a b c d | c b a)
The input is extended by reflecting about the center of the last pixel. This mode is also sometimes referred to as whole-sample symmetric.
- ‘wrap’ (a b c d | a b c d | a b c d)
The input is extended by wrapping around to the opposite edge.
For consistency with the interpolation functions, the following mode names can also be used:
- ‘grid-constant’
This is a synonym for ‘constant’.
- ‘grid-mirror’
This is a synonym for ‘reflect’.
- ‘grid-wrap’
This is a synonym for ‘wrap’.
- cvalscalar, optional
Value to fill past edges of input if mode is ‘constant’. Default is 0.0.
- truncatefloat
Truncate the filter at this many standard deviations. Default is 4.0.
- gaussian_filterndarray
Returned array of same shape as input.
The multidimensional filter is implemented as a sequence of 1-D convolution filters. The intermediate arrays are stored in the same data type as the output. Therefore, for output types with a limited precision, the results may be imprecise because intermediate results may be stored with insufficient precision.
>>> from scipy.ndimage import gaussian_filter >>> a = np.arange(50, step=2).reshape((5,5)) >>> a array([[ 0, 2, 4, 6, 8], [10, 12, 14, 16, 18], [20, 22, 24, 26, 28], [30, 32, 34, 36, 38], [40, 42, 44, 46, 48]]) >>> gaussian_filter(a, sigma=1) array([[ 4, 6, 8, 9, 11], [10, 12, 14, 15, 17], [20, 22, 24, 25, 27], [29, 31, 33, 34, 36], [35, 37, 39, 40, 42]])
>>> from scipy import misc >>> import matplotlib.pyplot as plt >>> fig = plt.figure() >>> plt.gray() # show the filtered result in grayscale >>> ax1 = fig.add_subplot(121) # left side >>> ax2 = fig.add_subplot(122) # right side >>> ascent = misc.ascent() >>> result = gaussian_filter(ascent, sigma=5) >>> ax1.imshow(ascent) >>> ax2.imshow(result) >>> plt.show()
- searchsorted(v, side='left', sorter=None)¶
Find indices where elements of v should be inserted in a to maintain order.
For full documentation, see numpy.searchsorted
numpy.searchsorted : equivalent function
- set_fill_value(value=None)¶
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D ¶
- setfield(val, dtype, offset=0)¶
Put a value into a specified place in a field defined by a data-type.
Place val into a’s field defined by dtype and beginning offset bytes into the field.
- valobject
Value to be placed in field.
- dtypedtype object
Data-type of the field in which to place val.
- offsetint, optional
The number of bytes into the field at which to place val.
None
getfield
>>> x = np.eye(3) >>> x.getfield(np.float64) array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) >>> x.setfield(3, np.int32) >>> x.getfield(np.int32) array([[3, 3, 3], [3, 3, 3], [3, 3, 3]], dtype=int32) >>> x array([[1.0e+000, 1.5e-323, 1.5e-323], [1.5e-323, 1.0e+000, 1.5e-323], [1.5e-323, 1.5e-323, 1.0e+000]]) >>> x.setfield(np.eye(3), np.int32) >>> x array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])
- setflags(write=None, align=None, uic=None)¶
Set array flags WRITEABLE, ALIGNED, (WRITEBACKIFCOPY and UPDATEIFCOPY), respectively.
These Boolean-valued flags affect how numpy interprets the memory area used by a (see Notes below). The ALIGNED flag can only be set to True if the data is actually aligned according to the type. The WRITEBACKIFCOPY and (deprecated) UPDATEIFCOPY flags can never be set to True. The flag WRITEABLE can only be set to True if the array owns its own memory, or the ultimate owner of the memory exposes a writeable buffer interface, or is a string. (The exception for string is made so that unpickling can be done without copying memory.)
- writebool, optional
Describes whether or not a can be written to.
- alignbool, optional
Describes whether or not a is aligned properly for its type.
- uicbool, optional
Describes whether or not a is a copy of another “base” array.
Array flags provide information about how the memory area used for the array is to be interpreted. There are 7 Boolean flags in use, only four of which can be changed by the user: WRITEBACKIFCOPY, UPDATEIFCOPY, WRITEABLE, and ALIGNED.
WRITEABLE (W) the data area can be written to;
ALIGNED (A) the data and strides are aligned appropriately for the hardware (as determined by the compiler);
UPDATEIFCOPY (U) (deprecated), replaced by WRITEBACKIFCOPY;
WRITEBACKIFCOPY (X) this array is a copy of some other array (referenced by .base). When the C-API function PyArray_ResolveWritebackIfCopy is called, the base array will be updated with the contents of this array.
All flags can be accessed using the single (upper case) letter as well as the full name.
>>> y = np.array([[3, 1, 7], ... [2, 0, 0], ... [8, 5, 9]]) >>> y array([[3, 1, 7], [2, 0, 0], [8, 5, 9]]) >>> y.flags C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False >>> y.setflags(write=0, align=0) >>> y.flags C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : False ALIGNED : False WRITEBACKIFCOPY : False UPDATEIFCOPY : False >>> y.setflags(uic=1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: cannot set WRITEBACKIFCOPY flag to True
- sgolay2d(points=15, poly=1, derivative=None)¶
Implements a 2D Savitsky Golay Filter for a 2D array (e.g. image).
- Parameters
img (ImageArray or ImageFile) – image to be filtered
- Keyword Arguments
points (int) – The number of points in the window aperture. Must be an odd number. (default 15)
poly (int) – Degree of polynomial to use in the filter. (defatult 1)
- Type of defivative to calculate. Can be:
None - smooth only (default) “x”,”y” - calculate dIntentity/dx or dIntensity/dy “both” - calculate the full derivative and return magnitud and angle.
- ReturnsL
- (imageArray or ImageFile):
filtered image.
- Raises
ValueError if points, order or derivative are incorrect. –
Notes
Adapted from code on the scipy cookbook : https://scipy-cookbook.readthedocs.io/items/SavitzkyGolay.html
- shrink_mask()¶
Reduce a mask to nomask when possible.
None
None
>>> x = np.ma.array([[1,2 ], [3, 4]], mask=[0]*4) >>> x.mask array([[False, False], [False, False]]) >>> x.shrink_mask() masked_array( data=[[1, 2], [3, 4]], mask=False, fill_value=999999) >>> x.mask False
- sign_loss(dtypeobj)¶
Warn over loss of sign information when converting image.
- soften_mask()¶
Force the mask to soft.
Whether the mask of a masked array is hard or soft is determined by its ~ma.MaskedArray.hardmask property. soften_mask sets ~ma.MaskedArray.hardmask to
False
.ma.MaskedArray.hardmask
- sort(axis=- 1, kind=None, order=None, endwith=True, fill_value=None)¶
Sort the array, in-place
- aarray_like
Array to be sorted.
- axisint, optional
Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.
- kind{‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’}, optional
The sorting algorithm used.
- orderlist, optional
When a is a structured array, this argument specifies which fields to compare first, second, and so on. This list does not need to include all of the fields.
- endwith{True, False}, optional
Whether missing values (if any) should be treated as the largest values (True) or the smallest values (False) When the array contains unmasked values sorting at the same extremes of the datatype, the ordering of these values and the masked values is undefined.
- fill_valuescalar or None, optional
Value used internally for the masked values. If
fill_value
is not None, it supersedesendwith
.
- sorted_arrayndarray
Array of the same type and shape as a.
numpy.ndarray.sort : Method to sort an array in-place. argsort : Indirect sort. lexsort : Indirect stable sort on multiple keys. searchsorted : Find elements in a sorted array.
See
sort
for notes on the different sorting algorithms.>>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0]) >>> # Default >>> a.sort() >>> a masked_array(data=[1, 3, 5, --, --], mask=[False, False, False, True, True], fill_value=999999)
>>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0]) >>> # Put missing values in the front >>> a.sort(endwith=False) >>> a masked_array(data=[--, --, 1, 3, 5], mask=[ True, True, False, False, False], fill_value=999999)
>>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0]) >>> # fill_value takes over endwith >>> a.sort(endwith=False, fill_value=3) >>> a masked_array(data=[1, --, --, 3, 5], mask=[False, True, True, False, False], fill_value=999999)
- span()¶
Return the minimum and maximum values in the image.
- squeeze(axis=None)¶
Remove axes of length one from a.
Refer to numpy.squeeze for full documentation.
numpy.squeeze : equivalent function
- std(axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>)¶
Returns the standard deviation of the array elements along given axis.
Masked entries are ignored.
Refer to numpy.std for full documentation.
numpy.ndarray.std : corresponding function for ndarrays numpy.std : Equivalent function
- subtract_image(background, contrast=16, clip=True, offset=0.5)¶
Subtract a background image from the ImageArray.
Multiply the contrast by the contrast parameter. If clip is on then clip the intensity after for the maximum allowed data range.
- sum(axis=None, dtype=None, out=None, keepdims=<no value>)¶
Return the sum of the array elements over the given axis.
Masked elements are set to 0 internally.
Refer to numpy.sum for full documentation.
numpy.ndarray.sum : corresponding function for ndarrays numpy.sum : equivalent function
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.sum() 25 >>> x.sum(axis=1) masked_array(data=[4, 5, 16], mask=[False, False, False], fill_value=999999) >>> x.sum(axis=0) masked_array(data=[8, 5, 12], mask=[False, False, False], fill_value=999999) >>> print(type(x.sum(axis=0, dtype=np.int64)[0])) <class 'numpy.int64'>
- swapaxes(axis1, axis2)¶
Return a view of the array with axis1 and axis2 interchanged.
Refer to numpy.swapaxes for full documentation.
numpy.swapaxes : equivalent function
- take(indices, axis=None, out=None, mode='raise')¶
- threshold_minmax(threshmin=0.1, threshmax=0.9)¶
Return a boolean array which is thresholded between threshmin and threshmax.
(ie True if value is between threshmin and threshmax)
- tobytes(fill_value=None, order='C')¶
Return the array data as a string containing the raw bytes in the array.
The array is filled with a fill value before the string conversion.
New in version 1.9.0.
- fill_valuescalar, optional
Value used to fill in the masked values. Default is None, in which case MaskedArray.fill_value is used.
- order{‘C’,’F’,’A’}, optional
Order of the data item in the copy. Default is ‘C’.
‘C’ – C order (row major).
‘F’ – Fortran order (column major).
‘A’ – Any, current order of array.
None – Same as ‘A’.
numpy.ndarray.tobytes tolist, tofile
As for ndarray.tobytes, information about the shape, dtype, etc., but also about fill_value, will be lost.
>>> x = np.ma.array(np.array([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]]) >>> x.tobytes() b'\x01\x00\x00\x00\x00\x00\x00\x00?B\x0f\x00\x00\x00\x00\x00?B\x0f\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'
- tofile(fid, sep='', format='%s')¶
Save a masked array to a file in binary format.
Warning
This function is not implemented yet.
- NotImplementedError
When tofile is called.
- toflex()¶
Transforms a masked array into a flexible-type array.
The flexible type array that is returned will have two fields:
the
_data
field stores the_data
part of the array.the
_mask
field stores the_mask
part of the array.
None
- recordndarray
A new flexible-type ndarray with two fields: the first element containing a value, the second element containing the corresponding mask boolean. The returned record shape matches self.shape.
A side-effect of transforming a masked array into a flexible ndarray is that meta information (
fill_value
, …) will be lost.>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.toflex() array([[(1, False), (2, True), (3, False)], [(4, True), (5, False), (6, True)], [(7, False), (8, True), (9, False)]], dtype=[('_data', '<i8'), ('_mask', '?')])
- tolist(fill_value=None)¶
Return the data portion of the masked array as a hierarchical Python list.
Data items are converted to the nearest compatible Python type. Masked values are converted to fill_value. If fill_value is None, the corresponding entries in the output list will be
None
.- fill_valuescalar, optional
The value to use for invalid entries. Default is None.
- resultlist
The Python list representation of the masked array.
>>> x = np.ma.array([[1,2,3], [4,5,6], [7,8,9]], mask=[0] + [1,0]*4) >>> x.tolist() [[1, None, 3], [None, 5, None], [7, None, 9]] >>> x.tolist(-999) [[1, -999, 3], [-999, 5, -999], [7, -999, 9]]
- torecords()¶
Transforms a masked array into a flexible-type array.
The flexible type array that is returned will have two fields:
the
_data
field stores the_data
part of the array.the
_mask
field stores the_mask
part of the array.
None
- recordndarray
A new flexible-type ndarray with two fields: the first element containing a value, the second element containing the corresponding mask boolean. The returned record shape matches self.shape.
A side-effect of transforming a masked array into a flexible ndarray is that meta information (
fill_value
, …) will be lost.>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.toflex() array([[(1, False), (2, True), (3, False)], [(4, True), (5, False), (6, True)], [(7, False), (8, True), (9, False)]], dtype=[('_data', '<i8'), ('_mask', '?')])
- tostring(fill_value=None, order='C')¶
A compatibility alias for tobytes, with exactly the same behavior.
Despite its name, it returns bytes not strs.
Deprecated since version 1.19.0.
- trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)¶
Return the sum along diagonals of the array.
Refer to numpy.trace for full documentation.
numpy.trace : equivalent function
- translate(translation, add_metadata=False, order=3, mode='wrap', cval=None)¶
Translate the image.
Areas lost by move are cropped, and areas gained are made black (0) The area not lost or cropped is added as a metadata parameter ‘translation_limits’
- Parameters
translate (2-tuple) – translation (x,y)
- Keyword Arguments
add_metadata (bool) – Record the shift in the image metadata order (int): Interpolation order (default, 3, bi-cubic)
mode (str) – How to handle points outside the original image. See
skimage.transform.warp()
. Defaults to “wrap”cval (float) – The value to fill with if mode is constant. If not speficied or None, defaults to the mean pixcel value.
- Returns
im (ImageArray) – translated image
- translate_limits(translation, reverse=False)¶
Find the limits of an image after a translation.
After using ImageArray.translate some areas will be black, this finds the max area that still has original pixels in
- Parameters
translation – 2-tuple the (x,y) translation applied to the image
- Keyword Arguments
reverse (bool) – whether to reverse the translation vector (default False, no)
- Returns
limits –
- 4-tuple
(xmin,xmax,ymin,ymax) the maximum coordinates of the image with original information
- transpose(*axes)¶
Returns a view of the array with axes transposed.
For a 1-D array this has no effect, as a transposed vector is simply the same vector. To convert a 1-D array into a 2D column vector, an additional dimension must be added. np.atleast2d(a).T achieves this, as does a[:, np.newaxis]. For a 2-D array, this is a standard matrix transpose. For an n-D array, if axes are given, their order indicates how the axes are permuted (see Examples). If axes are not provided and
a.shape = (i[0], i[1], ... i[n-2], i[n-1])
, thena.transpose().shape = (i[n-1], i[n-2], ... i[1], i[0])
.axes : None, tuple of ints, or n ints
None or no argument: reverses the order of the axes.
tuple of ints: i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.
n ints: same as an n-tuple of the same ints (this form is intended simply as a “convenience” alternative to the tuple form)
- outndarray
View of a, with axes suitably permuted.
transpose : Equivalent function ndarray.T : Array property returning the array transposed. ndarray.reshape : Give a new shape to an array without changing its data.
>>> a = np.array([[1, 2], [3, 4]]) >>> a array([[1, 2], [3, 4]]) >>> a.transpose() array([[1, 3], [2, 4]]) >>> a.transpose((1, 0)) array([[1, 3], [2, 4]]) >>> a.transpose(1, 0) array([[1, 3], [2, 4]])
Copy the mask and set the sharedmask flag to False.
Whether the mask is shared between masked arrays can be seen from the sharedmask property. unshare_mask ensures the mask is not shared. A copy of the mask is only made if it was shared.
sharedmask
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() Any ¶
Return the values of the metadata dictionary.
- var(axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>)¶
Compute the variance along the specified axis.
Returns the variance of the array elements, a measure of the spread of a distribution. The variance is computed for the flattened array by default, otherwise over the specified axis.
- aarray_like
Array containing numbers whose variance is desired. If a is not an array, a conversion is attempted.
- axisNone or int or tuple of ints, optional
Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array.
New in version 1.7.0.
If this is a tuple of ints, a variance is performed over multiple axes, instead of a single axis or all the axes as before.
- dtypedata-type, optional
Type to use in computing the variance. For arrays of integer type the default is float64; for arrays of float types it is the same as the array type.
- outndarray, optional
Alternate output array in which to place the result. It must have the same shape as the expected output, but the type is cast if necessary.
- ddofint, optional
“Delta Degrees of Freedom”: the divisor used in the calculation is
N - ddof
, whereN
represents the number of elements. By default ddof is zero.- keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
If the default value is passed, then keepdims will not be passed through to the var method of sub-classes of ndarray, however any non-default value will be. If the sub-class’ method does not implement keepdims any exceptions will be raised.
- wherearray_like of bool, optional
Elements to include in the variance. See ~numpy.ufunc.reduce for details.
New in version 1.20.0.
- variancendarray, see dtype parameter above
If
out=None
, returns a new array containing the variance; otherwise, a reference to the output array is returned.
std, mean, nanmean, nanstd, nanvar Output type determination
The variance is the average of the squared deviations from the mean, i.e.,
var = mean(x)
, wherex = abs(a - a.mean())**2
.The mean is typically calculated as
x.sum() / N
, whereN = len(x)
. If, however, ddof is specified, the divisorN - ddof
is used instead. In standard statistical practice,ddof=1
provides an unbiased estimator of the variance of a hypothetical infinite population.ddof=0
provides a maximum likelihood estimate of the variance for normally distributed variables.Note that for complex numbers, the absolute value is taken before squaring, so that the result is always real and nonnegative.
For floating-point input, the variance is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-accuracy accumulator using the
dtype
keyword can alleviate this issue.>>> a = np.array([[1, 2], [3, 4]]) >>> np.var(a) 1.25 >>> np.var(a, axis=0) array([1., 1.]) >>> np.var(a, axis=1) array([0.25, 0.25])
In single precision, var() can be inaccurate:
>>> a = np.zeros((2, 512*512), dtype=np.float32) >>> a[0, :] = 1.0 >>> a[1, :] = 0.1 >>> np.var(a) 0.20250003
Computing the variance in float64 is more accurate:
>>> np.var(a, dtype=np.float64) 0.20249999932944759 # may vary >>> ((1-0.55)**2 + (0.1-0.55)**2)/2 0.2025
Specifying a where argument:
>>> a = np.array([[14, 8, 11, 10], [7, 9, 10, 11], [10, 15, 5, 10]]) >>> np.var(a) 6.833333333333333 # may vary >>> np.var(a, where=[[True], [True], [False]]) 4.0
- view(dtype=None, type=None, fill_value=None)¶
Return a view of the MaskedArray data.
- dtypedata-type or ndarray sub-class, optional
Data-type descriptor of the returned view, e.g., float32 or int16. The default, None, results in the view having the same data-type as a. As with
ndarray.view
, dtype can also be specified as an ndarray sub-class, which then specifies the type of the returned object (this is equivalent to setting thetype
parameter).- typePython type, optional
Type of the returned view, either ndarray or a subclass. The default None results in type preservation.
- fill_valuescalar, optional
The value to use for invalid entries (None by default). If None, then this argument is inferred from the passed dtype, or in its absence the original array, as discussed in the notes below.
numpy.ndarray.view : Equivalent method on ndarray object.
a.view()
is used two different ways:a.view(some_dtype)
ora.view(dtype=some_dtype)
constructs a view of the array’s memory with a different data-type. This can cause a reinterpretation of the bytes of memory.a.view(ndarray_subclass)
ora.view(type=ndarray_subclass)
just returns an instance of ndarray_subclass that looks at the same array (same shape, dtype, etc.) This does not cause a reinterpretation of the memory.If fill_value is not specified, but dtype is specified (and is not an ndarray sub-class), the fill_value of the MaskedArray will be reset. If neither fill_value nor dtype are specified (or if dtype is an ndarray sub-class), then the fill value is preserved. Finally, if fill_value is specified, but dtype is not, the fill value is set to the specified value.
For
a.view(some_dtype)
, ifsome_dtype
has a different number of bytes per entry than the previous dtype (for example, converting a regular array to a structured array), then the behavior of the view cannot be predicted just from the superficial appearance ofa
(shown byprint(a)
). It also depends on exactly howa
is stored in memory. Therefore ifa
is C-ordered versus fortran-ordered, versus defined as a slice or transpose, etc., the view may give different results.