Skip to content
Matthijs van Eede edited this page Jul 26, 2016 · 2 revisions

Welcome to the pyminc wiki!

Main arguments and their meaning

Argument Meaning
dtype data representation in Python; the numpy array stored in the .data attribute
volumeType data representation of the MINC file on disk
labels (boolean) indicating whether to treat the values in the file as integer
data (boolean) indicating whether or not to copy the data from the input

Debug information

You can turn on debug messages (mostly for developer's use) by setting the PYMINCDEBUG environment variable:

export PYMINCDEBUG=1

Reading MINC files (volumeFromFile)

Any MINC file

The simplest way to read in a MINC file is using the function volumeFromFile:

import pyminc.volumes.factory as pyminc

minc_vol = pyminc.volumeFromFile("input.mnc")

# the data is stored in the .data attribute, and by default is read in with double precision:
minc_vol.data.dtype
# dtype('float64')

# get some information from the data:
minc_vol.data.mean()

# close the volume and free memory:
minc_vol.closeVolume()

Segmentation/label MINC file

If you want to deal with MINC files that store labels, you should use the "label" argument while loading the file. This ensures that the values in the file are mapped onto integers (e.g., to prevent the value 5 to be read in as 5.000002). Note: if you don't specify the dtype (numpy representation) of the volume, it will be set to either the volumeType of the MINC volume (how it is stored on disk) or unsigned int if the data on disk is not some unsigned integer format.

import pyminc.volumes.factory as pyminc
import numpy as np

minc_labels = pyminc.volumeFromFile("labels.mnc", labels=True)

# dtype is based on the file on disk by default. In this case
# the file on disk is stored as unsigned byte, and so we have:
minc_labels.dtype
# 'ubyte'

# if we want to be able to deal with more integer values, we can
# specify this explicitly:
minc_labels_2 = pyminc.volumeFromFile("labels.mnc", labels=True, dtype="ushort")
minc_labels_2.dtype
# 'ushort'

# get some information about the labels:
np.unique(minc_labels.data)

# close files
minc_labels.closeVolume()
minc_labels_2.closeVolume()

Creating files based on MINC file (volumeLikeFile)

import pyminc.volumes.factory as pyminc

out_vol = pyminc.volumeLikeFile("input.mnc", "output.mnc")

# by default the volumeType of the output file is based on the input file
# the dtype is double, and the data from the input file is not copied over:
out_vol.data.mean()
# 0.0

# set some constant value and write the file out to disk:
out_vol.data[::] = 5.0
out_vol.writeFile()

Creating files from a pyminc object (volumeFromInstance)

import pyminc.volumes.factory as pyminc
import numpy as np

in_vol = pyminc.volumeFromFile("input.mnc")
out_vol = pyminc.volumeFromInstance(in_vol, "output.mnc")

# the rest works the same as with volumeFromFile()
# lets set some different values this time:

out_vol.data = np.arange(out_vol.sizes[0]*out_vol.sizes[1]*out_vol.sizes[2]).reshape(out_vol.sizes[0], out_vol.sizes[1], out_vol.sizes[2])

out_vol.writeFile()

Creating files from data (volumeFromData)

import pyminc.volumes.factory as pyminc
import numpy as np

# generate some data
data_block = np.arange(24000).reshape(20,30,40)

# the shape of the data does not need to be set explicitly; it is determined from the data block provided
out_vol = pyminc.volumeFromData("output.mnc", data_block, dimnames=("xspace", "yspace", "zspace"), starts=(0, 0, 0), steps=(1, 1, 1), volumeType="uint")

out_vol.writeFile()