Commit 59b0e5f5edf6b0497e1a0c37d46acca5c542343d

Authored by David Mayerich
1 parent 2119be83

updated envi file to support adding elements (like wavelength units)

docs/nwt_format.pptx
No preview for this file type
@@ -9,7 +9,6 @@ import os @@ -9,7 +9,6 @@ import os
9 import numpy 9 import numpy
10 import scipy 10 import scipy
11 import matplotlib.pyplot as plt 11 import matplotlib.pyplot as plt
12 -#import pyprind  
13 import sys 12 import sys
14 from math import floor 13 from math import floor
15 import progressbar 14 import progressbar
@@ -186,31 +185,62 @@ class envi_header: @@ -186,31 +185,62 @@ class envi_header:
186 185
187 #save an ENVI header 186 #save an ENVI header
188 def save(self, fname): 187 def save(self, fname):
189 - f = open(fname, "w")  
190 - f.write("ENVI\n")  
191 - f.write("description = {" + self.description + "}" + "\n")  
192 - f.write("samples = " + str(self.samples) + "\n")  
193 - f.write("lines = " + str(self.lines) + "\n")  
194 - f.write("bands = " + str(self.bands) + "\n")  
195 - f.write("header offset = " + str(self.header_offset) + "\n")  
196 - f.write("file type = ENVI Standard" + "\n")  
197 - f.write("data type = " + str(self.get_envi_type(self.type)) + "\n")  
198 - f.write("interleave = " + self.interleave + "\n")  
199 - f.write("sensor type = " + self.sensor_type + "\n")  
200 - f.write("byte order = " + str(self.byte_order) + "\n")  
201 - f.write("x start = " + str(self.x_start) + "\n")  
202 - f.write("y start = " + str(self.y_start) + "\n")  
203 - f.write("wavelength units = " + self.wavelength_units + "\n")  
204 - f.write("z plot titles = {" + self.z_plot_titles + "}" + "\n") 188 + f = open(fname, "w")
  189 + f.write("ENVI\n")
  190 + f.write("description = {" + self.description + "}" + "\n")
  191 + f.write("samples = " + str(self.samples) + "\n")
  192 + f.write("lines = " + str(self.lines) + "\n")
  193 + f.write("bands = " + str(self.bands) + "\n")
  194 + f.write("header offset = " + str(self.header_offset) + "\n")
  195 + f.write("file type = ENVI Standard" + "\n")
  196 + f.write("data type = " + str(self.get_envi_type(self.type)) + "\n")
  197 + f.write("interleave = " + self.interleave + "\n")
  198 + f.write("sensor type = " + self.sensor_type + "\n")
  199 + f.write("byte order = " + str(self.byte_order) + "\n")
  200 + f.write("x start = " + str(self.x_start) + "\n")
  201 + f.write("y start = " + str(self.y_start) + "\n")
  202 + f.write("wavelength units = " + self.wavelength_units + "\n")
  203 + f.write("z plot titles = {" + self.z_plot_titles + "}" + "\n")
  204 +
  205 + # save the wavelength values
  206 + if self.wavelength != []:
  207 + if len(self.wavelength) == self.bands:
  208 + f.write("wavelength = {")
  209 + f.write(",".join(map(str, self.wavelength)))
  210 + f.write("}\n")
  211 + else:
  212 + raise Exception("ENVI HEADER ERROR: Number of wavelengths does not match number of bands")
205 213
206 - f.close() 214 + f.close()
207 215
208 #sets the properties of the header to match those of the input array 216 #sets the properties of the header to match those of the input array
209 - def set(self, A):  
210 - self.type = A.dtype  
211 - self.samples = A.shape[2]  
212 - self.lines = A.shape[1]  
213 - self.bands = A.shape[0] 217 + def setprops(self, A, interleave="BSQ", wavelength=[]):
  218 + # determine the data type automatically
  219 + self.type = A.dtype
  220 +
  221 + # determine the ordering based on the specified interleave
  222 + if interleave == "BSQ":
  223 + self.samples = A.shape[2]
  224 + self.lines = A.shape[1]
  225 + self.bands = A.shape[0]
  226 + elif interleave == "BIP":
  227 + self.samples = A.shape[1]
  228 + self.lines = A.shape[2]
  229 + self.bands = A.shape[0]
  230 + elif interleave == "BIL":
  231 + self.samples = A.shape[0]
  232 + self.lines = A.shape[2]
  233 + self.bands = A.shape[1]
  234 + else:
  235 + raise Exception("invalid interleave format (requires 'BSQ', 'BIP', or 'BIL') - interleave is set to {}".interleave)
  236 +
  237 + # if wavelength units are given, make sure that they match the number of bands
  238 + if wavelength != []:
  239 + if len(wavelength) != self.bands:
  240 + raise Exception("invalid number of wavelengths specified")
  241 + else:
  242 + self.wavelength = wavelength
  243 +
