def test_basic(self): """ Fire a couple requests and check the answer they give. """ def someWork(): time.sleep(0.001) return "Hello," callback_result = [''] def callback(result): callback_result[0] = result def test(s): req = Request(someWork) req.notify_finished(callback) s2 = req.wait() time.sleep(0.001) return s2 + s req = Request( partial(test, s = " World!") ) req.notify_finished(callback) # Wait for the result assert req.wait() == "Hello, World!" # Wait for it assert req.wait() == "Hello, World!" # It's already finished, should be same answer assert callback_result[0] == "Hello, World!" # From the callback requests = [] for i in range(10): req = Request( partial(test, s = "hallo %d" %i) ) requests.append(req) for r in requests: r.wait()
def test_cancel_basic(self): """ Start a workload and cancel it. Verify that it was actually cancelled before all the work was finished. """ if Request.global_thread_pool.num_workers == 0: raise nose.SkipTest def workload(): time.sleep(0.1) return 1 got_cancel = [False] workcounter = [0] def big_workload(): try: requests = [] for i in range(100): requests.append(Request(workload)) for r in requests: workcounter[0] += r.wait() assert False, "Shouldn't get to this line. This test is designed so that big_workload should be cancelled before it finishes all its work" for r in requests: assert not r.cancelled except Request.CancellationException: got_cancel[0] = True except Exception as ex: import traceback traceback.print_exc() raise completed = [False] def handle_complete(result): completed[0] = True req = Request(big_workload) req.notify_finished(handle_complete) req.submit() while workcounter[0] == 0: time.sleep(0.001) req.cancel() time.sleep(1) assert req.cancelled assert not completed[0] assert got_cancel[0] # Make sure this test is functioning properly: # The cancellation should have occurred in the middle (not before the request even got started) # If not, then adjust the timing of the cancellation, above. assert workcounter[ 0] != 0, "This timing-sensitive test needs to be tweaked." assert workcounter[ 0] != 100, "This timing-sensitive test needs to be tweaked."
def test_result_discarded(self): """ After a request is deleted, its result should be discarded. """ import weakref from functools import partial def f(): return numpy.zeros( (10,), dtype=numpy.uint8 ) + 1 w = [None] def onfinish(r, result): w[0] = weakref.ref(result) req = Request(f) req.notify_finished( partial(onfinish, req) ) req.submit() req.wait() del req # The ThreadPool._Worker loop has a local reference (next_task), # so wait just a tic for the ThreadPool worker to cycle back to the top of its loop (and discard the reference) time.sleep(0.1) assert w[0]() is None
def test_result_discarded(self): """ After a request is deleted, its result should be discarded. """ import weakref from functools import partial def f(): return numpy.zeros((10, ), dtype=numpy.uint8) + 1 w = [None] def onfinish(r, result): w[0] = weakref.ref(result) req = Request(f) req.notify_finished(partial(onfinish, req)) req.submit() req.wait() del req # The ThreadPool._Worker loop has a local reference (next_task), # so wait just a tic for the ThreadPool worker to cycle back to the top of its loop (and discard the reference) time.sleep(0.1) assert w[0]() is None
def test_cancel_basic(self): """ Start a workload and cancel it. Verify that it was actually cancelled before all the work was finished. """ if Request.global_thread_pool.num_workers == 0: raise nose.SkipTest def workload(): time.sleep(0.1) return 1 got_cancel = [False] workcounter = [0] def big_workload(): try: requests = [] for i in range(100): requests.append( Request(workload) ) for r in requests: workcounter[0] += r.wait() assert False, "Shouldn't get to this line. This test is designed so that big_workload should be cancelled before it finishes all its work" for r in requests: assert not r.cancelled except Request.CancellationException: got_cancel[0] = True except Exception as ex: import traceback traceback.print_exc() raise completed = [False] def handle_complete( result ): completed[0] = True req = Request( big_workload ) req.notify_finished( handle_complete ) req.submit() while workcounter[0] == 0: time.sleep(0.001) req.cancel() time.sleep(1) assert req.cancelled assert not completed[0] assert got_cancel[0] # Make sure this test is functioning properly: # The cancellation should have occurred in the middle (not before the request even got started) # If not, then adjust the timing of the cancellation, above. assert workcounter[0] != 0, "This timing-sensitive test needs to be tweaked." assert workcounter[0] != 100, "This timing-sensitive test needs to be tweaked."
def _impl_test_pool_results_discarded(): """ After a RequestPool executes, none of its data should linger if the user didn't hang on to it. """ import weakref from functools import partial import threading result_refs = [] def workload(): # In this test, all results are discarded immediately after the # request exits. Therefore, AT NO POINT IN TIME, should more than N requests be alive. live_result_refs = [w for w in result_refs if w() is not None] assert ( len(live_result_refs) <= Request.global_thread_pool.num_workers ), "There should not be more than {} result references alive at one time!".format( Request.global_thread_pool.num_workers ) return numpy.zeros((10,), dtype=numpy.uint8) + 1 lock = threading.Lock() def handle_result(req, result): with lock: result_refs.append(weakref.ref(result)) def handle_cancelled(req, *args): assert False def handle_failed(req, exc, exc_info): raise exc pool = RequestPool() for _ in range(100): req = Request(workload) req.notify_finished(partial(handle_result, req)) req.notify_cancelled(partial(handle_cancelled, req)) req.notify_failed(partial(handle_failed, req)) pool.add(req) del req pool.wait() # This test verifies that # (1) references to all child requests have been discarded once the pool is complete, and # (2) therefore, all references to the RESULTS in those child requests are also discarded. # There is a tiny window of time between a request being 'complete' (for all intents and purposes), # but before its main execute function has exited back to the main ThreadPool._Worker loop. # The request is not finally discarded until that loop discards it, so let's wait a tiny extra bit of time. time.sleep(0.01) # Now check that ALL results are truly lost. for ref in result_refs: assert ref() is None, "Some data was not discarded."
def _onExportTifButtonPressed(self): options = QFileDialog.Options() if ilastik_config.getboolean("ilastik", "debug"): options |= QFileDialog.DontUseNativeDialog directory = QFileDialog.getExistingDirectory(self, 'Select Directory',os.getenv('HOME'), options=options) if directory is None or len(str(directory)) == 0: print "cancelled." return print 'Saving results as tiffs...' label2color = self.mainOperator.label2color lshape = list(self.mainOperator.LabelImage.meta.shape) def _handle_progress(x): self.applet.progressSignal.emit(x) def _export(): num_files = float(len(label2color)) for t, label2color_at in enumerate(label2color): if len(label2color_at) == 0: continue print 'exporting tiffs for t = ' + str(t) roi = SubRegion(self.mainOperator.LabelImage, start=[t,] + 4*[0,], stop=[t+1,] + list(lshape[1:])) labelImage = self.mainOperator.LabelImage.get(roi).wait() relabeled = relabel(labelImage[0,...,0],label2color_at) for i in range(relabeled.shape[2]): out_im = relabeled[:,:,i] out_fn = str(directory) + '/vis_t' + str(t).zfill(4) + '_z' + str(i).zfill(4) + '.tif' vigra.impex.writeImage(np.asarray(out_im,dtype=np.uint32), out_fn) _handle_progress(t/num_files * 100) print 'Tiffs exported.' def _handle_finished(*args): self._drawer.exportTifButton.setEnabled(True) self.applet.progressSignal.emit(100) def _handle_failure( exc, exc_info ): import traceback, sys traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during export. See traceback above.\n") self.applet.progressSignal.emit(100) self._drawer.exportTifButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(False) self.applet.progressSignal.emit(0) req = Request( _export ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def _onExportTifButtonPressed(self): options = QFileDialog.Options() if ilastik_config.getboolean("ilastik", "debug"): options |= QFileDialog.DontUseNativeDialog directory = encode_from_qstring(QFileDialog.getExistingDirectory(self, 'Select Directory',os.path.expanduser("~"), options=options)) if directory is None or len(str(directory)) == 0: logger.info( "cancelled." ) return logger.info( 'Saving results as tiffs...' ) label2color = self.mainOperator.label2color lshape = list(self.mainOperator.LabelImage.meta.shape) def _handle_progress(x): self.applet.progressSignal.emit(x) def _export(): num_files = float(len(label2color)) for t, label2color_at in enumerate(label2color): if len(label2color_at) == 0: continue logger.info( 'exporting tiffs for t = ' + str(t) ) roi = SubRegion(self.mainOperator.LabelImage, start=[t,] + 4*[0,], stop=[t+1,] + list(lshape[1:])) labelImage = self.mainOperator.LabelImage.get(roi).wait() relabeled = relabel(labelImage[0,...,0],label2color_at) for i in range(relabeled.shape[2]): out_im = relabeled[:,:,i] out_fn = str(directory) + '/vis_t' + str(t).zfill(4) + '_z' + str(i).zfill(4) + '.tif' vigra.impex.writeImage(np.asarray(out_im,dtype=np.uint32), out_fn) _handle_progress(t/num_files * 100) logger.info( 'Tiffs exported.' ) def _handle_finished(*args): self._drawer.exportTifButton.setEnabled(True) self.applet.progressSignal.emit(100) def _handle_failure( exc, exc_info ): msg = "Exception raised during export. See traceback above.\n" log_exception( logger, msg, exc_info ) self.applet.progressSignal.emit(100) self._drawer.exportTifButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(False) self.applet.progressSignal.emit(0) req = Request( _export ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def test_callWaitDuringCallback(self): """ When using request.notify_finished(...) to handle request completions, the handler should be allowed to call request.wait() on the request that it's handling. """ def handler(req, result): req.wait() def workFn(): pass req = Request(workFn) req.notify_finished( partial(handler, req) ) #req.submit() req.wait()
def test_callWaitDuringCallback(self): """ When using request.notify_finished(...) to handle request completions, the handler should be allowed to call request.wait() on the request that it's handling. """ def handler(req, result): req.wait() def workFn(): pass req = Request(workFn) req.notify_finished(partial(handler, req)) #req.submit() req.wait()
def test_cancel_basic(self): """ Start a workload and cancel it. Verify that it was actually cancelled before all the work was finished. """ counter_lock = threading.RLock() def workload(): time.sleep(0.1) return 1 got_cancel = [False] workcounter = [0] def big_workload(): try: requests = [] for i in range(100): requests.append( Request(workload) ) for r in requests: workcounter[0] += r.wait() assert False, "Shouldn't get to this line. This test is designed so that big_workload should be cancelled before it finishes all its work" for r in requests: assert not r.cancelled except Request.CancellationException: got_cancel[0] = True completed = [False] def handle_complete( result ): completed[0] = True req = Request( big_workload ) req.notify_finished( handle_complete ) req.submit() time.sleep(.5) req.cancel() assert req.cancelled time.sleep(2) assert not completed[0] assert got_cancel[0] # Make sure this test is functioning properly: # The cancellation should have occurred in the middle (not before the request even got started) # If not, then adjust the timing of the cancellation, above. assert workcounter[0] != 0 assert workcounter[0] != 100
def test_block_during_calback(self): """ It is valid for request finish handlers to fire off and wait for requests. This tests that feature. """ def workload(): time.sleep(0.1) return 1 total_result = [0] def handler(result): req = Request(workload) total_result[0] = result + req.wait() # Waiting on some other request from WITHIN a request callback req = Request( workload ) req.notify_finished( handler ) assert req.wait() == 1 assert total_result[0] == 2
def someWork(depth, force=False, i=-1): #print 'depth=', depth, 'i=', i if depth > 0 and (force or random.random() > 0.5): requests = [] for i in range(10): req = Request( partial(someWork, depth=depth-1, i=i) ) req.notify_finished( partial(completionHandler, req=req) ) requests.append(req) allRequests.append(req) requestLock.acquire() requestCounter[0] += 1 requestLock.release() for r in requests: r.wait() return requestCounter[0]
def someWork(depth, force=False, i=-1): #print 'depth=', depth, 'i=', i if depth > 0 and (force or random.random() > 0.5): requests = [] for i in range(10): req = Request(partial(someWork, depth=depth - 1, i=i)) req.notify_finished(partial(completionHandler, req=req)) requests.append(req) allRequests.append(req) requestLock.acquire() requestCounter[0] += 1 requestLock.release() for r in requests: r.wait() return requestCounter[0]
def test_callbacks_before_wait_returns(self): """ If the user adds callbacks to the request via notify_finished() BEFORE the request is submitted, then wait() should block for the completion of all those callbacks before returning. Any callbacks added AFTER the request has already been submitted are NOT guaranteed to be executed before wait() returns, but they will still be executed. """ def someQuickWork(): return 42 callback_results = [] def slowCallback(n, result): time.sleep(0.1) callback_results.append(n) req = Request(someQuickWork) req.notify_finished(partial(slowCallback, 1)) req.notify_finished(partial(slowCallback, 2)) req.notify_finished(partial(slowCallback, 3)) result = req.wait() assert result == 42 assert callback_results == [ 1, 2, 3 ], "wait() returned before callbacks were complete! Got: {}".format( callback_results) req.notify_finished(partial(slowCallback, 4)) req.wait() assert callback_results == [ 1, 2, 3, 4 ], "Callback on already-finished request wasn't executed."
def test_callbacks_before_wait_returns(self): """ If the user adds callbacks to the request via notify_finished() BEFORE the request is submitted, then wait() should block for the completion of all those callbacks before returning. Any callbacks added AFTER the request has already been submitted are NOT guaranteed to be executed before wait() returns, but they will still be executed. """ def someQuickWork(): return 42 callback_results = [] def slowCallback(n, result): time.sleep(0.1) callback_results.append(n) req = Request( someQuickWork ) req.notify_finished( partial(slowCallback, 1) ) req.notify_finished( partial(slowCallback, 2) ) req.notify_finished( partial(slowCallback, 3) ) result = req.wait() assert result == 42 assert callback_results == [1,2,3], "wait() returned before callbacks were complete! Got: {}".format( callback_results ) req.notify_finished( partial(slowCallback, 4) ) req.wait() assert callback_results == [1,2,3,4], "Callback on already-finished request wasn't executed."
def test_result_discarded(self): """ After a request is deleted, its result should be discarded. """ import weakref from functools import partial def f(): return numpy.zeros((10, ), dtype=numpy.uint8) + 1 w = [None] def onfinish(r, result): w[0] = weakref.ref(result) req = Request(f) req.notify_finished(partial(onfinish, req)) req.submit() req.wait() del req assert w[0]() is None
def _onTrackButtonPressed( self ): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return withMergerResolution = self._drawer.mergerResolutionBox.isChecked() withTracklets = True numStages = 6 # creating traxel store # generating probabilities # insert energies # convexify costs # solver # compute lineages if withMergerResolution: numStages += 1 # merger resolution if withTracklets: numStages += 3 # initializing tracklet graph, finding tracklets, contracting edges in tracklet graph self.progressWindow = TrackProgressDialog(parent=self,numStages=numStages) self.progressWindow.run() self.progressWindow.show() self.progressVisitor = GuiProgressVisitor(progressWindow=self.progressWindow) def _track(): self.applet.busy = True self.applet.appletStateUpdateRequested() maxDist = self._drawer.maxDistBox.value() maxObj = self._drawer.maxObjectsBox.value() divThreshold = self._drawer.divThreshBox.value() from_t = self._drawer.from_time.value() to_t = self._drawer.to_time.value() from_x = self._drawer.from_x.value() to_x = self._drawer.to_x.value() from_y = self._drawer.from_y.value() to_y = self._drawer.to_y.value() from_z = self._drawer.from_z.value() to_z = self._drawer.to_z.value() from_size = self._drawer.from_size.value() to_size = self._drawer.to_size.value() self.time_range = list(range(from_t, to_t + 1)) avgSize = [self._drawer.avgSizeBox.value()] cplex_timeout = None if len(str(self._drawer.timeoutBox.text())): cplex_timeout = int(self._drawer.timeoutBox.text()) withTracklets = True sizeDependent = self._drawer.sizeDepBox.isChecked() hardPrior = self._drawer.hardPriorBox.isChecked() classifierPrior = self._drawer.classifierPriorBox.isChecked() detWeight = self._drawer.detWeightBox.value() divWeight = self._drawer.divWeightBox.value() transWeight = self._drawer.transWeightBox.value() withDivisions = self._drawer.divisionsBox.isChecked() withOpticalCorrection = self._drawer.opticalBox.isChecked() withMergerResolution = self._drawer.mergerResolutionBox.isChecked() borderAwareWidth = self._drawer.bordWidthBox.value() withArmaCoordinates = True appearanceCost = self._drawer.appearanceBox.value() disappearanceCost = self._drawer.disappearanceBox.value() solverName = self._drawer.solverComboBox.currentText() ndim=3 if (to_z - from_z == 0): ndim=2 try: self.mainOperator.track( time_range = self.time_range, x_range = (from_x, to_x + 1), y_range = (from_y, to_y + 1), z_range = (from_z, to_z + 1), size_range = (from_size, to_size + 1), x_scale = self._drawer.x_scale.value(), y_scale = self._drawer.y_scale.value(), z_scale = self._drawer.z_scale.value(), maxDist=maxDist, maxObj = maxObj, divThreshold=divThreshold, avgSize=avgSize, withTracklets=withTracklets, sizeDependent=sizeDependent, detWeight=detWeight, divWeight=divWeight, transWeight=transWeight, withDivisions=withDivisions, withOpticalCorrection=withOpticalCorrection, withClassifierPrior=classifierPrior, ndim=ndim, withMergerResolution=withMergerResolution, borderAwareWidth = borderAwareWidth, withArmaCoordinates = withArmaCoordinates, cplex_timeout = cplex_timeout, appearance_cost = appearanceCost, disappearance_cost = disappearanceCost, #graph_building_parameter_changed = True, #trainingToHardConstraints = self._drawer.trainingToHardConstraints.isChecked(), max_nearest_neighbors = self._maxNearestNeighbors, solverName=solverName, progressWindow=self.progressWindow, progressVisitor=self.progressVisitor ) except Exception: self.progressWindow.onTrackDone() ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) self._criticalMessage("Exception(" + str(ex_type) + "): " + str(ex)) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested() self._drawer.TrackButton.setEnabled(True) self._drawer.exportButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(True) self._setLayerVisible("Objects", False) def _handle_failure( exc, exc_info ): self.applet.busy = False self.applet.appletStateUpdateRequested() traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during tracking. See traceback above.\n") self._drawer.TrackButton.setEnabled(True) if self.progressWindow is not None: self.progressWindow.onTrackDone() req = Request( _track ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def test(s): req = Request(someWork) req.notify_finished(callback) s2 = req.wait() time.sleep(0.001) return s2 + s
def _onTrackButtonPressed( self ): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return def _track(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() maxDist = self._drawer.maxDistBox.value() maxObj = self._drawer.maxObjectsBox.value() divThreshold = self._drawer.divThreshBox.value() from_t = self._drawer.from_time.value() to_t = self._drawer.to_time.value() from_x = self._drawer.from_x.value() to_x = self._drawer.to_x.value() from_y = self._drawer.from_y.value() to_y = self._drawer.to_y.value() from_z = self._drawer.from_z.value() to_z = self._drawer.to_z.value() from_size = self._drawer.from_size.value() to_size = self._drawer.to_size.value() self.time_range = range(from_t, to_t + 1) avgSize = [self._drawer.avgSizeBox.value()] cplex_timeout = None if len(str(self._drawer.timeoutBox.text())): cplex_timeout = int(self._drawer.timeoutBox.text()) withTracklets = self._drawer.trackletsBox.isChecked() sizeDependent = self._drawer.sizeDepBox.isChecked() hardPrior = self._drawer.hardPriorBox.isChecked() classifierPrior = self._drawer.classifierPriorBox.isChecked() divWeight = self._drawer.divWeightBox.value() transWeight = self._drawer.transWeightBox.value() withDivisions = self._drawer.divisionsBox.isChecked() withOpticalCorrection = self._drawer.opticalBox.isChecked() withMergerResolution = self._drawer.mergerResolutionBox.isChecked() borderAwareWidth = self._drawer.bordWidthBox.value() withArmaCoordinates = True appearanceCost = self._drawer.appearanceBox.value() disappearanceCost = self._drawer.disappearanceBox.value() motionModelWeight = self._drawer.motionModelWeightBox.value() solver = self._drawer.solverComboBox.currentText() ndim=3 if (to_z - from_z == 0): ndim=2 try: self.mainOperator.track( time_range = self.time_range, x_range = (from_x, to_x + 1), y_range = (from_y, to_y + 1), z_range = (from_z, to_z + 1), size_range = (from_size, to_size + 1), x_scale = self._drawer.x_scale.value(), y_scale = self._drawer.y_scale.value(), z_scale = self._drawer.z_scale.value(), maxDist=maxDist, maxObj = maxObj, divThreshold=divThreshold, avgSize=avgSize, withTracklets=withTracklets, sizeDependent=sizeDependent, divWeight=divWeight, transWeight=transWeight, withDivisions=withDivisions, withOpticalCorrection=withOpticalCorrection, withClassifierPrior=classifierPrior, ndim=ndim, withMergerResolution=withMergerResolution, borderAwareWidth = borderAwareWidth, withArmaCoordinates = withArmaCoordinates, cplex_timeout = cplex_timeout, appearance_cost = appearanceCost, disappearance_cost = disappearanceCost, motionModelWeight=motionModelWeight, force_build_hypotheses_graph = False, max_nearest_neighbors=self._drawer.maxNearestNeighborsSpinBox.value(), solverName=solver ) # update showing the merger legend, # as it might be (no longer) needed if merger resolving # is disabled(enabled) self._setMergerLegend(self.mergerLabels, self._drawer.maxObjectsBox.value()) except Exception as ex: log_exception(logger, "Error during tracking. See above error traceback.") self._criticalMessage("Error during tracking. See error log.\n\n" "Exception was:\n\n{})".format( ex )) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self.applet.progressSignal.emit(100) self._drawer.TrackButton.setEnabled(True) self._drawer.exportButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(True) self._setLayerVisible("Objects", False) def _handle_failure( exc, exc_info ): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self.applet.progressSignal.emit(100) traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during tracking. See traceback above.\n") self._drawer.TrackButton.setEnabled(True) self.applet.progressSignal.emit(0) self.applet.progressSignal.emit(-1) req = Request( _track ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def _onRunStructuredLearningButtonPressed(self, withBatchProcessing=False): numStages = 4 # creating traxel store # generating probabilities # insert energies # structured learning if WITH_HYTRA: self.progressWindow = TrackProgressDialog(parent=self, numStages=numStages) self.progressWindow.run() self.progressWindow.show() self.progressVisitor = GuiProgressVisitor( progressWindow=self.progressWindow) else: self.progressWindow = None self.progressVisitor = DefaultProgressVisitor() def _learn(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() try: self.topLevelOperatorView._runStructuredLearning( (self._drawer.from_z.value(), self._drawer.to_z.value()), self._maxNumObj, self._maxNearestNeighbors, self._drawer.maxDistBox.value(), self._drawer.divThreshBox.value(), (self._drawer.x_scale.value(), self._drawer.y_scale.value(), self._drawer.z_scale.value()), (self._drawer.from_size.value(), self._drawer.to_size.value()), self._drawer.divisionsBox.isChecked(), self._drawer.bordWidthBox.value(), self._drawer.classifierPriorBox.isChecked(), withBatchProcessing, progressWindow=self.progressWindow, progressVisitor=self.progressVisitor) except Exception: ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) self._criticalMessage("Exception(" + str(ex_type) + "): " + str(ex)) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() def _handle_failure(exc, exc_info): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() traceback.print_exception(*exc_info) sys.stderr.write( "Exception raised during learning. See traceback above.\n") if self.progressWindow is not None: self.progressWindow.onTrackDone() req = Request(_learn) req.notify_failed(_handle_failure) req.notify_finished(_handle_finished) req.submit()
def _onExportButtonPressed(self): options = QFileDialog.Options() if ilastik_config.getboolean("ilastik", "debug"): options |= QFileDialog.DontUseNativeDialog directory = encode_from_qstring( QFileDialog.getExistingDirectory(self, 'Select Directory', os.path.expanduser("~"), options=options)) if directory is None or len(str(directory)) == 0: logger.info("cancelled.") return def _handle_progress(x): self.applet.progressSignal.emit(x) def _export(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() t_from = None # determine from_time (it could has been changed in the GUI meanwhile) for t_from, label2color_at in enumerate( self.mainOperator.label2color): if len(label2color_at) == 0: continue else: break if t_from is None: self._criticalMessage("There is nothing to export.") return t_from = int(t_from) logger.info("Saving first label image...") key = [] for idx, flag in enumerate( axisTagsToString( self.mainOperator.LabelImage.meta.axistags)): if flag is 't': key.append(slice(t_from, t_from + 1)) elif flag is 'c': key.append(slice(0, 1)) else: key.append( slice(0, self.mainOperator.LabelImage.meta.shape[idx])) roi = SubRegion(self.mainOperator.LabelImage, key) labelImage = self.mainOperator.LabelImage.get(roi).wait() labelImage = labelImage[0, ..., 0] try: # write_events([], str(directory), t_from, labelImage) events = self.mainOperator.EventsVector.value logger.info("Saving events...") logger.info("Length of events " + str(len(events))) num_files = float(len(events)) for i in sorted(events.keys()): events_at = events[i] i = int(i) t = t_from + i key[0] = slice(t, t + 1) roi = SubRegion(self.mainOperator.LabelImage, key) labelImage = self.mainOperator.LabelImage.get(roi).wait() labelImage = labelImage[0, ..., 0] if self.withMergers: write_events(events_at, str(directory), t, labelImage, self.mainOperator.mergers) else: write_events(events_at, str(directory), t, labelImage) _handle_progress(i / num_files * 100) except IOError as e: self._criticalMessage("Cannot export the tracking results. Maybe these files already exist. "\ "Please delete them or choose a different directory.") return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self._drawer.exportButton.setEnabled(True) self.applet.progressSignal.emit(100) def _handle_failure(exc, exc_info): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() msg = "Exception raised during export. See traceback above.\n" log_exception(logger, msg, exc_info=exc_info) self.applet.progressSignal.emit(100) self._drawer.exportButton.setEnabled(True) self._drawer.exportButton.setEnabled(False) self.applet.progressSignal.emit(0) req = Request(_export) req.notify_failed(_handle_failure) req.notify_finished(_handle_finished) req.submit()
def _onExportButtonPressed(self): options = QFileDialog.Options() if ilastik_config.getboolean("ilastik", "debug"): options |= QFileDialog.DontUseNativeDialog directory = QFileDialog.getExistingDirectory(self, 'Select Directory', os.getenv('HOME'), options=options) if directory is None or len(str(directory)) == 0: print "cancelled." return def _handle_progress(x): self.applet.progressSignal.emit(x) def _export(): t_from = None # determine from_time (it could has been changed in the GUI meanwhile) for t_from, label2color_at in enumerate( self.mainOperator.label2color): if len(label2color_at) == 0: continue else: break if t_from == None: return print "Saving first label image..." key = [] for idx, flag in enumerate( axisTagsToString( self.mainOperator.LabelImage.meta.axistags)): if flag is 't': key.append(slice(t_from, t_from + 1)) elif flag is 'c': key.append(slice(0, 1)) else: key.append( slice(0, self.mainOperator.LabelImage.meta.shape[idx])) roi = SubRegion(self.mainOperator.LabelImage, key) labelImage = self.mainOperator.LabelImage.get(roi).wait() labelImage = labelImage[0, ..., 0] try: write_events([], str(directory), t_from, labelImage) events = self.mainOperator.events print "Saving events..." print "Length of events " + str(len(events)) num_files = float(len(events)) for i, events_at in enumerate(events): t = t_from + i key[0] = slice(t + 1, t + 2) roi = SubRegion(self.mainOperator.LabelImage, key) labelImage = self.mainOperator.LabelImage.get(roi).wait() labelImage = labelImage[0, ..., 0] if self.withMergers: write_events(events_at, str(directory), t + 1, labelImage, self.mainOperator.mergers) else: write_events(events_at, str(directory), t + 1, labelImage) _handle_progress(i / num_files * 100) except IOError as e: self._criticalMessage("Cannot export the tracking results. Maybe these files already exist. "\ "Please delete them or choose a different directory.") return def _handle_finished(*args): self._drawer.exportButton.setEnabled(True) self.applet.progressSignal.emit(100) def _handle_failure(exc, exc_info): import traceback, sys traceback.print_exception(*exc_info) sys.stderr.write( "Exception raised during export. See traceback above.\n") self.applet.progressSignal.emit(100) self._drawer.exportButton.setEnabled(True) self._drawer.exportButton.setEnabled(False) self.applet.progressSignal.emit(0) req = Request(_export) req.notify_failed(_handle_failure) req.notify_finished(_handle_finished) req.submit()
def _onTrackButtonPressed(self): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return withMergerResolution = self._drawer.mergerResolutionBox.isChecked() withTracklets = True numStages = 6 # creating traxel store # generating probabilities # insert energies # convexify costs # solver # compute lineages if withMergerResolution: numStages += 1 # merger resolution if withTracklets: numStages += 3 # initializing tracklet graph, finding tracklets, contracting edges in tracklet graph if WITH_HYTRA: self.progressWindow = TrackProgressDialog(parent=self, numStages=numStages) self.progressWindow.run() self.progressWindow.show() self.progressVisitor = GuiProgressVisitor( progressWindow=self.progressWindow) else: self.progressWindow = None self.progressVisitor = DefaultProgressVisitor() def _track(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() maxDist = self._drawer.maxDistBox.value() maxObj = self._drawer.maxObjectsBox.value() divThreshold = self._drawer.divThreshBox.value() from_t = self._drawer.from_time.value() to_t = self._drawer.to_time.value() from_x = self._drawer.from_x.value() to_x = self._drawer.to_x.value() from_y = self._drawer.from_y.value() to_y = self._drawer.to_y.value() from_z = self._drawer.from_z.value() to_z = self._drawer.to_z.value() from_size = self._drawer.from_size.value() to_size = self._drawer.to_size.value() self.time_range = range(from_t, to_t + 1) avgSize = [self._drawer.avgSizeBox.value()] cplex_timeout = None if len(str(self._drawer.timeoutBox.text())): cplex_timeout = int(self._drawer.timeoutBox.text()) withTracklets = True sizeDependent = self._drawer.sizeDepBox.isChecked() hardPrior = self._drawer.hardPriorBox.isChecked() classifierPrior = self._drawer.classifierPriorBox.isChecked() detWeight = self._drawer.detWeightBox.value() divWeight = self._drawer.divWeightBox.value() transWeight = self._drawer.transWeightBox.value() withDivisions = self._drawer.divisionsBox.isChecked() withOpticalCorrection = self._drawer.opticalBox.isChecked() withMergerResolution = self._drawer.mergerResolutionBox.isChecked() borderAwareWidth = self._drawer.bordWidthBox.value() withArmaCoordinates = True appearanceCost = self._drawer.appearanceBox.value() disappearanceCost = self._drawer.disappearanceBox.value() solverName = self._drawer.solverComboBox.currentText() ndim = 3 if (to_z - from_z == 0): ndim = 2 try: self.mainOperator.track( time_range=self.time_range, x_range=(from_x, to_x + 1), y_range=(from_y, to_y + 1), z_range=(from_z, to_z + 1), size_range=(from_size, to_size + 1), x_scale=self._drawer.x_scale.value(), y_scale=self._drawer.y_scale.value(), z_scale=self._drawer.z_scale.value(), maxDist=maxDist, maxObj=maxObj, divThreshold=divThreshold, avgSize=avgSize, withTracklets=withTracklets, sizeDependent=sizeDependent, detWeight=detWeight, divWeight=divWeight, transWeight=transWeight, withDivisions=withDivisions, withOpticalCorrection=withOpticalCorrection, withClassifierPrior=classifierPrior, ndim=ndim, withMergerResolution=withMergerResolution, borderAwareWidth=borderAwareWidth, withArmaCoordinates=withArmaCoordinates, cplex_timeout=cplex_timeout, appearance_cost=appearanceCost, disappearance_cost=disappearanceCost, #graph_building_parameter_changed = True, #trainingToHardConstraints = self._drawer.trainingToHardConstraints.isChecked(), max_nearest_neighbors=self._maxNearestNeighbors, solverName=solverName, progressWindow=self.progressWindow, progressVisitor=self.progressVisitor) except Exception: ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) self._criticalMessage("Exception(" + str(ex_type) + "): " + str(ex)) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self._drawer.TrackButton.setEnabled(True) self._drawer.exportButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(True) self._setLayerVisible("Objects", False) def _handle_failure(exc, exc_info): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() traceback.print_exception(*exc_info) sys.stderr.write( "Exception raised during tracking. See traceback above.\n") self._drawer.TrackButton.setEnabled(True) if self.progressWindow is not None: self.progressWindow.onTrackDone() req = Request(_track) req.notify_failed(_handle_failure) req.notify_finished(_handle_finished) req.submit()
def _onRunStructuredLearningButtonPressed(self, withBatchProcessing=False): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return numStages = 6 # object features # detection probabilities # creating traxel store # generating probabilities # insert energies # structured learning if self._drawer.divisionsBox.isChecked(): # division probabilities numStages +=1 self.progressWindow = TrackProgressDialog(parent=self,numStages=numStages) self.progressWindow.run() self.progressWindow.show() self.progressVisitor = GuiProgressVisitor(progressWindow=self.progressWindow) def _learn(): self.applet.busy = True self.applet.appletStateUpdateRequested() try: self.topLevelOperatorView._runStructuredLearning( (self._drawer.from_z.value(),self._drawer.to_z.value()), self._maxNumObj, self._maxNearestNeighbors, self._drawer.maxDistBox.value(), self._drawer.divThreshBox.value(), (self._drawer.x_scale.value(), self._drawer.y_scale.value(), self._drawer.z_scale.value()), (self._drawer.from_size.value(), self._drawer.to_size.value()), self._drawer.divisionsBox.isChecked(), self._drawer.bordWidthBox.value(), self._drawer.classifierPriorBox.isChecked(), withBatchProcessing, progressWindow=self.progressWindow, progressVisitor=self.progressVisitor ) except Exception: ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) self._criticalMessage("Exception(" + str(ex_type) + "): " + str(ex)) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested() def _handle_failure( exc, exc_info ): self.applet.busy = False self.applet.appletStateUpdateRequested() traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during learning. See traceback above.\n") if self.progressWindow is not None: self.progressWindow.onTrackDone() req = Request( _learn ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def _onExportButtonPressed(self): options = QFileDialog.Options() if ilastik_config.getboolean("ilastik", "debug"): options |= QFileDialog.DontUseNativeDialog directory = encode_from_qstring(QFileDialog.getExistingDirectory(self, 'Select Directory',os.path.expanduser("~"), options=options)) if directory is None or len(str(directory)) == 0: logger.info( "cancelled." ) return def _handle_progress(x): self.applet.progressSignal.emit(x) def _export(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() if hasattr(self.mainOperator,"RelabeledImage"): labelImageSlot = self.mainOperator.RelabeledImage else: labelImageSlot = self.mainOperator.LabelImage logger.info( "Saving first label image..." ) key = [] for idx, flag in enumerate(axisTagsToString(labelImageSlot.meta.axistags)): if flag is 't': key.append(slice(0,labelImageSlot.meta.shape[idx]))#slice(t_from,t_from+1)) elif flag is 'c': key.append(slice(0,1)) else: key.append(slice(0,labelImageSlot.meta.shape[idx])) try: events = self.mainOperator.EventsVector.value logger.info( "Saving events..." ) logger.info( "Length of events " + str(len(events)) ) num_files = float(len(events)) for i in sorted(events.keys()): events_at = events[i] i = int(i) t = i key[0] = slice(t,t+1) roi = SubRegion(labelImageSlot, key) labelImage = labelImageSlot.get(roi).wait() labelImage = labelImage[0,...,0] write_events(events_at, str(directory), t, labelImage) _handle_progress(i/num_files * 100) except IOError as e: self._criticalMessage("Cannot export the tracking results. Maybe these files already exist. "\ "Please delete them or choose a different directory.") return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self._drawer.exportButton.setEnabled(True) self.applet.progressSignal.emit(100) def _handle_failure( exc, exc_info ): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() msg = "Exception raised during export. See traceback above.\n" log_exception( logger, msg, exc_info=exc_info ) self.applet.progressSignal.emit(100) self._drawer.exportButton.setEnabled(True) self._drawer.exportButton.setEnabled(False) self.applet.progressSignal.emit(0) req = Request( _export ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def _onTrackButtonPressed( self ): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return def _track(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() maxDist = self._drawer.maxDistBox.value() maxObj = self._drawer.maxObjectsBox.value() divThreshold = self._drawer.divThreshBox.value() from_t = self._drawer.from_time.value() to_t = self._drawer.to_time.value() from_x = self._drawer.from_x.value() to_x = self._drawer.to_x.value() from_y = self._drawer.from_y.value() to_y = self._drawer.to_y.value() from_z = self._drawer.from_z.value() to_z = self._drawer.to_z.value() from_size = self._drawer.from_size.value() to_size = self._drawer.to_size.value() self.time_range = range(from_t, to_t + 1) avgSize = [self._drawer.avgSizeBox.value()] cplex_timeout = None if len(str(self._drawer.timeoutBox.text())): cplex_timeout = int(self._drawer.timeoutBox.text()) withTracklets = self._drawer.trackletsBox.isChecked() sizeDependent = self._drawer.sizeDepBox.isChecked() hardPrior = self._drawer.hardPriorBox.isChecked() classifierPrior = self._drawer.classifierPriorBox.isChecked() divWeight = self._drawer.divWeightBox.value() transWeight = self._drawer.transWeightBox.value() withDivisions = self._drawer.divisionsBox.isChecked() withOpticalCorrection = self._drawer.opticalBox.isChecked() withMergerResolution = self._drawer.mergerResolutionBox.isChecked() borderAwareWidth = self._drawer.bordWidthBox.value() withArmaCoordinates = True appearanceCost = self._drawer.appearanceBox.value() disappearanceCost = self._drawer.disappearanceBox.value() ndim=3 if (to_z - from_z == 0): ndim=2 try: self.mainOperator.track( time_range = self.time_range, x_range = (from_x, to_x + 1), y_range = (from_y, to_y + 1), z_range = (from_z, to_z + 1), size_range = (from_size, to_size + 1), x_scale = self._drawer.x_scale.value(), y_scale = self._drawer.y_scale.value(), z_scale = self._drawer.z_scale.value(), maxDist=maxDist, maxObj = maxObj, divThreshold=divThreshold, avgSize=avgSize, withTracklets=withTracklets, sizeDependent=sizeDependent, divWeight=divWeight, transWeight=transWeight, withDivisions=withDivisions, withOpticalCorrection=withOpticalCorrection, withClassifierPrior=classifierPrior, ndim=ndim, withMergerResolution=withMergerResolution, borderAwareWidth = borderAwareWidth, withArmaCoordinates = withArmaCoordinates, cplex_timeout = cplex_timeout, appearance_cost = appearanceCost, disappearance_cost = disappearanceCost ) except Exception: ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) self._criticalMessage("Exception(" + str(ex_type) + "): " + str(ex)) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self.applet.progressSignal.emit(100) self._drawer.TrackButton.setEnabled(True) self._drawer.exportButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(True) self._setLayerVisible("Objects", False) def _handle_failure( exc, exc_info ): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self.applet.progressSignal.emit(100) traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during tracking. See traceback above.\n") self._drawer.TrackButton.setEnabled(True) self.applet.progressSignal.emit(0) self.applet.progressSignal.emit(-1) req = Request( _track ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def _onTrackButtonPressed(self): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return def _track(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() maxDist = self._drawer.maxDistBox.value() maxObj = self._drawer.maxObjectsBox.value() divThreshold = self._drawer.divThreshBox.value() from_t = self._drawer.from_time.value() to_t = self._drawer.to_time.value() from_x = self._drawer.from_x.value() to_x = self._drawer.to_x.value() from_y = self._drawer.from_y.value() to_y = self._drawer.to_y.value() from_z = self._drawer.from_z.value() to_z = self._drawer.to_z.value() from_size = self._drawer.from_size.value() to_size = self._drawer.to_size.value() self.time_range = range(from_t, to_t + 1) avgSize = [self._drawer.avgSizeBox.value()] cplex_timeout = None if len(str(self._drawer.timeoutBox.text())): cplex_timeout = int(self._drawer.timeoutBox.text()) withTracklets = self._drawer.trackletsBox.isChecked() sizeDependent = self._drawer.sizeDepBox.isChecked() hardPrior = self._drawer.hardPriorBox.isChecked() classifierPrior = self._drawer.classifierPriorBox.isChecked() divWeight = self._drawer.divWeightBox.value() transWeight = self._drawer.transWeightBox.value() withDivisions = self._drawer.divisionsBox.isChecked() withOpticalCorrection = self._drawer.opticalBox.isChecked() withMergerResolution = self._drawer.mergerResolutionBox.isChecked() borderAwareWidth = self._drawer.bordWidthBox.value() withArmaCoordinates = True appearanceCost = self._drawer.appearanceBox.value() disappearanceCost = self._drawer.disappearanceBox.value() ndim = 3 if (to_z - from_z == 0): ndim = 2 try: self.mainOperator.track( time_range=self.time_range, x_range=(from_x, to_x + 1), y_range=(from_y, to_y + 1), z_range=(from_z, to_z + 1), size_range=(from_size, to_size + 1), x_scale=self._drawer.x_scale.value(), y_scale=self._drawer.y_scale.value(), z_scale=self._drawer.z_scale.value(), maxDist=maxDist, maxObj=maxObj, divThreshold=divThreshold, avgSize=avgSize, withTracklets=withTracklets, sizeDependent=sizeDependent, divWeight=divWeight, transWeight=transWeight, withDivisions=withDivisions, withOpticalCorrection=withOpticalCorrection, withClassifierPrior=classifierPrior, ndim=ndim, withMergerResolution=withMergerResolution, borderAwareWidth=borderAwareWidth, withArmaCoordinates=withArmaCoordinates, cplex_timeout=cplex_timeout, appearance_cost=appearanceCost, disappearance_cost=disappearanceCost) except Exception: ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) self._criticalMessage("Exception(" + str(ex_type) + "): " + str(ex)) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self.applet.progressSignal.emit(100) self._drawer.TrackButton.setEnabled(True) self._drawer.exportButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(True) self._setLayerVisible("Objects", False) def _handle_failure(exc, exc_info): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self.applet.progressSignal.emit(100) traceback.print_exception(*exc_info) sys.stderr.write( "Exception raised during tracking. See traceback above.\n") self._drawer.TrackButton.setEnabled(True) self.applet.progressSignal.emit(0) self.applet.progressSignal.emit(-1) req = Request(_track) req.notify_failed(_handle_failure) req.notify_finished(_handle_finished) req.submit()
def _onTrackButtonPressed( self ): if not self.mainOperator.ObjectFeatures.ready(): self._criticalMessage("You have to compute object features first.") return def _track(): self.applet.busy = True self.applet.appletStateUpdateRequested.emit() maxDist = self._drawer.maxDistBox.value() maxObj = self._drawer.maxObjectsBox.value() divThreshold = self._drawer.divThreshBox.value() from_t = self._drawer.from_time.value() to_t = self._drawer.to_time.value() from_x = self._drawer.from_x.value() to_x = self._drawer.to_x.value() from_y = self._drawer.from_y.value() to_y = self._drawer.to_y.value() from_z = self._drawer.from_z.value() to_z = self._drawer.to_z.value() from_size = self._drawer.from_size.value() to_size = self._drawer.to_size.value() self.time_range = range(from_t, to_t + 1) avgSize = [self._drawer.avgSizeBox.value()] cplex_timeout = None if len(str(self._drawer.timeoutBox.text())): cplex_timeout = int(self._drawer.timeoutBox.text()) withTracklets = self._drawer.trackletsBox.isChecked() sizeDependent = self._drawer.sizeDepBox.isChecked() hardPrior = self._drawer.hardPriorBox.isChecked() classifierPrior = self._drawer.classifierPriorBox.isChecked() divWeight = self._drawer.divWeightBox.value() transWeight = self._drawer.transWeightBox.value() withDivisions = self._drawer.divisionsBox.isChecked() withOpticalCorrection = self._drawer.opticalBox.isChecked() withMergerResolution = self._drawer.mergerResolutionBox.isChecked() borderAwareWidth = self._drawer.bordWidthBox.value() withArmaCoordinates = True appearanceCost = self._drawer.appearanceBox.value() disappearanceCost = self._drawer.disappearanceBox.value() motionModelWeight = self._drawer.motionModelWeightBox.value() solver = self._drawer.solverComboBox.currentText() ndim=3 if (to_z - from_z == 0): ndim=2 try: self.mainOperator.track( time_range = self.time_range, x_range = (from_x, to_x + 1), y_range = (from_y, to_y + 1), z_range = (from_z, to_z + 1), size_range = (from_size, to_size + 1), x_scale = self._drawer.x_scale.value(), y_scale = self._drawer.y_scale.value(), z_scale = self._drawer.z_scale.value(), maxDist=maxDist, maxObj = maxObj, divThreshold=divThreshold, avgSize=avgSize, withTracklets=withTracklets, sizeDependent=sizeDependent, detWeight=10.0, divWeight=divWeight, transWeight=transWeight, withDivisions=withDivisions, withOpticalCorrection=withOpticalCorrection, withClassifierPrior=classifierPrior, ndim=ndim, withMergerResolution=withMergerResolution, borderAwareWidth =borderAwareWidth, withArmaCoordinates =withArmaCoordinates, cplex_timeout =cplex_timeout, appearance_cost =appearanceCost, disappearance_cost =disappearanceCost, motionModelWeight=motionModelWeight, force_build_hypotheses_graph =False, max_nearest_neighbors=self._drawer.maxNearestNeighborsSpinBox.value(), numFramesPerSplit=self._drawer.numFramesPerSplitSpinBox.value(), solverName=solver ) except Exception as ex: log_exception(logger, "Error during tracking. See above error traceback.") self._criticalMessage("Error during tracking. See error log.\n\n" "Exception was:\n\n{})".format( ex )) return def _handle_finished(*args): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() self._drawer.TrackButton.setEnabled(True) self._drawer.exportButton.setEnabled(True) self._drawer.exportTifButton.setEnabled(True) self._setLayerVisible("Objects", False) # update showing the merger legend, # as it might be (no longer) needed if merger resolving # is disabled(enabled) self._setMergerLegend(self.mergerLabels, self._drawer.maxObjectsBox.value()) def _handle_failure( exc, exc_info ): self.applet.busy = False self.applet.appletStateUpdateRequested.emit() traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during tracking. See traceback above.\n") self._drawer.TrackButton.setEnabled(True) req = Request( _track ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()
def _onExportButtonPressed(self): options = QFileDialog.Options() if ilastik_config.getboolean("ilastik", "debug"): options |= QFileDialog.DontUseNativeDialog directory = QFileDialog.getExistingDirectory(self, 'Select Directory',os.getenv('HOME'), options=options) if directory is None or len(str(directory)) == 0: print "cancelled." return def _handle_progress(x): self.applet.progressSignal.emit(x) def _export(): t_from = None # determine from_time (it could has been changed in the GUI meanwhile) for t_from, label2color_at in enumerate(self.mainOperator.label2color): if len(label2color_at) == 0: continue else: break if t_from == None: return print "Saving first label image..." key = [] for idx, flag in enumerate(axisTagsToString(self.mainOperator.LabelImage.meta.axistags)): if flag is 't': key.append(slice(t_from,t_from+1)) elif flag is 'c': key.append(slice(0,1)) else: key.append(slice(0,self.mainOperator.LabelImage.meta.shape[idx])) roi = SubRegion(self.mainOperator.LabelImage, key) labelImage = self.mainOperator.LabelImage.get(roi).wait() labelImage = labelImage[0,...,0] try: write_events([], str(directory), t_from, labelImage) events = self.mainOperator.events print "Saving events..." print "Length of events " + str(len(events)) num_files = float(len(events)) for i, events_at in enumerate(events): t = t_from + i key[0] = slice(t+1,t+2) roi = SubRegion(self.mainOperator.LabelImage, key) labelImage = self.mainOperator.LabelImage.get(roi).wait() labelImage = labelImage[0,...,0] if self.withMergers: write_events(events_at, str(directory), t+1, labelImage, self.mainOperator.mergers) else: write_events(events_at, str(directory), t+1, labelImage) _handle_progress(i/num_files * 100) except IOError as e: self._criticalMessage("Cannot export the tracking results. Maybe these files already exist. "\ "Please delete them or choose a different directory.") return def _handle_finished(*args): self._drawer.exportButton.setEnabled(True) self.applet.progressSignal.emit(100) def _handle_failure( exc, exc_info ): import traceback, sys traceback.print_exception(*exc_info) sys.stderr.write("Exception raised during export. See traceback above.\n") self.applet.progressSignal.emit(100) self._drawer.exportButton.setEnabled(True) self._drawer.exportButton.setEnabled(False) self.applet.progressSignal.emit(0) req = Request( _export ) req.notify_failed( _handle_failure ) req.notify_finished( _handle_finished ) req.submit()