From 4145f4b51729b0059b788a8b37a4856a199d1ffe Mon Sep 17 00:00:00 2001 From: "borondics.accounts@gmail.com" Date: Tue, 12 Nov 2024 16:39:07 +0100 Subject: [PATCH 1/8] 50x speedup, numerically more stable and less than 1e-10 difference. --- orangecontrib/spectroscopy/widgets/owcos.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index 132756c70..a73bd2ac0 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -41,11 +41,9 @@ def calc_cos(table1, table2): sync = series1.T @ series2 / (len(series1) - 1) # Hilbert-Noda transformation matrix - hn = np.zeros((len(series1), len(series1))) - for i in range(len(series1)): - for j in range(len(series1)): - if i != j: - hn[i, j] = 1 / np.pi / (j - i) + i, j = np.ogrid[:len(series1), :len(series1)] + with np.errstate(divide='ignore'): + hn2 = np.where(i != j, 1 / (np.pi * (j - i)), 0) # asynchronous correlation asyn = series1.T @ hn @ series2 / (len(series1) - 1) From 9750eb4ecc477973acdd3a6c54facf4c652c6ec2 Mon Sep 17 00:00:00 2001 From: "borondics.accounts@gmail.com" Date: Tue, 12 Nov 2024 16:43:01 +0100 Subject: [PATCH 2/8] Remove unnecessary example --- orangecontrib/spectroscopy/widgets/owcos.py | 23 --------------------- 1 file changed, 23 deletions(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index a73bd2ac0..b26e2f242 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -52,29 +52,6 @@ def calc_cos(table1, table2): # TODO handle non continuous data (after cut widget) -# class to multithread the isocurve calculation -# def run(data: Table, -# variable: Optional[Union[Variable, bool]], -# feature_name: str, -# remove_redundant_inst: bool, -# state: TaskState -# ) -> Table: -# if not data: -# return None -# -# def callback(i: float, status=""): -# state.set_progress_value(i * 100) -# if status: -# state.set_status(status) -# if state.is_interruption_requested(): -# raise Exception -# -# # the isocurve calculation needs to happen here -# return Table.transpose(data, variable, feature_name=feature_name, -# remove_redundant_inst=remove_redundant_inst, - # progress_callback=callback) - - class ParameterSetter(CommonParameterSetter): LEFT_AXIS_LABEL, TOP_AXIS_LABEL = "Left axis title", "Top axis title" FIGTITLE_LABEL_SIZE = "Title font size" From 0411323eaee06d86fa00550a278e04855129f20a Mon Sep 17 00:00:00 2001 From: "borondics.accounts@gmail.com" Date: Tue, 12 Nov 2024 18:14:18 +0100 Subject: [PATCH 3/8] fix typo --- orangecontrib/spectroscopy/widgets/owcos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index b26e2f242..707a90f1d 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -43,7 +43,7 @@ def calc_cos(table1, table2): # Hilbert-Noda transformation matrix i, j = np.ogrid[:len(series1), :len(series1)] with np.errstate(divide='ignore'): - hn2 = np.where(i != j, 1 / (np.pi * (j - i)), 0) + hn = np.where(i != j, 1 / (np.pi * (j - i)), 0) # asynchronous correlation asyn = series1.T @ hn @ series2 / (len(series1) - 1) From c03d0522b4d480b61cf011dc8cd2527fb789e66e Mon Sep 17 00:00:00 2001 From: "borondics.accounts@gmail.com" Date: Thu, 14 Nov 2024 17:01:33 +0100 Subject: [PATCH 4/8] Plotting speedup. --- orangecontrib/spectroscopy/widgets/owcos.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index 707a90f1d..a5a10adff 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -15,7 +15,7 @@ from orangewidget.utils.visual_settings_dlg import VisualSettingsDialog from orangecontrib.spectroscopy.data import getx from orangecontrib.spectroscopy.widgets.owhyper import ImageColorLegend -from orangecontrib.spectroscopy.widgets.owspectra import InteractiveViewBox +from orangecontrib.spectroscopy.widgets.owspectra import InteractiveViewBox, PlotCurvesItem from orangecontrib.spectroscopy.widgets.gui import float_to_str_decimals as strdec, pixel_decimals @@ -168,7 +168,7 @@ class Outputs: settingsHandler = settings.DomainContextHandler() selector = settings.Setting(0) - isonum = settings.Setting(0) + isonum = settings.Setting(5) visual_settings = settings.Setting({}, schema_only=True) # autocommit = settings.Setting(True) @@ -417,18 +417,26 @@ def plotCOS(self): self.cbarCOS.set_range(-1 * np.nanmax(np.absolute(cosmat)), np.nanmax(np.absolute(cosmat))) self.cbarCOS.set_colors(np.array(colorcet.diverging_bwr_40_95_c42) * 255) - for s in leftSP: - self.left_plot.plot(s, leftSPwn, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) + left_indices = np.linspace(0, len(leftSP)-1, 100, dtype=int) + for s in leftSP[left_indices]: + pt = pg.PlotCurveItem(s, leftSPwn, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) + self.left_plot.addItem(pt, ignoreBounds=True) self.left_plot.plot(leftSP.mean(axis=0), leftSPwn, pen=p) self.left_plot.addItem(self.left_hLine) - for s in topSP: - self.top_plot.plot(topSPwn, s, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) + self.left_plot.setXRange(np.min(leftSP), np.max(leftSP)) + + top_indices = np.linspace(0, len(topSP)-1, 100, dtype=int) + for s in topSP[top_indices]: + pt = pg.PlotCurveItem(topSPwn, s, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) + self.top_plot.addItem(pt, ignoreBounds=True) self.top_plot.plot(topSPwn, topSP.mean(axis=0), pen=p) self.top_plot.addItem(self.top_vLine) + self.top_plot.setYRange(np.min(topSP), np.max(topSP)) + def hideitems_for_saving(self): self.hLine.hide() self.vLine.hide() From 2b5fc7b3698e89b5e80e8393cae501eac725bd6d Mon Sep 17 00:00:00 2001 From: "borondics.accounts@gmail.com" Date: Thu, 14 Nov 2024 21:05:04 +0100 Subject: [PATCH 5/8] Fixing sampling. --- orangecontrib/spectroscopy/widgets/owcos.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index a5a10adff..634b208db 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -417,7 +417,11 @@ def plotCOS(self): self.cbarCOS.set_range(-1 * np.nanmax(np.absolute(cosmat)), np.nanmax(np.absolute(cosmat))) self.cbarCOS.set_colors(np.array(colorcet.diverging_bwr_40_95_c42) * 255) - left_indices = np.linspace(0, len(leftSP)-1, 100, dtype=int) + if len(leftSP) > 100: + left_indices = np.linspace(0, len(leftSP)-1, 100, dtype=int) + else: + left_indices = np.linspace(0, len(leftSP)-1, len(leftSP), dtype=int) + for s in leftSP[left_indices]: pt = pg.PlotCurveItem(s, leftSPwn, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) self.left_plot.addItem(pt, ignoreBounds=True) @@ -427,7 +431,11 @@ def plotCOS(self): self.left_plot.setXRange(np.min(leftSP), np.max(leftSP)) - top_indices = np.linspace(0, len(topSP)-1, 100, dtype=int) + if len(topSP) > 100: + top_indices = np.linspace(0, len(topSP)-1, 100, dtype=int) + else: + top_indices = np.linspace(0, len(topSP)-1, len(topSP), dtype=int) + for s in topSP[top_indices]: pt = pg.PlotCurveItem(topSPwn, s, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) self.top_plot.addItem(pt, ignoreBounds=True) @@ -464,4 +472,6 @@ def commit(self): # pylint: disable=ungrouped-imports from Orange.widgets.utils.widgetpreview import WidgetPreview - WidgetPreview(OWCos).run(set_data1=Orange.data.Table("collagen"), set_data2=None) + data1 = Orange.data.Table("collagen")[100:230] + data2 = Orange.data.Table("collagen")[600:730] + WidgetPreview(OWCos).run(set_data1=data1, set_data2=data2) From bc46ac26c5fa0b5a9452c236bba24aa2e6a2380c Mon Sep 17 00:00:00 2001 From: "borondics.accounts@gmail.com" Date: Fri, 15 Nov 2024 11:42:46 +0100 Subject: [PATCH 6/8] Not ignoring division by zero until Orange allows numpy>2.0. --- orangecontrib/spectroscopy/widgets/owcos.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index 634b208db..ee6de141b 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -42,8 +42,10 @@ def calc_cos(table1, table2): # Hilbert-Noda transformation matrix i, j = np.ogrid[:len(series1), :len(series1)] - with np.errstate(divide='ignore'): - hn = np.where(i != j, 1 / (np.pi * (j - i)), 0) + + # TODO - We could use the code below after allowing numpy>2.0 + # with np.errstate(divide='ignore'): + hn = np.where(i != j, 1 / (np.pi * (j - i)), 0) # asynchronous correlation asyn = series1.T @ hn @ series2 / (len(series1) - 1) From a6f91541c73847b6e09e476328f746d094e6bf71 Mon Sep 17 00:00:00 2001 From: Marko Toplak Date: Wed, 20 Nov 2024 19:39:22 +0100 Subject: [PATCH 7/8] lint --- orangecontrib/spectroscopy/widgets/owcos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index ee6de141b..ce2e840ba 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -15,7 +15,7 @@ from orangewidget.utils.visual_settings_dlg import VisualSettingsDialog from orangecontrib.spectroscopy.data import getx from orangecontrib.spectroscopy.widgets.owhyper import ImageColorLegend -from orangecontrib.spectroscopy.widgets.owspectra import InteractiveViewBox, PlotCurvesItem +from orangecontrib.spectroscopy.widgets.owspectra import InteractiveViewBox from orangecontrib.spectroscopy.widgets.gui import float_to_str_decimals as strdec, pixel_decimals From 33fc9103fc389cb15d1691bc6fecb1f04b701270 Mon Sep 17 00:00:00 2001 From: Marko Toplak Date: Wed, 20 Nov 2024 19:42:03 +0100 Subject: [PATCH 8/8] simplify code --- orangecontrib/spectroscopy/widgets/owcos.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/orangecontrib/spectroscopy/widgets/owcos.py b/orangecontrib/spectroscopy/widgets/owcos.py index ce2e840ba..227550c20 100644 --- a/orangecontrib/spectroscopy/widgets/owcos.py +++ b/orangecontrib/spectroscopy/widgets/owcos.py @@ -419,10 +419,7 @@ def plotCOS(self): self.cbarCOS.set_range(-1 * np.nanmax(np.absolute(cosmat)), np.nanmax(np.absolute(cosmat))) self.cbarCOS.set_colors(np.array(colorcet.diverging_bwr_40_95_c42) * 255) - if len(leftSP) > 100: - left_indices = np.linspace(0, len(leftSP)-1, 100, dtype=int) - else: - left_indices = np.linspace(0, len(leftSP)-1, len(leftSP), dtype=int) + left_indices = np.linspace(0, len(leftSP)-1, min(100, len(leftSP)), dtype=int) for s in leftSP[left_indices]: pt = pg.PlotCurveItem(s, leftSPwn, pen=pg.mkPen(color=(50, 50, 50), width=0.5)) @@ -433,10 +430,7 @@ def plotCOS(self): self.left_plot.setXRange(np.min(leftSP), np.max(leftSP)) - if len(topSP) > 100: - top_indices = np.linspace(0, len(topSP)-1, 100, dtype=int) - else: - top_indices = np.linspace(0, len(topSP)-1, len(topSP), dtype=int) + top_indices = np.linspace(0, len(topSP)-1, min(100, len(topSP)), dtype=int) for s in topSP[top_indices]: pt = pg.PlotCurveItem(topSPwn, s, pen=pg.mkPen(color=(50, 50, 50), width=0.5))