214 244
215 245
216 class envi: 246 class envi:
@@ -413,8 +443,8 @@ class envi: @@ -413,8 +443,8 @@ class envi:
413 #read a batch of data based on the mask 443 #read a batch of data based on the mask
414 def loadbatch(self, npixels): 444 def loadbatch(self, npixels):
415 i = numpy.flatnonzero(self.mask) #get the indices of valid pixels 445 i = numpy.flatnonzero(self.mask) #get the indices of valid pixels
416 - if len(i) == self.idx: #if all of the pixels have been read, return an empyt array  
417 - return [] 446 + if len(i) == self.idx: #if all of the pixels have been read, return an empyt array
  447 + return []
418 npixels = min(npixels, len(i) - self.idx) #if there aren't enough pixels, change the batch size 448 npixels = min(npixels, len(i) - self.idx) #if there aren't enough pixels, change the batch size
419 B = self.header.bands 449 B = self.header.bands
420 450
@@ -439,14 +469,14 @@ class envi: @@ -439,14 +469,14 @@ class envi:
439 469
440 #returns an image of the pixels that have been read using batch loading 470 #returns an image of the pixels that have been read using batch loading
441 def batchmask(self): 471 def batchmask(self):
442 - #allocate a new mask  
443 - outmask = numpy.zeros(self.mask.shape, dtype=numpy.bool) 472 + #allocate a new mask
  473 + outmask = numpy.zeros(self.mask.shape, dtype=numpy.bool)
444 474
445 - #zero out any unclassified pixels  
446 - idx = self.getidx()  
447 - i = numpy.nonzero(self.mask)  
448 - outmask[i[0][0:idx], i[1][0:idx]] = self.mask[i[0][0:idx], i[1][0:idx]]  
449 - return outmask 475 + #zero out any unclassified pixels
  476 + idx = self.getidx()
  477 + i = numpy.nonzero(self.mask)
  478 + outmask[i[0][0:idx], i[1][0:idx]] = self.mask[i[0][0:idx], i[1][0:idx]]
  479 + return outmask
450 480
451 def close(self): 481 def close(self):
452 self.file.close() 482 self.file.close()
@@ -455,11 +485,11 @@ class envi: @@ -455,11 +485,11 @@ class envi:
455 self.file.close() 485 self.file.close()
456 486
457 #saves an array as an ENVI file 487 #saves an array as an ENVI file
458 -def save_envi(A, fname): 488 +def save_envi(A, fname, interleave="BSQ", wavelength=[]):
459 489
460 #create and save a header file 490 #create and save a header file
461 header = envi_header(); 491 header = envi_header();
462 - header.set(A) 492 + header.setprops(A, interleave, wavelength)
463 header.save(fname + ".hdr") 493 header.save(fname + ".hdr")
464 494
465 #save the raw data 495 #save the raw data
python/muve-align.py
@@ -96,8 +96,8 @@ def align(A, B, max_power=5): @@ -96,8 +96,8 @@ def align(A, B, max_power=5):
96 #warp_matrix[0, 2] = 0 96 #warp_matrix[0, 2] = 0
97 # return warp_matrix 97 # return warp_matrix
98 98
99 -fmask = "Z:/jack/TinkParaffinLung0.005S/*.png"  
100 -out_dir = "Z:/jack/TinkParaffinLung0.005S/aligned" 99 +fmask = "D:/Dropbox/todo/lab-presentations/jack/TinkParaffinLung0.005S/*.png"
  100 +out_dir = "D:/Dropbox/todo/lab-presentations/jack/TinkParaffinLung0.005S/aligned"
101 101
102 if not os.path.isdir(out_dir): 102 if not os.path.isdir(out_dir):
103 os.mkdir(out_dir) 103 os.mkdir(out_dir)
@@ -155,4 +155,4 @@ ax.set_xlabel("Press 'z' and 'x' to change slices and arrow keys to align") @@ -155,4 +155,4 @@ ax.set_xlabel("Press 'z' and 'x' to change slices and arrow keys to align")
155 plt.show() 155 plt.show()
156 156
157 I = apply_alignment(S, warps) 157 I = apply_alignment(S, warps)
158 -imagestack.save(I, out_dir + "/aligned_", ".bmp")  
159 \ No newline at end of file 158 \ No newline at end of file
  159 +imagestack.save(I, out_dir + "/aligned_", "bmp")
160 \ No newline at end of file 160 \ No newline at end of file