def __init__(self, parent = None): QtGui.QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.WordleView = vtkvtg.vtkQtWordleView() # Set widget for the wordle view self.ui.centralWidget.layout().addWidget(self.WordleView.GetWidget()) term_list = [ "straw", "gold", "name", "miller", "manikin", "daughter", "little", "queen", "man", "came", "girl", "give", "spin", "full", "whirr", "spun", "night", "one", "child", "king", "room", "answered", "morning", "names", "took", "began", "thought", "time", "find", "world", "day", "leg", "whole", "another", "three", "rumpelstiltskin", "knew", "left", "help", "still", "crying", "gave", "good", "round", "wheel", "second", "opened", "messenger", "ever", "told", "quite", "alone", "mistress", "larger", "became", "first", "brought", "put", "ring", "devil", "next", "house", "taken", "life", "door", "fire", "reel", "glad", "must", "beautiful", "heard", "promised", "saw", "perhaps", "also", "poor", "shall", "third", "wife", "necklace", "bring", "daybreak", "earth", "hopped", "inquiries", "cried", "dearer", "front", "far", "bid", "early", "every", "hands", "foot", "course", "always", "cry", "become", "conrad", "art", "hare", "hard", "back", "alive", "die", "curious", "bake", "even", "appear", "fox", "enough", "finger", "harry", "caspar", "greedy", "found", "country", "days", "asked", "already", "afterwards", "burning", "imagine", "delighted", "frightened", "appeared", "idea", "clever", "else", "alas", "astonished", "grew", "happened", "heart", "deep", "high", "evening", "anger", "commanded", "happen", "beyond", "end", "able", "forest", "jumping", "brew", "important", "balthazar", "inquire", "glittering" ] size_list = [ 12.0, 10.0, 10.0, 9.0, 9.0, 9.0, 9.0, 8.0, 8.0, 8.0, 7.0, 7.0, 6.0, 6.0, 6.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, ] terms = vtk.vtkStringArray() terms.SetName('dictionary') terms.SetNumberOfComponents(1) for term in term_list: terms.InsertNextValue(term) coeffs = vtk.vtkDoubleArray() coeffs.SetName('coefficient') coeffs.SetNumberOfComponents(1) for size in size_list: coeffs.InsertNextValue(size) # Create a table with some points in it... self.table = vtk.vtkTable() self.table.AddColumn(terms) self.table.AddColumn(coeffs) self.vt = vtk.vtkViewTheme() lut = vtk.vtkLookupTable() lut.SetHueRange(0,0) lut.SetValueRange(0,1) lut.SetSaturationRange(1,1) lut.Build() # Set value for no color by array self.vt.SetPointColor(0,0,0) # Set LUT for color by array self.vt.SetPointLookupTable(lut) # ViewTheme Background color is black by default self.vt.SetBackgroundColor(1,1,1) self.vt2 = vtk.vtkViewTheme() lut2 = vtk.vtkLookupTable() lut2.SetHueRange(0, 0.66) lut2.SetValueRange(0.7, 0.7) lut2.SetSaturationRange(1, 1) lut2.Build() # Set value for no color by array self.vt2.SetPointColor(0,0,0) # Set LUT for color by array self.vt2.SetPointLookupTable(lut2) # ViewTheme Background color is black by default self.vt2.SetBackgroundColor(1,1,1) self.WordleView.AddRepresentationFromInput(self.table) self.WordleView.SetFieldType(vtkvtg.vtkQtWordleView.ROW_DATA) self.WordleView.SetColorByArray(True) self.WordleView.SetColorArrayName('coefficient') self.WordleView.SetTermsArrayName('dictionary') self.WordleView.SetSizeArrayName('coefficient') self.WordleView.SetOutputImageDataDimensions(200, 200) self.WordleView.ApplyViewTheme(self.vt) self.WordleView.SetMaxNumberOfWords(150); self.WordleView.SetFontFamily("Rockwell") self.WordleView.SetFontStyle(vtkvtg.vtkQtWordleView.StyleNormal) self.WordleView.SetFontWeight(99) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.HORIZONTAL) self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.MOSTLY_HORIZONTAL) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.HALF_AND_HALF) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.MOSTLY_VERTICAL) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.VERTICAL) self.WordleView.SetLayoutPathShape(vtkvtg.vtkQtWordleView.CIRCULAR_PATH) # self.WordleView.SetLayoutPathShape(vtkvtg.vtkQtWordleView.SQUARE_PATH) self.WordleView.Update() self.WordleView.ZoomToBounds() self.color_by_array = True self.font_flag = True QtCore.QObject.connect(self.ui.actionExit, QtCore.SIGNAL("triggered()"), self.fileExit)
def LoadData(self): """Routine that does the actual data loading and some format conversion. If a valid file name is given in the constructor, then this routine is called automatically. If you haven't given a file name in the constructor then you'll have to call SetFileName() before calling this.""" # ---------- # Load and construct whole graph and multi-resolution data from Matlab structure print 'Trying to load data set from .mat file... ', self.data_file if len(self.data_file) == 0: print "No data file name error!!!" raise IOError, "No data file name: Use SetFileName('file.mat') before LoadData()" print 'Trying to really load now...' try: MatInput = scipy.io.loadmat(self.data_file, struct_as_record=True, chars_as_strings=True) except: print 'loadmat crapping out for some reason...' raise IOError, "Can't load supplied matlab file" # return # Get variables out of Matlab structure print 'Transferring variables from Matlab structures' # Now using single structure, S, to save important variables from Matlab S = None if MatInput.has_key('S'): S = MatInput['S'] else: raise IOError, "Matlab file doesn't have S structure. Must be from old gen file..." # NOTE: Accessing data from S to a convenient numpy array / value # 2D arrays: xx = S['xx'].flat[0] # 1D arrays: xx = S['xx'].flat[0].flatten() # Scalars: xx = S['xx'].flat[0].flatten()[0] def s_2Darray(name): return S[name].flat[0] def s_1Darray(name): return S[name].flat[0].flatten() def s_listOf1Darrays(name): tmp = S[name].flat[0].flatten().tolist() if tmp[0].shape[1] == 1: return [xx.T[0] for xx in tmp] else: return [xx[0] for xx in tmp] def s_listOf2Darrays(name): return S[name].flat[0].flatten().tolist() def s_listOfStrings(name): return [xx[0] for xx in S[name].flat[0].flatten().tolist()] def s_listOf1DarraysOffset(name): tmp = S[name].flat[0].flatten().tolist() if tmp[0].shape[1] == 1: return [xx.T[0]-1 for xx in tmp] else: return [xx[0]-1 for xx in tmp] def s_scalar(name): return S[name].flat[0].flatten()[0] def s_logical(name): return (S[name].flat[0].flatten()[0] != 0) # Flags self.isImageData = s_logical('isImageData') self.isTextData = s_logical('isTextData') self.isCompressed = s_logical('isCompressed') self.hasLabels = s_logical('hasLabels') self.hasLabelMeanings = s_logical('hasLabelMeanings') self.hasLabelSetNames = s_logical('hasLabelSetNames') self.hasDocTitles = s_logical('hasDocTitles') self.hasDocFileNames = s_logical('hasDocFileNames') self.X = N.mat(s_2Darray('X')) # Test if original images are downsampled in this data if self.isCompressed: self.V = N.mat(s_2Darray('V')) self.cm = N.mat(s_1Darray('cm')) # not sure if should be matrix or array... # Various plain matrices # NOTE: Have to be careful of anything that can have a 0 value in Matlab # because it might be naturally imported as an unsigned int, and then # when you subtract 1 from it you don't get a negative number as you'd expect self.cp = (s_1Darray('cp').astype('int16') - 1) # change here to zero-based indexing self.IniLabels = (s_1Darray('IniLabels') - 1) # change here to zero-based indexing self.NumberInNet = s_1Darray('NumberInNet') self.Scales = (s_1Darray('Scales') - 1) # zero-based self.IsALeaf = s_1Darray('IsALeaf').astype('bool') self.LeafNodes = (s_1Darray('LeafNodes') - 1) # zero-based # LeafNodesImap maps LeafNode entries, which are node indices, to indices of # arrays like CelWavCoeffs and CelScalCoeffs (in the non-scale, first dimension) self.LeafNodesImap = (s_1Darray('LeafNodesImap').astype('int16') - 1) # zero-based self.EigenVecs = s_2Darray('EigenVecs') self.EigenVals = s_1Darray('EigenVals') self.CelWavCoeffs = s_2Darray('CelWavCoeffs') self.CelScalCoeffs = s_2Darray('CelScalCoeffs') if self.isImageData: self.imR = s_scalar('imR') self.imC = s_scalar('imC') else: # Not sure whether will set this in Matlab or here... self.imR = 200 self.imC = 200 # Load in category labels, but map them to sequential integers starting at 0 if self.hasLabels: labels_array = s_2Darray('Labels') # ncats x npoints 2d array self.cat_labels = N.zeros_like(labels_array) for ii in range(labels_array.shape[0]): cl_unique = set(labels_array[ii,:]) cl_map = {} for jj,vv in enumerate(cl_unique): cl_map[vv] = jj self.cat_labels[ii,:] = N.array([cl_map[vv] for vv in labels_array[ii,:]]) if self.hasLabels: self.label_names = [] # Check whether there are labels names and if there are the right number if self.hasLabelSetNames: names_array = s_1Darray('LabelSetNames') if names_array.size == self.cat_labels.shape[0]: for name_ar in names_array: self.label_names.append(name_ar[0] + '_ids') # Else generate fake names else: for ii in range(self.cat_labels.shape[0]): self.label_names.append('label_' + str(ii) + '_ids') if self.hasLabelMeanings: self.LabelMeanings = s_listOfStrings('LabelMeanings') # Need the max number of dims at each scale to fill in zeros for pcoords plot # Create copies for both wavelet coeffs and scaling functions since these often # have different dimensionalities self.WavMaxDim = N.zeros(self.CelWavCoeffs.shape[1],dtype='int') for row in range(self.CelWavCoeffs.shape[0]): for col in range(self.CelWavCoeffs.shape[1]): if self.WavMaxDim[col] < self.CelWavCoeffs[row,col].shape[1]: self.WavMaxDim[col] = self.CelWavCoeffs[row,col].shape[1] self.ScalMaxDim = N.zeros(self.CelScalCoeffs.shape[1],dtype='int') for row in range(self.CelScalCoeffs.shape[0]): for col in range(self.CelScalCoeffs.shape[1]): if self.ScalMaxDim[col] < self.CelScalCoeffs[row,col].shape[1]: self.ScalMaxDim[col] = self.CelScalCoeffs[row,col].shape[1] # Gather helpful statistics to be used by other classes print 'Calulating extrema of coefficients' self.WavCoeffMax = -1e200 self.WavCoeffMin = 1e200 self.ScalCoeffMax = -1e200 self.ScalCoeffMin = 1e200 for ii in range(self.CelWavCoeffs.shape[0]): for jj in range(self.CelWavCoeffs.shape[1]): if (self.CelWavCoeffs[ii,jj].size > 0): wmax = N.amax(self.CelWavCoeffs[ii,jj]) wmin = N.amin(self.CelWavCoeffs[ii,jj]) if (wmax > self.WavCoeffMax): self.WavCoeffMax = wmax if (wmin < self.WavCoeffMin): self.WavCoeffMin = wmin if (self.CelScalCoeffs[ii,jj].size > 0): smax = N.amax(self.CelScalCoeffs[ii,jj]) smin = N.amin(self.CelScalCoeffs[ii,jj]) if (smax > self.ScalCoeffMax): self.ScalCoeffMax = smax if (smin < self.ScalCoeffMin): self.ScalCoeffMin = smin # NumPts = Number of data points (here number of individual images) self.NumPts = self.IniLabels.shape[0] self.AmbientDimension = s_scalar('AmbientDimension') # used to call this D # Converting cell arrays to lists of numpy arrays self.PointsInNet = s_listOf1DarraysOffset('PointsInNet') # Points In Net, 0-based indices self.ScalFuns = s_listOf2Darrays('ScalFuns') # Scaling functions self.WavBases = s_listOf2Darrays('WavBases') # Wavelet bases self.Centers = s_listOf1Darrays('Centers') # Center of each node # Creating a storage space for ordering of leaf nodes in icicle view with # default values of ordering according to Matlab-saved LeafNodes ice_leaf_ids = self.LeafNodes ice_leaf_xmins = N.arange(ice_leaf_ids.size) ice_ids_mapped = self.LeafNodesImap[ice_leaf_ids] self.mapped_leaf_pos = N.zeros_like(ice_leaf_xmins) self.mapped_leaf_pos[ice_ids_mapped] = ice_leaf_xmins # Flag to set whether generic routines should return Wavelet or Scaling Function # coefficients / images -- "wav" or "scal" self.SetCoeffSource("wavelet") # -- Wordle -- # Flag to indicate whether images should be generated directly # or by passing terms through QtWordleView self.WordleImages = False self.qinit = None self.WordleView = None self.WordleTable = None self.Terms = None if self.isTextData: print "Starting text data area" self.WordleImages = True mat_terms = s_listOfStrings('Terms') self.Terms = vtk.vtkStringArray() self.Terms.SetName('dictionary') self.Terms.SetNumberOfComponents(1) for term in mat_terms: self.Terms.InsertNextValue(term) if self.hasDocTitles: self.DocTitles = s_listOfStrings('DocTitles') print "Creating initial table" # Init Table and put in some sample data that will be replaced later basis_idx = 0 coeffs = VN.numpy_to_vtk(self.WavBases[basis_idx][:,0]*100, deep=True) coeffs.SetName('coefficient') c_sign = VN.numpy_to_vtk(N.sign(self.WavBases[basis_idx][:,0]), deep=True) c_sign.SetName('sign') # Create a table with some points in it... self.WordleTable = vtk.vtkTable() self.WordleTable.AddColumn(self.Terms) self.WordleTable.AddColumn(coeffs) self.WordleTable.AddColumn(c_sign) # self.qinit = vtk.vtkQtInitialization() self.WordleView = vtkvtg.vtkQtWordleView() vt = vtk.vtkViewTheme() lut = self.GetDivergingLUT() # Set value for no color by array vt.SetPointColor(0,0,0) # Set LUT for color by array vt.SetPointLookupTable(lut) # ViewTheme Background color is black by default vt.SetBackgroundColor(1,1,1) self.WordleView.SetFieldType(vtkvtg.vtkQtWordleView.ROW_DATA) self.WordleView.AddRepresentationFromInput(self.WordleTable) self.WordleView.SetColorByArray(True) self.WordleView.ApplyViewTheme(vt) self.WordleView.SetColorArrayName('sign') self.WordleView.SetTermsArrayName('dictionary') self.WordleView.SetSizeArrayName('coefficient') self.WordleView.SetOutputImageDataDimensions(200, 200) self.WordleView.SetMaxNumberOfWords(50); self.WordleView.SetFontFamily("Rockwell") self.WordleView.SetFontStyle(vtkvtg.vtkQtWordleView.StyleNormal) self.WordleView.SetFontWeight(99) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.HORIZONTAL) self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.MOSTLY_HORIZONTAL) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.HALF_AND_HALF) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.MOSTLY_VERTICAL) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.VERTICAL) # self.WordleView.SetLayoutPathShape(vtkvtg.vtkQtWordleView.CIRCULAR_PATH) self.WordleView.SetLayoutPathShape(vtkvtg.vtkQtWordleView.SQUARE_PATH) self.data_loaded = True
Example using PyQt4 & VTK Infovis 10 Sept 2009 -- E Monson """ import vtk import vtkvtg import sys import numpy as N from vtk.util import numpy_support as VN import scipy.io qinit = vtk.vtkQtInitialization() WordleView = vtkvtg.vtkQtWordleView() data_dir = "/Users/emonson/Data/Fodava/EMoGWDataSets/" data_file = data_dir + "sciNews_20110216.mat" print "Trying to load data set from .mat file..." try: MatInput = scipy.io.loadmat(data_file, struct_as_record=True, chars_as_strings=True) except: raise IOError, "Can't load supplied matlab file" mat_terms = MatInput["terms"].T[0] WavBases = [] # Wavelet bases Centers = [] # Center of each node
def __init__(self, parent = None): QtGui.QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.WordleView = vtkvtg.vtkQtWordleView() # Set widget for the wordle view self.ui.centralWidget.layout().addWidget(self.WordleView.GetWidget()) data_dir = '/Users/emonson/Data/Fodava/EMoGWDataSets/' data_file = data_dir + 'sciNews_20110216.mat' print 'Trying to load data set from .mat file...' try: MatInput = scipy.io.loadmat(data_file, struct_as_record=True, chars_as_strings=True) except: raise IOError, "Can't load supplied matlab file" self.mat_terms = MatInput['terms'].T[0] self.WavBases = [] # Wavelet bases self.Centers = [] # Center of each node # NodeWavCoeffs = [] # NodeScalCoeffs = [] for ii in range(MatInput['PointsInNet'].shape[1]): self.WavBases.append(N.mat(MatInput['WavBases'][0,ii])) # matrix self.Centers.append(N.mat(MatInput['Centers'][0,ii][0])) # matrix terms = vtk.vtkStringArray() terms.SetName('dictionary') terms.SetNumberOfComponents(1) for term in self.mat_terms: terms.InsertNextValue(term[0]) self.basis_idx = 0 coeffs = VN.numpy_to_vtk(self.WavBases[self.basis_idx][:,0]*100, deep=True) coeffs.SetName('coefficient') c_sign = VN.numpy_to_vtk(N.sign(self.WavBases[self.basis_idx][:,0]), deep=True) c_sign.SetName('sign') # Create a table with some points in it... self.table = vtk.vtkTable() self.table.AddColumn(terms) self.table.AddColumn(coeffs) self.table.AddColumn(c_sign) vt = vtk.vtkViewTheme() lut = vtk.vtkLookupTable() lut.SetHueRange(0, 0.66) lut.SetValueRange(0.7, 0.7) lut.SetSaturationRange(1, 1) lut.Build() # Set value for no color by array vt.SetPointColor(0,0,0) # Set LUT for color by array vt.SetPointLookupTable(lut) # ViewTheme Background color is black by default vt.SetBackgroundColor(1,1,1) self.WordleView.AddRepresentationFromInput(self.table) self.WordleView.SetFieldType(vtkvtg.vtkQtWordleView.ROW_DATA) self.WordleView.SetColorByArray(True) self.WordleView.SetColorArrayName('sign') self.WordleView.SetTermsArrayName('dictionary') self.WordleView.SetSizeArrayName('coefficient') self.WordleView.ApplyViewTheme(vt) self.WordleView.SetMaxNumberOfWords(150); self.WordleView.SetFontFamily("Rockwell") self.WordleView.SetFontStyle(vtkvtg.vtkQtWordleView.StyleNormal) self.WordleView.SetFontWeight(99) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.HORIZONTAL) self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.MOSTLY_HORIZONTAL) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.HALF_AND_HALF) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.MOSTLY_VERTICAL) # self.WordleView.SetOrientation(vtkvtg.vtkQtWordleView.VERTICAL) self.WordleView.SetLayoutPathShape(vtkvtg.vtkQtWordleView.CIRCULAR_PATH) # self.WordleView.SetLayoutPathShape(vtkvtg.vtkQtWordleView.SQUARE_PATH) QtCore.QObject.connect(self.ui.actionExit, QtCore.SIGNAL("triggered()"), self.fileExit) # Do first update before changing to debug mode... self.WordleView.Update() self.WordleView.ZoomToBounds()