IO#
Unified IO interface for all ClearMap data sources.
This module is the single entry point for reading, writing, and inspecting
data in any format that ClearMap understands. It dispatches every call to the
appropriate format-specific sub-module based on the type or file extension of
the source, so calling code never needs to import TIF, MMP, NPY,
etc. directly.
Supported formats#
Extension |
Module |
Notes |
|---|---|---|
|
Memory-mapped NumPy arrays (default for large 3-D volumes) |
|
|
Single files and file-list expressions |
|
|
||
|
MetaImage header + raw data |
|
|
Point / coordinate tables |
|
|
graph-tool graphs (optional dependency) |
|
|
Ordered lists of files matched by a tag expression |
|
|
In-memory NumPy arrays |
|
|
Memory maps passed directly |
|
shared memory |
Shared-memory arrays for parallel processing |
Source routing#
source_to_module() maps any source specification to its handler module:
A
Sourceinstance → the module that created it.A
strorExpression→location_to_module()(file-list expression, or extension lookup).A
np.ndarray/list/tuple→SMAif in shared memory, elseNPY.A
np.memmap→MMP.
Core functions#
Reading and writing
import ClearMap.IO.IO as io
data = io.read('signal.tif') # returns np.ndarray
data = io.read('volume.npy',
slicing=(slice(0, 100),)) # sub-slice
io.write('output.tif', data)
Source objects — richer than raw arrays; carry shape, dtype, and location:
src = io.as_source('volume.npy')
print(src.shape, src.dtype, src.order)
data = src[10:20, :, :] # lazy slicing
Initialising a sink before parallel workers write into it:
# Open existing file or create it if absent
sink = io.initialize('counts.npy',
shape_=(512, 512, 256),
dtype_=np.uint16,
order_='F')
File-list expressions — match tiles with tag patterns:
files = io.file_list('raw/tile_<X,2>_<Y,2>.tif')
Bulk conversion between formats (parallelised):
io.convert_files(files, extension='.npy', processes=8)
Property helpers#
shape(), dtype(), order(), location(),
element_strides(), memory(), buffer() —
each accepts any source specification (path, array, or
Source) and returns the corresponding attribute
without requiring the caller to construct a Source explicitly.
File-path utilities#
The following functions from FileUtils are re-exported
here for convenience:
is_file, is_directory, file_extension, join, split,
abspath, create_directory, delete_directory, copy_file,
link_file, delete_file.
See also
ClearMap.IO.SourceBase Source class and AbstractSource / VirtualSource.
ClearMap.IO.MMPMemory-mapped arrays (primary large-data format).
ClearMap.IO.FileListTag-expression file lists.
ClearMap.IO.workspace2High-level asset management built on top of this module.
- as_source(source_, slicing=None, *args, **kwargs)[source]#
Convert source specification to a Source class.
- Parameters:
source (object) – The source specification.
slicing (int, slice, list of slices or None) – Optional slicing to apply to the source after opening.
Returns
- sourceSource class
The source class.
- buffer(source_)[source]#
Returns an io buffer of the data array of a source for use with e.g. cython.
- Parameters:
source (source specification) – The source specification.
Returns
- bufferarray or memmap
A buffer to read and write data.
- convert(source_, sink, processes=None, verbose=False, **kwargs)[source]#
Transforms a source into another format.
- Parameters:
source (source specification) – The source or list of sources.
sink (source specification) – The sink or list of sinks.
Returns
- sinksink specification
The sink or list of sinks.
- convert_files(filenames, extension=None, path=None, processes=None, verbose=False, workspace=None, verify=False)[source]#
Transforms list of files to their sink format in parallel.
- Parameters:
filenames (list of str | list of pathlib.Path) – The filenames to convert
extension (str) – The new file format extension.
path (str or None) – Optional path specification.
processes (int, 'serial' or None) – The number of processes to use for parallel conversion.
verbose (bool) – If True, print progress information.
Returns
- filenameslist of str
The new file names.
- create(source_, *args, **kwargs)[source]#
Create a data source on disk.
- Parameters:
source (str, pathlib.Path, array, Source class) – The source to write data to.
Returns
- sinkstr, array or Source class
The sink to which the data was written.
- element_strides(source_)[source]#
Returns the strides of the data array of a source.
Returns
- stridestuple of int
The strides of the source.
- file_list(expression=None, file_list=None, sort=True, verbose=False)[source]#
Returns the list of files that match the tag expression.
- Parameters:
expression (str | Path | te.Expression | None) – The regular expression the file names should match.
sort (bool) – If True, sort files naturally.
verbose (bool) – If True, print warning if no files exists.
Returns
- file_listlist of str
The list of files that matched the expression.
- filename_to_module(filename)[source]#
Returns the IO module associated with a filename.
- Parameters:
filename (str) – The file name.
Returns
- modulemodule
The module that handles the IO of the file.
- get_info(d_type)[source]#
Get the numpy info object for a data type. (automatically determines if integer or float)
- Parameters:
d_type (dtype) – The data type to get the info for.
Returns
- info: numpy info object
The info object for the data type.
- get_value(source_, value_type)[source]#
Get the minimal or maximal value of a source data type.
- Parameters:
Returns
- value: number
The value of the data type.
- initialize(source_=None, shape_=None, dtype_=None, order_=None, location_=None, memory_=None, like=None, hint=None, **kwargs)[source]#
Initialize (open to edit or create if missing) a source with specified properties.
Note
The source is created on disk or in memory if it does not exist so processes can start writing into it.
- Parameters:
source (str, array, Source class) – The source to write data to.
shape (tuple or None) – The desired shape of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid shape shapes are tested to match.
dtype (type, str or None) – The desired dtype of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid dtype the types are tested to match.
order ('C', 'F' or None) – The desired order of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid order the orders are tested to match.
location (str or None) – The desired location of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid location the locations need to match.
memory ('shared' or None) – The memory type of the source. If ‘shared’ a shared array is created.
like (str, array or Source class) – Infer the source parameter from this source.
hint (str, array or Source class) – If parameters for source creation are missing use the ones from this hint source.
Returns
- source: Source class
The initialized source.
- initialize_buffer(source_, shape=None, dtype=None, order=None, location=None, memory=None, like=None, **kwargs)[source]#
Initialize a buffer with specific properties.
- Parameters:
source (str, array, Source class) – The source to write data to.
shape (tuple or None) – The desired shape of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid shape shapes are tested to match.
dtype (type, str or None) – The desired dtype of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid dtype the types are tested to match.
order ('C', 'F' or None) – The desired order of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid order the orders are tested to match.
location (str or None) – The desired location of the source. If None, inferred from existing file or from the like parameter. If not None and source has a valid location the locations need to match.
memory ('shared' or None) – The memory type of the source. If ‘shared’ a shared array is created.
like (str, array or Source class) – Infer the source parameter from this source.
Returns
- bufferarray
The initialized buffer to use tih e.g. cython.
Note
The buffer is created if it does not exist.
- is_source(source_, exists=True)[source]#
Checks if source_ is a valid Source.
- Parameters:
source (object) – Source to check.
exists (bool) – If True, check if source exists in case it has a location.
Returns
- is_sourcebool
True if source is a valid source.
- location(source_)[source]#
Returns the location of a source.
Returns
- locationstr or None
The location of the source.
- location_to_module(location_)[source]#
Returns the IO module associated with a location string.
- Parameters:
location (str or te.Expression or pathlib.Path) – Location of the source.
Returns
- modulemodule
The module that handles the IO of the source specified by its location.
- max_value(source_)[source]#
Returns the maximal value of a source data type.
Returns
- max_valuenumber
The maximal value for the data type of the source
- memory(source_)[source]#
Returns the memory type of source_.
Returns
- memorystr or None
The memory type of the source.
- min_value(source_)[source]#
Returns the minimal value of a source data type.
Returns
- min_valuenumber
The minimal value for the data type of the source
- ndim(source_)[source]#
Returns number of dimensions of a source.
Returns
- ndimint
The number of dimensions in the source.
- order(source_)[source]#
Returns order of a source.
Returns
- order‘C’, ‘F’, or None
The order of the source data items.
- read(source_, *args, **kwargs)[source]#
Read data from a data source.
Warning
For some modules (file types) this does an active read for others, it just does an open and returns a Source class that can be used to read the data.
- Parameters:
source (str, pathlib.Path, array, Source class) – The source to read the data from.
Returns
- dataarray
The data of the source.
- shape(source_)[source]#
Returns shape of a source.
Returns
- shapetuple of ints
The shape of the source.
- source(source_, slicing=None, *args, **kwargs)[source]#
Convert source specification to a Source class.
- Parameters:
source (object) – The source specification.
Returns
- sourceSource class
The source class.
- source_to_module(source_)[source]#
Returns IO module associated with a source.
- Parameters:
source (object) – The source specification.
Returns
- typemodule
The module that handles the IO of the source.
- write(sink, data, *args, **kwargs)[source]#
Write data to a data source.
- Parameters:
Returns
- sinkstr, array or Source class
The sink to which the data was written.
- source_modules = [<module 'ClearMap.IO.NPY' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/NPY.py'>, <module 'ClearMap.IO.TIF' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/TIF.py'>, <module 'ClearMap.IO.MMP' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/MMP.py'>, <module 'ClearMap.IO.SMA' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/SMA.py'>, <module 'ClearMap.IO.FileList' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/FileList.py'>, <module 'ClearMap.IO.NRRD' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/NRRD.py'>, <module 'ClearMap.IO.MHD' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/MHD.py'>, <module 'ClearMap.IO.CSV' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/CSV.py'>, <module 'ClearMap.IO.GT' from '/home/charly.rousseau/code/icm/ClearMap2/ClearMap/IO/GT.py'>]#
The valid source modules.