MibBackup

Undo history for the open datasets. A single instance lives on models.MibModel as obj.Backup; entries are written by models.MibModel.backup() and replayed by models.MibModel.undo() (the Ctrl+Z shortcut). History depth comes from Preferences → Backup and undo (max_steps, and max3d_steps for 3-D entries).

obj.mibModel.backup('selection', 0);        % 2-D: current slice only
obj.mibModel.backup('labels', 1);           % 3-D: whole volume
obj.mibModel.backup('modelLayers', 1);      % whole segmentation layers

Calling undo() with no index toggles between the last two states (undo → redo → undo); undo(index) navigates to a specific point in the history.

Backup types

The first argument of models.MibModel.backup() selects what is captured.

Type

What is stored

What undo restores

'image'

Pixel snapshot of the image layer plus its metadata dictionary and viewPort

Pixels, metadata, colour-channel count and viewport

'selection'

Pixel snapshot of the selection layer

The selection layer

'mask'

Pixel snapshot of the mask layer

The mask layer; sets maskExist

'labels'

Pixel snapshot of the model layer ('model' is accepted as an alias for MIB2 compatibility)

The model layer; sets modelExist

'everything'

Raw packed uint8 of a type-63 layer — model, mask and selection in one array. Substituted automatically, see below

All three layers at once

'modelLayers'

Copies of the labels, selection and mask layer objects, plus maskExist, modelExist and the selected material

The three layer objects, including the model type, material names and colours

'annotations'

Label texts, values and positions from core.Annotations

The full annotation list

'measurements'

measure.Data from core.Measurements

The measurements table

'lines3d'

A copy of the core.Lines3D object

The skeleton / lines object

'mibDataset'

Deep copy of the whole core.MibDataset — image, all layers, ROIs, annotations, lines, measurements and every value property

The entire dataset container (replaces obj.I{id} and fires NewDataset)

Choosing a type

  • Editing pixels of one layer → the matching layer type ('selection', 'mask', 'labels', 'image'). Cheapest and most precise.

  • Replacing the labels layer with one of a different model type (63 ↔ 255 / 65535 / 4294967295, or an indexed-object model) → 'modelLayers'. A pixel snapshot cannot be written back once the type changed, because a type-63 layer keeps mask and selection in bits 7-8 of obj.labels while the larger types keep them as standalone layers. Used by models.MibModel.convertModel() and models.MibModel.stitchModelInstances().

  • Rewriting the whole container (dimensions, orientation, ROIs and pixels at once) → 'mibDataset'.

Note

'modelLayers' and 'mibDataset' both rely on MATLAB copy-on-write, so neither duplicates memory when the entry is created. The difference is what the entry keeps alive afterwards: 'mibDataset' pins the image array as well, so a later edit of the image forks it. 'modelLayers' also restores in a narrower way — it assigns three layer handles on the existing dataset, whereas 'mibDataset' swaps the dataset handle itself and rolls back the image, ROIs and annotations along with the model.

Automatic substitutions

  • 'model' → 'labels'.

  • For a core.MibLabels63 model, 'selection', 'mask' and 'labels' → 'everything', because all three layers share one array.

When a backup is skipped

models.MibModel.backup() returns silently — no entry is created — when:

  • the undo system is switched off (Backup.enableSwitch == 0);

  • the dataset is in Virtual mode and a model layer type was requested;

  • 'modelLayers' was requested for a non-Standard dataset;

  • a 3-D entry was requested while max3d_steps == 0;

  • the dataset is 5-D (depth > 1 and time > 1) and no explicit .x/.y/.z/.t range was given;

  • enableSelection == 0 for a model layer type;

  • 'mask' was requested but no mask exists.

Storage options

backup(type, switch3d, getDataOptions) — switch3d is 0 for the current 2-D slice and 1 for the whole 3-D volume (a .z range covering a single slice forces 0). getDataOptions accepts:

Field

Meaning

.id

Dataset container index; defaults to the active dataset

.blockModeSwitch

Store only the currently visible block; overrides .x / .y

.x / .y / .z / .t

[min, max] ranges of the region to store

.roiId

