Source code for magni.afm.io._data_attachment
"""
..
Copyright (c) 2014-2017, Magni developers.
All rights reserved.
See LICENSE.rst for further information.
Module providing functionality for attaching data.
The reading of an .mi file is logically separated into four steps of which the
functionality provided by this module performs the third step.
Routine listings
----------------
attach_data(obj, data)
Attach data to a hierarchical object-structure.
"""
from __future__ import division
import numpy as np
[docs]def attach_data(obj, data):
"""
Attach data to a hierachical object-structure.
In the case of .mi image files, the data should be attached to the buffers
of the file. In the case of .mi spectroscopy files, the data should be
attached to the chunks of the file.
Parameters
----------
obj : object
The hierarchical object-structure to attach data to.
data : numpy.ndarray
The 1-dimensional data.
See Also
--------
magni.afm.io.read_mi_file : Function using the present function.
"""
if obj['attrs']['fileType'].lower() == 'image':
_attach_image_data(obj, data)
else: # 'spectroscopy'
_attach_spectroscopy_data(obj, data)
[docs]def _attach_image_data(obj, data):
"""
Attach data to a hierarchical object-structure representing an image.
The data should be attached to the buffers of the file.
Parameters
----------
obj : object
The hierarchical object-structure to attach data to.
data : numpy.ndarray
The 1-dimensional data.
See Also
--------
attach_data : Function using the present function.
"""
obj, data, thumbnail = _handle_format_inconsistency(obj, data)
width, height = obj['attrs']['xPixels'], obj['attrs']['yPixels']
size = width * height
index = 0
for buffer_ in obj['buffers']:
buffer_['data'] = data[index:index + size]
buffer_['data'] = buffer_['data'].reshape(height, width)
buffer_['data'] = buffer_['data'][::-1, :]
if obj['attrs']['data'].lower() in ('binary', 'binary_32', 'ascii'):
if 'bufferRange' not in buffer_['attrs']:
raise IOError("Each buffer must have a 'bufferRange' header "
"parameter.")
if not isinstance(buffer_['attrs']['bufferRange'], float):
msg = "The 'bufferRange' buffer parameter must have type {!r}."
raise IOError(msg.format(float))
buffer_['data'] = buffer_['attrs']['bufferRange'] * buffer_['data']
index = index + size
obj['buffers'] = obj['buffers'] + thumbnail
if data.shape[0] != index:
raise IOError("The file contains different amount of data than "
"specified by the file headers.")
[docs]def _attach_spectroscopy_data(obj, data):
"""
Attach data to a hierarchical object-structure representing a spectroscopy.
The data should be attached to the chunks of the file.
Parameters
----------
obj : object
The hierarchical object-structure to attach data to.
data : numpy.ndarray
The 1-dimensional data.
See Also
--------
attach_data : Function using the present function.
"""
index = 0
for buffer_ in obj['buffers'][1:]:
for chunk in buffer_['data']:
chunk['data'] = data[index:index + chunk['attrs']['samples']]
index = index + chunk['attrs']['samples']
if data.shape[0] != index:
raise IOError("The file contains different amount of data than "
"specified by the file headers.")