def sendVmnetEvent(self, eventName): binding = ("interface", "string", self.vmnetReal) Mgmt.open() Mgmt.event(eventName, binding) Mgmt.close() Logging.log(Logging.LOG_INFO, "Event %s on interface %s sent" % ( eventName, self.vmnetReal))
def getPolyDataInput(self, mapIndex): """ Return the input imagedata #n """ if not self.dataUnit: self.dataUnit = scripting.combinedDataUnit # By default, asking for say, input number 1 gives you # the first (0th actually) input mapping # these can be thought of as being specified in the GUI where you have as many # selections of input data as the filter defines (with the variable numberOfInputs) if mapIndex not in self.inputMapping: self.setInputChannel(mapIndex, mapIndex-1) # Input mapping 0 means to return the input from the filter stack above if self.inputMapping[mapIndex] == 0 and self.dataUnit and self.dataUnit.isProcessed(): try: image = self.polyInput except: traceback.print_exc() Logging.info("No input with number %d" %self.inputIndex, self.inputs, kw = "processing") else: # If input from stack is not requested, or the dataunit is not processed, then just return # the image data from the corresponding channel Logging.info("Using input from channel %d as input %d" % (self.inputMapping[mapIndex] - 1, mapIndex), \ kw = "processing") image = self.getPolyDataInputFromChannel(self.inputMapping[mapIndex] - 1) return image
def onSetToColor(self, event): """ Set the ctf to be a specific color """ col = event.GetValue() color = col.Red(), col.Green(), col.Blue() if 255 not in color: mval = max(color) coeff = 255.0 / mval ncolor = [int(x * coeff) for x in color] Logging.info("New color = ", ncolor, kw = "ctf") dlg = wx.MessageDialog(self, "The color you selected: %d,%d,%d is incorrect." "At least one of the R, G or B components\n" "of the color must be 255. Therefore, " "I have modified the color a bit. " "It is now %d,%d,%d. Have a nice day." % (color[0], color[1], color[2], ncolor[0], ncolor[1], ncolor[2]), "Selected color is incorrect", wx.OK | wx.ICON_WARNING) dlg.ShowModal() dlg.Destroy() color = ncolor r, g, b = color self.redpoints = [(0, 0), (self.maxval, r)] self.greenpoints = [(0, 0), (self.maxval, g)] self.bluepoints = [(0, 0), (self.maxval, b)] self.points = [self.redpoints, self.greenpoints, self.bluepoints, self.alphapoints] self.freeMode = 0 self.upToDate = 0 self.updateGraph() self.updateCTFView() lib.messenger.send(None, "data_changed", 0) self.colorBtn.SetColour(col)
def enableRPS(self): # Enable RPS Logging.log(Logging.LOG_INFO, "Enabling RPS") commandList = ["/bin/cat", "/cgroup/cpuset/esx/cpuset.cpus"] (retcode, output, errmsg) = self.launchProcessAndWait( commandList, logLevel = Logging.LOG_INFO) cpus = output.split("-") no_of_cpus = int(cpus[1]) - int(cpus[0]) + 1 mask = '' for bit in range(no_of_cpus): mask = mask + '1' hex_mask = hex(int(mask, 2)).replace('0x', '') rps_file = '/sys/class/net/' + self.vmnetReal + '/queues/rx-0/rps_cpus' cmd = "/bin/echo %s > %s" % (hex_mask, rps_file) process = subprocess.Popen( cmd, bufsize=-1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output, errmsg = process.communicate() if process.returncode != 0: Logging.log(Logging.LOG_ERR, "Launch process '%s' with error(%s):%s" % ( ' '.join(cmd), process.returncode, errmsg))
def onFreeMode(self, event): """ Toggle free mode on / off """ was = 0 if self.freeMode:was = 1 if not was and event.GetIsDown(): self.updateCTFFromPoints() self.updateGraph() self.freeMode = 1 self.setFromColorTransferFunction(self.ctf, self.otf) if was: self.updateCTFFromPoints() self.freeMode = event.GetIsDown() if not self.freeMode and was and self.hasPainted: Logging.info("Analyzing free mode for points", kw = "ctf") self.getPointsFromFree() n = len(self.points) tot = 0 for i, pts in enumerate(self.points): tot += len(pts) maxpts = self.maxNodes.GetValue() if maxpts < tot: self.onSetMaxNodes(None) self.updateGraph()
def createLine(self, x1, y1, x2, y2, color = "WHITE", brush = None, **kws): """ Draws a line from (x1,y1) to (x2,y2). The method takes into account the scale factor """ if brush: self.dc.SetBrush(brush) self.dc.SetPen(wx.Pen(color)) # (x1,y1) and (x2,y2) are in coordinates where # origo is the lower left corner x12 = x1 + self.xoffset y12 = self.maxy - y1 + self.yoffset y22 = self.maxy - y2 + self.yoffset x22 = x2 + self.xoffset arr = None try: self.dc.DrawLine(x12 / self.scale, y12 / self.scale, x22 / self.scale, y22 / self.scale) except: Logging.info("Failed to draw line from %f/%f,%f/%f to %f/%f,%f/%f" % (x12, self.scale, y12, self.scale, x22, self.scale, y22, self.scale), kw = "ctf") if kws.has_key("arrow"): if kws["arrow"] == "HORIZONTAL": lst = [(x22 / self.scale - 3, y22 / self.scale - 3), (x22 / self.scale, y22 / self.scale), (x22 / self.scale - 3, y22 / self.scale + 3)] elif kws["arrow"] == "VERTICAL": lst = [(x22 / self.scale - 3, y22 / self.scale + 3), (x22 / self.scale, y22 / self.scale), (x22 / self.scale + 3, y22 / self.scale + 3)] self.dc.DrawPolygon(lst)
def paintFreeMode(self, redfunc, greenfunc, bluefunc, alphafunc, maximumValue = -1): """ Paints the graph of the function specified as a list of all values of the function """ self.dc = wx.MemoryDC() self.dc.SelectObject(self.buffer) d = self.maxx / float(maximumValue) if d < 1:d = 1 if not self.background: Logging.info("Constructing background from minval = %d, maxval = %d" % (self.minval, self.maxval)) self.background = self.drawBackground(self.minval, self.maxval) self.dc.BeginDrawing() self.dc.DrawBitmap(self.background, 0, 0) coeff = float(self.maxx) / maximumValue redline = [(int(x * coeff), self.maxy - y) for x, y in enumerate(redfunc)] greenline = [(int(x * coeff), self.maxy - y) for x, y in enumerate(greenfunc)] blueline = [(int(x * coeff), self.maxy - y) for x, y in enumerate(bluefunc)] alphaline = [(int(x * coeff), self.maxy - y) for x, y in enumerate(alphafunc)] self.dc.SetPen(wx.Pen(wx.Colour(255, 0, 0), 1)) self.dc.DrawLines(redline, self.xoffset, self.yoffset) self.dc.SetPen(wx.Pen(wx.Colour(0, 255, 0), 1)) self.dc.DrawLines(greenline, self.xoffset, self.yoffset) self.dc.SetPen(wx.Pen(wx.Colour(0, 0, 255), 1)) self.dc.DrawLines(blueline, self.xoffset, self.yoffset) self.dc.SetPen(wx.Pen(wx.Colour(255, 255, 255), 1)) self.dc.DrawLines(alphaline, self.xoffset, self.yoffset) self.dc.SelectObject(wx.NullBitmap) self.dc = None
def setScatterplot(self, plot): """ Sets the scatterplot as vtkImageData """ self.scatterplot = plot x0, x1 = self.scatterplot.GetScalarRange() Logging.info("Scalar range of scatterplot=", x0, x1, kw = "processing")
def handle_baddresses(command): #Copied from handle_addresses #Note: This only works while Jacob's name is "Dingus Eck" log.debug("Sender obj:", command.senderObj) JACOB_CONSTANT = (any(name in command.senderObj.getGMName().lower() for name in ['eck','dan'])) if command.senderObj else False log.debug("JACOB: ", JACOB_CONSTANT) log.debug("Name checking:", command.senderObj.getGMName().lower()) for i in ['eck','dan']: log.debug("Checking",i,":",i in command.senderObj.getGMName().lower()) names = [] add = [] users = command.group.users.getUsersSorted(lambda user: user.getName()) for user in users: baseAddress = user.getAddress() if baseAddress: names.append("Addresses for " + user.getName() + ":\n") add.append("--" + baseAddress + "\n") for modifier in Events.ADDRESS_MODIFIERS: #Goes through all possible address types subAddress = user.getAddress(modifier) if subAddress: add[-1] += "--" + modifier.title() + ": " + subAddress + "\n" random.shuffle(names) random.shuffle(add) if JACOB_CONSTANT: log.command("JACOB_CONSTANT ACTIVE") add = [(''.join(random.sample(i[:-1], len(i)-1)))+"\n" for i in add] return "".join([(names[i] + add[i]) for i in range(len(names))])
def addInput(self, dataunit, data): """ Adds an input for the color merging filter """ settings = dataunit.getSettings() if not settings.get("PreviewChannel"): Logging.info("Not including ", dataunit, "in merging", kw="processing") return Module.addInput(self, dataunit, data) self.n += 1 ctf = settings.get("ColorTransferFunction") # Logging.info("ctf=",ctf,kw="processing") self.ctfs.append(ctf) self.alphaTF = settings.get("AlphaTransferFunction") self.alphaMode = settings.get("AlphaMode") # print "n=",self.n,"self.settings=",self.settings # itf=self.settings.getCounted("IntensityTransferFunction",self.n) itf = settings.get("IntensityTransferFunction") if not itf: Logging.info("Didn't get iTF", kw="processing") self.intensityTransferFunctions.append(itf)
def setThresholds(self, ch1lower, ch1upper, ch2lower, ch2upper): """ Set the thresholds this scatteplot is set to """ Logging.info("\nScatterplot thresholds set to (%d-%d) (%d-%d)"%(ch1lower,ch1upper,ch2lower,ch2upper)) self.lower1, self.upper1, self.lower2, self.upper2 = ch1lower, ch1upper, ch2lower, ch2upper self.paintPreview()
def updateSettings(self): """ Sets the settings of this object based on the datasource """ if not self.settings: Logging.info("No settings present, won't update", kw = "dataunit") return if not (self.settings and self.settings.get("ColorTransferFunction")): ctf = self.getColorTransferFunction() # Watch out not to overwrite the palette #self.ctf = self.settings.get("ColorTransferFunction") #ctf = self.ctf self.settings.set("ColorTransferFunction", ctf) #if ctf and self.settings.get("Type") == "ColorMergingSettings": # self.settings.set("MergingColorTransferFunction", ctf) if self.dataSource: self.settings.set("VoxelSize", self.dataSource.getVoxelSize()) self.settings.set("Spacing", self.dataSource.getSpacing()) self.settings.set("Dimensions", self.dataSource.getDimensions()) self.settings.set("BitDepth", self.dataSource.getBitDepth()) self.settings.set("EmissionWavelength", self.dataSource.getEmissionWavelength()) self.settings.set("ExcitationWavelength", self.dataSource.getExcitationWavelength()) self.settings.set("NumericalAperture", self.dataSource.getNumericalAperture()) self.settings.set("TimeStamps", self.dataSource.getTimeStamps()) self.settings.set("AbsoluteTimeStamps", self.dataSource.getAbsoluteTimeStamps())
def __init__(self, filename = ""): """ Constructor of MRCDataSource """ DataSource.__init__(self) self.filename = filename self.setPath(filename) self.bitDepth = 0 self.spacing = None self.voxelSize = None # Create vtkMRCReader for reading MRC files self.reader = vtkbxd.vtkMRCReader() self.reader.AddObserver('ProgressEvent', lib.messenger.send) lib.messenger.connect(self.reader, 'ProgressEvent', self.updateProgress) if self.filename: self.reader.SetFileName(self.convertFileName(self.filename)) if self.reader.OpenFile(): if self.reader.ReadHeader(): self.setOriginalScalarRange() else: Logging.error("Failed to read the header of the MRC file correctly", "Error in MRCDataSource.py in __init.py__, failed to read the header of the MRC file: %s" %(self.filename)) return else: Logging.error("Failed to open file", "Error in MRCDataSource.py in __init__, failed to open file: %s" %(self.filename)) return
def updatePreview(self, renew = 1): """ Update the preview @param renew A boolean flag indicating whether the method should recalculate the images """ if not self.enabled: print "\n\n\nPREVIEWFRAME NOT ENABLED, WON'T RENDER" if scripting.inIO: return if self.renewNext: renew = 1 self.renewNext = 0 if not self.dataUnit: self.paintPreview() return if not self.enabled: Logging.info("Preview not enabled, won't render", kw = "preview") return self.updateColor() if not self.running: renew = 1 self.running = 1 if self.dataUnit.isProcessed(): try: z = self.z self.rawImages = [] for source in self.dataUnit.getSourceDataUnits(): self.rawImages.append(source.getTimepoint(self.timePoint)) preview = self.dataUnit.doPreview(z, renew, self.timePoint) #Logging.info("Got preview",preview.GetDimensions(),kw="preview") except Logging.GUIError, ex: ex.show() return
def setZoomFactor(self, newFactor): """ Sets the factor by which the image should be zoomed """ self.zoomToFitFlag = False Logging.info("Setting zoom factor to ", newFactor, kw = "preview") x, y = [a*newFactor for a in (self.dataDimX, self.dataDimY)] if scripting.resampleToFit: optimize.set_target_size(x, y) newFactor = 1 px, py = self.paintSize x = max(px, x) y = max(py, y) Logging.info("New dims for buffer=",x,y,kw="preview") self.buffer = wx.EmptyBitmap(x, y) self.setScrollbars(x, y) if newFactor < self.zoomFactor: # black the preview slice = self.slice self.slice = None self.paintPreview() self.slice = slice self.zoomFactor = newFactor scripting.zoomFactor = newFactor self.updateAnnotations()
def execute(self, inputs, update = 0, last = 0): """ Execute the filter with given inputs and return the output """ if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs): return None images = [self.getInput(x) for x in range(1,3)] self.eventDesc="Calculating colocalization thresholds" self.colocAutoThreshold.RemoveAllInputs() images[0].SetUpdateExtent(images[0].GetWholeExtent()) images[1].SetUpdateExtent(images[1].GetWholeExtent()) images[0].Update() images[1].Update() maxval = 2**max([self.getInputDataUnit(x).getBitDepth() for x in range(1,3)]) - 1 Logging.info("Maximum value = %d"%maxval, kw="processing") self.colocAutoThreshold.AddInput(images[0]) self.colocAutoThreshold.AddInput(images[1]) self.colocAutoThreshold.SetUpperThresholdCh1(maxval) self.colocAutoThreshold.SetUpperThresholdCh2(maxval) self.colocAutoThreshold.Update() slope = self.colocAutoThreshold.GetSlope() intercept = self.colocAutoThreshold.GetIntercept() ch1th = self.colocAutoThreshold.GetCh1ThresholdMax() ch2th = self.colocAutoThreshold.GetCh2ThresholdMax() self.setResultVariable("Slope", slope) self.setResultVariable("Intercept", intercept) self.setResultVariable("Ch1ThresholdMax", int(ch1th)) self.setResultVariable("Ch2ThresholdMax", int(ch2th)) Logging.info("Auto threshold ch1 = %d, ch2 = %d"%(self.getResultVariable("Ch1ThresholdMax"),self.getResultVariable("Ch2ThresholdMax"))) return self.getInput(1)
def setDataUnit(self, dataUnit, selectedItem = -1): """ Set the dataunit used for preview. Should be a combined data unit, the source units of which we can get and read as ImageData """ Logging.info("Setting dataunit of PreviewFrame to %s"%str(dataUnit), kw="preview") if not dataUnit: self.dataUnit = None self.z = 0 self.slice = None self.updatePreview() self.Refresh() return self.dataUnit = dataUnit self.settings = dataUnit.getSettings() self.updateColor() InteractivePanel.setDataUnit(self, self.dataUnit) try: count = dataUnit.getNumberOfTimepoints() x, y, z = dataUnit.getDimensions() except Logging.GUIError, ex: ex.show() return
def setToRelativeSize(self, size): """ Set duration of all items in this track """ n = len(self.items) total = self.splineEditor.getSplineLength(0, n - 1) if not n: return tot = 0 last = 0 Logging.info("total size=", total, " (pixels=", size, ")", kw = "animator") for i in self.items: if i.isStopped(): i.setWidth(30) continue n = self.getSplineLength(i.getItemNumber()) if n: percent = float(n) / total Logging.info("item ", i.getItemNumber(), "is ", n, "which is ", percent, "percent", kw = "animator") i.setWidth(size * percent) tot += size * percent last = i else: i.setWidth(8) self.updateLayout()
def activate(self, sidebarwin): """ Set the mode of visualization """ self.sidebarWin = sidebarwin Logging.info("Disabling tasks in menu", kw = "visualizer") self.menuManager.mainToolbar.EnableTool(MenuManager.ID_ADJUST, 0) self.menuManager.mainToolbar.EnableTool(MenuManager.ID_RESTORE, 0) self.menuManager.mainToolbar.EnableTool(MenuManager.ID_COLOCALIZATION, 0) self.menuManager.mainToolbar.EnableTool(MenuManager.ID_COLORMERGING, 0) self.visualizer.sliderPanel.Show(0) self.origSliderWinSize = self.visualizer.sliderWin.GetSize() self.visualizer.sliderWin.SetDefaultSize((-1, 64)) if not self.urmaswin: self.urmaswin = GUI.Urmas.UrmasWindow.UrmasWindow(self.parent, \ self.visualizer.menuManager, \ self.visualizer.mainwin.taskWin, \ self.visualizer) else: print "Restoring",self.urmaswin self.urmaswin.Show(1) self.parent.Show(1) self.urmaswin.enableRendering(1) self.urmaswin.controlpanel.Show(1) self.urmaswin.createMenu(self.visualizer.menuManager) wx.CallAfter(self.urmaswin.updateRenderWindow) return self.urmaswin
def doOperation(self, preview = 0): """ Processes the dataset in specified ways """ t1 = time.time() # Map scalars with intensity transfer list n = 0 if len(self.images) > 1: settings = self.dataunits[0].getSettings() n = settings.get("PreviewedDataset") Logging.info("More than one source dataset for data processing, using %dth" % n, kw = "processing") mapdata = self.images[n] mapIntensities = vtkbxd.vtkImageMapToIntensities() #mapIntensities.GetOutput().ReleaseDataFlagOn() mapIntensities.AddObserver("ProgressEvent", lib.messenger.send) lib.messenger.connect(mapIntensities, "ProgressEvent", self.updateProgress) mapIntensities.SetIntensityTransferFunction(self.intensityTransferFunctions[n]) mapIntensities.SetInput(mapdata) #data = self.getLimitedOutput(mapIntensities) data = mapIntensities.GetOutput() t2 = time.time() #Logging.info("Processing took %.4f seconds"%(t2-t1)) #data.ReleaseDataFlagOff() return data
def getTimepointSlicesAt(self, slice): """ Sets the slice to show """ self.slice = slice # if we're showing each slice of one timepoint # instead of one slice of each timepoint, call the # appropriate function self.slices = [] if not self.showTimepoints: return self.setTimepoint(self.timepoint) count = self.dataUnit.getNumberOfTimepoints() for tp in range(0, count): if self.dataUnit.isProcessed(): image = self.dataUnit.doPreview(self.slice, 1, tp) image.Update() self.ctf = self.dataUnit.getSourceDataUnits()[0].getColorTransferFunction() Logging.info("Using ", image, "for gallery", kw = "preview") else: image = self.dataUnit.getTimepoint(tp) x, y, z = self.dataUnit.getDimensions() image = optimize.optimize(image, updateExtent = (0, x - 1, 0, y - 1, self.slice, self.slice)) self.ctf = self.dataUnit.getColorTransferFunction() image = lib.ImageOperations.getSlice(image, self.slice) image.Update() tp = vtk.vtkImageData() tp.DeepCopy(image) self.slices.append(tp) self.calculateBuffer() self.updatePreview()
def onDown(self, event): """ Event handler for when the mouse is pressed down over this item. Will store the position in order to enable dragging. """ x, y = event.GetPosition() ex, ey = event.GetPosition() #print "onDow()",x,y self.dragMode = 0 w, h = self.GetSize() posx, posy = self.GetPosition() x -= posx Logging.info("Item number #%d selected" % self.itemnum, kw = "animator") if self.itemnum == 0 and x < DRAG_OFFSET: # Drag mode where first item is dragged, this affects the # empty space at the front self.dragMode = 2 elif x < DRAG_OFFSET: # drag mode where an item is dragged from the front self.dragMode = 3 self.beginX = ex elif abs(x - w) < DRAG_OFFSET: # drag mode where an item is dragged from behind self.dragMode = 1 self.beginX = ex return
def __del__(self): self._loop.assert_thread() Logging.info('TcpServer::__del__ [%s]' % self._name) for it in self._connections: it.get_loop().run_in_loop(it.connection_destroyed) pass
def getDataSet(self, i, raw = 0): """ Returns the timepoint at the specified index Parameters: i The timepoint to retrieve raw A flag indicating that the data is not to be processed in any way """ # No timepoint can be returned, if this LsmDataSource instance does not # know what channel it is supposed to handle within the lsm-file. self.setCurrentTimepoint(i) if self.channelNum == -1: Logging.error("No channel number specified", "LSM Data Source got a request for dataset from timepoint " "%d, but no channel number has been specified" % (i)) return None self.timepoint = i self.reader.SetUpdateChannel(self.channelNum) self.reader.SetUpdateTimePoint(i) data = self.reader.GetOutput() if raw: return data if self.resampling or self.mask: data = self.getResampledData(data, i) if self.explicitScale or (data.GetScalarType() != 3 and not raw): data = self.getIntensityScaledData(data) return data
def remove_connection_inloop(self, conn): self._loop.assert_thread() Logging.info('TcpServer::remove_connection [%s] - [%s]' % (self._name, conn.name())) del self._connections[conn.name()] loop = conn.get_loop() loop.run_in_loop(conn.connection_destroyed)
def restoreFromCache(self, cachedSettings = None): """ Restore settings for the dataunit and source dataunits from a cache entry """ # Load the cached settings if not cachedSettings: if self.cacheKey: cachedSettings, cacheParser = scripting.getSettingsFromCache(self.cacheKey) if not cachedSettings: Logging.info("No settings found in cache", kw = "caching") return Logging.info("Restoring settings with key %s from cache" % (str(self.cacheKey)), kw = "caching") combined = cachedSettings[0] self.dataUnit.setSettings(combined) sources = self.dataUnit.getSourceDataUnits() for i, setting in enumerate(cachedSettings[1:]): #print "Setting settings of source %d"%i #DataUnitSetting.DataUnitSettings.initialize(setting,sources[i],len(sources),sources[i].getNumberOfTimepoints()) sources[i].setSettings(setting) #tf=setting.get("IntensityTransferFunction") #print setting,tf #print "\n\nSetting itf ",i,"= itf with 0=",tf.GetValue(0),"and 255=",tf.GetValue(255) self.settings = sources[self.settingsIndex].getSettings() self.cacheParser = cacheParser self.updateSettings(force = True)
def getSpacing(self): if not self.spacing: a, b, c = self.reader.GetVoxelSizes() Logging.info("Voxel sizes = ", a, b, c, kw = "lsmreader") self.spacing = [1, b / a, c / a] return self.spacing
def start(self): self.cleanWildDhcpds() # XXX/jshilkaitis: WARNING -- lame shitty hack that will break if # VMware fixes vmnet-dhcpd. Today, vmnet-dhcpd assumes that the # last character of the interface passed to it is the last # character of the interface's associated device. Thus, for # vmlocal, vmnet-dhcpd tries to attach to /dev/vmnetl (that's a # lowercase L). It turns out we can hack around this by # appending a 0 to the interface name. vmlocal0 causes # vmnet-dhcpd to attach to /dev/vmnet0, ESXi's vmk0 properly gets # a DHCP address from dhcpd and everything is happy. This is # total crap and is brittle since VMware fixing dhcpd will # almost certainly break us, but it works and, IMO, is better than # slapping a random 0 after vmlocal that the user can see. # I apologize to future generations for my expediency. # # P.S. I think my hack-guilt can be measured by the length # of the XXX comment preceding the hack. XXXlameHackSuffixForVMLocalInterface = "0" commandList = [self.vmnetDhcpdCmd, "-d", "-cf", self.dhcpdConf, "-lf", self.dhcpdLease, "-pf", self.dhcpdPidFileName, "%s%s" % (self.vmnetReal, XXXlameHackSuffixForVMLocalInterface)] Logging.log(Logging.LOG_INFO, "Starting dhcpd on %s" % ( self.interfaceName)) self.pid = self.launchProcess(commandList)
def setCameraParameters(self, cam, renderer, point, focal): """ Sets the camera parameters """ if point: cam.SetPosition(point) cam.SetFocalPoint(focal) #viewUp,focalPoint=orientation #cam.SetFocalPoint(focalPoint) # if the track wishes to maintain up direction #cam.SetViewUp(viewUp) if self.currTrack and self.currTrack.maintainUpDirection: Logging.info("Orthogonalize view up", kw = "animator") cam.SetViewUp((0, 0, 1)) cam.ComputeViewPlaneNormal() cam.OrthogonalizeViewUp() elif self.currTrack: # if there's movement in z direction if self.lastpoint and abs(self.lastpoint[2] - point[2]) > 2: Logging.info("Orthogonalize because oldz!=newz", kw = "animator") cam.OrthogonalizeViewUp() self.lastpoint = point renderer.ResetCameraClippingRange()
def calculateBuffer(self): """ Calculate the drawing buffer required """ if not self.imagedata: return x, y, z = self.imagedata.GetDimensions() x, y, z = self.dataUnit.getDimensions() if not self.sizeChanged and (x, y, z) == self.oldBufferDims and self.oldBufferMaxXY == (self.maxClientSizeX, self.maxClientSizeY): return self.oldBufferDims = (x, y, z) self.oldBufferMaxXY = (self.maxClientSizeX, self.maxClientSizeY) x, y, z = [int(i * self.zoomFactor) for i in (x, y, z)] Logging.info("scaled size =", x, y, z, kw = "visualizer") x += z * self.zoomZ * self.zspacing + 2 * self.xmargin y += z * self.zoomZ * self.zspacing+ 2 * self.ymargin x = int(max(x, self.maxClientSizeX)) y = int(max(y, self.maxClientSizeY)) self.paintSize = (x, y) if self.buffer.GetWidth() != x or self.buffer.GetHeight() != y: self.buffer = wx.EmptyBitmap(x, y) Logging.info("Paint size=", self.paintSize, kw = "preview") self.setScrollbars(x, y)
import datetime from telepot.namedtuple import InlineKeyboardButton, InlineKeyboardMarkup import Logging import BotWrappers import Category import Pages from CreatorID import creator_hash_id #============================================================================== # logging #============================================================================== log = Logging.get_logger(__name__, "DEBUG") #============================================================================== # Constants #============================================================================== MAX_UPLOADS = 5 cat_price = 10 #============================================================================== # Handle #============================================================================== def handle_content(content, bot, user, catdb, mediavotedb): log.debug("content handler")
def execute(self, inputs, update=0, last=0): """ Execute the filter with given inputs and return the output """ if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs): return None images = [self.getInput(x) for x in range(1, 5)] self.eventDesc = "Calculating colocalization" self.colocFilter.RemoveAllInputs() self.colocAutoThreshold.RemoveAllInputs() depth = 8 if self.parameters["OneBit"]: depth = 1 self.colocFilter.SetOutputDepth(depth) ch1thresmax = self.getPrecedingResultVariable("Ch1ThresholdMax") ch2thresmax = self.getPrecedingResultVariable("Ch2ThresholdMax") Logging.info("result vars ch1, ch2=", ch1thresmax, ch2thresmax) if ch1thresmax != None and ch2thresmax != None and self.prevFilter.getName( ) == "Calculate thresholds for colocalization": slope = self.getPrecedingResultVariable("Slope") intercept = self.getPrecedingResultVariable("Intercept") print "Got slope, intercept", slope, intercept lib.messenger.send(self, "set_slope_intercept", slope, intercept) self.set("LowerThresholdCh1", ch1thresmax) self.set("LowerThresholdCh2", ch2thresmax) lib.messenger.send(self, "update_LowerThresholdCh1") lib.messenger.send(self, "update_LowerThresholdCh2") lib.messenger.send(self, "update_UpperThresholdCh1") lib.messenger.send(self, "update_UpperThresholdCh2") self.colocAutoThreshold.AddInput(images[0]) self.colocAutoThreshold.AddInput(images[1]) self.colocAutoThreshold.AddInput(images[2]) self.colocAutoThreshold.AddInput(images[3]) self.colocAutoThreshold.SetUseMask(1) # When we set the lower thresholds, then the given thresholds will be used #self.colocAutoThreshold.SetLowerThresholdCh1(self.parameters["LowerThresholdCh1"]) #self.colocAutoThreshold.SetLowerThresholdCh2(self.parameters["LowerThresholdCh2"]) #self.colocAutoThreshold.SetUpperThresholdCh1(self.parameters["UpperThresholdCh1"]) #self.colocAutoThreshold.SetUpperThresholdCh2(self.parameters["UpperThresholdCh2"]) #if self.oldThresholds != (ch1Lower, ch1Upper, ch2Lower, ch2Upper): # Logging.info("Calculating statistics") self.colocAutoThreshold.Update() for variable in self.resultVariables.keys(): if hasattr(self.colocAutoThreshold, "Get%s" % variable): self.setResultVariable( variable, eval("self.colocAutoThreshold.Get%s()" % variable)) data = images[2] if self.parameters["Costes"] or self.parameters[ "Fay"] or self.parameters["Steensel"]: self.calculatePValue(images) if self.listctrl: self.listctrl.updateListCtrl(self.getResultVariableDict()) return data
Created on Sun Jun 24 11:54:27 2018 @author: Mauro """ import os import Databases import Logging import MediaVote #============================================================================== # Logging #============================================================================== log = Logging.get_logger(__name__, "WARNING") #============================================================================== # Media vote database #============================================================================== class MediaVoteDB: def __init__(self): self.folder = "./databases/media_db" if not os.path.isdir(self.folder): os.mkdir(self.folder) self.database = Databases.Database(self.folder, "media_") self.database.loadDB() self.database.update_uid() log.info("database loaded")
import Config import Logging from Server import ServerClass from Openvswitch import Openvswitch logger = Logging.get_logger('hicloud.vmc.Plugin') __obj_vmi = None __obj_ovs = None def init(): ###(daemon_config): logger.info("hicloud.vmc.plugins.init is running") config = Config.load(str('/vmc160/hicloud/vmc.yaml')) ###general_config = Config.load(project_path('/etc/hicloud/general.yaml')) ###config["portal_url"] = general_config["portal_url"] # this should dispatch 'hicloud.vmc.*' to individual log file Logging.get_logger('hicloud.vmc') ###filename=config['log_file']) logger.debug('initializing %s' % __name__) global __obj_vmi if __obj_vmi: logger.error('reinitialized of vm is not supported') return __obj_vmi = ServerClass() global __obj_ovs if __obj_ovs: logger.error('reinitialized of ovs is not supported') return
#!/usr/bin/env python import asyncore, socket, signal, os, importlib from os import path MyName = path.splitext(path.basename(__file__))[0] from Config import Config MyConf = Config(MyName).conf import Logging logger = Logging.get_logger(dict(MyConf.items('Logging'))) logger.info('created logger') from RedisBackend import RedisBackend MyDB = RedisBackend(host = MyConf.get('RedisBackend', 'redis_host'), db = MyConf.get('RedisBackend', 'redis_db')) mod = importlib.import_module(MyConf.get('PostfixPolicyServer', 'service_configuration'), MyConf) PolicyServers = mod.PolicyServers(MyDB) def shutdown_handler(signum, frame): global PolicyServers for PolicyServer in PolicyServers: PolicyServer.cleanup() del PolicyServer raise asyncore.ExitNow() if __name__ == "__main__": logger.info('starting up') signal.signal(signal.SIGHUP, shutdown_handler)
def getModules(self, moduleSubDir, globExtension=None, callback=None, moduleType="Module", classEndsWith=""): """ Dynamically loads classes in a directory and returns a dictionary that contains information about them. The returned directory will contain entries like: moddict["BXCDataSource"] -> (moduleClass, settingClass, loadedModule) The method adds a relative path with the dir that contains the modules to load, to sys.path. It then uses __import__ to load them with their basenames. This means that the dynamic loading relies on the current working directory being set to the "main source dir". """ if not globExtension: globExtension = self.moduleTypes.get(moduleSubDir, "*.py") globPath, pathForSysPath = self._createGlobPathAndSysPath( moduleSubDir, globExtension) modulePathList = glob.glob(globPath) modulePathList = self._removeIgnoredModules(modulePathList) Logging.info("Modules from path %s are %s" % (globPath, str(modulePathList)), kw="modules") sys.path.insert(0, pathForSysPath) # Return cached result, if it exists if moduleSubDir in self.mcache: return self.mcache[moduleSubDir] moddict = {} for modulePathWithExtension in modulePathList: if modulePathWithExtension.endswith(".pyc"): continue moduleName = self._createModuleNameToLoad(modulePathWithExtension) try: loadedModule = __import__(moduleName, globals(), locals(), []) except ImportError: traceback.print_exc() Logging.info("Failed to load module %s" % moduleName, kw="modules") continue moduleNameInDictionary = None # Try to set the moduleName first from getName, then getShortDesc, finally setting it to mod # if these don't exist try: moduleNameInDictionary = loadedModule.getName() except AttributeError: try: moduleNameInDictionary = loadedModule.getShortDesc() except AttributeError: moduleNameInDictionary = moduleName if callback: callback("Loading %s %s..." % (moduleType, moduleNameInDictionary)) if hasattr(loadedModule, "getClass"): moduleClass = loadedModule.getClass() else: try: moduleClass = loadedModule.__dict__["%s%s" % (moduleName, classEndsWith)] except: continue settingClass = None if hasattr(loadedModule, "getConfigPanel"): settingClass = loadedModule.getConfigPanel() moddict[moduleNameInDictionary] = (moduleClass, settingClass, loadedModule) self.mcache[moduleSubDir] = moddict return moddict
def createFolder(directory): #Creates a folder if it does not exist if not os.path.exists(directory): log.file("Path to", directory, "does not exist! Creating folders") os.makedirs(directory)
def main(): Url = const.url password = const.password username = const.username filename = os.path.abspath(__file__) log = Logging.getLogger(filename, 'Resumability_DR') try: log.info("Test Resumability_DR begins") GUI = WebUtils() GUI.Enable_Flash() GUI.login_EC(Url, username, password) try: GUI.Create_DR(name=const.DRName, bkp_ip=const.BKPIP) except Exception as j: print "Error: Exception occure while creating DR", str(j) sys.exit(1) time.sleep(20) print "Waiting till schedule duration for DR to begin" log.info("Waiting till schedule duration for DR to begin") time.sleep(90) user = const.node_username pwd = const.node_password host1 = const.Node1_IP vsm_ip = const.VSM1Ip SSH = SSHConnection() output = SSH.cbdpctl_status(user, pwd, host1, vsm_ip) print output if output[0] == "transferring": try: GUI.DR_Enable_Disable("Disable") log.info("DR transfer disabled successfully") time.sleep(30) t = SSHConnection() t.createSSHConnection(host=host1, username=user, password=pwd) time.sleep(5) out = t.exec_cmd("jls") out1 = out.split() if vsm_ip in out1: jls = out1.index(vsm_ip) jls_id = int(out1[jls - 1]) time.sleep(20) output = t.exec_cmd("jexec %s cbdpctl -c list" % jls_id) if not output: log.info( "Pass: Verified that DR transfer did not continued after disabling transfer" ) else: log.error("Fail: DR transfer is still in-progress") t.close() except Exception as z: log.error("Error: Exception occured while disabling transfer" ), str(z) raise try: time.sleep(10) log.info("Enabling DR transfer to verify resumability") GUI.DR_Enable_Disable("Enable") log.info("DR transfer enabled successfully") except Exception as f: log.error( "Error: Exception occured while enabling transfer"), str(f) raise time.sleep(90) out1 = SSH.cbdpctl_status(user, pwd, host1, vsm_ip) print out1 bytestransfered = out1[1] time.sleep(50) out2 = SSH.cbdpctl_status(user, pwd, host1, vsm_ip) print out2 cstatus = out2[0] cbytestransfered = out2[1] if cstatus == "transferring" and int(cbytestransfered) > int( bytestransfered): log.info("Pass: DR transfer resumed successfully") elif cstatus == "uptodate": log.info("Pass: DR resumed and completed successfully") else: log.error("Fail: DR resume failed") time.sleep(2) GUI.close_browser() elif output[1] == "uptodate": log.info("Base snapshot transfered") else: log.info("Failed to get DR status, please recheck") except Exception as e: log.error("Exception Occured: While logging into EC"), str(e) sys.exit(1)
def handle_restart(command): Events.NonBlockingRestartLock.acquire(blocking = False) log.command("SIGNALLING SERVER RESTART")
def getReadersFromFilenames(self): """ create the reader list from a given set of file names and parameters """ for i in self.readers: del i self.readers = [] if not self.filenames: raise Logging.GUIError("No files could be found", \ "For some reason, no files were listed to be imported.") files = self.filenames print "Determining readers from ", self.filenames isRGB = 1 self.ext = files[0].split(".")[-1].lower() dim = self.dimMapping[self.ext] # Initially flip the image if it's tiff, png or jpg. # In setVerticalFlip we negate the setting to have it set correctly. if self.ext.lower() in ["png", "jpg", "jpeg"]: self.flipVertically = True if self.ext in ["tif", "tiff"]: reader = vtkbxd.vtkExtTIFFReader() reader.SetFileName(files[0]) reader.UpdateInformation() if reader.GetNumberOfScalarComponents() >= 3: print "MODE IS RGB, IS AN RGB IMAGE" else: print "MODE ISN'T RGB, THEREFORE NOT RGB" isRGB = 0 rdr = self.getReaderByExtension(self.ext, isRGB) rdr.SetFileName(files[0]) if rdr.GetNumberOfSubFiles() > 1: dim = 3 self.isRGB = isRGB self.is3D = (dim == 3) dirName = os.path.dirname(files[0]) print "THERE ARE", self.slicesPerTimepoint, "SLICES PER TIMEPOINT" self.ext = files[0].split(".")[-1].lower() if dim == 3: totalFiles = len(files) for i, file in enumerate(files): rdr = self.getReaderByExtension(self.ext, isRGB) rdr.SetFileName(file) self.readers.append(rdr) return totalFiles = len(files) / self.slicesPerTimepoint imgAmnt = len(files) if totalFiles == 1: rdr = self.getReaderByExtension(self.ext, isRGB) arr = vtk.vtkStringArray() for fileName in files: arr.InsertNextValue(os.path.join(dirName, fileName)) rdr.SetFileNames(arr) self.readers.append(rdr) return if imgAmnt > 1: # If the pattern doesn't have %, then we just use # the given filenames and allocate them to timepoints # using slicesPerTimepoint slices per timepoint ntps = len(files) / self.slicesPerTimepoint filelst = files[:] # dirn #TODO: what was this? for tp in range(0, ntps): rdr = self.getReaderByExtension(self.ext, isRGB) arr = vtk.vtkStringArray() for i in range(0, self.slicesPerTimepoint): arr.InsertNextValue(filelst[0]) filelst = filelst[1:] rdr.SetFileNames(arr) rdr.SetDataExtent(0, self.x - 1, 0, self.y - 1, 0, self.slicesPerTimepoint - 1) rdr.SetDataSpacing(self.spacing) rdr.SetDataOrigin(0, 0, 0) self.readers.append(rdr) return elif imgAmnt == 1: # If only one file rdr = self.getReaderByExtension(self.ext, isRGB) rdr.SetDataExtent(0, self.x - 1, 0, self.y - 1, 0, self.slicesPerTimepoint - 1) rdr.SetDataSpacing(self.spacing) rdr.SetDataOrigin(0, 0, 0) rdr.SetFileName(files[0]) Logging.info("Reader = ", rdr, kw="io") self.readers.append(rdr)
def run(self): url = self.url filename = os.path.basename(str(url)) filename, ext = os.path.splitext(filename) if len(filename) > kMaxFilenameLenPrint: filename = filename[:kMaxFilenameLenPrint] + "..." filename = filename + ext print_prefix = "wget (%s)" % filename Logging.log("%s: start download %s" % (print_prefix, url)) args = [ "wget", "--continue", "--no-check-certificate", # SSL errors ignored, like in list-dir "--force-directories", "--directory-prefix", "downloads/", "--progress=dot:mega", # see also the progress handling below "--tries=%i" % kWgetNumTries, # note that we also do our own retry-handling "--timeout=%i" % kWgetTimeout, str(url) ] Logging.log(" ".join(map(repr, args))) from subprocess import Popen, PIPE, STDOUT env = os.environ.copy() env["LANG"] = env["LC"] = env["LC_ALL"] = "en_US.UTF-8" devnull = open(os.devnull, "rb") p = Popen(args, stdin=devnull, stdout=PIPE, stderr=STDOUT, bufsize=0, env=env) progress_line_idx = 0 while p.returncode is None: line = p.stdout.readline() self.last_output_time = time.time() line = convert_to_unicode(line) line = line.rstrip() if not line: pass # Cleanup output a bit. elif _wget_is_progress_line(line): if progress_line_idx % kWgetProgessLineMod == 0: Logging.log("%s progress: %s" % (print_prefix, line)) progress_line_idx += 1 else: Logging.log("%s: %s" % (print_prefix, line)) # The only good way to check for certain errors. if line.startswith("No such file "): p.kill() raise DownloadFatalError("error: " + line) if line.startswith("No such directory "): p.kill() raise DownloadFatalError("error: " + line) if "404 Not Found" in line: p.kill() raise DownloadFatalError("error: " + line) p.poll() if p.returncode != 0: raise DownloadTemporaryError("return code %i" % p.returncode) Logging.log("%s done." % print_prefix)
def handle_shutdown(command): Events.NonBlockingShutdownLock.acquire(blocking = False) log.command("SIGNALLING SERVER SHUTDOWN")
# -*- coding: utf-8 -*- """ Created on Sat Jul 21 12:15:08 2018 @author: Mauro """ import threading import time import Logging log = Logging.get_logger(__name__, "INFO") class Routine: def __init__(self): pass def routine_func(self, usersdb, mediavotedb, catdb): log.info("routine function started") log.debug("Updating karma...") for user in usersdb.getUsersList(): user.calculateKarma(mediavotedb) usersdb.setUser(user) usersdb.update() log.debug("Updating categories db...")
def do_human_affection(self): log.command("Searching for human affection") self.recipientObj = self.group.users.getUser(self.leftString + " " + self.rightString)
def setTimepoint(self, tp, update=1): """ Set the timepoint """ start = time.time() recalculate = False # This doesn't work when adjust is open so now just recalculate every tim recalculate = True #if tp != self.timepoint or self.dataUnitChanged: #recalculate = True self.timepoint = tp if not scripting.renderingEnabled: return if recalculate or not self.imagedata: if self.dataUnit.isProcessed(): image = self.dataUnit.doPreview( scripting.WHOLE_DATASET_NO_ALPHA, 1, self.timepoint) image.ReleaseDataFlagOff() else: image = self.dataUnit.getTimepoint(tp) image.UpdateInformation() image.SetUpdateExtent(image.GetWholeExtent()) self.ctf = self.dataUnit.getColorTransferFunction() image.Update() self.cachedImage = image self.imagedata = image #lib.ImageOperations.imageDataTo3Component(self.imagedata, self.ctf) self.zspacing = image.GetSpacing()[2] self.dataUnitChanged = False if self.fitLater: self.fitLater = 0 self.zoomToFit() return self.dims = self.imagedata.GetDimensions() self.slices = [] # obtain the slices z = self.z / self.zoomZ if self.zoomFactor != 1: if self.interpolation: imgslice = self.zoomImageWithInterpolation(self.imagedata, self.zoomFactor, self.interpolation, z, ctf=self.ctf) else: img = lib.ImageOperations.scaleImage(self.imagedata, self.zoomFactor, z) imgslice = lib.ImageOperations.vtkImageDataToWxImage( img, ctf=self.ctf) else: imgslice = lib.ImageOperations.vtkImageDataToWxImage( self.imagedata, z, ctf=self.ctf) self.slices.append(imgslice) Logging.info("\n\nzspacing = %f\n" % self.zspacing, kw="preview") start = time.time() imgslice = self.getPlane(self.imagedata, "zy", self.x, self.y, int(z)) #print "GET ZY PLANE TOOK:", time.time()-start, "\n\n\n" w, h = imgslice.GetDimensions()[0:2] interpolation = self.interpolation if self.interpolation == -1: interpolation = self.getInterpolationForSize(w, h, self.zoomFactor) if not interpolation: interpolation = 1 if self.zoomFactor != 1 or self.zspacing != 1: imgslice = lib.ImageOperations.scaleImage( imgslice, self.zoomFactor, interpolation=interpolation, yfactor=1, xfactor=self.zspacing) imgslice = lib.ImageOperations.vtkImageDataToWxImage(imgslice, ctf=self.ctf) self.slices.append(imgslice) start = time.time() imgslice = self.getPlane(self.imagedata, "xz", self.x, self.y, z) #print "GET XZ PLANE TOOK:", time.time()-start, "\n\n\n" if self.zoomFactor != 1 or self.zoomZ != 1 or self.zspacing != 1: imgslice = lib.ImageOperations.scaleImage( imgslice, self.zoomFactor, interpolation=interpolation, yfactor=self.zspacing, xfactor=1) imgslice = lib.ImageOperations.vtkImageDataToWxImage(imgslice, ctf=self.ctf) self.slices.append(imgslice) self.calculateBuffer() if update: self.updatePreview()
# Load randomly generated secret key from file # Reference: http://flask.pocoo.org/snippets/104/ # Run make_secret_key to create a new key and save it in secret_key key_file = configuration.Configuration.SecretKey() app.config['SECRET_KEY'] = open(key_file, 'r').read() # From Flask tutorial # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # Start logging Logging.EnableServerLogging() logger = logging.getLogger("app") # All views must be imported after the app is defined from app.views import page_views from app.views import json_views from Version import GetVersion logger.info( "################################################################################" ) logger.info("Starting AtHomeFRB version %s", GetVersion()) logger.info("Using configuration file %s", configuration.Configuration.get_configuration_file_path())
def filter_move_knowledge(move_history, player_index): return { 'chosenCards': get_cards_chosen_by_players(move_history), 'hand': Logging.names_to_cards(move_history[player_index]['beforeAction']['hand']) }
def loadFromFile(self, filename): """ Loads the specified .bxc-file and imports data from it. Also returns a DataUnit of the type stored in the loaded .bxc-file or None if something goes wrong. The dataunit is returned in a list with one item for interoperability with LSM data source """ if not self.baseFilename: self.baseFilename = filename self.shortname = os.path.basename(filename) dataUnitFormat = self.loadBxdFile(filename) Logging.info("format of unit = ", dataUnitFormat, kw = "datasource") if (not dataUnitFormat) or (not self.parser): Logging.info("No dataUnitFormat or parser: %s and %s"%(dataUnitFormat, self.parser), kw = "datasource") return None # Then, the number of datasets/timepoints that belong to this dataset # series try: count = self.parser.get("ImageData", "numberOfFiles") except ConfigParser.NoOptionError: count = self.parser.get("ImageData", "numberoffiles") Logging.info("format = ", dataUnitFormat, "count = ", count, kw = "datasource") # Then read the .vti-filenames and store them in the dataSets-list: filedir = os.path.dirname(filename) hasPolydata = self.parser.has_section("PolyData") for i in range(int(count)): currentFile = "file_%d"%i filename = self.parser.get("ImageData", currentFile) if hasPolydata: print "GOT polydata" polyFileName = self.parser.get("PolyData", currentFile) self.polyDataFiles.append(polyFileName) reader = vtk.vtkXMLImageDataReader() filepath = os.path.join(filedir, filename) if not reader.CanReadFile(filepath): Logging.error("Cannot read file", "Cannot read source XML Image Data File %s"%filename) return self.dataSets.append(filename) # If everything went well, we create a new DataUnit-instance of the # correct subclass, so that the DataUnit-instance can take over and # resume data processing. First, we return the DataUnit to the caller, # so it can set a reference to it: dataunit = DataUnit() settings = DataUnitSettings() settings.setType("") settings = settings.readFrom(self.parser) self.originalDimensions = eval(settings.get("Dimensions")) self.settings = settings dataunit.setDataSource(self) dataunit.setSettings(settings) data = dataunit.getTimepoint(0) dataunits = [dataunit] if data.GetNumberOfScalarComponents() == 3: for i in range(0, 3) : dataSource = RGBComponentDataSource(self, i) dataunit = DataUnit() dataunit.setDataSource(dataSource) settings = DataUnitSettings() settings = settings.readFrom(self.parser) dataunit.setSettings(settings) dataunits.append(dataunit) return dataunits
'-n', help='DB in CouchDB for output', type=str, required=True) parser.add_argument('--type', '-t', help='event type', type=str, required=True) parser.add_argument('--logfileless', action='store_true', help='this will disable writing out a log file') parser.add_argument('--run', help='run number', type=int, required=True) Logging.addLogLevelOptionToArgs(parser) # adds --log_level args = parser.parse_args() Logging.setupLogging(args.log_level, args.name) log = logging.getLogger('root').getChild(sys.argv[0].split('.')[0]) log.debug('Commandline args: %s', str(args)) Configuration.name = args.name Configuration.run = args.run config = Configuration.CouchConfiguration() db = config.getCurrentDB() # Note the key below: "[document.number_run, document.number_event]" which # is important because CouchDB will sort by this. It's part of our ROOT # file specification that it be sorted like this.
def onLeftDown(self, event): """ Handler for mouse clicks """ # if left mouse key is not down or the key down is related to # interactive panel events if self.action: event.Skip() return if not event.LeftIsDown(): event.Skip() return x, y = event.GetPosition() x -= self.xmargin y -= self.ymargin x, y = self.getScrolledXY(x, y) #x /= float(self.zoomFactor) #y /= float(self.zoomFactor) dims = self.imagedata.GetDimensions() # the yz plane # calculate scaled margins, because the click coordinates are scaled as well sxmargin = self.xmargin / self.zoomFactor symargin = self.ymargin / self.zoomFactor if x >= dims[0] + sxmargin + dims[2] * self.zspacing: x = dims[0] + sxmargin + dims[2] * self.zspacing - 1 if y >= dims[1] + symargin + dims[2] * self.zspacing: y = dims[1] + symargin + dims[2] * self.zspacing - 1 if x > dims[0] + (sxmargin) and y > 0 and y < dims[ 1] and x < dims[0] + sxmargin + dims[2] * self.zspacing: nz = x - dims[0] - sxmargin nz /= self.zspacing ny = y nx = self.x elif x > 0 and x < dims[0] and y > 0 and y < dims[1]: nx = x ny = y nz = self.z # the xz plane elif x > 0 and x < dims[0] and y > dims[1] + symargin and y < dims[ 1] + symargin + dims[2] * self.zspacing: nx = x nz = y - dims[1] - symargin nz /= self.zspacing ny = self.y # the gray area elif x > dims[0] + sxmargin and x < dims[0] + sxmargin + dims[ 2] * self.zspacing and y > dims[1] + symargin and y < dims[ 1] + symargin + dims[2] * self.zspacing: if y > x: nz = y - dims[1] - symargin else: nz = x - dims[0] - sxmargin nx = self.x ny = self.y else: Logging.info("Out of bounds (%d,%d)" % (x, y), kw="preview") return self.drawPos = [math.ceil(a * self.zoomFactor) for a in (nx, ny, nz)] if self.x != nx or self.y != ny or self.z != nz: self.x, self.y, self.z = int(nx), int(ny), int(nz) self.setTimepoint(self.timepoint) else: self.updatePreview() self.noUpdate = 1 lib.messenger.send(None, "zslice_changed", nz) self.noUpdate = 0 ncomps = self.imagedata.GetNumberOfScalarComponents() if ncomps == 1: scalar = self.imagedata.GetScalarComponentAsDouble( self.x, self.y, self.z, 0) rv = -1 gv = -1 bv = -1 alpha = -1 val = [0, 0, 0] self.ctf.GetColor(scalar, val) r, g, b = val else: rv = self.imagedata.GetScalarComponentAsDouble( self.x, self.y, self.z, 0) gv = self.imagedata.GetScalarComponentAsDouble( self.x, self.y, self.z, 1) bv = self.imagedata.GetScalarComponentAsDouble( self.x, self.y, self.z, 2) r, g, b = rv, gv, bv scalar = 0xdeadbeef alpha = -1 if ncomps > 3: alpha = self.imagedata.GetScalarComponentAsDouble( self.x, self.y, self.z, 3) rx, ry, rz = self.x, self.y, self.z lib.messenger.send(None, "get_voxel_at", rx, ry, rz, scalar, rv, gv, bv, r, g, b, alpha, self.ctf)
def showInfo(self, obj=None, evt=None, dataunit=None): """ Sets the infowidget to show info on a given dataunit """ if not dataunit: dims = (0, 0, 0) resampledims = (0, 0, 0) rsVoxelsize = (0, 0, 0) spacing = (0, 0, 0) odims = (0, 0, 0) voxelsize = (0, 0, 0) bitdepth = 8 intlower = 0 intupper = 255 tps = 0 excitation = "n/a" emission = "n/a" else: # Bug-fix (Mac): double click on a multi-channeled dataset's parent in the file tree if isinstance(dataunit, str): return dims = dataunit.getDimensions() odims = (0, 0, 0) resampledims = dataunit.dataSource.getResampleDimensions() if resampledims: odims = dataunit.dataSource.getOriginalDimensions() lib.messenger.send(None, "set_resample_dims", resampledims, odims) else: lib.messenger.send(None, "set_current_dims", dims) spacing = dataunit.getSpacing() voxelsize = dataunit.getVoxelSize() rsVoxelsize = lsize = dataunit.getResampledVoxelSize() bitdepth = dataunit.getBitDepth() em = dataunit.getEmissionWavelength() ex = dataunit.getExcitationWavelength() if not em: emission = "n/a" else: emission = "%d nm" % em if not ex: excitation = "n/a" else: excitation = "%d nm" % ex intlower, intupper = dataunit.getScalarRange() Logging.info("Dataset bit depth =", bitdepth, kw="trivial") unit = dataunit ctf = dataunit.getColorTransferFunction() if dataunit.getBitDepth() == 32: self.colorBtn.setColorTransferFunction(ctf) self.colorBtn.Enable(0) else: self.colorBtn.setColorTransferFunction(ctf) self.colorBtn.Enable(1) self.dataUnit = unit #self.taskName.SetLabel(dataunit.getName()) dunitname = dataunit.getName() # The 0 tells preview to view source dataunit 0 #self.preview.setDataUnit(self.dataUnit,0) tps = dataunit.getNumberOfTimepoints() if not resampledims: voxelX, voxelY, voxelZ = voxelsize ovoxelX, ovoxelY, ovoxelZ = voxelsize xdim, ydim, zdim = dims oxdim, oydim, ozdim = odims else: voxelX, voxelY, voxelZ = rsVoxelsize ovoxelX, ovoxelY, ovoxelZ = voxelsize xdim, ydim, zdim = resampledims oxdim, oydim, ozdim = odims voxelX *= 1000000 voxelY *= 1000000 voxelZ *= 1000000 ovoxelX *= 1000000 ovoxelY *= 1000000 ovoxelZ *= 1000000 spX, spY, spZ = spacing if resampledims: rxdim, rydim, rzdim = resampledims else: rxdim, rydim, rzdim = 0, 0, 0 col = self.GetBackgroundColour() intlower = str(intlower) intupper = str(intupper) bgcol = "#%2x%2x%2x" % (col.Red(), col.Green(), col.Blue()) dict = { "dunitname": dunitname, "smX": smX, "xdim": xdim, "ydim": ydim, "zdim": zdim, "voxelX": voxelX, "voxelY": voxelY, "voxelZ": voxelZ, "rxdim": rxdim, "rydim": rydim, "rzdim": rzdim, "rxdimm": rxdim * voxelX, "rydimm": rydim * voxelY, "rzdimm": rzdim * voxelZ, "spX": spX, "spY": spY, "spZ": spZ, "xdimm": xdim * voxelX, "ydimm": ydim * voxelY, "zdimm": zdim * voxelZ, "oydim": oydim, "oxdim": oxdim, "ozdim": ozdim, "oxdimm": oxdim * ovoxelX, "oydimm": oydim * ovoxelY, "ozdimm": ozdim * ovoxelZ, "bgcolor": bgcol, "fe": "</font > ", "nf": " < font size=\"normal\">", "tps": tps, "bitdepth": bitdepth, "intlower": intlower, "intupper": intupper, "excitation": excitation, "emission": emission } if resampledims and resampledims != (0, 0, 0): self.htmlpage.SetPage(infoStringResample % dict) else: self.htmlpage.SetPage(infoString % dict)
def get_cards_chosen_by_player(player_move_history): return Logging.names_to_cards([player_move_history['beforeAction']['hand'][chosen_card_index] for chosen_card_index in player_move_history['chosenCardIndices']])
def convertVTKtoITK(self, image, cast=None): """ Convert the image data to ITK image """ if "itkImage" in str(image.__class__): return image if not self.itkFlag: lib.messenger.send( None, "show_error", "Non-ITK filter tries to convert to ITK", "A non-ITK filter %s tried to convert data to ITK image data" % self.name) return image extent = image.GetWholeExtent() if extent[5] - extent[4] == 0: dim = 2 else: dim = 3 if cast == types.FloatType: typestr = "itk.VTKImageToImageFilter.IF%d" % dim ImageType = eval(typestr) scalarType = "float" elif not cast: scalarType = image.GetScalarTypeAsString() if scalarType in [ "unsigned int", "unsigned long", "unsigned long long" ]: conv = vtk.vtkImageCast() conv.SetInput(image) typestr = "itk.VTKImageToImageFilter.IUL%d" % dim ImageType = eval(typestr) conv.SetOutputScalarTypeToUnsignedLong() image = conv.GetOutput() elif scalarType == "unsigned short": typestr = "itk.VTKImageToImageFilter.IUS%d" % dim ImageType = eval(typestr) elif scalarType == "float": typestr = "itk.VTKImageToImageFilter.IF%d" % dim ImageType = eval(typestr) else: typestr = "itk.VTKImageToImageFilter.IUC%d" % dim ImageType = eval(typestr) else: typestr = "itk.VTKImageToImageFilter.IUC%d" % dim ImageType = eval(typestr) Logging.info("Scalar type = %s" % scalarType) self.vtkToItk = ImageType.New() #if self.prevFilter and self.prevFilter.getITK(): # return image if cast: icast = vtk.vtkImageCast() if cast == types.FloatType: icast.SetOutputScalarTypeToFloat() icast.SetInput(image) image = icast.GetOutput() self.vtkToItk.SetInput(image) output = self.vtkToItk.GetOutput() output.Update() return output
dqn.processor = train_processor experiment_name = "NoseTip" history_train = dqn.fit(train_env, nb_steps=500, nb_max_episode_steps=100, log_interval=30000, visualize=False, verbose=2) dqn.save_weights(experiment_name, overwrite=True) print("******", train_env.wander) L = Logging() episode_count = L.log_train_history(history_train, experiment_name) test_env = ImageEnvironment(images=test_images, landmarks=test_landmarks, state_size=state_window_size, seed=6, step_size=step_size) test_env.debug = False dqn.load_weights(experiment_name) # Need to update the agent to have the test environment processor dqn.processor = ImageProcessor(test_env) # Test on one of the episodes nb_episodes = 300
import re import datefinder import Logging import logging Logging.configure_logging('/tmp/kafkalog') def extract_patient_info(file_content, classname, patient_name_extraction_model): #print(file_content) # TODO: # Need to initialize this object during application init # Need to change the model path. (We should hard code this or get it from a property file.) if (classname == 'Prior Authorization'): name_regex = [ "(?i)PA for\s(:|\s|)+", "(?i)patient name(:|\s|)+", "Pt(:|\s|)+", "(?i)(\b|^)Patient(:|\s)+", "(?i)(\b|^)Name(:|\s|)+", "(?i)resident(:|\s|)+", "(^)(RE|Re)(:|\s)+" ] elif ('cvs/pharmacy' in str(file_content)): print('Matched cvs pharmacy') name_regex = [ "(?i)(\b|^)Patient(:|\s)+(?i)(\b|^)Name(:|\s|)+", "(?i)(\s|^)Patient(:|\s)+(?i)(\s|^)Name(:|\s|)+", "(?i)(\b|^)Patient(:|\s)+" ]
def execute(self, inputs, update=0, last=0): """ Execute the filter with given inputs and return the output """ if not lib.ProcessingFilter.ProcessingFilter.execute(self, inputs): return None self.progressObj.setProgress(0.0) self.updateProgress(None, "ProgressEvent") rightDataType = True labelImage = self.getInput(1) labelImage.Update() if "vtkImageData" in str(labelImage.__class__): rightDataType = (labelImage.GetScalarType() == 9) elif not "itkImageUL" in str(labelImage.__class__): rightDataType = False if not rightDataType: Logging.error( "Incompatible data type", "Please convert the input to label data of type unsigned long. Segmented data can be labeled with 'Connected component labeling' or 'Object separation' in 'Segmentation -> Object Processing'." ) origImage = self.getInput(2) origImage.Update() origRange = origImage.GetScalarRange() print "Input for label shape=", self.getInputDataUnit(1) print "Orig. dataunit = ", self.getInputDataUnit(2) diritk = dir(itk) if "LabelImageToStatisticsLabelMapFilter" in diritk and "LabelMap" in diritk and "StatisticsLabelObject" and "LabelGeometryImageFilter" in diritk: newITKStatistics = 1 else: newITKStatistics = 0 # Do necessary conversions of datatype origVTK = origImage if self.parameters["AvgInt"] or self.parameters[ "NonZero"] or newITKStatistics: origITK = self.convertVTKtoITK(origVTK) # Cannot have two convertVTKtoITK in same filter if self.parameters["AvgInt"] or self.parameters["Area"]: labelVTK = self.convertITKtoVTK(labelImage) if "itkImage" not in str(labelImage.__class__): extent = labelImage.GetWholeExtent() if extent[5] - extent[4] == 0: dim = 2 else: dim = 3 scalarType = labelImage.GetScalarType() if scalarType != 9: # Convert to unsigned long castVTK = vtk.vtkImageCast() castVTK.SetOutputScalarTypeToUnsignedLong() castVTK.SetInput(labelImage) labelImage = castVTK.GetOutput() labelImage.Update() vtkItk = eval("itk.VTKImageToImageFilter.IUL%d.New()" % dim) vtkItk.SetInput(labelImage) labelITK = vtkItk.GetOutput() labelITK.Update() else: labelITK = labelImage dim = labelITK.GetLargestPossibleRegion().GetSize( ).GetSizeDimension() # Initializations spacing = self.dataUnit.getSpacing() x, y, z = self.dataUnit.getVoxelSize() x *= 1000000 y *= 1000000 z *= 1000000 if z == 0: z = 1.0 vol = x * y * z voxelSizes = [x, y, z] values = [] centersofmass = [] umcentersofmass = [] avgints = [] avgintsstderrs = [] objIntSums = [] avgDists = [] avgDistsStdErrs = [] objAreasUm = [] objRoundness = [] objMinorLength = [] objMajorLength = [] objElongation = [] objAngleMinX = [] objAngleMinY = [] objAngleMinZ = [] objAngleMajX = [] objAngleMajY = [] objAngleMajZ = [] objSmoothness = [] ignoreLargest = 1 currFilter = self while currFilter: if currFilter.ignoreObjects > ignoreLargest: ignoreLargest = currFilter.ignoreObjects currFilter = currFilter.prevFilter startIntensity = ignoreLargest print "Ignoring", startIntensity, "first objects" if newITKStatistics: # Change spacing for correct results, or not #changeInfoLabel = itk.ChangeInformationImageFilter[labelITK].New() #changeInfoLabel.SetInput(labelITK) #changeInfoLabel.ChangeSpacingOn() #changeInfoOrig = itk.ChangeInformationImageFilter[origITK].New() #changeInfoOrig.SetInput(origITK) #changeInfoOrig.ChangeSpacingOn() if dim == 3: lm = itk.LabelMap._3.New() #changeInfoLabel.SetOutputSpacing(voxelSizes) #changeInfoOrig.SetOutputSpacing(voxelSizes) else: lm = itk.LabelMap._2.New() #changeInfoLabel.SetOutputSpacing(voxelSizes[:2]) #changeInfoOrig.SetOutputSpacing(voxelSizes[:2]) labelStatistics = itk.LabelImageToStatisticsLabelMapFilter[ labelITK, origITK, lm].New() #labelStatistics.SetInput1(changeInfoLabel.GetOutput()) #labelStatistics.SetInput2(changeInfoOrig.GetOutput()) labelStatistics.SetInput1(labelITK) labelStatistics.SetInput2(origITK) if self.parameters["Area"]: labelStatistics.ComputePerimeterOn() labelStatistics.Update() labelMap = labelStatistics.GetOutput() numberOfLabels = labelMap.GetNumberOfLabelObjects() else: labelShape = itk.LabelShapeImageFilter[labelITK].New() labelShape.SetInput(labelITK) data = labelShape.GetOutput() data.Update() numberOfLabels = labelShape.GetNumberOfLabels() if self.parameters["AvgInt"]: avgintCalc = itk.LabelStatisticsImageFilter[origITK, labelITK].New() avgintCalc.SetInput(origITK) avgintCalc.SetLabelInput(labelITK) avgintCalc.Update() self.progressObj.setProgress(0.2) self.updateProgress(None, "ProgressEvent") # Area calculation pipeline if self.parameters["Area"]: voxelArea = x * y * 2 + x * z * 2 + y * z * 2 largestSize = labelITK.GetLargestPossibleRegion().GetSize() # if 2D image, calculate area using volume if largestSize.GetSizeDimension() > 2 and largestSize.GetElement( 2) > 1: areaSpacing = labelVTK.GetSpacing() objectThreshold = vtk.vtkImageThreshold() objectThreshold.SetInput(labelVTK) objectThreshold.SetOutputScalarTypeToUnsignedChar() objectThreshold.SetInValue(255) objectThreshold.SetOutValue(0) marchingCubes = vtk.vtkMarchingCubes() #marchingCubes.SetInput(labelVTK) marchingCubes.SetInput(objectThreshold.GetOutput()) massProperties = vtk.vtkMassProperties() massProperties.SetInput(marchingCubes.GetOutput()) areaDiv = (areaSpacing[0] / x)**2 if self.parameters["Smoothness"]: smoothDecimate = vtk.vtkDecimatePro() smoothProperties = vtk.vtkMassProperties() smoothDecimate.SetTargetReduction(0.9) smoothDecimate.PreserveTopologyOff() smoothDecimate.SetInput(marchingCubes.GetOutput()) smoothProperties.SetInput(smoothDecimate.GetOutput()) # Filter needed for axes calculations if self.parameters["Axes"] and newITKStatistics: labelGeometry = itk.LabelGeometryImageFilter[labelITK, labelITK].New() labelGeometry.SetCalculateOrientedBoundingBox(1) labelGeometry.SetInput(labelITK) labelGeometry.Update() # Get results and do some calculations for each object tott = 0 voxelSize = voxelSizes[0] * voxelSizes[1] * voxelSizes[2] for i in range(startIntensity, numberOfLabels + 1): areaInUm = 0.0 avgInt = 0 avgIntStdErr = 0.0 roundness = 0.0 objIntSum = 0.0 minorLength = 0.0 majorLength = 0.0 elongation = 0.0 angleMinX = 0.0 angleMinY = 0.0 angleMinZ = 0.0 angleMajX = 0.0 angleMajY = 0.0 angleMajZ = 0.0 smoothness = 0.0 if newITKStatistics: try: labelObj = labelMap.GetLabelObject(i) except: continue volume = labelObj.GetSize() #com = labelObj.GetCenterOfGravity() com = labelObj.GetCentroid() c = [] c2 = [] for k in range(0, com.GetPointDimension()): v = com[k] v /= spacing[k] c.append(v) c2.append(v * voxelSizes[k]) if com.GetPointDimension() == 2: c.append(0) c2.append(0.0) if self.parameters["AvgInt"]: avgInt = labelObj.GetMean() avgIntStdErr = math.sqrt( labelObj.GetVariance()) / math.sqrt(volume) objIntSum = avgInt * volume #if self.parameters["Area"]: # areaInUm = labelObj.GetPerimeter() # roundness = labelObj.GetRoundness() # Get area of object, copied old way because roundness is not # working if self.parameters["Area"]: if largestSize.GetSizeDimension( ) > 2 and largestSize.GetElement(2) > 1: objectThreshold.ThresholdBetween(i, i) marchingCubes.SetValue(0, 255) polydata = marchingCubes.GetOutput() polydata.Update() if polydata.GetNumberOfPolys() > 0: massProperties.Update() areaInUm = massProperties.GetSurfaceArea( ) / areaDiv else: areaInUm = voxelArea # Calculate roundness hypersphereR = ((3 * volume * vol) / (4 * math.pi))**(1 / 3.0) hypersphereArea = 3 * volume * vol / hypersphereR roundness = hypersphereArea / areaInUm # Calculate surface smoothness if self.parameters["Smoothness"]: # Smooth surface with vtkDecimatePro. polydata = smoothDecimate.GetOutput() polydata.Update() if polydata.GetNumberOfPolys() > 0: smoothProperties.Update() smoothArea = smoothProperties.GetSurfaceArea( ) / areaDiv smoothness = smoothArea / areaInUm else: areaInUm = volume * x * y if self.parameters["Axes"]: vert = labelGeometry.GetOrientedBoundingBoxVertices(i) vertices = [] for vNum in range(vert.size()): vertices.append(vert.pop()) boxVect = [] if dim == 3: vertNums = [1, 2, 4] else: vertNums = [1, 2] for vertNum in vertNums: vertex1 = vertices[0] vertex2 = vertices[vertNum] boxVect.append([ abs(vertex2[dimN] - vertex1[dimN]) * voxelSizes[dimN] for dimN in range(dim) ]) boxVectLen = [] minAxNum = -1 majAxNum = -1 minorLength = -1 majorLength = -1 for num, vect in enumerate(boxVect): length = 0.0 for vectComp in vect: length += vectComp**2 length = math.sqrt(length) boxVectLen.append(length) if length > majorLength: majorLength = length majAxNum = num if length < minorLength or minorLength < 0: minorLength = length minAxNum = num elongation = majorLength / minorLength # Calculate angle between major, minor axes and x,y,z axes for dimN in range(dim): boxVect[minAxNum][dimN] /= minorLength boxVect[majAxNum][dimN] /= majorLength vecX = (1.0, 0.0, 0.0) vecY = (0.0, 1.0, 0.0) vecZ = (0.0, 0.0, 1.0) angleMinX = lib.Math.angle(boxVect[minAxNum], vecX) angleMinY = lib.Math.angle(boxVect[minAxNum], vecY) angleMinZ = lib.Math.angle(boxVect[minAxNum], vecZ) angleMajX = lib.Math.angle(boxVect[majAxNum], vecX) angleMajY = lib.Math.angle(boxVect[majAxNum], vecY) angleMajZ = lib.Math.angle(boxVect[majAxNum], vecZ) else: if not labelShape.HasLabel(i): continue else: volume = labelShape.GetVolume(i) centerOfMass = labelShape.GetCenterOfGravity(i) if self.parameters["AvgInt"]: avgInt = avgintCalc.GetMean(i) avgIntStdErr = math.sqrt(abs( avgintCalc.GetVariance(i))) / math.sqrt(volume) objIntSum = avgintCalc.GetSum(i) c = [] c2 = [] for k in range(0, dim): v = centerOfMass.GetElement(k) c.append(v) c2.append(v * voxelSizes[k]) if dim == 2: c.append(0) c2.append(0.0) # Get area of object if self.parameters["Area"]: if largestSize.GetSizeDimension( ) > 2 and largestSize.GetElement(2) > 1: objectThreshold.ThresholdBetween(i, i) marchingCubes.SetValue(0, 255) polydata = marchingCubes.GetOutput() polydata.Update() if polydata.GetNumberOfPolys() > 0: massProperties.Update() areaInUm = massProperties.GetSurfaceArea( ) / areaDiv else: areaInUm = voxelArea # Calculate roundness hypersphereR = ((3 * volume * vol) / (4 * math.pi))**(1 / 3.0) hypersphereArea = 3 * volume * vol / hypersphereR roundness = hypersphereArea / areaInUm else: areaInUm = volume * x * y # Add object results to result arrays centersofmass.append(tuple(c)) umcentersofmass.append(tuple(c2)) values.append((volume, volume * vol)) avgints.append(avgInt) avgintsstderrs.append(avgIntStdErr) objIntSums.append(objIntSum) objAreasUm.append(areaInUm) objRoundness.append(roundness) objMinorLength.append(minorLength) objMajorLength.append(majorLength) objElongation.append(elongation) objAngleMinX.append(angleMinX) objAngleMinY.append(angleMinY) objAngleMinZ.append(angleMinZ) objAngleMajX.append(angleMajX) objAngleMajY.append(angleMajY) objAngleMajZ.append(angleMajZ) objSmoothness.append(smoothness) self.progressObj.setProgress(0.7) self.updateProgress(None, "ProgressEvent") # Do distance calculations t0 = time.time() for i, cm in enumerate(umcentersofmass): distList = [] if self.parameters["AvgDist"]: for j, cm2 in enumerate(umcentersofmass): if i == j: continue dx = cm[0] - cm2[0] dy = cm[1] - cm2[1] dz = cm[2] - cm2[2] dist = math.sqrt(dx * dx + dy * dy + dz * dz) distList.append(dist) avgDist, avgDistStd, avgDistStdErr = lib.Math.meanstdeverr( distList) avgDists.append(avgDist) avgDistsStdErrs.append(avgDistStdErr) print "Distance calculations took", time.time() - t0 self.progressObj.setProgress(0.8) self.updateProgress(None, "ProgressEvent") # Calculate average values and errors n = len(values) avgint, avgintstd, avgintstderr = lib.Math.meanstdeverr(avgints) intSum = sum(objIntSums, 0.0) ums = [x[1] for x in values] avgums, avgumsstd, avgumsstderr = lib.Math.meanstdeverr(ums) sumums = sum(ums, 0.0) pxs = [x[0] for x in values] avgpxs, avgpxsstd, avgpxsstderr = lib.Math.meanstdeverr(pxs) distMean, distStd, distStdErr = lib.Math.meanstdeverr(avgDists) avground, avgroundstd, avgroundstderr = lib.Math.meanstdeverr( objRoundness) avgAreaUm, avgAreaUmStd, avgAreaUmStdErr = lib.Math.meanstdeverr( objAreasUm) areaSumUm = sum(objAreasUm, 0.0) avgminlen, avgminlenstd, avgminlenstderr = lib.Math.meanstdeverr( objMinorLength) avgmajlen, avgmajlenstd, avgmajlenstderr = lib.Math.meanstdeverr( objMajorLength) avgelon, avgelonstd, avgelonstderr = lib.Math.meanstdeverr( objElongation) avgangminx, avgangminxstd, avgangminxstderr = lib.Math.meanstdeverr( objAngleMinX) avgangminy, avgangminystd, avgangminystderr = lib.Math.meanstdeverr( objAngleMinY) avgangminz, avgangminzstd, avgangminzstderr = lib.Math.meanstdeverr( objAngleMinZ) avgangmajx, avgangmajxstd, avgangmajxstderr = lib.Math.meanstdeverr( objAngleMajX) avgangmajy, avgangmajystd, avgangmajystderr = lib.Math.meanstdeverr( objAngleMajY) avgangmajz, avgangmajzstd, avgangmajzstderr = lib.Math.meanstdeverr( objAngleMajZ) avgsmooth, avgsmoothstd, avgsmoothstderr = lib.Math.meanstdeverr( objSmoothness) # Calculate average intensity outside objects avgIntOutsideObjs = 0.0 avgIntOutsideObjsStdErr = 0.0 avgIntOutsideObjsNonZero = 0.0 avgIntOutsideObjsNonZeroStdErr = 0.0 nonZeroVoxels = -1 if self.parameters["AvgInt"]: variances = 0.0 allVoxels = 0 for i in range(0, startIntensity): if newITKStatistics: try: labelObj = labelMap.GetLabelObject(i) voxelAmount = labelObj.GetSize() allVoxels += voxelAmount avgIntOutsideObjs += labelObj.GetMean() * voxelAmount variances += voxelAmount * abs(labelObj.GetVariance()) except: pass else: if labelShape.HasLabel(i): voxelAmount = labelShape.GetVolume(i) allVoxels += voxelAmount avgIntOutsideObjs += avgintCalc.GetMean( i) * voxelAmount if voxelAmount > 1: variances += voxelAmount * abs( avgintCalc.GetVariance(i)) if allVoxels > 0: avgIntOutsideObjs /= allVoxels avgIntOutsideObjsStdErr = math.sqrt( variances / allVoxels) / math.sqrt(allVoxels) labelAverage = vtkbxd.vtkImageLabelAverage() labelAverage.AddInput(origVTK) labelAverage.AddInput(labelVTK) labelAverage.SetBackgroundLevel(startIntensity) labelAverage.Update() avgIntOutsideObjsNonZero = labelAverage.GetAverageOutsideLabels() if labelAverage.GetVoxelsOutsideLabels() == 0: avgIntOutsideObjsNonZeroStdErr = 0.0 else: avgIntOutsideObjsNonZeroStdErr = labelAverage.GetOutsideLabelsStdDev( ) / math.sqrt(labelAverage.GetVoxelsOutsideLabels()) # Get also non zero voxels here that there is no need to recalculate nonZeroVoxels = labelAverage.GetNonZeroVoxels() self.progressObj.setProgress(0.9) self.updateProgress(None, "ProgressEvent") # Calculate average intensity inside objects avgIntInsideObjs = 0.0 avgIntInsideObjsStdErr = 0.0 if self.parameters["AvgInt"]: variances = 0.0 allVoxels = 0 for i in range(startIntensity, numberOfLabels + 1): if newITKStatistics: try: labelObj = labelMap.GetLabelObject(i) voxelAmount = labelObj.GetSize() allVoxels += voxelAmount avgIntInsideObjs += labelObj.GetMean() * voxelAmount if voxelAmount > 1: variances += voxelAmount * abs( labelObj.GetVariance()) except: pass else: if labelShape.HasLabel(i): voxelAmount = labelShape.GetVolume(i) allVoxels += voxelAmount avgIntInsideObjs += avgintCalc.GetMean(i) * voxelAmount variances += voxelAmount * abs( avgintCalc.GetVariance(i)) if allVoxels > 0: avgIntInsideObjs /= allVoxels avgIntInsideObjsStdErr = math.sqrt( variances / allVoxels) / math.sqrt(allVoxels) # Calculate non-zero voxels if self.parameters["NonZero"] and nonZeroVoxels < 0: labelShape = itk.LabelShapeImageFilter[origITK].New() labelShape.SetInput(origITK) labelShape.Update() for i in range(1, int(origRange[1]) + 1): if labelShape.HasLabel(i): nonZeroVoxels += labelShape.GetVolume(i) # Set results self.values = values self.centersofmass = centersofmass self.umcentersofmass = umcentersofmass self.avgIntList = avgints self.avgIntStdErrList = avgintsstderrs self.intSums = objIntSums self.avgDistList = avgDists self.avgDistStdErrList = avgDistsStdErrs self.objAreasUm = objAreasUm self.objRoundness = objRoundness self.objMinorLength = objMinorLength self.objMajorLength = objMajorLength self.objElongation = objElongation self.objAngleMinX = objAngleMinX self.objAngleMinY = objAngleMinY self.objAngleMinZ = objAngleMinZ self.objAngleMajX = objAngleMajX self.objAngleMajY = objAngleMajY self.objAngleMajZ = objAngleMajZ self.intSum = intSum self.objSmoothness = objSmoothness #self.distMean = distMean #self.distStdErr = distStdErr #self.avgRoundness = avground #self.avgRoundnessStdErr = avgroundstderr #self.avgIntInsideObjs = avgIntInsideObjs #self.avgIntInsideObjsStdErr = avgIntInsideObjsStdErr #self.avgIntOutsideObjs = avgIntOutsideObjs #self.avgIntOutsideObjsStdErr = avgIntOutsideObjsStdErr #self.avgIntOutsideObjsNonZero = avgIntOutsideObjsNonZero #self.avgIntOutsideObjsNonZeroStdErr = avgIntOutsideObjsNonZeroStdErr self.setResultVariable("NumberOfObjects", len(values)) self.setResultVariable("ObjAvgVolInVoxels", avgpxs) self.setResultVariable("ObjAvgVolInUm", avgums) self.setResultVariable("ObjVolSumInUm", sumums) self.setResultVariable("ObjAvgAreaInUm", avgAreaUm) self.setResultVariable("ObjAreaSumInUm", areaSumUm) self.setResultVariable("ObjAvgIntensity", avgint) self.setResultVariable("AvgIntOutsideObjs", avgIntOutsideObjs) self.setResultVariable("AvgIntOutsideObjsNonZero", avgIntOutsideObjsNonZero) self.setResultVariable("AvgIntInsideObjs", avgIntInsideObjs) self.setResultVariable("NonZeroVoxels", nonZeroVoxels) self.setResultVariable("AverageDistance", distMean) self.setResultVariable("AvgDistanceStdErr", distStdErr) self.setResultVariable("ObjAvgVolInVoxelsStdErr", avgpxsstderr) self.setResultVariable("ObjAvgVolInUmStdErr", avgumsstderr) self.setResultVariable("ObjAvgAreaInUmStdErr", avgAreaUmStdErr) self.setResultVariable("ObjAvgIntensityStdErr", avgintstderr) self.setResultVariable("AvgIntOutsideObjsStdErr", avgIntOutsideObjsStdErr) self.setResultVariable("AvgIntOutsideObjsNonZeroStdErr", avgIntOutsideObjsNonZeroStdErr) self.setResultVariable("AvgIntInsideObjsStdErr", avgIntInsideObjsStdErr) self.setResultVariable("ObjIntensitySum", intSum) self.setResultVariable("ObjAvgRoundness", avground) self.setResultVariable("ObjAvgRoundnessStdErr", avgroundstderr) self.setResultVariable("ObjAvgMajorAxisLen", avgmajlen) self.setResultVariable("ObjAvgMajorAxisLenStdErr", avgmajlenstderr) self.setResultVariable("ObjAvgMinorAxisLen", avgminlen) self.setResultVariable("ObjAvgMinorAxisLenStdErr", avgminlenstderr) self.setResultVariable("ObjAvgElongation", avgelon) self.setResultVariable("ObjAvgElongationStdErr", avgelonstderr) self.setResultVariable("ObjAvgAngleXMajorAxis", avgangmajx) self.setResultVariable("ObjAvgAngleXMajorAxisStdErr", avgangmajxstderr) self.setResultVariable("ObjAvgAngleYMajorAxis", avgangmajy) self.setResultVariable("ObjAvgAngleYMajorAxisStdErr", avgangmajystderr) self.setResultVariable("ObjAvgAngleZMajorAxis", avgangmajz) self.setResultVariable("ObjAvgAngleZMajorAxisStdErr", avgangmajzstderr) self.setResultVariable("ObjAvgAngleXMinorAxis", avgangminx) self.setResultVariable("ObjAvgAngleXMinorAxisStdErr", avgangminxstderr) self.setResultVariable("ObjAvgAngleYMinorAxis", avgangminy) self.setResultVariable("ObjAvgAngleYMinorAxisStdErr", avgangminystderr) self.setResultVariable("ObjAvgAngleZMinorAxis", avgangminz) self.setResultVariable("ObjAvgAngleZMinorAxisStdErr", avgangminzstderr) self.setResultVariable("ObjAvgSmoothness", avgsmooth) self.setResultVariable("ObjAvgSmoothnessStdErr", avgsmoothstderr) self.stats = [ n, avgums, avgumsstderr, avgpxs, avgpxsstderr, avgAreaUm, avgAreaUmStdErr, avgint, avgintstderr, avgIntOutsideObjs, avgIntOutsideObjsStdErr, distMean, distStdErr, sumums, areaSumUm, avgIntOutsideObjsNonZero, avgIntOutsideObjsNonZeroStdErr, avgIntInsideObjs, avgIntInsideObjsStdErr, nonZeroVoxels, avground, avgroundstderr, intSum, avgmajlen, avgmajlenstderr, avgminlen, avgminlenstderr, avgelon, avgelonstderr, avgangmajx, avgangmajxstderr, avgangmajy, avgangmajystderr, avgangmajz, avgangmajzstderr, avgangminx, avgangminxstderr, avgangminy, avgangminystderr, avgangminz, avgangminzstderr, avgsmooth, avgsmoothstderr ] if self.reportGUI: self.reportGUI.DeleteAllItems() self.reportGUI.setVolumes(values) self.reportGUI.setCentersOfMass(centersofmass) self.reportGUI.setAverageIntensities(avgints, avgintsstderrs) self.reportGUI.setIntensitySums(objIntSums) self.reportGUI.setAverageDistances(avgDists, avgDistsStdErrs) self.reportGUI.setAreasUm(objAreasUm) self.reportGUI.setRoundness(objRoundness) self.reportGUI.setMajorAxisLengths(objMajorLength) self.reportGUI.setMinorAxisLengths(objMinorLength) self.reportGUI.setElongations(objElongation) self.reportGUI.setMajorAngles(objAngleMajX, objAngleMajY, objAngleMajZ) self.reportGUI.setMinorAngles(objAngleMinX, objAngleMinY, objAngleMinZ) self.reportGUI.setSmoothness(objSmoothness) self.totalGUI.setStats(self.stats) self.progressObj.setProgress(1.0) self.updateProgress(None, "ProgressEvent") return self.getInput(1)
def updateSettings(self, force = 0, *args): """ A method used to set the GUI widgets to their proper values based on the selected channel, the settings of which are stored in the instance variable self.settings """ Logging.info("Updating coloc settings", kw="processing") if self.settings: format = self.settings.get("ColocalizationDepth") scalar = self.settings.get("OutputScalar") self.constColocCheckbox.SetValue(format == 1) self.constColocValue.SetValue("%d" %int(scalar)) self.constColocValue.Enable(format == 1) th = self.settings.getCounted("ColocalizationLowerThreshold", 0) if th != None: self.lowerthreshold1.SetValue(th) th = self.settings.getCounted("ColocalizationUpperThreshold", 0) if th != None: self.upperthreshold1.SetValue(th) th = self.settings.getCounted("ColocalizationLowerThreshold", 1) if th != None: self.lowerthreshold2.SetValue(th) th = self.settings.getCounted("ColocalizationUpperThreshold", 1) if th != None: self.upperthreshold2.SetValue(th) method = self.settings.get("Method") if method is not None: self.radiobox.SetSelection(int(method)) flag = (int(method) == 1) self.iterations.Enable(flag) self.NA.Enable(flag) self.Ch2Lambda.Enable(flag) self.fixedPSF.Enable(flag) psf = self.settings.get("PSF") if psf is not None: self.fixedPSF.SetValue("%.4f"%float(psf)) iterations = self.settings.get("NumIterations") if iterations is not None: self.iterations.SetValue(int(iterations)) na = self.settings.get("NumericalAperture") if na is not None: self.NA.SetValue("%.4f"%float(na)) ch2lambda = self.settings.get("Ch2Lambda") if ch2lambda is not None: self.Ch2Lambda.SetValue("%d"%int(ch2lambda)) # If the scatterplot exists, update it's thresholds if self.scatterPlot: sources = self.dataUnit.getSourceDataUnits() ch1 = sources[0].getSettings() ch2 = sources[1].getSettings() ch1lower, ch1upper = ch1.get("ColocalizationLowerThreshold"), ch1.get("ColocalizationUpperThreshold") ch2lower, ch2upper = ch2.get("ColocalizationLowerThreshold"), ch2.get("ColocalizationUpperThreshold") self.scatterPlot.setThresholds(ch1lower, ch1upper, ch2lower, ch2upper) if self.dataUnit and self.settings: ctf = self.dataUnit.getSettings().get("ColorTransferFunction") if ctf and self.colorBtn: self.colorBtn.setColorTransferFunction(ctf) self.colorBtn.Refresh()
def readFrom(self, parser): """ Attempt to read all registered keys from a parser """ self.parser = parser if not self.get("Type"): self.parser = parser type = None try: type = parser.get("Type", "Type") except ConfigParser.NoOptionError: type = parser.get("Type", "type") except ConfigParser.NoSectionError: pass # if we can determine the settings type, then we instantiate a class corresponding # to that type and read the settings using that class # this is done so that all the settings will be read correctly # if the type cannot be determined, then just read the settings that we know how if type: if type in self.modules: settingsclass = self.modules[type][2].getSettingsClass() else: settingsclass = self.__class__ Logging.info("Type = %s, settings class = %s" % (type, str(settingsclass)), kw="processing") obj = settingsclass(self.dataSetNumber) obj.setType(type) return obj.readFrom(parser) for key in self.registered.keys(): ser = self.serialized[key] if key in self.counted: try: n = parser.get("Count", key) except: try: n = parser.get("Count", key.lower()) except: Logging.info("Got no key count for %s" % key, kw="dataunit") continue n = int(n) Logging.info("Got %d keys for %s" % (n, key), kw="dataunit") for i in range(n + 1): ckey = "%s[%d]" % (key, i) try: try: value = parser.get(key, ckey) except ConfigParser.NoOptionError: try: value = parser.get(key, ckey.lower()) except ConfigParser.NoOptionError: continue if ser: value = self.deserialize(key, value) #Logging.info("Deserialized ", key, " = ", value, kw = "dataunit") self.setCounted(key, i, value) self.counted[key] = i except ConfigParser.NoSectionError: Logging.info("Got no keys for section %s" % key, kw="dataunit") else: #value = parser.get("ColorTransferFunction", "ColorTransferFunction") try: try: value = parser.get(key, key) except ConfigParser.NoOptionError: value = parser.get(key, key.lower()) if ser: #Logging.info("Trying to deserialize ", key, value, kw = "dataunit") value = self.deserialize(key, value) #Logging.info("Deserialized ", key, " = ", value, kw = "dataunit") self.set(key, value) except ConfigParser.NoSectionError: #Logging.info("Got no keys for section %s" %key, kw = "dataunit") pass return self
def main(): """Initialize All Modules""" tokenList = Files.getTokenList() #[0] should be mine, and [1] should be my alt for group creation log.debug("Tokens:",tokenList) #Just my user id and token put = ("27094908", tokenList[0]) #Totally Not The NSA Token Groups.Group.overlord = tokenList[1] #Just things #log.network.debug.disable() log.command.low.enable() log.user.low.enable() #First load all the groups log.info("========== PRE-INIT (LOAD) ==========") toLoadList = [] for folder in Files.getDirsInDir(): if "Group " in folder: #If it a folder containing a group to load number = int(folder.split(" ")[-1]) obj = (number, folder,) #Tuple of number and folder name flag = True for i in range(len(toLoadList)): #Sort the list if number < toLoadList[i][0]: toLoadList.insert(i, obj) flag = False break if flag: #If did not insert anywhere else toLoadList.append(obj) #Loadsd the groups guarenteed in order for group in toLoadList: Groups.loadGroup(group[1]) log.info("Groups: ", Groups.getSortedList()) #Do init and post-init log.info("========== INIT ==========") for group in list(Groups.getSortedList()): group.init() ### Named groups should be made after INIT to avoid duplicated initial users testGroup = makeNamedGroup(1, "23199161", put) toriGroup = makeNamedGroup(15, "23317842", put) groupFam = makeNamedGroup(2, "13972393", put) groupFam.setBot("cfe41d49a83d73874f4aa547b9") try: #This is so we can have our finally block remove any extra threads in case of error log.info("========== POST-INIT ==========") for group in list(Groups.getSortedList()): try: group.postInit() except AssertionError: pass log.info("========== GROUP CLEANUP ==========") deletionList = Groups.getSortedList() deletionList.reverse() for i in deletionList.copy(): if i.markedForDeletion: log.info("Deleting group", i) i.deleteSelf() del i del deletionList #try: def postEarlyMorningFact(): joke = Jokes.funFacts.getJoke() if type(joke) == tuple: return Jokes.funFacts._postJoke(groupFam, ("Oh boy 3 A.M.!\n"+joke[0], joke[1])) return Jokes.funFacts._postJoke(groupFam, "Oh boy 3 A.M.!\n" + joke) server = Server(('', Network.SERVER_CONNECTION_PORT), ServerHandler) #Update things for the groups every day at 5 a.m. log.info("Starting daily triggers") updaterDaily = Events.PeriodicUpdater(time(5, 0), timedelta(1), Groups.groupDailyDuties) earlyMorningFacts = Events.PeriodicUpdater(time(3, 0), timedelta(1), postEarlyMorningFact) log.info("========== BEGINNING SERVER RECEIVING ==========") try: server.serve_forever() except KeyboardInterrupt: pass if server.exitValue != None: #Value of none means exited due to KeyboardInterrupt or something else log.info("Server shutting down from user input") if server.exitValue: #Exit value true means restart return 0; else: #False means shutdown raise AssertionError("Signal to main that we are done") #TESTING CODE: if Events.IS_TESTING: import traceback while True: print("> ", end = "") try: statement = input() if statement.lower() == "quit": break elif "=" in statement: exec(statement) else: print(eval(statement)) except Exception as e: if isinstance(e, KeyboardInterrupt) or isinstance(e, EOFError): from os import _exit _exit(0) #Screw this crap, just get out of here. It's only for testing else: traceback.print_exc() raise AssertionError("Signal to main that we are done") #We need to kill all threads before exiting finally: Events.stopAllTimers() Events.SyncSave().saveAll(final = True)
def addSplinePoint(self, position, update=1, **kws): """ A method to add a new item to this track """ h = self.height itemkws = {"itemnum": position, "editable": self.editable} if kws.has_key("point"): point = kws["point"] else: point = self.splineEditor.getRandomPoint() itemkws["point"] = point # print "Setting point to ",point pts = [] for item in self.items: # print "item=",item if not hasattr(item, "stopitem"): pts.append(item.getPoint()) if position >= len(pts) - 1: Logging.info("append pts, pos=%d,len=%d" % (position, len(pts)), kw="animator") pts.append(point) else: Logging.info("insert pts", kw="animator") pts.insert(position, point) Logging.info("spline pts=", pts, kw="animator") if len(pts) >= 2: self.splineEditor.setClosed(self.closed) self.splineEditor.setSplinePoints(pts) self.control.setSplineInteractionCallback(self.updateLabels) item = self.itemClass(self, "%d" % position, (20, h), **itemkws) if self.color: item.setColor(self.color, self.headercolor) if position >= len(pts) - 1: print "appending item", item.itemnum self.items.append(item) else: print "Inserting items" self.items.insert(position, item) # +1 accounts for the empty item spc = 0 for i, item in enumerate(self.items): if not item.isStopped(): self.items[i].setItemNumber(spc) self.items[i].setText("%d" % spc) spc += 1 if update: self.items[i].updateItem() self.items[i].drawItem() self.updatePositions() if update: self.paintTrack() self.Refresh() #self.sizer.Fit(self) return item