Skip to content

Commit

Permalink
Merge pull request #319 from ales-erjavec/geo-datasets-init
Browse files Browse the repository at this point in the history
[ENH] GEO Datasets: Move GDSInfo initialization in a worker thread
  • Loading branch information
PrimozGodec authored Mar 20, 2024
2 parents b2f9763 + e206735 commit 08841de
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
repos:
- repo: https://github.com/psf/black
rev: 23.7.0
rev: 24.3.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/timothycrosley/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
language_version: python3

- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8
language_version: python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def setUp(self):
self.test_sample = 'GDS1001'
self.test_organism = '10090'
self.widget = self.create_widget(OWGEODatasets)
self.wait_until_finished(self.widget)
self.process_events()

def test_minimum_size(self):
pass
Expand Down
71 changes: 45 additions & 26 deletions orangecontrib/bioinformatics/widgets/OWGEODatasets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
""" Gene Expression Omnibus datasets widget """

import sys
from types import SimpleNamespace
from typing import Any, Optional, DefaultDict
from typing import Any, Union, Optional, DefaultDict
from collections import defaultdict

import requests

from AnyQt.QtCore import Qt
from AnyQt.QtCore import Qt, QTimer
from AnyQt.QtWidgets import (
QSplitter,
QTreeWidget,
Expand Down Expand Up @@ -63,6 +64,8 @@ def callback():
nonlocal current_iter
current_iter += 1
state.set_progress_value(100 * (current_iter / max_iter))
if state.is_interruption_requested():
raise KeyboardInterrupt

state.set_status("Downloading...")
res.gds_dataset = dataset_download(
Expand All @@ -71,6 +74,16 @@ def callback():
return res


class GDSInfoResult(SimpleNamespace):
gds_info: Optional[GDSInfo]


def run_gds_info_task(state: TaskState):
state.set_status("Fetching index...")
gds_info = GDSInfo()
return GDSInfoResult(gds_info=gds_info)


class GEODatasetsModel(TableModel):
(
indicator_col,
Expand Down Expand Up @@ -108,9 +121,9 @@ def render_pubmed_url(row):
TableModel.Column(
title(""),
{
Qt.DisplayRole: lambda row: " "
if is_cached(items[row]["name"])
else "",
Qt.DisplayRole: lambda row: (
" " if is_cached(items[row]["name"]) else ""
),
Qt.UserRole: lambda row: items[row],
},
),
Expand Down Expand Up @@ -211,6 +224,7 @@ class Warning(OWWidget.Warning):

class Error(OWWidget.Error):
no_connection = Msg("Widget can't connect to serverfiles.")
error = Msg("{}")

class Outputs:
gds_data = Output("Expression Data", Table)
Expand All @@ -232,14 +246,7 @@ class Outputs:
def __init__(self):
OWWidget.__init__(self)
ConcurrentWidgetMixin.__init__(self)

try:
self.gds_info: Optional[GDSInfo] = GDSInfo()
except requests.exceptions.ConnectionError:
self.gds_info = {}
self.Error.no_connection()
return

self.gds_info: Optional[GDSInfo] = None
self.gds_data: Optional[Table] = None
self.__updating_filter = False

Expand Down Expand Up @@ -291,7 +298,7 @@ def __init__(self):
self.table_view.verticalHeader().setVisible(False)
self.table_view.viewport().setMouseTracking(True)

self.table_model = GEODatasetsModel(self.gds_info)
self.table_model = GEODatasetsModel({})
self.proxy_model = FilterProxyModel()
self.proxy_model.setSourceModel(self.table_model)
self.table_view.setModel(self.proxy_model)
Expand Down Expand Up @@ -343,7 +350,7 @@ def __init__(self):
)
self._apply_filter()

self.commit()
self.start(run_gds_info_task)

def _splitter_moved(self, *args):
self.splitter_settings = [bytes(sp.saveState()) for sp in self.splitters]
Expand Down Expand Up @@ -452,7 +459,7 @@ def current_changed():

def _run(self):
self.Warning.using_local_files.clear()
if self.selected_gds is not None:
if self.gds_info and self.selected_gds is not None:
self.gds_data = None
self.start(
run_download_task,
Expand Down Expand Up @@ -541,16 +548,28 @@ def commit(self):
self._run()

def on_exception(self, ex: Exception):
self.Warning.using_local_files()

def on_done(self, result: Result):
assert isinstance(result.gds_dataset, Table)
self.gds_data = result.gds_dataset

if self.gds_info:
self.table_model.update_cache_indicator()

self.Outputs.gds_data.send(self.gds_data)
if isinstance(ex, requests.exceptions.ConnectionError):
self.Warning.using_local_files()
else:
self.Error.error("", exc_info=ex)

def on_done(self, result: Union[GDSInfoResult, Result]):
self.Error.error.clear()
if isinstance(result, GDSInfoResult):
self.gds_info = result.gds_info
self.table_model = GEODatasetsModel(result.gds_info)
self.proxy_model.setSourceModel(self.table_model)
self.table_view.resizeColumnsToContents()
self._set_selection()
QTimer.singleShot(0, lambda: self.unconditional_commit())
else:
assert isinstance(result.gds_dataset, Table)
self.gds_data = result.gds_dataset

if self.gds_info:
self.table_model.update_cache_indicator()

self.Outputs.gds_data.send(self.gds_data)

def on_partial_result(self, result: Any) -> None:
pass
Expand Down

0 comments on commit 08841de

Please sign in to comment.