本文整理汇总了Python中Orange.widgets.utils.concurrent.ThreadExecutor.submit方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadExecutor.submit方法的具体用法?Python ThreadExecutor.submit怎么用?Python ThreadExecutor.submit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Orange.widgets.utils.concurrent.ThreadExecutor
的用法示例。
在下文中一共展示了ThreadExecutor.submit方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_executor
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
def test_executor(self):
executor = ThreadExecutor()
f1 = executor.submit(pow, 100, 100)
f2 = executor.submit(lambda: 1 / 0)
f3 = executor.submit(QThread.currentThread)
self.assertTrue(f1.result(), pow(100, 100))
with self.assertRaises(ZeroDivisionError):
f2.result()
self.assertIsInstance(f2.exception(), ZeroDivisionError)
self.assertIsNot(f3.result(), QThread.currentThread())
示例2: test_methodinvoke
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
def test_methodinvoke(self):
executor = ThreadExecutor()
state = [None, None]
class StateSetter(QObject):
@pyqtSlot(object)
def set_state(self, value):
state[0] = value
state[1] = QThread.currentThread()
def func(callback):
callback(QThread.currentThread())
obj = StateSetter()
f1 = executor.submit(func, methodinvoke(obj, "set_state", (object,)))
f1.result()
# So invoked method can be called from the event loop
self.app.processEvents()
self.assertIs(state[1], QThread.currentThread(),
"set_state was called from the wrong thread")
self.assertIsNot(state[0], QThread.currentThread(),
"set_state was invoked in the main thread")
executor.shutdown(wait=True)
示例3: OWGeneInfo
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.treeWidget.setItemDelegate(
gui.LinkStyledItemDelegate(self.treeWidget))
self.treeWidget.viewport().setMouseTracking(True)
self.mainArea.layout().addWidget(self.treeWidget)
box = gui.widgetBox(self.mainArea, "", orientation="horizontal")
gui.button(box, self, "Select Filtered", callback=self.selectFiltered)
gui.button(box, self, "Clear Selection",
callback=self.treeWidget.clearSelection)
self.geneinfo = []
self.cells = []
self.row2geneinfo = {}
self.data = None
# : (# input genes, # matches genes)
self.matchedInfo = 0, 0
self.setBlocking(True)
self.executor = ThreadExecutor(self)
self.progressBarInit()
task = Task(
function=partial(
taxonomy.ensure_downloaded,
callback=methodinvoke(self, "advance", ())
)
)
task.resultReady.connect(self.initialize)
task.exceptionReady.connect(self._onInitializeError)
self.initfuture = self.executor.submit(task)
def sizeHint(self):
return QSize(1024, 720)
@Slot()
def advance(self):
assert self.thread() is QThread.currentThread()
self.progressBarSet(self.progressBarValue + 1,
processEvents=None)
def initialize(self):
if self.__initialized:
# Already initialized
return
self.__initialized = True
self.organisms = sorted(
set([name.split(".")[-2] for name in
serverfiles.listfiles("NCBI_geneinfo")] +
gene.NCBIGeneInfo.common_taxids())
)
self.organismComboBox.addItems(
[taxonomy.name(tax_id) for tax_id in self.organisms]
)
if self.taxid in self.organisms:
self.organism_index = self.organisms.index(self.taxid)
else:
self.organism_index = 0
self.taxid = self.organisms[self.organism_index]
self.altSourceCheck.setVisible(self.taxid == DICTY_TAXID)
示例4: OWGOEnrichmentAnalysis
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.listView.header().setSortIndicatorShown(True)
self.listView.setSortingEnabled(True)
self.listView.setItemDelegateForColumn(
6, EnrichmentColumnItemDelegate(self))
self.listView.setRootIsDecorated(True)
self.listView.itemSelectionChanged.connect(self.ViewSelectionChanged)
# table of significant GO terms
self.sigTerms = QTreeWidget(self.splitter)
self.sigTerms.setColumnCount(len(self.DAGcolumns))
self.sigTerms.setHeaderLabels(self.DAGcolumns)
self.sigTerms.setSortingEnabled(True)
self.sigTerms.setSelectionMode(QTreeView.ExtendedSelection)
self.sigTerms.setItemDelegateForColumn(
6, EnrichmentColumnItemDelegate(self))
self.sigTerms.itemSelectionChanged.connect(self.TableSelectionChanged)
self.sigTableTermsSorted = []
self.graph = {}
self.inputTab.layout().addStretch(1)
self.filterTab.layout().addStretch(1)
self.selectTab.layout().addStretch(1)
self.setBlocking(True)
self._executor = ThreadExecutor()
self._init = EnsureDownloaded(
[(taxonomy.Taxonomy.DOMAIN, taxonomy.Taxonomy.FILENAME),
("GO", "taxonomy.pickle")]
)
self._init.finished.connect(self.__initialize_finish)
self._executor.submit(self._init)
def sizeHint(self):
return QSize(1000, 700)
def __initialize_finish(self):
self.setBlocking(False)
try:
self.annotationFiles = listAvailable()
except ConnectTimeout:
self.error(2, "Internet connection error, unable to load data. " + \
"Check connection and create a new GO Browser widget.")
self.filterTab.setEnabled(False)
self.inputTab.setEnabled(False)
self.selectTab.setEnabled(False)
self.listView.setEnabled(False)
self.sigTerms.setEnabled(False)
else:
self.annotationCodes = sorted(self.annotationFiles.keys())
self.annotationComboBox.clear()
self.annotationComboBox.addItems(self.annotationCodes)
self.annotationComboBox.setCurrentIndex(self.annotationIndex)
self.__state = OWGOEnrichmentAnalysis.Ready
def __on_evidenceChanged(self):
for etype, cb in self.evidenceCheckBoxDict.items():
self.useEvidenceType[etype] = cb.isChecked()
self._updateEnrichment()
def UpdateGeneMatcher(self):
"""Open the Gene matcher settings dialog."""
示例5: OWLearningCurveC
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
)
else:
learning_curve_func = partial(
learning_curve_with_test_data,
learners, self.data, self.testdata, times=self.folds,
proportions=self.curvePoints,
)
# [end-snippet-6]
# [start-snippet-7]
# setup the task state
self._task = task = Task()
# The learning_curve[_with_test_data] also takes a callback function
# to report the progress. We instrument this callback to both invoke
# the appropriate slots on this widget for reporting the progress
# (in a thread safe manner) and to implement cooperative cancellation.
set_progress = methodinvoke(self, "setProgressValue", (float,))
def callback(finished):
# check if the task has been cancelled and raise an exception
# from within. This 'strategy' can only be used with code that
# properly cleans up after itself in the case of an exception
# (does not leave any global locks, opened file descriptors, ...)
if task.cancelled:
raise KeyboardInterrupt()
set_progress(finished * 100)
# capture the callback in the partial function
learning_curve_func = partial(learning_curve_func, callback=callback)
# [end-snippet-7]
# [start-snippet-8]
self.progressBarInit()
# Submit the evaluation function to the executor and fill in the
# task with the resultant Future.
task.future = self._executor.submit(learning_curve_func)
# Setup the FutureWatcher to notify us of completion
task.watcher = FutureWatcher(task.future)
# by using FutureWatcher we ensure `_task_finished` slot will be
# called from the main GUI thread by the Qt's event loop
task.watcher.done.connect(self._task_finished)
# [end-snippet-8]
# [start-snippet-progress]
@pyqtSlot(float)
def setProgressValue(self, value):
assert self.thread() is QThread.currentThread()
self.progressBarSet(value)
# [end-snippet-progress]
# [start-snippet-9]
@pyqtSlot(concurrent.futures.Future)
def _task_finished(self, f):
"""
Parameters
----------
f : Future
The future instance holding the result of learner evaluation.
"""
assert self.thread() is QThread.currentThread()
assert self._task is not None
assert self._task.future is f
assert f.done()
self._task = None
self.progressBarFinished()
try:
示例6: OWSetEnrichment
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.mainArea.layout().addWidget(hWidget)
self.filterLineEdit.textChanged.connect(
self.filterAnnotationsChartView)
self.annotationsChartView = QtGui.QTreeView(
alternatingRowColors=True,
sortingEnabled=True,
selectionMode=QtGui.QTreeView.ExtendedSelection,
rootIsDecorated=False,
editTriggers=QtGui.QTreeView.NoEditTriggers,
)
self.annotationsChartView.viewport().setMouseTracking(True)
self.mainArea.layout().addWidget(self.annotationsChartView)
contextEventFilter = gui.VisibleHeaderSectionContextEventFilter(
self.annotationsChartView)
self.annotationsChartView.header().installEventFilter(contextEventFilter)
self.groupsWidget.itemClicked.connect(self.subsetSelectionChanged)
gui.auto_commit(self.controlArea, self, "autocommit", "Commit")
self.setBlocking(True)
task = EnsureDownloaded(
[("Taxonomy", "ncbi_taxonomy.tar.gz"),
(geneset.sfdomain, "index.pck")]
)
task.finished.connect(self.__initialize_finish)
self.setStatusMessage("Initializing")
self._executor = ThreadExecutor(
parent=self, threadPool=QtCore.QThreadPool(self))
self._executor.submit(task)
def sizeHint(self):
return QtCore.QSize(1024, 600)
def __initialize_finish(self):
# Finalize the the widget's initialization (preferably after
# ensuring all required databases have been downloaded.
sets = geneset.list_all()
taxids = set(taxonomy.common_taxids() +
list(filter(None, [tid for _, tid, _ in sets])))
organisms = [(tid, name_or_none(tid)) for tid in taxids]
organisms = [(tid, name) for tid, name in organisms
if name is not None]
organisms = [(None, "None")] + sorted(organisms)
taxids = [tid for tid, _ in organisms]
names = [name for _, name in organisms]
self.taxid_list = taxids
self.speciesComboBox.clear()
self.speciesComboBox.addItems(names)
self.genesets = sets
if self.taxid in self.taxid_list:
taxid = self.taxid
else:
taxid = self.taxid_list[0]
self.taxid = None
self.setCurrentOrganism(taxid)
self.setBlocking(False)
示例7: OWImportImages
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.__pendingTask = taskstate = namespace(
task=task,
startdir=startdir,
future=None,
watcher=None,
cancelled=False,
cancel=None,
)
def cancel():
# Cancel the task and disconnect
if taskstate.future.cancel():
pass
else:
taskstate.task.cancelled = True
taskstate.cancelled = True
try:
taskstate.future.result(timeout=3)
except UserInterruptError:
pass
except TimeoutError:
log.info("The task did not stop in in a timely manner")
taskstate.watcher.finished.disconnect(self.__onRunFinished)
taskstate.cancel = cancel
def run_image_scan_task_interupt():
try:
return task.run()
except UserInterruptError:
# Suppress interrupt errors, so they are not logged
return
taskstate.future = self.__executor.submit(run_image_scan_task_interupt)
taskstate.watcher = FutureWatcher(taskstate.future)
taskstate.watcher.finished.connect(self.__onRunFinished)
@Slot()
def __onRunFinished(self):
assert QThread.currentThread() is self.thread()
assert self.__state == State.Processing
assert self.__pendingTask is not None
assert self.sender() is self.__pendingTask.watcher
assert self.__pendingTask.future.done()
task = self.__pendingTask
self.__pendingTask = None
try:
image_meta = task.future.result()
except Exception as err:
sys.excepthook(*sys.exc_info())
state = State.Error
image_meta = []
self.error(traceback.format_exc())
else:
state = State.Done
self.error()
categories = {}
for imeta in image_meta:
# derive categories from the path relative to the starting dir
dirname = os.path.dirname(imeta.path)
relpath = os.path.relpath(dirname, task.startdir)
categories[dirname] = relpath
示例8: OWGeneNetwork
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
else:
return []
def invalidate(self):
self._invalidated = True
if self.nettask is not None:
self.nettask.finished.disconnect(self._on_result_ready)
self.nettask.future().cancel()
self.nettask = None
@Slot()
def advance(self):
self.progressBarValue = (self.progressBarValue + 1) % 100
@Slot(float)
def set_progress(self, value):
self.progressBarSet(value, processEvents=None)
def commit(self):
include_neighborhood = self.include_neighborhood
query_genes = self.query_genes()
source = SOURCES[self.network_source]
if source.score_filter:
min_score = self.min_score
assert source.name == "STRING"
min_score = min_score * 1000
else:
min_score = None
taxid = self.taxid
progress = methodinvoke(self, "advance")
if self.geneinfo is None:
self.geneinfo = self.executor.submit(
fetch_ncbi_geneinfo, taxid, progress
)
geneinfo_f = self.geneinfo
taxmap = source.tax_mapping
db_taxid = taxmap.get(taxid, taxid)
if db_taxid is None:
raise ValueError("invalid taxid for this network")
def fetch_network():
geneinfo = geneinfo_f.result()
ppidb = fetch_ppidb(source, db_taxid, progress)
return get_gene_network(ppidb, geneinfo, db_taxid, query_genes,
include_neighborhood=include_neighborhood,
min_score=min_score,
progress=methodinvoke(self, "set_progress", (float,)))
self.nettask = Task(function=fetch_network)
self.nettask.finished.connect(self._on_result_ready)
self.executor.submit(self.nettask)
self.setBlocking(True)
self.setEnabled(False)
self.progressBarInit()
self._invalidated = False
self._update_info()
@Slot(object)
def _on_result_ready(self,):
self.progressBarFinished()
示例9: OWTestLearners
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.data, learners_c, **common_args
)
elif self.resampling == OWTestLearners.ShuffleSplit:
train_size = self.SampleSizes[self.sample_size] / 100
test_f = partial(
Orange.evaluation.ShuffleSplit,
self.data, learners_c,
n_resamples=self.NRepeats[self.n_repeats],
train_size=train_size, test_size=None,
stratified=self.shuffle_stratified,
random_state=rstate, **common_args
)
elif self.resampling == OWTestLearners.TestOnTrain:
test_f = partial(
Orange.evaluation.TestOnTrainingData,
self.data, learners_c, **common_args
)
elif self.resampling == OWTestLearners.TestOnTest:
test_f = partial(
Orange.evaluation.TestOnTestData,
self.data, self.test_data, learners_c, **common_args
)
else:
assert False, "self.resampling %s" % self.resampling
def replace_learners(evalfunc, *args, **kwargs):
res = evalfunc(*args, **kwargs)
assert all(lc is lo for lc, lo in zip(learners_c, res.learners))
res.learners[:] = learners
return res
test_f = partial(replace_learners, test_f)
self.__submit(test_f)
def __submit(self, testfunc):
# type: (Callable[[Callable[float]], Results]) -> None
"""
Submit a testing function for evaluation
MUST not be called if an evaluation is already pending/running.
Cancel the existing task first.
Parameters
----------
testfunc : Callable[[Callable[float]], Results])
Must be a callable taking a single `callback` argument and
returning a Results instance
"""
assert self.__state != State.Running
# Setup the task
task = Task()
def progress_callback(finished):
if task.cancelled:
raise UserInterrupt()
QMetaObject.invokeMethod(
self, "setProgressValue", Qt.QueuedConnection,
Q_ARG(float, 100 * finished)
)
def ondone(_):
QMetaObject.invokeMethod(
self, "__task_complete", Qt.QueuedConnection,
Q_ARG(object, task))
示例10: OWKMeans
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.clusterings[self.k_from + idx] = str(ex)
@Slot(int, object)
def __clustering_complete(self, _, result):
# type: (int, KMeansModel) -> None
assert QThread.currentThread() is self.thread()
assert self.__task is not None
self.clusterings[result.k] = result
@Slot()
def __commit_finished(self):
assert QThread.currentThread() is self.thread()
assert self.__task is not None
assert self.data is not None
self.__task = None
self.setBlocking(False)
self.progressBarFinished()
if self.optimize_k:
self.update_results()
if self.optimize_k and all(isinstance(self.clusterings[i], str)
for i in range(self.k_from, self.k_to + 1)):
# Show the error of the last clustering
self.Error.failed(self.clusterings[self.k_to])
self.send_data()
def __launch_tasks(self, ks):
# type: (List[int]) -> None
"""Execute clustering in separate threads for all given ks."""
futures = [self.__executor.submit(
self._compute_clustering,
data=self.data,
k=k,
init=self.INIT_METHODS[self.smart_init][1],
n_init=self.n_init,
max_iter=self.max_iterations,
silhouette=True,
random_state=RANDOM_STATE,
) for k in ks]
watcher = FutureSetWatcher(futures)
watcher.resultReadyAt.connect(self.__clustering_complete)
watcher.progressChanged.connect(self.__progress_changed)
watcher.exceptionReadyAt.connect(self.__on_exception)
watcher.doneAll.connect(self.__commit_finished)
self.__task = Task(futures, watcher)
self.progressBarInit(processEvents=False)
self.setBlocking(True)
def cancel(self):
if self.__task is not None:
task, self.__task = self.__task, None
task.cancel()
task.watcher.resultReadyAt.disconnect(self.__clustering_complete)
task.watcher.progressChanged.disconnect(self.__progress_changed)
task.watcher.exceptionReadyAt.disconnect(self.__on_exception)
task.watcher.doneAll.disconnect(self.__commit_finished)
self.progressBarFinished()
self.setBlocking(False)
示例11: OWImportImages
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
self.__pendingTask = taskstate = namespace(
task=task,
startdir=startdir,
future=None,
watcher=None,
cancelled=False,
cancel=None,
)
def cancel():
# Cancel the task and disconnect
if taskstate.future.cancel():
pass
else:
taskstate.task.cancelled = True
taskstate.cancelled = True
try:
taskstate.future.result(timeout=3)
except UserInterruptError:
pass
except TimeoutError:
log.info("The task did not stop in in a timely manner")
taskstate.watcher.finished.disconnect(self.__onRunFinished)
taskstate.cancel = cancel
def run_image_scan_task_interupt():
try:
return task(startdir)
except UserInterruptError:
# Suppress interrupt errors, so they are not logged
return
taskstate.future = self.__executor.submit(run_image_scan_task_interupt)
taskstate.watcher = FutureWatcher(taskstate.future)
taskstate.watcher.finished.connect(self.__onRunFinished)
@Slot()
def __onRunFinished(self):
assert QThread.currentThread() is self.thread()
assert self.__state == State.Processing
assert self.__pendingTask is not None
assert self.sender() is self.__pendingTask.watcher
assert self.__pendingTask.future.done()
task = self.__pendingTask
self.__pendingTask = None
try:
data, n_skipped = task.future.result()
except Exception:
sys.excepthook(*sys.exc_info())
state = State.Error
data = None
n_skipped = 0
self.error(traceback.format_exc())
else:
state = State.Done
self.error()
if data:
self._n_image_data = len(data)
self._n_image_categories = len(data.domain.class_var.values)\
if data.domain.class_var else 0
else:
self._n_image_data, self._n_image_categories = 0, 0
示例12: OWNNLearner
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
if self._task is not None:
# First make sure any pending tasks are cancelled.
self.cancel()
assert self._task is None
max_iter = self.learner.kwargs["max_iter"]
# Setup the task state
task = Task()
lastemitted = 0.
def callback(iteration):
nonlocal task # type: Task
nonlocal lastemitted
if task.isInterruptionRequested():
raise CancelTaskException()
progress = round(iteration / max_iter * 100)
if progress != lastemitted:
task.emitProgressUpdate(progress)
lastemitted = progress
# copy to set the callback so that the learner output is not modified
# (currently we can not pass callbacks to learners __call__)
learner = copy.copy(self.learner)
learner.callback = callback
def build_model(data, learner):
try:
return learner(data)
except CancelTaskException:
return None
build_model_func = partial(build_model, self.data, learner)
task.setFuture(self._executor.submit(build_model_func))
task.done.connect(self._task_finished)
task.progressChanged.connect(self.setProgressValue)
self._task = task
self.progressBarInit()
self.setBlocking(True)
@Slot(concurrent.futures.Future)
def _task_finished(self, f):
"""
Parameters
----------
f : Future
The future instance holding the built model
"""
assert self.thread() is QThread.currentThread()
assert self._task is not None
assert self._task.future is f
assert f.done()
self._task.deleteLater()
self._task = None
self.setBlocking(False)
self.progressBarFinished()
try:
self.model = f.result()
except Exception as ex: # pylint: disable=broad-except
# Log the exception with a traceback
log = logging.getLogger()
log.exception(__name__, exc_info=True)
self.model = None
self.show_fitting_failed(ex)
else:
self.model.name = self.learner_name
self.model.instances = self.data
self.model.skl_model.orange_callback = None # remove unpicklable callback
self.Outputs.model.send(self.model)
def cancel(self):
"""
Cancel the current task (if any).
"""
if self._task is not None:
self._task.cancel()
assert self._task.future.done()
# disconnect from the task
self._task.done.disconnect(self._task_finished)
self._task.progressChanged.disconnect(self.setProgressValue)
self._task.deleteLater()
self._task = None
self.progressBarFinished()
self.setBlocking(False)
def onDeleteWidget(self):
self.cancel()
super().onDeleteWidget()
@classmethod
def migrate_settings(cls, settings, version):
if not version:
alpha = settings.pop("alpha", None)
if alpha is not None:
settings["alpha_index"] = \
np.argmin(np.abs(np.array(cls.alphas) - alpha))
示例13: OWNNLearner
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
else:
self.Outputs.model.send(self.model)
@Slot(float)
def setProgressValue(self, value):
assert self.thread() is QThread.currentThread()
self.progressBarSet(value)
def __update(self):
if self._task is not None:
# First make sure any pending tasks are cancelled.
self.cancel()
assert self._task is None
max_iter = self.learner.kwargs["max_iter"]
# Setup the task state
task = Task()
lastemitted = 0.
def callback(iteration):
nonlocal task # type: Task
nonlocal lastemitted
if task.isInterruptionRequested():
raise CancelTaskException()
progress = round(iteration / max_iter * 100)
if progress != lastemitted:
task.emitProgressUpdate(progress)
lastemitted = progress
# copy to set the callback so that the learner output is not modified
# (currently we can not pass callbacks to learners __call__)
learner = copy.copy(self.learner)
learner.callback = callback
def build_model(data, learner):
try:
return learner(data)
except CancelTaskException:
return None
build_model_func = partial(build_model, self.data, learner)
task.setFuture(self._executor.submit(build_model_func))
task.done.connect(self._task_finished)
task.progressChanged.connect(self.setProgressValue)
self._task = task
self.progressBarInit()
self.setBlocking(True)
@Slot(concurrent.futures.Future)
def _task_finished(self, f):
"""
Parameters
----------
f : Future
The future instance holding the built model
"""
assert self.thread() is QThread.currentThread()
assert self._task is not None
assert self._task.future is f
assert f.done()
self._task.deleteLater()
self._task = None
self.setBlocking(False)
self.progressBarFinished()
try:
self.model = f.result()
except Exception as ex: # pylint: disable=broad-except
# Log the exception with a traceback
log = logging.getLogger()
log.exception(__name__, exc_info=True)
self.model = None
self.show_fitting_failed(ex)
else:
self.model.name = self.learner_name
self.model.instances = self.data
self.Outputs.model.send(self.model)
def cancel(self):
"""
Cancel the current task (if any).
"""
if self._task is not None:
self._task.cancel()
assert self._task.future.done()
# disconnect from the task
self._task.done.disconnect(self._task_finished)
self._task.progressChanged.disconnect(self.setProgressValue)
self._task.deleteLater()
self._task = None
self.progressBarFinished()
self.setBlocking(False)
def onDeleteWidget(self):
self.cancel()
super().onDeleteWidget()
示例14: OWExplainPredictions
# 需要导入模块: from Orange.widgets.utils.concurrent import ThreadExecutor [as 别名]
# 或者: from Orange.widgets.utils.concurrent.ThreadExecutor import submit [as 别名]
#.........这里部分代码省略.........
if self.model is not None:
# calculate contributions
if self.e is None:
self.e = ExplainPredictions(self.data,
self.model,
batch_size=min(
len(self.data.X), 500),
p_val=self.gui_p_val,
error=self.gui_error)
self._task = task = Task()
def callback(progress):
nonlocal task
# update progress bar
QMetaObject.invokeMethod(
self, "set_progress_value", Qt.QueuedConnection, Q_ARG(int, progress))
if task.canceled:
return True
return False
def callback_update(table):
QMetaObject.invokeMethod(
self, "update_view", Qt.QueuedConnection, Q_ARG(Orange.data.Table, table))
def callback_prediction(class_value):
QMetaObject.invokeMethod(
self, "update_model_prediction", Qt.QueuedConnection, Q_ARG(float, class_value))
self.was_canceled = False
explain_func = partial(
self.e.anytime_explain, self.to_explain[0], callback=callback, update_func=callback_update, update_prediction=callback_prediction)
self.progressBarInit(processEvents=None)
task.future = self._executor.submit(explain_func)
task.watcher = FutureWatcher(task.future)
task.watcher.done.connect(self._task_finished)
self.cancel_button.setDisabled(False)
@pyqtSlot(Orange.data.Table)
def update_view(self, table):
self.explanations = table
self.sort_explanations()
self.draw()
self.commit_output()
@pyqtSlot(float)
def update_model_prediction(self, value):
self._print_prediction(value)
@pyqtSlot(int)
def set_progress_value(self, value):
self.progressBarSet(value, processEvents=False)
@pyqtSlot(concurrent.futures.Future)
def _task_finished(self, f):
"""
Parameters:
----------
f: conncurent.futures.Future
future instance holding the result of learner evaluation
"""
assert self.thread() is QThread.currentThread()
assert self._task is not None
assert self._task.future is f
assert f.done()