Skip to content

Module: micpy.bin

The micpy.bin module provides methods to read and write binary files.

Chunk dataclass

A chunk of uncompressed binary data.

iterate(file, chunk_size, compressed=True, offset=0, decompressobj=None) staticmethod

Yield chunks of uncompressed binary data.

Field

Bases: ndarray

A field.

dimensions(shape) staticmethod

Get the number of dimensions of a shape.

from_bytes(data, shape=None, spacing=None) staticmethod

Create a new time step from bytes.

read(position, shape=None, spacing=None) staticmethod

Read a field from a binary file.

to_bytes()

Convert the field to bytes.

to_file(file, geometry=True)

Write the field to a binary file.

Parameters:

Name Type Description Default
file IO[bytes]

Binary file.

required
geometry bool

True if geometry should be written, False otherwise. Defaults to True.

True

write(filename, compressed=True, geometry=True)

Write the field to a binary file.

Parameters:

Name Type Description Default
filename str

Filename of the binary file.

required
compressed bool

True if file should be compressed, False otherwise. Defaults to True.

True
geometry bool

True if geometry should be written, False otherwise. Defaults to True.

True

File

A binary file.

__init__(filename, chunk_size=Chunk.DEFAULT_SIZE, verbose=True)

Initialize a binary file.

Parameters:

Name Type Description Default
filename str

File name.

required
chunk_size int

Chunk size in bytes. Defaults to 8388608 (8 MiB).

DEFAULT_SIZE
verbose bool

Verbose output. Defaults to True.

True

Raises:

Type Description
`FileNotFoundError`

If file is not found.

close()

Close the file.

create_index()

Create an index of the file.

find_geometry(compressed=None)

Find geometry file and read it.

Parameters:

Name Type Description Default
compressed bool

True if file is compressed, False otherwise. Defaults to None (auto).

None

Raises:

Type Description
`GeometryFileNotFoundError`

If no geometry file is found.

`MultipleGeometryFilesError`

If multiple geometry files are found.

index()

Get the index of the file.

iterate()

Iterate over fields in the file.

Returns:

Type Description
Generator[Field, None, None]

A generator of fields.

open()

Open the file.

print_geometry()

Get a string representation of the geometry.

read(key=None)

Read a series of fields from the file.

Parameters:

Name Type Description Default
key Union[int, slice, list, Callable[[Field], bool]]

Key to list of

None

Returns:

Type Description
Series

Series of fields.

read_field(field_id)

Read a field from the file.

Parameters:

Name Type Description Default
field_id int

Field ID.

required

Returns:

Type Description
Field

Field.

read_geometry(filename, compressed=None)

Read geometry from a file.

Parameters:

Name Type Description Default
filename str

Filename of a geometry file.

required
compressed bool

True if file is compressed, False otherwise. Defaults to None (auto).

None

set_geometry(shape, spacing)

Set the geometry.

Parameters:

Name Type Description Default
shape Tuple[int, int, int]

Shape of the geometry (z, y, x).

required
spacing Tuple[float, float, float]

Spacing of the geometry (dz, dy, dx) in μm.

required

times()

Get the times of the fields in the file.

Returns:

Type Description
List[float]

List of times.

update_index()

Update the index of the file.

Footer

A field footer.

to_bytes()

Convert the footer to bytes.

Header

A field header.

from_bytes(data) staticmethod

Create a new header from bytes.

read(filename, compressed=True) staticmethod

Read the header of a binary file.

to_bytes()

Convert the header to bytes.

Index

Bases: List[Position]

An index of fields in a binary file.

from_file(file, verbose=True, chunk_size=Chunk.DEFAULT_SIZE, compressed=True, position=None) staticmethod

Build an index from a binary file.

from_filename(filename, verbose=True, chunk_size=Chunk.DEFAULT_SIZE, compressed=True) staticmethod

Build an index from a binary file.

PlotArgs dataclass

Arguments for plotting a field.

Parameters:

Name Type Description Default
title str

Title of the plot. Defaults to None.

None
xlabel str

Label of the x-axis. Defaults to None.

None
ylabel str

Label of the y-axis. Defaults to None.

None
figsize Tuple[float, float]

Figure size. Defaults to None.

None
dpi int

Figure DPI. Defaults to None.

None
aspect str

Aspect ratio. Defaults to equal.

'equal'
ax Axes

Axes of the plot. Defaults to None.

None
cax Axes

Axes of the color bar. Defaults to None.

None
vmin float

Minimum value of the color bar. Defaults to None.

None
vmax float

Maximum value of the color bar. Defaults to None.

None
cmap str

Colormap. Defaults to micpy.

'micpy'

Position dataclass

A field position in a binary file.

iterate(file=None, compressed=True, chunk_size=Chunk.DEFAULT_SIZE, position=None) staticmethod

Yield positions of fields in a binary file.

Series

Bases: ndarray

get_field(index)

Get a field from the series.

Parameters:

Name Type Description Default
index int

Index of the field.

required

Returns:

Type Description
Field

Field.

get_series(key)

Get a series of fields.

Parameters:

Name Type Description Default
key Union[int, slice, list]

Key to list of field IDs, a slice object, or a list of field IDs.

required

Returns:

Type Description
Series

Series of fields.

iter_field()

Iterate over fields in the series.

Yields:

Type Description

Field.

write(filename, compressed=True, geometry=True)

Write the series to a binary file.

Parameters:

Name Type Description Default
filename str

Filename of the binary file.

required
compressed bool

True if file should be compressed, False otherwise. Defaults to True.

True
geometry bool

True if geometry should be written, False otherwise. Defaults to True.

True

plot(field, axis='y', index=0, args=None)

Plot a slice of the field.

Parameters:

Name Type Description Default
field Field

Field to plot.

required
axis str

Axis to plot. Possible values are x, y, and z. Defaults to y.

'y'
index int

Index of the slice. Defaults to 0.

0
args PlotArgs

Arguments for plotting. Defaults to None.

None

Returns:

Type Description
Tuple[Figure, Axes]

Matplotlib figure and axes of the plot.