Workspace module

OXASL - Workspace to store images and data associated with a processing pipeline

This class is conceptually simple - you can store pretty much any data by setting an attribute on the workspace and retrieve the resulting data as the same attribute. The workspace is backed by a directory and image data is save to files rather than store in memory.

This hides considerable complexity. Here are a few of the issues:

  • To ensure that image data really is kicked out of memory we create an ImageProxy object for each image. This simply stores the filename and metadata associated with an image. __getattribute__ is overridden to return ImageProxy attributes as the underlying Image.
  • There is a special ImageProxy for an AslImage. This might go away if we can represent the full state of an AslImage using metadata alone.

Copyright (c) 2008-2020 Univerisity of Oxford

class oxasl.workspace.AslImageProxy(fname, md=None)[source]

Reference to a saved AslImage and it’s metadata

img()[source]

Return an AslImage object from the file and stored metadata

class oxasl.workspace.ImageProxy(fname, md=None)[source]

Reference to a saved Image and it’s metadata

img()[source]

Return an Image object by loading from the file

class oxasl.workspace.Workspace(savedir=None, input_wsp='input', parent=None, search_childs=('prequantify', 'filter', 'corrected', 'preproc', 'input'), auto_asldata=False, **kwargs)[source]

A workspace for data processing

The contents of a workspace are modelled as attributes on the workspace object. An initial set of contents can be specified using keyword arguments. For command line tools, typically these are provided directly from the OptionParser results, e.g.

options, args = parser.parse_args(sys.argv) wsp = Workspace(**vars(options))

A workspace is always associated with a physical directory. Certain types of objects are automatically be saved to the workspace. If no save directory is specified a temporary directory is created

Supported types are currently:

  • fsl.data.image.Image - Saved as Nifti
  • 2D Numpy array - Saved as ASCII matrix

All other attributes are serialized to YAML and stored in a special _oxasl.yml file.

To avoid saving a particular item, use the add method rather than directly setting an attribute, as it supports a save option.

ifnone(attr, alternative)[source]

Return the value of an attribute, if set and not None, or otherwise the supplied alternative

set_item(name, value, save=True, save_name=None, save_fn=None)[source]

Add an item to the workspace

Normally this is achieved by assigning to an attribute directly, however this function exists to allow greater control where required.

The item will be set as an attribute on the workspace If a save directory is configured and the value is a supported type it will be saved there. This can be disabled by setting save=False

Parameters:
  • name – Name, must be a valid Python identifier
  • value – Value to set
  • save – If False, do not save item
  • save_name – If specified, alternative name to use for saving this item
  • save_fn – If specified, Callable which generates string representation of value suitable for saving the item to a file
sub(name, parent_default=True, **kwargs)[source]

Create a sub-workspace, (i.e. a subdir of this workspace)

This inherits the log configuration from the parent workspace. The savedir will be a subdirectory of the original workspace. Additional data may be set using keyword arguments. The child-workspace will be available as an attribute on the parent workspace.

Parameters:
  • name – Name of sub workspace
  • parent_default – If True, attribute values default to the parent workspace if not set on the sub-workspace
oxasl.workspace.matrix_to_text(mat)[source]

Convert matrix array to text using spaces/newlines as col/row delimiters

oxasl.workspace.mkdir(dirname, fail_if_exists=False, warn_if_exists=True, log=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]

Create a directory, including necessary subdirs

oxasl.workspace.text_to_matrix(text)[source]

Convert space or comma separated file to matrix