def SaveGeometry(self, filename=None): """Save the geometry to a file""" if filename is None: self._filename = EVSFileDialog.asksaveasfilename( message="Save Surface", filetypes=self._ft, defaultfile="surface.vtp", defaultextension="*.vtp" ) else: self._filename = filename if self._filename: try: self._writer.SetFileName(self._filename) except AttributeError, msg: logging.exception("IsoSurfaceDisplay") dlg = wx.MessageDialog(self._Dialog, str(msg), "Error", wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() self._writer.SetInputConnection( self._app_states[self._current_image_index].GetFactory().GetInputConnection() ) try: with wx.BusyCursor(): event.notify(ProgressEvent("Saving surface...", 0.0)) self._writer.Write() event.notify(ProgressEvent("Saving surface...", 1.0)) logging.info("Surface saved to '{0}'".format(self._filename)) except IOError, (errno, strerror): logging.exception("IsoSurfaceDisplay") dlg = wx.MessageDialog( self._Dialog, "[Errno %d]: %s" % (errno, strerror), "Error", wx.OK | wx.ICON_ERROR ) dlg.ShowModal() dlg.Destroy()
def SaveData(self, e=None, filename=""): """Save line/histogram data to a text file""" ft = collections.OrderedDict([]) ft["text files"] = ["*.txt"] curr_dir = self.GetCurrentDirectory() if filename == "": filename = EVSFileDialog.asksaveasfilename( message='Save Line Data', filetypes=ft, defaultdir=curr_dir, defaultextension='.txt') if not filename: return self.SaveCurrentDirectory(os.path.dirname(filename)) _file = open(filename, 'wt') # write out a header _file.write('#\n') _file.write('# Creation Date: %s\n' % datetime.datetime.now().isoformat()) if self.filename: filename = self.filename.encode( sys.getfilesystemencoding() or 'UTF-8') else: filename = '(unknown)' _file.write('# Image filename: %s\n' % filename) _file.write('# MicroView Software Version: %s\n' % PACKAGE_VERSION) _file.write('# MicroView Software SHA1: %s\n' % PACKAGE_SHA1) _file.write('#\n') n = len(self.xdata) numC = self.ydata.shape[-1] _format = '%f\t' + (numC - 1) * '%f\t' + '%f\n' _file.write(self.file_label + '\n') for xval, yval in zip(self.xdata, self.ydata): # take care -- yval could be from a multichannel image l = [xval] l.extend(yval) _file.write(_format % tuple(l)) _file.close()
def SavePointsToFile(self): if self._Mark1IsSet == 0 or self._Mark2IsSet == 0: return results_logger = logging.getLogger('results') # Get a filename to save points to ft = collections.OrderedDict() ft["text file"] = ["*.txt", "*.TXT"] curr_dir = self.GetCurrentDirectory() filename = EVSFileDialog.asksaveasfilename( message='Save Points', filetypes=ft, defaultdir=curr_dir, defaultextension='.txt') pt1 = self.GetFirstPoint() pt2 = self.GetSecondPoint() origin = self._ImageData.GetOrigin() spacing = self._ImageData.GetSpacing() if self._sMeasurementUnit == "mm": pt1 = (round(pt1[0], 3), round(pt1[1], 3), round(pt1[2], 3)) pt2 = (round(pt2[0], 3), round(pt2[1], 3), round(pt2[2], 3)) str1 = "(%0.3f,%0.3f,%0.3f) (%0.3f,%0.3f,%0.3f) (mm)" % (pt1[0], pt1[1], pt1[2], pt2[0], pt2[1], pt2[2]) else: pt1 = (round(pt1[0], 3), round(pt1[1], 3), round(pt1[2], 3)) pt2 = (round(pt2[0], 3), round(pt2[1], 3), round(pt2[2], 3)) pt1 = ((pt1[0] - origin[0]) / spacing[0], (pt1[1] - origin[ 1]) / spacing[1], (pt1[2] - origin[2]) / spacing[2]) pt2 = ((pt2[0] - origin[0]) / spacing[0], (pt2[1] - origin[ 1]) / spacing[1], (pt2[2] - origin[2]) / spacing[2]) str1 = "(%0.1f,%0.1f,%0.1f) (%0.1f,%0.1f,%0.1f) (%s)" % ( pt1[0], pt1[1], pt1[2], pt2[0], pt2[1], pt2[2], "pixels") if filename: try: file = open(filename, 'at') file.write(str1) file.write("\n") file.close() results_logger.info(str1) logging.info("Saved line length to '%s'" % filename) except: pass
def SaveGeometry(self, filename=None): """Save the geometry to a file""" if filename is None: self._filename = EVSFileDialog.asksaveasfilename( message='Save Surface', filetypes=self._ft, defaultfile='surface.vtp', defaultextension='*.vtp') else: self._filename = filename if self._filename: try: self._writer.SetFileName(self._filename) except AttributeError, msg: logging.exception("IsoSurfaceDisplay") dlg = wx.MessageDialog(self._Dialog, str(msg), 'Error', wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() self._writer.SetInputConnection(self._app_states[ self._current_image_index].GetFactory().GetInputConnection()) try: with wx.BusyCursor(): event.notify(ProgressEvent("Saving surface...", 0.0)) self._writer.Write() event.notify(ProgressEvent("Saving surface...", 1.0)) logging.info("Surface saved to '{0}'".format( self._filename)) except IOError, (errno, strerror): logging.exception("IsoSurfaceDisplay") dlg = wx.MessageDialog(self._Dialog, "[Errno %d]: %s" % (errno, strerror), 'Error', wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy()
def SaveLineGrayScaleValues(self, fmt): if (self._Mark1IsSet != 1) or (self._Mark2IsSet != 1): return x1, y1, z1 = self._Mark1.GetPosition() x2, y2, z2 = self._Mark2.GetPosition() # calculate the length of the line in pixels spacing = self._ImageData.GetSpacing() LineLength = math.sqrt(math.pow((x1 - x2) / spacing[0], 2) + math.pow((y1 - y2) / spacing[1], 2) + math.pow((z1 - z2) / spacing[2], 2)) line = vtk.vtkLineSource() line.SetPoint1(x1, y1, z1) line.SetPoint2(x2, y2, z2) if LineLength < 100: NumSegments = 100 else: NumSegments = math.ceil(LineLength) line.SetResolution(NumSegments) line.Update() ft = collections.OrderedDict() ft["text file"] = ["*.txt", "*.TXT"] curr_dir = self.GetCurrentDirectory() filename = EVSFileDialog.asksaveasfilename( message='Save Points', filetypes=ft, defaultdir=curr_dir, defaultextension='.txt') if not filename: return curr_dir = os.path.dirname(os.path.abspath(filename)) if filename: self.SaveCurrentDirectory(os.path.abspath(curr_dir)) if filename: if os.path.splitext(filename)[-1].lower() != '.txt': filename += ".txt" _file = open(filename, 'wt') # Write the header. self.WriteFileHeader(_file) origin = self._ImageData.GetOrigin() if self._sMeasurementUnit == "mm": # Calculate the length of the line in mm LineLength = math.sqrt(math.pow(x1 - x2, 2) + math.pow( y1 - y2, 2) + math.pow(z1 - z2, 2)) # Calculate point size in mm PointSize = LineLength / float(NumSegments) _file.write('# %s: %4.3f mm\n' % ("Line Length", LineLength)) _file.write('# %s: (%4.3f, %4.3f, %4.3f) mm\n' % ("Point 1", x1 - origin[0], y1 - origin[1], z1 - origin[2])) _file.write('# %s: (%4.3f, %4.3f, %4.3f) mm\n' % ("Point 2", x2 - origin[0], y2 - origin[1], z2 - origin[2])) _file.write('# %s: %7.6f\n#\n' % ("Point Size", PointSize)) else: # Calculate point size in pixels PointSize = LineLength / float(NumSegments) s = self._ImageData.GetSpacing() _file.write('# %s: %4.3f %s\n' % ( "Line Length", LineLength, "pixels")) _file.write('# %s: (%2.1f, %2.1f, %2.1f) %s\n' % ("Point 1", (x1 - origin[ 0]) / ( float)( s[0]), (y1 - origin[ 1]) / ( float)( s[1]), (z1 - origin[2]) / ( float)( s[2]), "pixels")) _file.write('# %s: (%2.1f, %2.1f, %2.1f) %s\n' % ("Point 2", (x2 - origin[0]) / ( float)( s[0]), (y2 - origin[1]) / ( float)( s[1]), (z2 - origin[2]) / ( float)( s[2]), "pixels")) _file.write('# %s: %7.6f %s\n' % ( "Point Size", PointSize, "pixels")) # Write the data. numScalars = line.GetOutput().GetNumberOfPoints() for n in range(numScalars): point = line.GetOutput().GetPoint(n) GrayScaleValue = self.TriLinearInterpolation( point[0], point[1], point[2]) if fmt: _file.write("%7.6f, %s" % ( PointSize * n, str(GrayScaleValue))) else: _file.write(str(GrayScaleValue)) if n != NumSegments: if fmt: _file.write("\n") else: _file.write(",") _file.close()