Table Of Contents

Previous topic

Building the documentation

Next topic

Code used by nx2img

This Page

Common code used by all programs

Filename generation

class nexus_image_tools.common.filename_generator(fname_template, start_index=0, stop_index=None)

Generate image file names

Generator class creating image file names. An instance of this class can be used in two ways

  • either as a generator function
  • or by retrieving an iterator

In the first case one can use

g = filename_generator("image_%07i.tif",10,100)
print(g())
#output: image_0000010.tif
print(g())
#output: image_0000011.tif

while the latter case is done with

g = filename_generator("image_%04i.cbf",10,15)

for filename in g:
    print(filename)

#output:
#image_0010.cbf
#image_0011.cbf
#image_0012.cbf
#image_0013.cbf
#...

If no stop_index is provided the generator will run infinitely.

__init__(fname_template, start_index=0, stop_index=None)

Constructor for filename_generator.

Parameters:
  • fname_template (str) – filename format
  • start_index (int) – index of the first file
  • stop_index (int) – index of the last file
static from_slice(file_template)

Static factory method to create a filename_generator instance from a sliced user input

Users typically provide a string description of the filename format along with a range. This textual representation has the form

filename_format:start_index:stop_index

Where filename_format is a C-style format string describing the structure of the filename and start_index and stop_index are integers denoting the numerical index of the first and the last file generated respectively.

For instance

image_%06i.cbf:100:1000
Parameters:file_template (str) – sliced file template
Returns:instance of filename_generator
Return type:filename_generator
class nexus_image_tools.common.filename_iterator(filename_format, start_index=0, stop_index=None)

Iterator type for filenames

This class is typically not instantiated by itself but rather by filename_generator.

If no stop_index is given the iterator will never stop.

__init__(filename_format, start_index=0, stop_index=None)

Constructor for filename_iterator

Parameters:
  • filename_format (str) – format definition for the filenames
  • start_index (int) – index of the first file
  • stop_index (int) – index of the last file
next()
nexus_image_tools.common.get_data(parent_group)

Image file formats

class nexus_image_tools.common.file_format

Enumeration for image file types

CBF = None

indicating a CBF file

TIFF = None

indicating a TIFF file

nexus_image_tools.common.get_file_extension(fformat)

Return the extension for a particular file format.

The extension returned already includes the period.

Parameters:fformat (file_format) – the file format enumeration
Returns:string with file extension
Return type:str
nexus_image_tools.common.get_format_from_extension(file_extension)

Get file format from file extension.

The extension passed can include the leading period but must not. So either form cbf or .cbf would be allowed.

Parameters:file_extension (str) – file extension
Returns:file format enumeration
Return type:file_format
Raises ValueError:
 if no format is know for this extension

Nexus utilities

class nexus_image_tools.common.nexus_iterators.field_iterator(field)

Iterator for a single field.

This iterator runs over the first index of a field and returns the content of the other dimensions as a numpy array.

Parameters:field (nxfield) – NeXus field over which to iterate
class nexus_image_tools.common.nexus_iterators.field_list_iterator(field_list)

Iterator over a list of NeXus fields

This iterator runs over a list of NeXus fields. The fields are iterated subsequently starting with the first one. ‘

Parameters:field_list (list) – list with instances of nxfield
nexus_image_tools.common.nexus_utils.is_detector(group)

Predicate function identifying a detector group

Function returns True if object passed as its argument is an instance of nxgroup and is of type NXdetector.

Parameters:group (nxobject) – NeXus object
Returns:True object is a detector group, False otherwise
Return type:Boolean
nexus_image_tools.common.nexus_utils.is_data(group)

Predicate function identifying an NXdata group

Function returns True if the object passed as its argument is an instance of nxgroup and is of type NXdata.

Parameters:group (nxobject) – NeXus object
Returns:True if group is an NXdata group, False otherwise
Return type:Boolean
nexus_image_tools.common.nexus_utils.has_data(group)

Predicate checking for data child

This predicate function returns True if the object group passed

  • is an instance of nxgroup
  • has a child with name data
  • which is an instance of nxfield
Parameters:group (nxobject) – NeXus object
Returns:True if group matches the above conditions, False otherwise
Return type:Boolean
nexus_image_tools.common.nexus_utils.get_entry(nexus_object)

Get the entry for a articular object

This function returns the NXentry group nexus_object belongs to.

Parameters:nexus_object (nxobject) – the object for which to find the entry
Returns:group of type NXentry
Return type:nxgroup
nexus_image_tools.common.nexus_utils.get_data(parent_group)