Global -> Data Handlers

Import data formats

class tardis_em.utils.load_data.ImportDataFromAmira(src_am: str, src_img: str | None = None)

LOADER FOR AMIRA SPATIAL GRAPH FILES

This class read any .am file and if the spatial graph is recognized it is converted into a numpy array as (N, 4) with class ids and coordinates for XYZ. Also, due to Amira’s design, file properties are encoded only in the image file therefore in order to properly ready spatial graph, class optionally requires amira binary or ASCII image file which contains transformation properties and pixel size. If the image file is not included, the spatial graph is returned without corrections.

Parameters:
  • src_am (str) – Amira spatial graph directory.

  • src_img (str, optional) – Amira binary or ASCII image file directory.

get_points() ndarray | None

General class function to retrieve point cloud.

Returns:

Point cloud as [X, Y, Z] after transformation and

pixel size correction.

Return type:

np.ndarray

get_segmented_points() ndarray | None

General class function to retrieve segmented point cloud.

Returns:

Point cloud as [ID, X, Y, Z].

Return type:

np.ndarray

get_labels() dict | None

General class function to read all labels from amira file.

Returns:

Dictionary with label IDs

Return type:

dict

get_image()

General class function to return image file.

Returns:

Image and if available pixel size data.

Return type:

np.ndarray, float

get_pixel_size() float

Catch pixel size value from image.

Returns:

Pixel size.

Return type:

float

tardis_em.utils.load_data.import_tiff(tiff: str)

Function to load any tiff file.

Parameters:

tiff (str) – Tiff file directory.

Returns:

Image data and unified pixel size.

Return type:

np.ndarray, float

class tardis_em.utils.load_data.MRCHeader(nx, ny, nz, mode, nxstart, nystart, nzstart, mx, my, mz, xlen, ylen, zlen, alpha, beta, gamma, mapc, mapr, maps, amin, amax, amean, ispg, next, creatid, nint, nreal, imodStamp, imodFlags, idtype, lens, nd1, nd2, vd1, vd2, tilt_ox, tilt_oy, tilt_oz, tilt_cx, tilt_cy, tilt_cz, xorg, yorg, zorg, cmap, stamp, rms, nlabl, labels)
alpha

Alias for field number 13

amax

Alias for field number 20

amean

Alias for field number 21

amin

Alias for field number 19

beta

Alias for field number 14

cmap

Alias for field number 44

creatid

Alias for field number 24

gamma

Alias for field number 15

idtype

Alias for field number 29

imodFlags

Alias for field number 28

imodStamp

Alias for field number 27

ispg

Alias for field number 22

labels

Alias for field number 48

lens

Alias for field number 30

mapc

Alias for field number 16

mapr

Alias for field number 17

maps

Alias for field number 18

mode

Alias for field number 3

mx

Alias for field number 7

my

Alias for field number 8

mz

Alias for field number 9

nd1

Alias for field number 31

nd2

Alias for field number 32

next

Alias for field number 23

nint

Alias for field number 25

nlabl

Alias for field number 47

nreal

Alias for field number 26

nx

Alias for field number 0

nxstart

Alias for field number 4

ny

Alias for field number 1

nystart

Alias for field number 5

nz

Alias for field number 2

nzstart

Alias for field number 6

rms

Alias for field number 46

stamp

Alias for field number 45

tilt_cx

Alias for field number 38

tilt_cy

Alias for field number 39

tilt_cz

Alias for field number 40

tilt_ox

Alias for field number 35

tilt_oy

Alias for field number 36

tilt_oz

Alias for field number 37

vd1

Alias for field number 33

vd2

Alias for field number 34

xlen

Alias for field number 10

xorg

Alias for field number 41

ylen

Alias for field number 11

yorg

Alias for field number 42

zlen

Alias for field number 12

zorg

Alias for field number 43

tardis_em.utils.load_data.mrc_read_header(mrc: str | bytes | None = None)

Helper function to read MRC header.

Parameters:

mrc (str) – MRC file directory.

Returns:

MRC header.

Return type:

class

tardis_em.utils.load_data.mrc_write_header(*args) bytes
tardis_em.utils.load_data.mrc_mode(mode: int, amin: int)

Helper function to decode MRC mode type.

mode int: MRC mode from mrc header. amin int: MRC minimum pixel value.

Returns:

Mode as np.dtype.

Return type:

np.dtype

tardis_em.utils.load_data.import_am(am_file: str)

Function to load Amira binary image data.

Parameters:

am_file (str) – Amira binary image .am file.

Returns:

Image file as well images parameters.

Return type:

np.ndarray, float, float, list

tardis_em.utils.load_data.load_mrc_file(mrc: str) Tuple[ndarray, float] | Tuple[None, float]

Function to load MRC 2014 file format.

Parameters:

mrc (str) – MRC file directory.

Returns:

Image data and pixel size.

Return type:

np.ndarray, float

tardis_em.utils.load_data.load_ply_scannet(ply: str, downscaling=0, color: str | None = None) Tuple[ndarray, ndarray] | ndarray

