Skip to content

Commit

Permalink
Align Datasets - table locking, deferred commits, get_column
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Feb 20, 2023
1 parent 1507d92 commit 5954734
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
6 changes: 6 additions & 0 deletions orangecontrib/single_cell/tests/test_owaligndatasets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import unittest

from sklearn import datasets
from Orange.data import ContinuousVariable, DiscreteVariable
import numpy as np
Expand Down Expand Up @@ -96,3 +98,7 @@ def test_source_id_defult(self):

self.send_signal(w.Inputs.data, data)
self.assertEqual("class", str(w.source_id))


if __name__ == "__main__":
unittest.main()
33 changes: 18 additions & 15 deletions orangecontrib/single_cell/widgets/owaligndatasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def set_data(self, data):
self.openContext(self.data.domain)
if self.source_id is None or self.source_id == '':
for model in self._feature_model:
y = np.array(self.data.get_column_view(model)[0], dtype=np.float64)
y = np.array(self.data.get_column(model), dtype=np.float64)
_, counts = np.unique(y, return_counts=True)
if np.isfinite(y).all() and min(counts) > 1:
self.source_id = model
Expand All @@ -244,7 +244,7 @@ def set_data(self, data):
if np.isnan(self.data.X).any():
self.Error.nan_input()
return
y = np.array(self.data.get_column_view(self.source_id)[0], dtype=np.float64)
y = np.array(self.data.get_column(self.source_id), dtype=np.float64)
_, counts = np.unique(y, return_counts=True)
if min(counts) < 2:
self.Error.no_instances()
Expand All @@ -265,7 +265,7 @@ def fit(self):
self.ncomponents = MAX_COMPONENTS

X = self.data.X
y = self.data.get_column_view(self.source_id)[0]
y = self.data.get_column(self.source_id)

if len(set(y)) < 2:
self.Error.only_one_dataset()
Expand All @@ -279,8 +279,7 @@ def fit(self):
self._use_genes = self._mas.use_genes

self._setup_plot()
if self.auto_commit:
self.commit()
self.commit.deferred()

def clear(self):
self.data = None
Expand Down Expand Up @@ -319,7 +318,7 @@ def clear_outputs(self):
self.Outputs.genes_components.send(None)

def _reset_max_components(self):
y = np.array(self.data.get_column_view(self.source_id)[0], dtype=np.float64)
y = np.array(self.data.get_column(self.source_id), dtype=np.float64)
_, counts = np.unique(y, return_counts=True)
global MAX_COMPONENTS
if min(counts) < MAX_COMPONENTS_DEFAULT or len(
Expand All @@ -346,7 +345,7 @@ def get_model(self):

self.fit()
self._setup_plot()
self.commit()
self.commit.deferred()

def _setup_plot(self):
self.plot.clear()
Expand Down Expand Up @@ -426,7 +425,7 @@ def _on_cut_changed(self, line):

self._line.setValue(value)
self._set_horline_pos()
self.commit()
self.commit.deferred()

def _update_selection_component_spin(self):
# cut changed by "ncomponents" spin.
Expand All @@ -437,12 +436,12 @@ def _update_selection_component_spin(self):
if np.floor(self._line.value()) + 1 != self.ncomponents:
self._line.setValue(self.ncomponents - 1)

self.commit()
self.commit.deferred()

def _invalidate_selection(self):
if self.data is not None:
self._transformed = None
self.commit()
self.commit.deferred()

def _update_scoring_combo(self):
self.fit()
Expand All @@ -469,7 +468,7 @@ def _update_combo_source_id(self):
self.clear_plot()
if self.data is None:
return
y = np.array(self.data.get_column_view(self.source_id)[0], dtype=np.float64)
y = np.array(self.data.get_column(self.source_id), dtype=np.float64)
_, counts = np.unique(y, return_counts=True)
if min(counts) < 2:
self.Error.no_instances()
Expand All @@ -489,16 +488,17 @@ def _update_axis(self):
axis.setTicks([[(i, str(i + 1)) for i in range(0, p, d)]])

def _has_nan_classes(self):
y = np.array(self.data.get_column_view(self.source_id)[0], dtype=np.float64)
y = np.array(self.data.get_column(self.source_id), dtype=np.float64)
return not np.isfinite(y).all()

@gui.deferred
def commit(self):
transformed_table = meta_genes = None
if self._mas is not None:
# Compute the full transform (MAX_COMPONENTS components) only once.
if self._transformed is None:
X = self.data.X
y = self.data.get_column_view(self.source_id)[0]
y = self.data.get_column(self.source_id)
self._transformed = self._mas.transform(X, y, normalize=self.quantile_normalization,
quantile=self.quantile_normalization_perc,
dtw=self.dynamic_time_warping)
Expand All @@ -518,14 +518,17 @@ def commit(self):
for gene in genes:
genes_components[gene - 1, key] = genes.index(gene) + 1
genes_components[genes_components == 0] = np.NaN
meta_genes.X = genes_components
with meta_genes.unlocked_reference(meta_genes.X):
meta_genes.X = genes_components
self.meta_genes = Table.from_numpy(Domain(attributes), genes_components)

# Transformed data
transformed = self._transformed
new_domain = add_columns(self.data.domain, attributes=attributes)
transformed_table_temp = self.data.transform(new_domain)
transformed_table_temp.X[:, -MAX_COMPONENTS:] = transformed
# safe to unlock since new attributes added to the domain
with transformed_table_temp.unlocked(transformed_table_temp.X):
transformed_table_temp.X[:, -MAX_COMPONENTS:] = transformed
self.transformed_table = Table.from_table(dom, transformed_table_temp)

ncomponents_attributes = tuple(ContinuousVariable.make("CCA{}".format(x + 1)) for x in
Expand Down

0 comments on commit 5954734

Please sign in to comment.