diff --git a/docs/nwt_format.pptx b/docs/nwt_format.pptx index 8433105..a963ae3 100644 Binary files a/docs/nwt_format.pptx and b/docs/nwt_format.pptx differ diff --git a/python/envi.py b/python/envi.py index b1b332d..045433e 100644 --- a/python/envi.py +++ b/python/envi.py @@ -9,7 +9,6 @@ import os import numpy import scipy import matplotlib.pyplot as plt -#import pyprind import sys from math import floor import progressbar @@ -186,31 +185,62 @@ class envi_header: #save an ENVI header def save(self, fname): - f = open(fname, "w") - f.write("ENVI\n") - f.write("description = {" + self.description + "}" + "\n") - f.write("samples = " + str(self.samples) + "\n") - f.write("lines = " + str(self.lines) + "\n") - f.write("bands = " + str(self.bands) + "\n") - f.write("header offset = " + str(self.header_offset) + "\n") - f.write("file type = ENVI Standard" + "\n") - f.write("data type = " + str(self.get_envi_type(self.type)) + "\n") - f.write("interleave = " + self.interleave + "\n") - f.write("sensor type = " + self.sensor_type + "\n") - f.write("byte order = " + str(self.byte_order) + "\n") - f.write("x start = " + str(self.x_start) + "\n") - f.write("y start = " + str(self.y_start) + "\n") - f.write("wavelength units = " + self.wavelength_units + "\n") - f.write("z plot titles = {" + self.z_plot_titles + "}" + "\n") + f = open(fname, "w") + f.write("ENVI\n") + f.write("description = {" + self.description + "}" + "\n") + f.write("samples = " + str(self.samples) + "\n") + f.write("lines = " + str(self.lines) + "\n") + f.write("bands = " + str(self.bands) + "\n") + f.write("header offset = " + str(self.header_offset) + "\n") + f.write("file type = ENVI Standard" + "\n") + f.write("data type = " + str(self.get_envi_type(self.type)) + "\n") + f.write("interleave = " + self.interleave + "\n") + f.write("sensor type = " + self.sensor_type + "\n") + f.write("byte order = " + str(self.byte_order) + "\n") + f.write("x start = " + str(self.x_start) + "\n") + f.write("y start = " + str(self.y_start) + "\n") + f.write("wavelength units = " + self.wavelength_units + "\n") + f.write("z plot titles = {" + self.z_plot_titles + "}" + "\n") + + # save the wavelength values + if self.wavelength != []: + if len(self.wavelength) == self.bands: + f.write("wavelength = {") + f.write(",".join(map(str, self.wavelength))) + f.write("}\n") + else: + raise Exception("ENVI HEADER ERROR: Number of wavelengths does not match number of bands") - f.close() + f.close() #sets the properties of the header to match those of the input array - def set(self, A): - self.type = A.dtype - self.samples = A.shape[2] - self.lines = A.shape[1] - self.bands = A.shape[0] + def setprops(self, A, interleave="BSQ", wavelength=[]): + # determine the data type automatically + self.type = A.dtype + + # determine the ordering based on the specified interleave + if interleave == "BSQ": + self.samples = A.shape[2] + self.lines = A.shape[1] + self.bands = A.shape[0] + elif interleave == "BIP": + self.samples = A.shape[1] + self.lines = A.shape[2] + self.bands = A.shape[0] + elif interleave == "BIL": + self.samples = A.shape[0] + self.lines = A.shape[2] + self.bands = A.shape[1] + else: + raise Exception("invalid interleave format (requires 'BSQ', 'BIP', or 'BIL') - interleave is set to {}".interleave) + + # if wavelength units are given, make sure that they match the number of bands + if wavelength != []: + if len(wavelength) != self.bands: + raise Exception("invalid number of wavelengths specified") + else: + self.wavelength = wavelength + class envi: @@ -413,8 +443,8 @@ class envi: #read a batch of data based on the mask def loadbatch(self, npixels): i = numpy.flatnonzero(self.mask) #get the indices of valid pixels - if len(i) == self.idx: #if all of the pixels have been read, return an empyt array - return [] + if len(i) == self.idx: #if all of the pixels have been read, return an empyt array + return [] npixels = min(npixels, len(i) - self.idx) #if there aren't enough pixels, change the batch size B = self.header.bands @@ -439,14 +469,14 @@ class envi: #returns an image of the pixels that have been read using batch loading def batchmask(self): - #allocate a new mask - outmask = numpy.zeros(self.mask.shape, dtype=numpy.bool) + #allocate a new mask + outmask = numpy.zeros(self.mask.shape, dtype=numpy.bool) - #zero out any unclassified pixels - idx = self.getidx() - i = numpy.nonzero(self.mask) - outmask[i[0][0:idx], i[1][0:idx]] = self.mask[i[0][0:idx], i[1][0:idx]] - return outmask + #zero out any unclassified pixels + idx = self.getidx() + i = numpy.nonzero(self.mask) + outmask[i[0][0:idx], i[1][0:idx]] = self.mask[i[0][0:idx], i[1][0:idx]] + return outmask def close(self): self.file.close() @@ -455,11 +485,11 @@ class envi: self.file.close() #saves an array as an ENVI file -def save_envi(A, fname): +def save_envi(A, fname, interleave="BSQ", wavelength=[]): #create and save a header file header = envi_header(); - header.set(A) + header.setprops(A, interleave, wavelength) header.save(fname + ".hdr") #save the raw data diff --git a/python/muve-align.py b/python/muve-align.py index 45816b1..ef99902 100644 --- a/python/muve-align.py +++ b/python/muve-align.py @@ -96,8 +96,8 @@ def align(A, B, max_power=5): #warp_matrix[0, 2] = 0 # return warp_matrix -fmask = "Z:/jack/TinkParaffinLung0.005S/*.png" -out_dir = "Z:/jack/TinkParaffinLung0.005S/aligned" +fmask = "D:/Dropbox/todo/lab-presentations/jack/TinkParaffinLung0.005S/*.png" +out_dir = "D:/Dropbox/todo/lab-presentations/jack/TinkParaffinLung0.005S/aligned" if not os.path.isdir(out_dir): os.mkdir(out_dir) @@ -155,4 +155,4 @@ ax.set_xlabel("Press 'z' and 'x' to change slices and arrow keys to align") plt.show() I = apply_alignment(S, warps) -imagestack.save(I, out_dir + "/aligned_", ".bmp") \ No newline at end of file +imagestack.save(I, out_dir + "/aligned_", "bmp") \ No newline at end of file -- libgit2 0.21.4