IO

Reading and writing data.

Reading images

PyLorentz.io.read.read_image(f: PathLike) tuple[numpy.ndarray, dict][source]

Uses Tifffile or ncempy.io load an image and read the scale if there is one.

Parameters:

f (str) – file to read

Raises:
  • NotImplementedError – If unknown scale type is given, or Tif series is given.

  • RuntimeError – If uknown file type is given, or number of pages in tif is wrong

Returns:

(image, mdata), image given as 2D or 3D numpy array,
mdata has keys:

filepath: str filename: str scale: nm/pixel defocus_values: nm scale_unit: str defocus_unit: str beam_energy: float

Return type:

tuple

PyLorentz.io.read.read_json(file)[source]

read json

Writing images

PyLorentz.io.write.write_tif(data: numpy.ndarray, path: PathLike, scale: float, v: float | None = 1, unit: str | None = 'nm', overwrite: bool | None = True, color: bool | None = False)[source]

scale in nm/pixel default, saves as float32 if greyscale, or uint8 if color image

PyLorentz.io.write.save_tif(data: numpy.ndarray, path: PathLike, scale: float, v: float | None = 1, unit: str | None = 'nm', overwrite: bool | None = True, color: bool | None = False)

scale in nm/pixel default, saves as float32 if greyscale, or uint8 if color image

PyLorentz.io.write.write_tiff(data: numpy.ndarray, path: PathLike, scale: float, v: float | None = 1, unit: str | None = 'nm', overwrite: bool | None = True, color: bool | None = False)

scale in nm/pixel default, saves as float32 if greyscale, or uint8 if color image

PyLorentz.io.write.overwrite_rename(filepath: PathLike, spacer: bool | None = '_', incr_number: str | None = True)[source]

Given a filepath, check if file exists already. If so, add numeral 1 to end, if already ends with a numeral increment by 1.

Parameters:

filepath (str) – filepath to be checked

Returns:

[description]

Return type:

Path

PyLorentz.io.write.overwrite_rename_dir(dirpath: PathLike, spacer: str | None = '_')[source]

Given a filepath, check if file exists already. If so, add numeral 1 to end, if already ends with a numeral increment by 1.

Parameters:

filepath (str) – filepath to be checked

Returns:

[description]

Return type:

str

PyLorentz.io.write.splitnum(s: str)[source]

split the trailing number off a string. Returns (stripped_string, number)

PyLorentz.io.write.prep_dict_for_json(d: any)[source]

still plenty of things it doesn’t handle

PyLorentz.io.write.write_json(d: dict, path: PathLike, overwrite: bool | None = True, v: int | None = 1)[source]
PyLorentz.io.write.format_defocus(defval: float | int, digits: int = 3, spacer: str = '')[source]

returns a string of defocus value converted to nm, um, or mm as appropriate

Reading and writing .ovf files

PyLorentz.io.ovf.read_ovf(file=None, mode='norm', B0=10000.0, v=1)[source]

Load a .ovf or .omf file of magnetization values.

This function takes magnetization output files from OOMMF or Mumax, pulls some data from the header and returns 3D arrays for each magnetization component as well as the pixel resolutions.

Parameters:
  • file (string) – Path to file

  • mode (string) –

    Define how the magnetization is scaled as it’s read from the file. OOMMF writes .omf files with vectors in units of A/m, while Mumax writes .omf files with vectors normalized. This allows the reading to scale the vectors appropriately to gauss or simply make sure everything is normalized (as is needed for the phase calculation).

    • ”norm”: (default) Normalize all vectors (does not change (0,0,0) vectors)

    • ”raw”: Don’t do anything with the values.

  • v (int) –

    Verbosity.

    • 0 : No output

    • 1 : Default output

    • 2 : Extended output, print full header.

Returns:

(mags, scale, zscale)

  • mags (4D array) – shape [3, dimz, dimy, dimx], along first axis is stacked

    (magz, magy, magx)

  • scale (float) – Scale of datafile in y/x direction (nm/pixel)

  • zscale (float) – Scale of datafile in z-direction (nm/pixel)

Return type:

tuple

PyLorentz.io.ovf.write_ovf(f: str, mags: numpy.ndarray, del_px: float, zscale: float, title: str = None, units='norm', overwrite=False)[source]

Write an ovf file from a numpy array

Parameters:
  • f (str) – File to write.

  • mags (np.ndarray) – Numpy vector arrays to write. Should have dimensions (3, dimz, dimy, dimx) with the vector components being (z, y, x) along the first axis.

  • del_px (float) – x-y direction scale in nm/pixel

  • zscale (float) – z-direction scale in nm/pixel

  • title (str, optional) – Title of file in .ovf header. Defaults to filename.

  • units (str, optional) – Units of vectors. If units==”norm”, will normalize all vectors prior to saving. Otherwise will leave unscaled and fill “units” portion of the .ovf header with arg. Defaults to ‘norm’.

Returns:

filepath that was written

Return type:

f (str)