Last active
October 5, 2015 13:48
-
-
Save omsai/2815351 to your computer and use it in GitHub Desktop.
iQ IDE plugin to export image stamps with sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#BACKGROUND | |
''' | |
Saves image stamps, sorted by time, in CSV file on desktop. | |
FIXME: For this to work, upstream needs to patch imagedisk/__init__.py to use | |
new 3.x Python style class foo(object): instead of class foo: syntax. | |
''' | |
import imagedisk | |
import iqtools | |
from numpy import dtype, array, savetxt | |
from os import path, getenv | |
FILE_SAVE_FOLDER = 'Desktop' | |
class iQImage2(imagedisk.iQImage): | |
''' | |
Monkey patch iQImage with 'metadata' ndarray. | |
''' | |
_STAMP_DTYPE_DEFAULT = 'float64' | |
_STAMP_DTYPE_OVERRIDES = { 'Z': 'float64' } | |
def __init__(self, iQImageDisk, title): | |
super(iQImage2, self).__init__(iQImageDisk, title) | |
@property | |
def stamps(self): | |
''' | |
Structured ndarray of image stamp metadata | |
>>> im.stamps.dtype.names | |
('Slice', 'Wavelength', 'Time', 'Event') | |
>>> im.stamps['Time'] | |
array([ 0, 1998, 14000, 16001, 18001, ... 12096], dtype=uint64) | |
''' | |
header = ['Slice'] + im.getStamp(1, showNames=True)[1] | |
# Create structured array type. | |
dim_type = [self._STAMP_DTYPE_DEFAULT] * len(header) | |
if self._STAMP_DTYPE_OVERRIDES != {}: | |
for i in range(len(header)): | |
if self._STAMP_DTYPE_OVERRIDES.has_key(dim_type[i]): | |
dim_type[i] = self._STAMP_DTYPE_OVERRIDES[dim_type[i]] | |
im_metadata_type = dtype(zip(header, dim_type)) | |
# Construct metadata ndarray. | |
array_content = [tuple([n] + im.getStamp(n)) for n in range(im.nslices)] | |
metadata_array = array(array_content, | |
dtype=im_metadata_type) | |
return metadata_array | |
imagedisk.iQImage = iQImage2 | |
# Main program. | |
id = imagedisk.iQImageDisk() | |
im = iqtools.dialogs.getDataset(id) | |
if im == None: | |
raise RuntimeError, 'User cancelled dataset selection' | |
# Create and sort the array | |
metadata = im.stamps | |
metadata.sort(order=['Time']) | |
# Write array to file. | |
file = path.join( | |
path.join(getenv('USERPROFILE'), FILE_SAVE_FOLDER), | |
'{basename}.{ext}'.format(basename=im.title, ext='txt') | |
) | |
delimiter = ',' | |
header_list =['{0:>11}'.format(dim) for dim in metadata.dtype.names] | |
header = delimiter.join(header_list) + '\n' | |
with open(file, 'w') as f: | |
f.write(header) | |
savetxt(f, metadata, fmt='%11d', delimiter=delimiter) | |
''' | |
ChangeLog | |
========= | |
2013-09-10 Pariksheet Nanda <p.nanda@andor.com> | |
* stamps: Fixed bug of negative 'Z' values incorrectly being typecast | |
to unsigned integers, by introducing new variable DTYPE_OVERRIDES to | |
respect values specified in _mm.py of imagedisk module. | |
* stamps: now a variable instead of a function. | |
2012-05-27 Pariksheet Nanda <p.nanda@andor.com> | |
* Initial release. | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment