def load_tiff_stack(fname, use_lib_tiff=False): """ Read a TIFF stack. We're using tifflib by default as, right now, only this works when the application is compile on Windows. [17/08/15] Bugs: known to fail with tiffs produced by Icy [23/07/15] """ if not check_file_exists(fname, "load_tiff_stack"): return if use_lib_tiff: try: from libtiff import TIFFfile except ImportError: # Suppresses error in IDE when libtiff not installed raise tiff = TIFFfile(fname) samples, sample_names = tiff.get_samples() # we should have just one print("Loading: " + tiff.get_info() + " with libtiff\n") im = np.asarray(samples[0]) else: print("Loading: " + fname + " with tifffile\n") from tifffile import imread im = imread(fname) im = im.swapaxes(1, 2) print("read image of size: cols: %d, rows: %d, layers: %d" % (im.shape[1], im.shape[2], im.shape[0])) return im
def loadTiffStack(fname,useLibTiff=False): """ Read a TIFF stack. We're using tifflib by default as, right now, only this works when the application is compile on Windows. [17/08/15] Bugs: known to fail with tiffs produced by Icy [23/07/15] """ if not os.path.exists(fname): print "imageStackLoader.loadTiffStack can not find %s" % fname return purePython = True if useLibTiff: from libtiff import TIFFfile import numpy as np tiff = TIFFfile(fname) samples, sample_names = tiff.get_samples() #we should have just one print "Loading:\n" + tiff.get_info() + " with libtiff\n" im = np.asarray(samples[0]) else: print "Loading:\n" + fname + " with tifffile\n" from tifffile import imread im = imread(fname) im=im.swapaxes(1,2) print "read image of size: cols: %d, rows: %d, layers: %d" % (im.shape[1],im.shape[2],im.shape[0]) return im
def process(self, options=None, validate=default_validate): template_xml = list(self.make_xml()) s = None for (detector, fn, uuid), tif_image in self.tif_images.items (): xml= ome.OME(ATTR('xsi','schemaLocation',"%s %s/ome.xsd" % ((namespace_map['ome'],)*2)), UUID = uuid) for item in template_xml: if item.tag.endswith('Image') and item.get('ID')!='Image:%s' % (detector): continue if item.tag.endswith('Instrument'): if detector=='Confocal': instrument = 'Airy' elif detector in ['Imperx', 'Andor']: instrument = 'Suga' else: instrument = None if instrument and item.get('ID')!='Instrument:%s' % (instrument): continue xml.append(item) if s is None and validate: s = etree.tostring(xml, pretty_print = True, xml_declaration=True) #print s validate_xml(xml) else: s = etree.tostring(xml, pretty_print = True, xml_declaration=True) tif_image.description = s if detector=='Confocal': c = tif_image.write_file(fn, compression='lzw') if c<1.0: print 'Resetting compression to none' tif_image.write_file(fn, compression='none') else: tif_image.write_file(fn, compression='none') if validate and 0: print 'Validating written data..', from libtiff import TIFFfile t = TIFFfile(fn) samples, sample_names = t.get_samples() assert len (sample_names)==1,`sample_names` samples = samples[0] samples_orig = tif_image.data if (samples != samples_orig).any(): print 'DATA CORRUPTION DETECTED!!' print 'original data:', samples_orig.dtype, samples_orig.shape, samples_orig.nbytes print 'written data:', samples.dtype, samples.shape, samples.nbytes diff = samples - samples_orig ia,ja,ka = diff.nonzero() print len(ia) print ia[:10] print ja[:10] print ka[:10] print samples[ia[:10],ja[:10], ka[:10]] print samples_orig[ia[:10],ja[:10], ka[:10]] else: print 'SUCCESS!' #validate = False return s
def run(): images = TIFFfile(args.image) labels = TIFFfile(args.label) samples, _ = images.get_samples() images = np.array(samples).transpose([1, 2, 3, 0]) images = np.array([cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) for im in images]) samples, _ = labels.get_samples() labels = np.array(samples).transpose([1, 2, 3, 0]) labels = np.array( [cv2.cvtColor(label, cv2.COLOR_BGR2GRAY) for label in labels]) m = np.array([int(x / 2) for x in args.shape]) seg = labels.copy() corner, partitions = compute_partitions(seg[...], [float(x) for x in args.thr], m, args.min_size) print(corner) totals = defaultdict(int) # partition -> voxel count indices = defaultdict(list) # partition -> [(vol_id, 1d index)] vol_shapes = partitions.shape uniques, counts = np.unique(partitions, return_counts=True) for val, cnt in zip(uniques, counts): if val == 255: continue totals[val] += cnt indices[val].extend( [flat_index for flat_index in np.flatnonzero(partitions == val)]) max_count = max(totals.values()) indices = np.concatenate([ np.resize(np.random.permutation(v), max_count) for v in indices.values() ], axis=0) np.random.shuffle(indices) coor = [] for coord_idx in indices: z, y, x = np.unravel_index(coord_idx, vol_shapes) coor.append([z + m[2], y + m[1], x + m[0]]) with h5py.File(args.save, 'w') as f: f.create_dataset('image', data=images, compression='gzip') f.create_dataset('label', data=labels, compression='gzip') f.create_dataset('coor', data=coor, compression='gzip')
def readTiff(fileName): """ Read a tiff file into a numpy array Usage: img = readTiff(fileName) """ tiff = TIFFfile(fileName) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)) out = np.concatenate(outList, axis=-1) tiff.close() return out
def read(fileName): """ Script to import tif file from imageJ, usage: zstack = tiff.read(inFileName) PTW 2015/01/29 """ tiff = TIFFfile(fileName) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)[...,np.newaxis]) out = np.concatenate(outList,axis=-1) out = np.rollaxis(out,0,3) out = np.flipud(out) tiff.close() return out
def read(fileName): """ Script to import tif file from imageJ, usage: zstack = tiff.read(inFileName) PTW 2015/01/29 """ tiff = TIFFfile(fileName) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)[..., np.newaxis]) out = np.concatenate(outList, axis=-1) out = np.rollaxis(out, 0, 3) out = np.flipud(out) tiff.close() return out
def read_tif(fn, div2=False, oldmethod=False, offset2=False): """Reads a 3D TIFF file and returns a 3D numpy matrix from a path Inputs : - fn (string): the path of the 3D TIFF file - div2 (bool): if `True`, only one every to planes in $z$ will be loaded Returns : - a 3D numpy matrix """ if offset2: kp = 1 else: kp = 0 if not oldmethod: ti = TIFFfile(fn) ti = ti.get_samples()[0][0].swapaxes(0, 2).swapaxes(0, 1) if div2: I = [] for i in range(ti.shape[2]): j = ti[:, :, i] if i % 2 == kp: I.append(j) ti = np.zeros((I[0].shape[0], I[0].shape[1], len(I))) for (i, j) in enumerate(I): ti[:, :, i] = j return ti else: # Kept for compatibility. Fails to load some 16 bits images. im = TIFF.open(fn) I = [] for (j, i) in enumerate(im.iter_images()): if div2 and (j + 1) % 2 == 0: pass else: I.append(i) ret = np.zeros((I[0].shape[0], I[1].shape[1], len(I))) for (i, ii) in enumerate(I): ret[:, :, i] = ii return ret
def iter_Image(self, func): sys.stdout.write("iter_Image: reading image data from TIFF files\n") for detector in self.data: sys.stdout.write(" detector: %s\n" % (detector)) d_index = self.data[detector] # write the content of tiff files to a single raw files f, fn, dtype = None, None, None time_set = set() mn, mx = None, None mnz = float(self.config["PROTOCOL_Z_STACKER_Minimum"]) mxz = float(self.config["PROTOCOL_Z_STACKER_Maximum"]) nz = int(self.config["PROTOCOL_Z_STACKER_NumberOfFrames"]) if nz > 1: dz = (mxz - mnz) / (nz - 1) else: dz = 0 plane_l = [] ti = -1 exptime = "0" if detector == "Confocal": exptime = float(self.config["CONFOCAL_PixelAcqusitionTime"]) * 1e-6 elif detector == "Andor": exptime = self.config["CAMERA_ANDOR_ExposureTime"] elif detector == "Imperx": for line in self.config["CAMERA_IMPERX_HardwareInformation"].split("\n"): if line.startswith("Exposure time:"): v, u = line[14:].lstrip().split() v = v.strip() u = u.strip() if u == "usec": exptime = float(v) * 1e-6 elif u == "msec": exptime = float(v) * 1e-3 elif u == "sec": exptime = float(v) else: raise NotImplementedError(` v, u, line `) else: raise NotImplementedError(` detector `) for t, index in sorted(d_index): if t not in time_set: time_set.add(t) ti += 1 zi = 0 else: zi += 1 z = mnz + dz * zi d = dict( DeltaT=str(t), TheT=str(ti), TheZ=str(zi), PositionZ=str(z), TheC="0", ExposureTime=str(exptime) ) plane_l.append(d) tif = TIFFfile(d_index[t, index]) samples, sample_names = tif.get_samples() assert len(sample_names) == 1, ` sample_names ` data = samples[0] if mn is None: mn, mx = data.min(), data.max() else: mn = min(data.min(), mn) mx = min(data.max(), mx) if f is None: shape = list(data.shape) dtype = data.dtype fn = tempfile.mktemp(suffix=".raw", prefix="%s_%s_" % (detector, dtype)) f = open(fn, "wb") else: assert dtype is data.dtype, ` dtype, data.dtype ` shape[0] += 1 data.tofile(f) sys.stdout.write( "\r copying TIFF image data to RAW file: %5s%% done" % (int(100.0 * (index + 1) / len(d_index))) ) sys.stdout.flush() if f is None: continue f.close() shape = tuple(shape) xsz = shape[2] ysz = shape[1] tsz = len(time_set) zsz = shape[0] // tsz order = "XYZTC" sys.stdout.write( "\n RAW file contains %sx%sx%sx%sx%s [%s] array, dtype=%s, MIN/MAX=%s/%s\n" % (xsz, ysz, zsz, tsz, 1, order, dtype, mn, mx) ) assert zsz * tsz == shape[0], ` zsz, tsz, shape ` tif_filename = "%s%s.ome.tif" % (self.file_prefix, detector) sys.stdout.write(" creating memmap image for OME-TIF file %r..." % (tif_filename)) sys.stdout.flush() mmap = numpy.memmap(fn, dtype=dtype, mode="r", shape=shape) tif_image = TIFFimage(mmap) atexit.register(os.remove, fn) tif_uuid = self._mk_uuid() self.tif_images[detector, tif_filename, tif_uuid] = tif_image sys.stdout.write(" done\n") sys.stdout.flush() pixels_d = {} channel_d = dict(SamplesPerPixel="1") lpath_l = [] # channel_d todo: ExcitationWavelength, EmissionWavelength, Fluor, NDFilter, PockelCellSetting, Color if detector in ["Confocal"]: objective = ome.ObjectiveSettings(ID="Objective:%s" % (self.config["olympus_optics_objective"])) instrument_id = "Instrument:Airy" pixels_d["PhysicalSizeX"] = str(self.config["CONFOCAL_PixelSizeX"]) pixels_d["PhysicalSizeY"] = str(self.config["CONFOCAL_PixelSizeY"]) pixels_d["TimeIncrement"] = str(self.config["CONFOCAL_TimeBetweenFrames"]) channel_d["Name"] = "Confocal" channel_d["IlluminationType"] = "Epifluorescence" channel_d["PinholeSize"] = "180" # todo: FluorescenceCorrelationSpectroscopy channel_d["AcquisitionMode"] = "LaserScanningConfocalMicroscopy" for i in range(1, 5): d1 = "AOTFLine%s" % i ft = confocal_filters.get(d1) if ft is None: continue fn = ft["ex"][0] fn = get_aotf_filter_name(fn, self.config) if "OFF" in fn: continue lpath_l.append(ome.ExcitationFilterRef(ID="Filter:%s:%s" % (d1, fn))) fn = confocal_filters["OpticalTableSplitter"]["di"][0] lpath_l.append(ome.DichroicRef(ID="Dichroic:OpticalTableSplitter:%s" % (fn))) d1 = "ThorlabsWheelPosition%s" % (self.config["thorlabs_filter_wheel_position"][3]) ft = confocal_filters.get(d1) if ft is not None: fn = ft["em"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d1, fn))) elif detector in ["Andor", "Imperx"]: objective = ome.ObjectiveSettings(ID="Objective:%s" % (self.config["optics_objective"])) instrument_id = "Instrument:Suga" channel_d["Name"] = "%s camera" % (detector) channel_d["AcquisitionMode"] = "WideField" pixels_d["PhysicalSizeX"] = pixels_d["PhysicalSizeY"] = str( self.config["CAMERA_%s_PixelSize" % (detector.upper())] ) tbf = float(self.config["CAMERA_%s_TimeBetweenFrames" % (detector.upper())]) d1 = "NikonTurretTopCube%s" % (self.config["top_turret_cube"][3]) d2 = "NikonTurretBottomCube%s" % (self.config["bottom_turret_cube"][3]) top_cube = nikon_filters[d1] bottom_cube = nikon_filters[d2] if detector == "Andor": channel_d["IlluminationType"] = "Epifluorescence" if self.config["CAMERA_ANDOR_FrameTransferMode"] == "1": m = re.search( r"Kinetic cycle time:\s*(?P<time>\d+[.]\d*)\s*sec", self.config["CAMERA_ANDOR_HardwareInformation"], re.M, ) pixels_d["TimeIncrement"] = str(m.group("time")) else: pixels_d["TimeIncrement"] = str(tbf) if "ex" in top_cube: fn = top_cube["ex"][0] lpath_l.append(ome.ExcitationFilterRef(ID="Filter:%s:%s" % (d1, fn))) if "di" in top_cube: fn = top_cube["di"][0] lpath_l.append(ome.DichroicRef(ID="Dichroic:%s:%s" % (d1, fn))) if "em" in top_cube: fn = top_cube["em"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d1, fn))) if "ex" in bottom_cube: fn = bottom_cube["ex"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d2, fn))) else: # m = re.search(r'Exposure time:\s*(?P<time>\d+[.]\d*)\s*msec', self.config['CAMERA_IMPERX_HardwareInformation'], re.M) # exp_time = float (m.group ('time')) if self.config["main_protocol_mode"].startswith("MyocyteMechanicsFluorescence"): tbf = float(self.config["PROTOCOL_MYOCYTE_MECHANICS_TimeBetweenSavedFrames"]) pixels_d["TimeIncrement"] = str(tbf) channel_d["IlluminationType"] = "Transmitted" if self.config["optics_transmission_light_filter"] != "Empty": fn = nikon_filters["NikonIllumination"]["ex"][0] lpath_l.append(ome.ExcitationFilterRef(ID="Filter:NikonIllumination:%s" % (fn))) if "di" in top_cube: fn = top_cube["di"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d1, fn))) if "em" in top_cube: fn = top_cube["em"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d1, fn))) if "em" in bottom_cube: fn = bottom_cube["em"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d2, fn))) if "di" in bottom_cube: fn = bottom_cube["di"][0] lpath_l.append(ome.EmissionFilterRef(ID="Filter:%s:%s" % (d2, fn))) else: raise NotImplementedError(` detector `) if zsz > 1: pixels_d["PhysicalSizeZ"] = str(dz) channel = ome.Channel(ID="Channel:%s" % (detector), **channel_d) lpath = ome.LightPath(*lpath_l) channel.append(lpath) # todo attributes: # todo elements: BIN:BinData, MetadataOnly, SA:AnnotationRef tiffdata = ome.TiffData(ome.UUID(tif_uuid, FileName=tif_filename)) pixels = ome.Pixels( channel, tiffdata, DimensionOrder=order, ID="Pixels:%s" % (detector), SizeX=str(xsz), SizeY=str(ysz), SizeZ=str(zsz), SizeT=str(tsz), SizeC=str(1), Type=self.dtype2PixelIType(dtype), **pixels_d ) for d in plane_l: pixels.append(ome.Plane(**d)) # todo attributes: Name # todo elements: Description, ExperimentRef, DatasetRef , # ImagingEnvironment, StageLabel, ROIRef, MicrobeamManipulationRef, AnnotationRef image = ome.Image( ome.AcquiredDate(self.get_AcquiredDate()), ome.ExperimenterRef(ID="Experimenter:%s" % (self.current_user)), ome.GroupRef(ID="Group:SysBio"), ome.InstrumentRef(ID=instrument_id), objective, pixels, ID="Image:%s" % (detector), ) if 0: image.append(sa.AnnotationRef(ID="Annotation:configuration.txt")) yield image return
from libtiff import TIFFfile import os import numpy as np from PIL import Image input_file = '../data/FIB_segmentaion/grayscale_maps_500.tif' out_path = '../data/raw' if not os.path.exists(out_path): os.makedirs(out_path) tif = TIFFfile(input_file) data, _ = tif.get_samples() data = data[0] print('the shape of data: ', data.shape) for k in range(data.shape[0]): Image.fromarray(data[k]).save( os.path.join(out_path, 'raw_' + str(k).zfill(4) + '.png')) print('Done')
def loadData(outputFilename, frames=None, ycrop=None, xcrop=None, transpose=False, datatype=np.float64): """ Reads data from a tiff, hdf5, or npy file and returns a numpy array. Parameters ---------- outputFilename : string The absolute or relative location of the particular hdf5 or tiff file to be read in. Filename must end in one of the following extensions ['tif', 'tiff', 'hdf5', 'h5', 'npy', 'lsm']. **If the file is hdf5, loadData assumes the data is in a dataset called \"/raw\"**. frames : array_like A tuple, list, or array containing the beginning and ending indices for the frames to be extracted. i.e. frames=[23,77] will return a numpy array containing the frames 23-76 within the file, 'filename'. xcrop : array_like A tuple, list, or array containing the beginning and ending indices for the columns to be extracted. This allows you to specify a subset or roi of the data to be returned. i.e. xcrop=[0,100] will return a numpy array containing first 100 columns of each frame within the file 'filename'. ycrop : array_like A tuple, list, or array containing the beginning and ending indices for the rows to be extracted. This allows you to specify a subset or roi of the data to be returned. i.e. ycrop=[0,100] will return a numpy array containing first 100 rows of each frame within the file 'filename'. transpose : boolean This specifies whether or not to transpose the last two dimensions of each frame. This might happen when reading and writing data between matlab and python, for example. dataset : numpy.dtype Specify the datatype of the returned numpy array. If dataset is of lower precision than the original data then truncation will take place. Returns ------- filename_data : array The data read from within the file specified in filename Notes ----- - If the file type is hdf5, loadData assumes the data to be read in is stored in a dataset called \"/raw\". - After the data has been read in, a .npy file is created and saved with a filename that specifies the parameters of the modified data. If in the future you wish to read in the same data, the .npy file will be read instead, saving time. Example ------- >>> fname = "data/testHDF5.hdf5" >>> yroi = [175,250] >>> xroi = [100,150] >>> hdfData = loadData(fname, frames=[0,32], ycrop=yroi, xcrop=xroi) >>> hdfData.shape (32, 75, 50) >>> hdfData.dtype dtype('float32') >>> hdfData = loadData(fname, ycrop=yroi, xcrop=xroi, datatype=np.int16) >>> hdfData.shape (544, 75, 50) >>> hdfData.dtype dtype('int16') """ print "-- Loading Data..." print "outputFilename = ", outputFilename filename = outputFilename.rstrip('/') print "filename = ", filename basePath, fName = os.path.split(filename) name, ext = os.path.splitext(fName) if basePath and not os.path.exists(basePath): raise IOError, "Directory does not exist: %s" % (basePath) elif not os.path.exists(filename): raise IOError, "File does not exist: %s" % (fName) npFilenameBase = os.path.join(basePath, name) if not frames is None: f0 = frames[0]; f1 = frames[1] npFilenameBase += "_frames" + str(f0) + "-" + str(f1-1) if not ycrop is None: y0 = ycrop[0]; y1 = ycrop[1] npFilenameBase += "_ycrop" + str(y0) + "-" + str(y1-1) if not xcrop is None: x0 = xcrop[0]; x1 = xcrop[1] npFilenameBase += "_xcrop" + str(x0) + "-" + str(x1-1) if transpose: npFilenameBase += "_T" npFilenameBase += "_" + str(np.dtype(datatype)) # File name for the numpy file np_filename = npFilenameBase + '.npy' # Check if a numpy file already exists for this data if os.path.exists(np_filename): print "\t- Numpy file already exists. Loading %s..." % (np_filename) # If so, load it and be done try: volumeData = np.load(np_filename) except IOError: raise IOError, "Error reading filename: \"%s\"" % filename return volumeData elif ext.lower() in ['.npy']: # If it is a numpy file try: volumeData = np.load(filename) except IOError: raise IOError, "Error reading filename: \"%s\"" % filename # Otherwise check if the data is in hdf5 format elif ext.lower() in ['.h5', '.hdf5']: from h5py import File print "\t- Reading from hdf5 file %s..." % (filename) try: h5File = File(filename, 'r') except IOError: raise IOError, "Error reading filename: \"%s\"" % (filename) volumeData = np.array(h5File["/raw"]) # Then check to see if it is an lsm file elif ext.lower() in ['.lsm']: print "\n\nIN LSM SECTION\n\n" from libtiff import TIFFfile try: tiff = TIFFfile(filename) except IOError: raise IOError, "Error opening lsm file: \"%s\"" % (filename) samples, sample_names = tiff.get_samples() outList = [] for sample in samples: outList.append(np.copy(sample)[...,np.newaxis]) out = np.concatenate(outList,axis=-1) out = np.rollaxis(out,0,3) data = np.swapaxes(out[:,:,:,0],0,2) volumeData = np.swapaxes(data,1,2) tiff.close() # Then finally it must a tif file (hopefully)... elif ext.lower() in ['.tif', '.tiff']: print "\t- Reading from tiff file %s..." % (filename) from libtiff import TIFF try: tiff = TIFF.open(filename, 'r') except IOError: raise IOError, "Error opening tiff file: \"%s\"" % (filename) for count,image in enumerate(tiff.iter_images()): image_shape = image.shape volumeData = np.ndarray((count+1, image_shape[0], image_shape[1]), order='c', dtype=np.float64) # DO NOT HARDCODE for count,image in enumerate(tiff.iter_images()): volumeData[count,:,:] = image else: assert False, "The filename must have one of the following extensions [\"h5\", \"hdf5\", \"tif\", \"tiff\", \"npy\"]" if transpose: # For some reason the hdf5 data is transposed when written from matlab... volumeData = np.swapaxes(volumeData,1,2) dims = volumeData.shape if frames is None: f0 = 0; f1 = dims[0] if ycrop is None: y0 = 0; y1 = dims[1] if xcrop is None: x0 = 0; x1 = dims[2] print "made it to here!" finalData = np.array(volumeData[f0:f1,y0:y1,x0:x1], dtype=datatype) print "\t- Saving to numpy data file %s" % (np_filename) # Save it so we don't have to parse the tiff/hdf5 file every time np.save(npFilenameBase, finalData) return finalData
parser.add_argument('files', type=argparse.FileType('r'), nargs='+', help='Files to process') args = parser.parse_args() center = None if args.center is not None: x,y = args.center.split(',') center = (float(x), float(y)) background = None if args.bg is not None: background = None # TODO for f in args.files: print "Processing %s" % f tif = TIFFfile(f.name) samples, sample_names = tif.get_samples() img = samples[0][0,:,:] img = blur_image(img, 3) print "Image size", img.shape #center = np.array(img.shape) / 2 if center is None: center = prompt_center(img) print "Image center", center pl.figure() pl.imshow(np.log1p(img)) pl.colorbar() pl.savefig('proj1.png') p = cartesian_projection(img, center, r_min=args.q_min, r_max=args.q_max) # TODO: Fix scaling
def iter_Image(self, func): sys.stdout.write('iter_Image: reading image data from TIFF files\n') for detector in self.data: sys.stdout.write(' detector: %s\n' % (detector)) d_index = self.data[detector] # write the content of tiff files to a single raw files f,fn,dtype = None, None, None time_set = set() mn, mx = None, None mnz = float(self.config['PROTOCOL_Z_STACKER_Minimum']) mxz = float(self.config['PROTOCOL_Z_STACKER_Maximum']) nz = int(self.config['PROTOCOL_Z_STACKER_NumberOfFrames']) if nz > 1: dz = (mxz-mnz)/(nz-1) else: dz = 0 plane_l = [] ti = -1 exptime = '0' if detector=='Confocal': exptime = float(self.config['CONFOCAL_PixelAcqusitionTime']) * 1e-6 elif detector=='Andor': exptime = self.config['CAMERA_ANDOR_ExposureTime'] elif detector=='Imperx': for line in self.config['CAMERA_IMPERX_HardwareInformation'].split('\n'): if line.startswith ('Exposure time:'): v,u = line[14:].lstrip().split() v = v.strip (); u = u.strip () if u=='usec': exptime = float(v)*1e-6 elif u=='msec': exptime = float(v)*1e-3 elif u=='sec': exptime = float(v) else: raise NotImplementedError (`v,u,line`) else: raise NotImplementedError(`detector`) for t, index in sorted(d_index): if t not in time_set: time_set.add(t) ti += 1 zi = 0 else: zi += 1 z = mnz + dz * zi d = dict(DeltaT=str(t), TheT=str(ti), TheZ = str(zi), PositionZ=str(z), TheC='0', ExposureTime=str(exptime)) plane_l.append(d) tif = TIFFfile(d_index[t, index]) samples, sample_names = tif.get_samples() assert len (sample_names)==1,`sample_names` data = samples[0] if mn is None: mn, mx = data.min(), data.max() else: mn = min (data.min(), mn) mx = min (data.max(), mx) if f is None: shape = list(data.shape) dtype = data.dtype fn = tempfile.mktemp(suffix='.raw', prefix='%s_%s_' % (detector, dtype)) f = open (fn, 'wb') else: assert dtype is data.dtype,`dtype,data.dtype` shape[0] += 1 data.tofile(f) sys.stdout.write('\r copying TIFF image data to RAW file: %5s%% done' % (int(100.0*(index+1)/len(d_index)))) sys.stdout.flush() if f is None: continue f.close () shape = tuple (shape) xsz = shape[2] ysz = shape[1] tsz = len(time_set) zsz = shape[0] // tsz order = 'XYZTC' sys.stdout.write("\n RAW file contains %sx%sx%sx%sx%s [%s] array, dtype=%s, MIN/MAX=%s/%s\n" \ % (xsz, ysz,zsz,tsz,1,order, dtype, mn,mx)) assert zsz*tsz==shape[0],`zsz,tsz,shape` tif_filename = '%s%s.ome.tif' % (self.file_prefix, detector) sys.stdout.write(" creating memmap image for OME-TIF file %r..." % (tif_filename)) sys.stdout.flush() mmap = numpy.memmap(fn, dtype=dtype, mode='r', shape=shape) tif_image = TIFFimage(mmap) atexit.register(os.remove, fn) tif_uuid = self._mk_uuid() self.tif_images[detector, tif_filename, tif_uuid] = tif_image sys.stdout.write (' done\n') sys.stdout.flush() pixels_d = {} channel_d = dict(SamplesPerPixel='1') lpath_l = [] #channel_d todo: ExcitationWavelength, EmissionWavelength, Fluor, NDFilter, PockelCellSetting, Color if detector in ['Confocal']: objective = ome.ObjectiveSettings(ID='Objective:%s' % (self.config['olympus_optics_objective'])) instrument_id = 'Instrument:Airy' pixels_d['PhysicalSizeX'] = str(self.config['CONFOCAL_PixelSizeX']) pixels_d['PhysicalSizeY'] = str(self.config['CONFOCAL_PixelSizeY']) pixels_d['TimeIncrement'] = str(self.config['CONFOCAL_TimeBetweenFrames']) channel_d['Name'] = 'Confocal' channel_d['IlluminationType'] = 'Epifluorescence' channel_d['PinholeSize'] = '180' # todo: FluorescenceCorrelationSpectroscopy channel_d['AcquisitionMode'] = 'LaserScanningConfocalMicroscopy' for i in range (1,5): d1 = 'AOTFLine%s' % i ft = confocal_filters.get(d1) if ft is None: continue fn = ft['ex'][0] fn = get_aotf_filter_name (fn, self.config) if 'OFF' in fn: continue lpath_l.append(ome.ExcitationFilterRef(ID='Filter:%s:%s' % (d1,fn))) fn = confocal_filters['OpticalTableSplitter']['di'][0] lpath_l.append(ome.DichroicRef(ID='Dichroic:OpticalTableSplitter:%s' % (fn))) d1 = 'ThorlabsWheelPosition%s' % (self.config['thorlabs_filter_wheel_position'][3]) ft = confocal_filters.get(d1) if ft is not None: fn = ft['em'][0] lpath_l.append(ome.EmissionFilterRef (ID='Filter:%s:%s' % (d1,fn))) elif detector in ['Andor', 'Imperx']: objective = ome.ObjectiveSettings(ID='Objective:%s' % (self.config['optics_objective'])) instrument_id = 'Instrument:Suga' channel_d['Name'] = '%s camera' % (detector) channel_d['AcquisitionMode'] = 'WideField' pixels_d['PhysicalSizeX'] = pixels_d['PhysicalSizeY'] = str(self.config['CAMERA_%s_PixelSize' % (detector.upper ())]) tbf = float(self.config['CAMERA_%s_TimeBetweenFrames' % (detector.upper ())]) d1 = 'NikonTurretTopCube%s' % (self.config['top_turret_cube'][3]) d2 = 'NikonTurretBottomCube%s' % (self.config['bottom_turret_cube'][3]) top_cube = nikon_filters[d1] bottom_cube = nikon_filters[d2] if detector=='Andor': channel_d['IlluminationType'] = 'Epifluorescence' if self.config['CAMERA_ANDOR_FrameTransferMode']=='1': m = re.search(r'Kinetic cycle time:\s*(?P<time>\d+[.]\d*)\s*sec', self.config['CAMERA_ANDOR_HardwareInformation'], re.M) pixels_d['TimeIncrement'] = str(m.group('time')) else: pixels_d['TimeIncrement'] = str(tbf) if 'ex' in top_cube: fn = top_cube['ex'][0] lpath_l.append(ome.ExcitationFilterRef(ID='Filter:%s:%s' % (d1,fn))) if 'di' in top_cube: fn = top_cube['di'][0] lpath_l.append(ome.DichroicRef(ID='Dichroic:%s:%s' % (d1,fn))) if 'em' in top_cube: fn = top_cube['em'][0] lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d1,fn))) if 'ex' in bottom_cube: fn = bottom_cube['ex'][0] lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d2,fn))) else: #m = re.search(r'Exposure time:\s*(?P<time>\d+[.]\d*)\s*msec', self.config['CAMERA_IMPERX_HardwareInformation'], re.M) #exp_time = float (m.group ('time')) if self.config['main_protocol_mode'].startswith('MyocyteMechanicsFluorescence'): tbf = float(self.config['PROTOCOL_MYOCYTE_MECHANICS_TimeBetweenSavedFrames']) pixels_d['TimeIncrement'] = str(tbf) channel_d['IlluminationType'] = 'Transmitted' if self.config['optics_transmission_light_filter']!='Empty': fn = nikon_filters['NikonIllumination']['ex'][0] lpath_l.append(ome.ExcitationFilterRef(ID='Filter:NikonIllumination:%s' % (fn))) if 'di' in top_cube: fn = top_cube['di'][0] lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d1, fn))) if 'em' in top_cube: fn = top_cube['em'][0] lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d1,fn))) if 'em' in bottom_cube: fn = bottom_cube['em'][0] lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d2,fn))) if 'di' in bottom_cube: fn = bottom_cube['di'][0] lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d2,fn))) else: raise NotImplementedError (`detector`) if zsz>1: pixels_d['PhysicalSizeZ'] = str(dz) channel = ome.Channel(ID='Channel:%s' % (detector), **channel_d) lpath = ome.LightPath(*lpath_l) channel.append (lpath) #todo attributes: #todo elements: BIN:BinData, MetadataOnly, SA:AnnotationRef tiffdata = ome.TiffData(ome.UUID (tif_uuid, FileName=tif_filename)) pixels = ome.Pixels(channel, tiffdata, DimensionOrder=order, ID='Pixels:%s' % (detector), SizeX = str(xsz), SizeY = str(ysz), SizeZ = str(zsz), SizeT=str(tsz), SizeC = str(1), Type = self.dtype2PixelIType (dtype), **pixels_d ) for d in plane_l: pixels.append(ome.Plane(**d)) #todo attributes: Name #todo elements: Description, ExperimentRef, DatasetRef , # ImagingEnvironment, StageLabel, ROIRef, MicrobeamManipulationRef, AnnotationRef image = ome.Image (ome.AcquiredDate (self.get_AcquiredDate()), ome.ExperimenterRef(ID='Experimenter:%s' % (self.current_user)), ome.GroupRef(ID='Group:SysBio'), ome.InstrumentRef(ID=instrument_id), objective, pixels, ID='Image:%s' % (detector)) if 0: image.append(sa.AnnotationRef (ID='Annotation:configuration.txt')) yield image return
def process(self, options=None, validate=default_validate): template_xml = list(self.make_xml()) s = None for (detector, fn, uuid), tif_image in self.tif_images.items(): xml = ome.OME(ATTR( 'xsi', 'schemaLocation', "%s %s/ome.xsd" % ((namespace_map['ome'], ) * 2)), UUID=uuid) for item in template_xml: if item.tag.endswith( 'Image') and item.get('ID') != 'Image:%s' % (detector): continue if item.tag.endswith('Instrument'): if detector == 'Confocal': instrument = 'Airy' elif detector in ['Imperx', 'Andor']: instrument = 'Suga' else: instrument = None if instrument and item.get( 'ID') != 'Instrument:%s' % (instrument): continue xml.append(item) if s is None and validate: s = etree.tostring(xml, pretty_print=True, xml_declaration=True) #print s validate_xml(xml) else: s = etree.tostring(xml, pretty_print=True, xml_declaration=True) tif_image.description = s if detector == 'Confocal': c = tif_image.write_file(fn, compression='lzw') if c < 1.0: print 'Resetting compression to none' tif_image.write_file(fn, compression='none') else: tif_image.write_file(fn, compression='none') if validate and 0: print 'Validating written data..', from libtiff import TIFFfile t = TIFFfile(fn) samples, sample_names = t.get_samples() assert len(sample_names) == 1, ` sample_names ` samples = samples[0] samples_orig = tif_image.data if (samples != samples_orig).any(): print 'DATA CORRUPTION DETECTED!!' print 'original data:', samples_orig.dtype, samples_orig.shape, samples_orig.nbytes print 'written data:', samples.dtype, samples.shape, samples.nbytes diff = samples - samples_orig ia, ja, ka = diff.nonzero() print len(ia) print ia[:10] print ja[:10] print ka[:10] print samples[ia[:10], ja[:10], ka[:10]] print samples_orig[ia[:10], ja[:10], ka[:10]] else: print 'SUCCESS!' #validate = False return s