Function to read .ply files. :param ply: File directory. :type ply: str :param downscaling: Downscaling point cloud by fixing voxel size defaults to 0.1. :type downscaling: float :param color: Optional color feature defaults to None. :type color: str, optional

Returns:

Label point cloud coordinates and optionally RGB value for

each point.

Return type:

np.ndarray

tardis_em.utils.load_data.load_ply_partnet(ply, downscaling=0) ndarray

Function to read .ply files. :param ply: File directory. :type ply: str :param downscaling: Downscaling point cloud by fixing voxel size. :type downscaling: float

Returns:

Labeled point cloud coordinates.

Return type:

np.ndarray

tardis_em.utils.load_data.load_txt_s3dis(txt: str, rgb=False, downscaling=0) Tuple[ndarray, ndarray] | ndarray

Function to read .txt Stanford 3D instance scene file.

Parameters:
  • txt (str) – File directory.

  • rgb (bool) – If True return RGB value.

  • downscaling (float) – Downscaling point cloud by fixing voxel size.

Returns:

Labeled point cloud coordinates.

Return type:

np.ndarray

tardis_em.utils.load_data.load_s3dis_scene(dir_: str, downscaling=0, random_ds=None, rgb=False) Tuple[ndarray, ndarray] | ndarray

Function to read .txt Stanford 3D instance scene files.

Parameters:
  • dir (str) – Folder directory with all instances.

  • downscaling (float) – Downscaling point cloud by fixing voxel size.

  • random_ds (None, float) – If not None, indicate ration of point to keep.

  • rgb (bool) – If True, load rgb value.

Returns:

Labeled point cloud coordinates.

Return type:

np.ndarray

tardis_em.utils.load_data.load_image(image: str, normalize=False) Tuple[ndarray, float]

Quick wrapper for loading image data based on detected file format.

Parameters:
  • image (str) – Image file directory.

  • normalize (bool) – Rescale histogram to 1% - 99% percentile.

Returns:

Image array and associated pixel size.

Return type:

np.ndarray, float

Export data formats

class tardis_em.utils.export_data.NumpyToAmira(as_point_cloud=False)

Builder of Amira file from numpy array. Support for 3D only! If 2D data, Z dim build with Z=0

check_3d(coord: ndarray | None = typing.List) List[ndarray] | ndarray

Check and correct if needed to 3D

Parameters:

coord (np.ndarray, list) – Coordinate file to check for 3D.

Returns:

The same or converted to 3D coordinates.

Return type:

Union[np.ndarray, List[np.ndarray]]

export_amira(file_dir: str, coords: tuple | list | ~numpy.ndarray = <class 'numpy.ndarray'>, labels: tuple | list | None = None, scores: list | None = None)

Save Amira file with all filaments without any labels

Parameters:
  • file_dir (str) – Directory where the file should be saved.

  • coords (np.ndarray, tuple) – 3D coordinate file.

  • labels (tuple, list, None) – Labels names.

  • scores (list, None) – List of confidence scores for each instance.

tardis_em.utils.export_data.to_mrc(data: ndarray, pixel_size: float, file_dir: str, org_header: MRCHeader | None = None)

Save MRC image file

Parameters:
  • data (np.ndarray) – Image file.

  • pixel_size (float) – Image original pixel size.

  • file_dir (str) – Directory where the file should be saved.

  • org_header (MRCHeader) – Optional original header

tardis_em.utils.export_data.to_am(data: ndarray, pixel_size: float, file_dir: str)

Save image to binary Amira image file.

Parameters:
  • data (np.ndarray) – Image file.

  • pixel_size (float) – Image original pixel size.

  • file_dir (str) – Directory where the file should be saved.

tardis_em.utils.export_data.to_stl(data: ndarray, file_dir: str)

Save a point cloud as a PLY file.

Parameters:
  • data (np.ndarray) – The name of the PLY file to create.

  • file_dir (str) – Output file location.

Image normalization

class tardis_em.utils.normalization.SimpleNormalize

SIMPLE IMAGE NORMALIZATION

Take int8-int32 image file with 0 - 255 value. All image value are spread between 0 - 1.

class tardis_em.utils.normalization.MinMaxNormalize

IMAGE NORMALIZATION BETWEEN MIN AND MAX VALUE

class tardis_em.utils.normalization.MeanStdNormalize

IMAGE NORMALIZATION BASED ON MEAN AND STD

class tardis_em.utils.normalization.RescaleNormalize(clip_range=(2, 98))

NORMALIZE IMAGE VALUE USING Skimage

Rescale intensity with top% and bottom% percentiles as default

Parameters:

clip_range – Histogram percentiles range crop.

class tardis_em.utils.normalization.FFTNormalize(method='affine', alpha=900, beta_=1, num_iters=100, sample=1, use_cuda=False)
static gmm_fit(x, pi=0.5, split=None, alpha=0.5, beta_=0.5, scale=1.0, tol=0.001, num_iters=100, share_var=True, verbose=False)
norm_fit(x, alpha=900, beta_=1, scale=1.0, num_iters=100, use_cuda=False)