Restrict to a ROI — [] current, 0 all shown, index for one, missing or negative for the full dataset

.magFactor

Pyramid level to capture at; filled in automatically for Virtual and BigData datasets

.LinkedData / .LinkedVariable

Extra state to travel with the entry (e.g. the SAM segmenter point list); .LinkedVariable.<Field> names the variable as seen from mibController

Model-type safety

Every pixel-layer entry is stamped with the model type it was captured at (storeOptions.modelType). If the live labels layer has since changed type, models.MibModel.undo() converts the layer back before applying the snapshot and stores the current layers as a 'modelLayers' entry for redo, so neither direction reinterprets packed bits as material indices.

Class reference

class core.MibBackup

Bases: handle

MIBBACKUP - This class is responsible to store the previous versions of the dataset, to be used for Undo (Ctrl+Z) command.

Constructor Summary
MibBackup(max_steps, max3d_steps)

MIBBACKUP - mibImageUndo class constructor.

Syntax:
obj = MibBackup(max_steps, max3d_steps)

Constructor for the mibImageUndo class. Create a new instance of the class with default parameters

Input Arguments:
  • max_steps - maximal length of the history log

  • max3d_steps - maximal length of the 3D history log

Property Summary
enableSwitch

Enable/disable undo operation

index3d

a variable to keep previous index of NaN element of the undoList structure, for use with Ctrl+Z

max3d_steps

a variable to limit maximal number of history steps

max_steps

a structure to store the list of the actions for undo:

  • .type - type of the data: ‘image’, ‘labels’, ‘selection’, ‘mask’, ‘measurement’, ‘everything’ (for model_type==uint6 only), ‘modelLayers’ (copies of the labels, selection and mask layer objects - survives a change of the model type)

  • .data - a field to store a cell with 3D dataset or 2D slice

  • .meta - meta dictionary, for the ‘image’ type

  • .options - a substructure with all additional parameters: .orient (1=xz, 2=yz, 3=yx), .switch3d, .x, .y, .z, .t, .viewPort (for ‘image’), .id (MIB container index)

  • .LinkedData - structure with additional data to be stored

  • .LinkedVariable - structure that keeps variable names for data stored in LinkedData; e.g. .LinkedData.Points and .LinkedVariable.Points = 'obj.mibModel.sessionSettings.SAMsegmenter.Points'

prevUndoIndex

a variable to keep index of NaN (currently restored dataset) element of the undoList structure

type

a variable to store whether Undo is available or not:

  • 1 - enable

  • 0 - disable

undoIndex

a variable to limit maximal number of history for the 3D datasets

undoList

‘image’, ‘labels’, ‘selection’, ‘mask’, ‘measurement’, ‘everything’ (for model_type==uint6 only)

Type:

a variable to store type of the data

Method Summary
clearContents()

CLEARCONTENTS - Set all elements of the class to default values.

Syntax:
obj.clearContents()
Usage:

Example 1

mibImageUndo.clearContents();

Example 2

clearContents(obj);% Call within the class
removeItem(index)

REMOVEITEM - Delete a stored item.

Syntax:
obj.removeItem(index)
Input Arguments:
  • index - (optional) - index of the item to remove, when empty will remove the last entry

Usage:

Example 1

mibImageUndo.removeItem(5);% delete item number 5

Example 2

removeItem(obj, 5);% Call within the class; delete item number 5
replaceItem(index, type, data, meta, options)

REPLACEITEM - Replace a stored item in the undo history.

Syntax:
obj.replaceItem(index, type, data, meta, options)

Replaces an existing entry in the undo history with a new dataset snapshot. Useful for correcting or updating previously stored states.

Input Arguments:
  • index - [numeric] index of the item to replace; when empty, replaces the last entry

  • type - [char] data type: 'image', 'labels', 'selection', 'mask', 'everything' (for uint6), or 'mibDataset'

  • data - [cell array or object] the new dataset to store

  • meta - (optional) [dictionary or NaN] imageData metadata; not required for labels/selection/mask/everything

  • options - (optional) [struct] storage parameters:

    • .orient - [numeric] dataset orientation (1=xz, 2=yz, 3=yx); NaN for 3D

    • .y - [1×2 numeric] [ymin, ymax] coordinates of stored region

    • .x - [1×2 numeric] [xmin, xmax] coordinates of stored region

    • .z - [1×2 numeric] [zmin, zmax] slice range

    • .t - [1×2 numeric] [tmin, tmax] time-point range

    • .viewPort - [struct] viewport structure (image only)

    • .switch3d - [logical] 1 for 3D dataset, 0 for 2D

    • .id - [numeric] MIB container index

