def testWorkerThreadLoopProtection(self): """ The worker threads should not die due to an exception raised within a request. """ for worker in Request.global_thread_pool.workers: assert worker.is_alive(), "Something is wrong with this test. All workers should be alive." def always_fails(): raise Exception() req = Request(always_fails) try: req.submit() except: if Request.global_thread_pool.num_workers > 0: raise else: if Request.global_thread_pool.num_workers == 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" try: req.wait() except: pass else: if Request.global_thread_pool.num_workers > 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" for worker in Request.global_thread_pool.workers: assert worker.is_alive(), "An exception was propagated to a worker run loop!"
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 getBigArray(directExecute, recursionDepth): """ Simulate the memory footprint of a series of computation steps. """ logger.debug("Usage delta before depth {}: {}".format( recursionDepth, getMemoryIncrease())) if recursionDepth == 0: # A 500MB result result = numpy.zeros(shape=resultShape, dtype=numpy.uint8) else: req = Request( partial(getBigArray, directExecute=directExecute, recursionDepth=recursionDepth - 1)) if not directExecute: # Force this request to be submitted to the thread pool, # not executed synchronously in this thread. req.submit() result = req.wait() + 1 # Note that we expect there to be 2X memory usage here: # 1x for our result and 1x for the child, which hasn't been cleaned up yet. memory_increase = getMemoryIncrease() logger.debug("Usage delta after depth {}: {}".format( recursionDepth, memory_increase)) assert memory_increase < 2.5 * resultSize, "Memory from finished requests didn't get freed!" return result
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 testWorkerThreadLoopProtection(self): """ The worker threads should not die due to an exception raised within a request. """ for worker in Request.global_thread_pool.workers: assert worker.is_alive( ), "Something is wrong with this test. All workers should be alive." def always_fails(): raise Exception() req = Request(always_fails) try: req.submit() except: if Request.global_thread_pool.num_workers > 0: raise else: if Request.global_thread_pool.num_workers == 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" try: req.wait() except: pass else: if Request.global_thread_pool.num_workers > 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" for worker in Request.global_thread_pool.workers: assert worker.is_alive( ), "An exception was propagated to a worker run loop!"
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_submit_should_assign_worker_and_execute(self): def work(): return 42 req = Request(work) req.submit() assert req.wait() == 42 assert req.assigned_worker in Request.global_thread_pool.workers
def testRequestLock(self): """ Test the special Request-aware lock. Launch 99 requests and threads that all must fight over access to the same list. The list will eventually be 0,1,2...99, and each request will append a single number to the list. Each request must wait its turn before it can append it's number and finish. """ # This test doesn't work if the request system is working in single-threaded 'debug' mode. # It depends on concurrent execution to make progress. Otherwise it hangs. if Request.global_thread_pool.num_workers == 0: raise nose.SkipTest req_lock = RequestLock() l = [0] def append_n(n): #print "Starting append_{}\n".format(n) while True: with req_lock: if l[-1] == n - 1: #print "***** Appending {}".format(n) l.append(n) return # Create 50 requests N = 50 reqs = [] for i in range(1, 2 * N, 2): req = Request(partial(append_n, i)) reqs.append(req) # Create 49 threads thrds = [] for i in range(2, 2 * N, 2): thrd = threading.Thread(target=partial(append_n, i)) thrds.append(thrd) # Submit in reverse order to ensure that no request finishes until they have all been started. # This proves that the requests really are being suspended. for req in reversed(reqs): req.submit() # Start all the threads for thrd in reversed(thrds): thrd.start() # All requests must finish for req in reqs: req.wait() # All threads should finish for thrd in thrds: thrd.join() assert l == list( range(100)), "Requests and/or threads finished in the wrong order!"
def test_failed_request2(self): """ A request is "failed" if it throws an exception while executing. The exception should be forwarded to ALL waiting requests, which should re-raise it. """ class CustomRuntimeError(RuntimeError): pass def impossible_workload(): time.sleep(0.2) raise CustomRuntimeError("Can't service your request") impossible_req = Request(impossible_workload) def wait_for_impossible(): # This request will fail... impossible_req.wait() # Since there are some exception guards in the code we're testing, # spit something out to stderr just to be sure this error # isn't getting swallowed accidentally. sys.stderr.write("ERROR: Shouldn't get here.") assert False, "Shouldn't get here." req1 = Request(wait_for_impossible) req2 = Request(wait_for_impossible) failed_ids = [] lock = threading.Lock() def handle_failed_req(req_id, failure_exc, exc_info): assert isinstance(failure_exc, CustomRuntimeError) with lock: failed_ids.append(req_id) req1.notify_failed( partial(handle_failed_req, 1) ) req2.notify_failed( partial(handle_failed_req, 2) ) req1.submit() req2.submit() try: req1.wait() except RuntimeError: pass else: assert False, "Expected an exception from that request, but didn't get it." try: req2.wait() except RuntimeError: pass else: assert False, "Expected an exception from that request, but didn't get it." assert 1 in failed_ids assert 2 in failed_ids
def test_failed_request2(self): """ A request is "failed" if it throws an exception while executing. The exception should be forwarded to ALL waiting requests, which should re-raise it. """ class CustomRuntimeError(RuntimeError): pass def impossible_workload(): time.sleep(0.2) raise CustomRuntimeError("Can't service your request") impossible_req = Request(impossible_workload) def wait_for_impossible(): # This request will fail... impossible_req.wait() # Since there are some exception guards in the code we're testing, # spit something out to stderr just to be sure this error # isn't getting swallowed accidentally. sys.stderr.write("ERROR: Shouldn't get here.") assert False, "Shouldn't get here." req1 = Request(wait_for_impossible) req2 = Request(wait_for_impossible) failed_ids = [] lock = threading.Lock() def handle_failed_req(req_id, failure_exc, exc_info): assert isinstance(failure_exc, CustomRuntimeError) with lock: failed_ids.append(req_id) req1.notify_failed(partial(handle_failed_req, 1)) req2.notify_failed(partial(handle_failed_req, 2)) req1.submit() req2.submit() try: req1.wait() except RuntimeError: pass else: assert False, "Expected an exception from that request, but didn't get it." try: req2.wait() except RuntimeError: pass else: assert False, "Expected an exception from that request, but didn't get it." assert 1 in failed_ids assert 2 in failed_ids
def test_submit_dependent_requests_should_execute_on_same_worker(self): more_work = Request(Work(lambda: 42)) req = Request(Work(lambda: more_work.wait())) req.submit() assert req.wait() == 42 assert req.assigned_worker in Request.global_thread_pool.workers assert req.assigned_worker == more_work.assigned_worker
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 testRequestLock(self): """ Test the special Request-aware lock. Launch 99 requests and threads that all must fight over access to the same list. The list will eventually be 0,1,2...99, and each request will append a single number to the list. Each request must wait its turn before it can append it's number and finish. """ # This test doesn't work if the request system is working in single-threaded 'debug' mode. # It depends on concurrent execution to make progress. Otherwise it hangs. if Request.global_thread_pool.num_workers == 0: raise nose.SkipTest req_lock = RequestLock() l = [0] def append_n(n): #print "Starting append_{}\n".format(n) while True: with req_lock: if l[-1] == n-1: #print "***** Appending {}".format(n) l.append(n) return # Create 50 requests N = 50 reqs = [] for i in range(1,2*N,2): req = Request( partial(append_n, i) ) reqs.append(req) # Create 49 threads thrds = [] for i in range(2,2*N,2): thrd = threading.Thread( target=partial(append_n, i) ) thrds.append(thrd) # Submit in reverse order to ensure that no request finishes until they have all been started. # This proves that the requests really are being suspended. for req in reversed(reqs): req.submit() # Start all the threads for thrd in reversed(thrds): thrd.start() # All requests must finish for req in reqs: req.wait() # All threads should finish for thrd in thrds: thrd.join() assert l == list(range(100)), "Requests and/or threads finished in the wrong order!"
def test_should_be_called_after_request_finishes(self): cb = mock.Mock() req = Request(lambda: 42) req.add_done_callback(cb) cb.assert_not_called() req.submit() req.wait() cb.assert_called_once_with(req)
def test_if_request_finished_should_call_immidiatelly(self): cb = mock.Mock() def work(): return 42 req = Request(work) req.submit() req.wait() req.add_done_callback(cb) cb.assert_called_once_with(req)
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 testSimpleRequestCondition(self): """ Test the SimpleRequestCondition, which is like threading.Condition, but with a subset of the functionality. (See the docs for details.) """ Request.reset_thread_pool(num_workers=1) N_ELEMENTS = 10 # It's tempting to simply use threading.Condition here, # but that doesn't quite work if the thread calling wait() is also a worker thread. # (threading.Condition uses threading.Lock() as it's 'waiter' lock, which blocks the entire worker.) # cond = threading.Condition( RequestLock() ) cond = SimpleRequestCondition() produced = [] consumed = [] def wait_for_all(): def f(i): time.sleep(0.2 * random.random()) with cond: produced.append(i) cond.notify() reqs = [] for i in range(N_ELEMENTS): req = Request(partial(f, i)) reqs.append(req) for req in reqs: req.submit() _consumed = consumed with cond: while len(_consumed) < N_ELEMENTS: while len(_consumed) == len(produced): cond.wait() logger.debug("copying {} elements".format( len(produced) - len(consumed))) _consumed += produced[len(_consumed):] # Force the request to run in a worker thread. # This should catch failures that can occur if the Condition's "waiter" lock isn't a request lock. req = Request(wait_for_all) req.submit() # Now block for completion req.wait() logger.debug("produced: {}".format(produced)) logger.debug("consumed: {}".format(consumed)) assert set(consumed) == set( range(N_ELEMENTS) ), "Expected set(range(N_ELEMENTS)), got {}".format(consumed)
def test_if_request_has_been_cancelled_callback_should_still_be_called(self): cb = mock.Mock() req = Request(lambda: 42) req.cancel() req.add_done_callback(cb) req.submit() with pytest.raises(Request.InvalidRequestException): req.wait() cb.assert_called_once_with(req)
def testSimpleRequestCondition(self): """ Test the SimpleRequestCondition, which is like threading.Condition, but with a subset of the functionality. (See the docs for details.) """ num_workers = Request.global_thread_pool.num_workers Request.reset_thread_pool(num_workers=1) N_ELEMENTS = 10 # It's tempting to simply use threading.Condition here, # but that doesn't quite work if the thread calling wait() is also a worker thread. # (threading.Condition uses threading.Lock() as it's 'waiter' lock, which blocks the entire worker.) # cond = threading.Condition( RequestLock() ) cond = SimpleRequestCondition() produced = [] consumed = [] def wait_for_all(): def f(i): time.sleep(0.2*random.random()) with cond: produced.append(i) cond.notify() reqs = [] for i in range(N_ELEMENTS): req = Request( partial(f, i) ) reqs.append( req ) for req in reqs: req.submit() _consumed = consumed with cond: while len(_consumed) < N_ELEMENTS: while len(_consumed) == len(produced): cond.wait() logger.debug( "copying {} elements".format( len(produced) - len(consumed) ) ) _consumed += produced[len(_consumed):] # Force the request to run in a worker thread. # This should catch failures that can occur if the Condition's "waiter" lock isn't a request lock. req = Request( wait_for_all ) req.submit() # Now block for completion req.wait() logger.debug( "produced: {}".format(produced) ) logger.debug( "consumed: {}".format(consumed) ) assert set(consumed) == set( range(N_ELEMENTS) ), "Expected set(range(N_ELEMENTS)), got {}".format( consumed ) Request.reset_thread_pool(num_workers)
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_signal_failed_should_be_called_on_exception(self, broken_fn): work, req = self.work_req(broken_fn) recv = mock.Mock() req = Request(work) req.notify_failed(recv) req.submit() with pytest.raises(TExc): assert req.wait() == 42 recv.assert_called_once() assert isinstance(recv.call_args[0][0], TExc)
def testRequestLock(self): """ Test the special Request-aware lock. Launch 99 requests and threads that all must fight over access to the same list. The list will eventually be 0,1,2...99, and each request will append a single number to the list. Each request must wait its turn before it can append it's number and finish. """ req_lock = RequestLock() l = [0] def append_n(n): #print "Starting append_{}\n".format(n) while True: with req_lock: if l[-1] == n - 1: #print "***** Appending {}".format(n) l.append(n) return # Create 50 requests reqs = [] for i in range(1, 100, 2): req = Request(partial(append_n, i)) reqs.append(req) # Create 49 threads thrds = [] for i in range(2, 100, 2): thrd = threading.Thread(target=partial(append_n, i)) thrds.append(thrd) # Submit in reverse order to ensure that no request finishes until they have all been started. # This proves that the requests really are being suspended. for req in reversed(reqs): req.submit() # Start all the threads for thrd in reversed(thrds): thrd.start() # All requests must finish for req in reqs: req.wait() # All threads should finish for thrd in thrds: thrd.join() assert l == list( range(100)), "Requests and/or threads finished in the wrong order!"
def testRequestLock(self): """ Test the special Request-aware lock. Launch 99 requests and threads that all must fight over access to the same list. The list will eventually be 0,1,2...99, and each request will append a single number to the list. Each request must wait its turn before it can append it's number and finish. """ req_lock = RequestLock() l = [0] def append_n(n): #print "Starting append_{}\n".format(n) while True: with req_lock: if l[-1] == n-1: #print "***** Appending {}".format(n) l.append(n) return # Create 50 requests reqs = [] for i in range(1,100,2): req = Request( partial(append_n, i) ) reqs.append(req) # Create 49 threads thrds = [] for i in range(2,100,2): thrd = threading.Thread( target=partial(append_n, i) ) thrds.append(thrd) # Submit in reverse order to ensure that no request finishes until they have all been started. # This proves that the requests really are being suspended. for req in reversed(reqs): req.submit() # Start all the threads for thrd in reversed(thrds): thrd.start() # All requests must finish for req in reqs: req.wait() # All threads should finish for thrd in thrds: thrd.join() assert l == list(range(100)), "Requests and/or threads finished in the wrong order!"
def test_signal_failed_called_even_when_subscription_happened_after_completion(self, broken_fn): work, req = self.work_req(broken_fn) recv = mock.Mock() req = Request(work) req.submit() with pytest.raises(TExc): assert req.wait() == 42 req.notify_failed(recv) recv.assert_called_once() assert isinstance(recv.call_args[0][0], TExc)
def test_if_request_failed_callback_should_still_be_called(self): cb = mock.Mock() def work(): raise Exception() req = Request(work) req.add_done_callback(cb) req.submit() with pytest.raises(Exception): req.wait() cb.assert_called_once_with(req)
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 wait_for_all(): def f(i): time.sleep(0.2*random.random()) with cond: produced.append(i) cond.notify() reqs = [] for i in range(N_ELEMENTS): req = Request( partial(f, i) ) reqs.append( req ) for req in reqs: req.submit() _consumed = consumed with cond: while len(_consumed) < N_ELEMENTS: while len(_consumed) == len(produced): cond.wait() logger.debug( "copying {} elements".format( len(produced) - len(consumed) ) ) _consumed += produced[len(_consumed):]
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 testWorkerThreadLoopProtection(self): """ The worker threads should not die due to an exception raised within a request. """ for worker in Request.global_thread_pool.workers: assert worker.is_alive( ), "Something is wrong with this test. All workers should be alive." def always_fails(): raise Exception("This is an intentional exception for this test.") req = Request(always_fails) # Must add a default fail handler or else it will log an exception by default. req.notify_failed(lambda *args: None) try: req.submit() except: if Request.global_thread_pool.num_workers > 0: raise else: if Request.global_thread_pool.num_workers == 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" try: req.wait() except: pass else: if Request.global_thread_pool.num_workers > 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" for worker in Request.global_thread_pool.workers: assert worker.is_alive( ), "An exception was propagated to a worker run loop!"
def testWorkerThreadLoopProtection(self): """ The worker threads should not die due to an exception raised within a request. """ for worker in Request.global_thread_pool.workers: assert worker.is_alive(), "Something is wrong with this test. All workers should be alive." def always_fails(): raise Exception("This is an intentional exception for this test.") req = Request(always_fails) # Must add a default fail handler or else it will log an exception by default. req.notify_failed(lambda *args: None) try: req.submit() except: if Request.global_thread_pool.num_workers > 0: raise else: if Request.global_thread_pool.num_workers == 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" try: req.wait() except: pass else: if Request.global_thread_pool.num_workers > 0: # In the single-threaded debug mode, the exception should be raised within submit() assert False, "Expected to request to raise an Exception!" for worker in Request.global_thread_pool.workers: assert worker.is_alive(), "An exception was propagated to a worker run loop!"
def getBigArray(directExecute, recursionDepth): """ Simulate the memory footprint of a series of computation steps. """ logger.debug( "Usage delta before depth {}: {} MB".format(recursionDepth, getMemoryIncreaseMb() ) ) if recursionDepth == 0: # A 500GB result result = numpy.zeros(shape=resultShape, dtype=numpy.uint8) else: req = Request( partial(getBigArray, directExecute=directExecute, recursionDepth=recursionDepth-1) ) if not directExecute: # Force this request to be submitted to the thread pool, # not executed synchronously in this thread. req.submit() result = req.wait() + 1 # Note that we expect there to be 2X memory usage here: # 1x for our result and 1x for the child, which hasn't been cleaned up yet. memory_increase_mb = getMemoryIncreaseMb() logger.debug( "Usage delta after depth {}: {} MB".format(recursionDepth, memory_increase_mb ) ) assert memory_increase_mb < 2.5*resultSize, "Memory from finished requests didn't get freed!" return result
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 test_dont_cancel_shared_request(self): """ Test that a request isn't cancelled if it has requests pending for it. """ if Request.global_thread_pool.num_workers == 0: raise nose.SkipTest cancelled_requests = [] def f1(): time.sleep(1) return "RESULT" r1 = Request(f1) r1.notify_cancelled(partial(cancelled_requests.append, 1)) def f2(): try: return r1.wait() except: cancelled_requests.append(2) r2 = Request(f2) def f3(): try: return r1.wait() except: cancelled_requests.append(3) r3 = Request(f3) def otherThread(): r2.wait() t = threading.Thread(target=otherThread) t.start() r3.submit() time.sleep(0.5) # By now both r2 and r3 are waiting for the result of r1 # Cancelling r3 should not cancel r1. r3.cancel() t.join() # Wait for r2 to finish time.sleep(0.5) assert r1.started assert r1.finished assert not r1.cancelled # Not cancelled, even though we cancelled a request that was waiting for it. assert 1 not in cancelled_requests assert r2.started assert r2.finished assert not r2.cancelled # Not cancelled. assert 1 not in cancelled_requests assert r2.wait() == "RESULT" assert r3.started assert r3.finished assert r3.cancelled # Successfully cancelled. assert 3 in cancelled_requests
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_dont_cancel_shared_request(self): """ Test that a request isn't cancelled if it has requests pending for it. """ cancelled_requests = [] def f1(): time.sleep(1) return "RESULT" r1 = Request(f1) r1.notify_cancelled( partial(cancelled_requests.append, 1) ) def f2(): try: return r1.wait() except: cancelled_requests.append(2) r2 = Request(f2) def f3(): try: return r1.wait() except: cancelled_requests.append(3) r3 = Request(f3) def otherThread(): r2.wait() t = threading.Thread(target=otherThread) t.start() r3.submit() time.sleep(0.5) # By now both r2 and r3 are waiting for the result of r1 # Cancelling r3 should not cancel r1. r3.cancel() t.join() # Wait for r2 to finish time.sleep(0.5) assert r1.started assert r1.finished assert not r1.cancelled # Not cancelled, even though we cancelled a request that was waiting for it. assert 1 not in cancelled_requests assert r2.started assert r2.finished assert not r2.cancelled # Not cancelled. assert 1 not in cancelled_requests assert r2.wait() == "RESULT" assert r3.started assert r3.finished assert r3.cancelled # Successfully cancelled. assert 3 in cancelled_requests
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 test_pool_results_discarded_REQUEST_CONTEXT(): mainreq = Request(_impl_test_pool_results_discarded) mainreq.submit() mainreq.wait()
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 _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 _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 _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() 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 _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 = 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()