Example 1 - define storage options for time-point 5:

storeOptions.t = [5, 5];

Example 2 - replace 1st stored image dataset:

obj.replaceItem(1, 'image', img, meta, storeOptions);

Example 3 - replace 1st stored selection (call within the class):

obj.replaceItem(1, 'selection', selection, NaN, storeOptions);
setNumberOfHistorySteps(max_steps, max3d_steps)

SETNUMBEROFHISTORYSTEPS - setNumberOfHistorySteps(obj, max_steps, max3d_steps).

Syntax:
obj.setNumberOfHistorySteps(max_steps, max3d_steps)

Set number of history steps for undo

store(type, data, meta, options)

STORE - Store a dataset snapshot in the undo history.

Syntax:
obj.store(type, data, meta, options)

Stores a snapshot of the specified data type to the undo history stack. Can store 2D slices or 3D volumes with associated metadata and coordinates.

Input Arguments:
  • type - [char] data type: 'image', 'labels', 'selection', 'mask', 'everything' (for uint6), 'modelLayers', 'lines3d', or 'mibDataset'

  • data - [cell array] 3D or 2D dataset to store; structure for labels and for 'modelLayers' layer copies; Lines3D object; or core.MibDataset for 'mibDataset' type

  • meta - (optional) [dictionary or NaN] imageData metadata; not required for labels/selection/mask/everything

  • options - (optional) [struct] storage parameters:

    • .orient - [numeric] dataset orientation (1=xz, 2=yz, 3=yx); NaN for 3D

    • .y - [1×2 numeric] [ymin, ymax] coordinates of stored region

    • .x - [1×2 numeric] [xmin, xmax] coordinates of stored region

    • .z - [1×2 numeric] [zmin, zmax] slice range

    • .t - [1×2 numeric] [tmin, tmax] time-point range

    • .viewPort - [struct] viewport structure (image only)

    • .switch3d - [logical] 1 for 3D dataset, 0 for 2D

    • .id - [numeric] MIB container index

Example 1 - define storage options for time-point 5:

storeOptions.t = [5, 5];

Example 2 - store 3D image dataset at time-point 5:

obj.store('image', img, meta, storeOptions);

Example 3 - store selection at time-point 5:

obj.store('selection', selection, NaN, storeOptions);
undo(index)

UNDO - Retrieve a stored dataset from the undo history.

Syntax:
[type, data, meta, options] = obj.undo(index)

Retrieves a snapshot from the undo history stack. Returns the dataset along with its type, metadata, and storage parameters.

Input Arguments:
  • index - (optional) [numeric] index of the dataset to restore; when omitted, returns the last stored dataset

Output Arguments:
  • type - [char] data type: 'image', 'labels', 'selection', 'mask', 'everything' (for uint6), etc.

  • data - [cell array or object] the retrieved dataset

  • meta - [dictionary or NaN] imageData metadata (NaN for 2D or non-image types)

  • options - [struct] storage parameters with fields:

    • .orient - [numeric] dataset orientation; NaN for 3D

    • .y - [1×2 numeric] [ymin, ymax] coordinates of stored region

    • .x - [1×2 numeric] [xmin, xmax] coordinates of stored region

    • .z - [1×2 numeric] [zmin, zmax] slice range

    • .t - [1×2 numeric] [tmin, tmax] time-point range

    • .viewPort - [struct] viewport structure (image only)

    • .switch3d - [logical] 1 for 3D dataset, 0 for 2D

    • .id - [numeric] MIB container index

Example 1 - recover the image with all metadata:

[type, img, meta, options] = obj.undo();

Example 2 - recover the image (call within the class):

[type, img] = undo(obj);