From 9339e15399321e378c5c530de7fbb07f42907a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Tue, 15 Jul 2025 16:28:43 +0200 Subject: [PATCH 1/2] make dwa filtering within a dte not sorted by name by default but only if using a True argument --- src/pymodaq_data/data.py | 31 +++++++++++++++++++++++-------- tests/data_test.py | 14 ++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/pymodaq_data/data.py b/src/pymodaq_data/data.py index ec231960..e83555c6 100644 --- a/src/pymodaq_data/data.py +++ b/src/pymodaq_data/data.py @@ -3210,7 +3210,7 @@ def get_dim_presents(self) -> List[str]: return dims - def get_data_from_source(self, source: DataSource, deepcopy=False) -> DataToExport: + def get_data_from_source(self, source: DataSource, deepcopy=False, sort_name=False) -> DataToExport: """Get the data matching the given DataSource Returns @@ -3218,7 +3218,7 @@ def get_data_from_source(self, source: DataSource, deepcopy=False) -> DataToExpo DataToExport: filtered with data matching the dimensionality """ source = enum_checker(DataSource, source) - return self.get_data_from_attribute('source', source, deepcopy=deepcopy) + return self.get_data_from_attribute('source', source, deepcopy=deepcopy, sort_name=sort_name) def get_data_from_missing_attribute(self, attribute: str, deepcopy=False) -> DataToExport: """ Get the data matching a given attribute value @@ -3238,23 +3238,38 @@ def get_data_from_missing_attribute(self, attribute: str, deepcopy=False) -> Dat else: return DataToExport(self.name, data=[dwa for dwa in self if not hasattr(dwa, attribute)]) - def get_data_from_attribute(self, attribute: str, attribute_value: Any, deepcopy=False) -> DataToExport: + def get_data_from_attribute(self, attribute: str, + attribute_value: Any, + deepcopy=False, + sort_name=False) -> DataToExport: """Get the data matching a given attribute value + Parameters + ---------- + attribute: str + The name of the attribute to sort data with + attribute_value: Any + The value of the attribute + deepcopy: bool + If True, the returned data are deepcopied from the original + sort_name: bool + If True the returned data are sorted alphabetically using their name, default is False + Returns ------- DataToExport: filtered with data matching the attribute presence and value """ selection = find_objects_in_list_from_attr_name_val(self.data, attribute, attribute_value, return_first=False) - selection.sort(key=lambda elt: elt[0].name) + if sort_name: + selection.sort(key=lambda elt: elt[0].name) if deepcopy: data = [sel[0].deepcopy() for sel in selection] else: data = [sel[0] for sel in selection] return DataToExport(name=self.name, data=data) - def get_data_from_dim(self, dim: DataDim, deepcopy=False) -> DataToExport: + def get_data_from_dim(self, dim: DataDim, deepcopy=False, sort_name=False) -> DataToExport: """Get the data matching the given DataDim Returns @@ -3262,9 +3277,9 @@ def get_data_from_dim(self, dim: DataDim, deepcopy=False) -> DataToExport: DataToExport: filtered with data matching the dimensionality """ dim = enum_checker(DataDim, dim) - return self.get_data_from_attribute('dim', dim, deepcopy=deepcopy) + return self.get_data_from_attribute('dim', dim, deepcopy=deepcopy, sort_name=sort_name) - def get_data_from_dims(self, dims: List[DataDim], deepcopy=False) -> DataToExport: + def get_data_from_dims(self, dims: List[DataDim], deepcopy=False, sort_name=False) -> DataToExport: """Get the data matching the given DataDim Returns @@ -3273,7 +3288,7 @@ def get_data_from_dims(self, dims: List[DataDim], deepcopy=False) -> DataToExpor """ data = DataToExport(name=self.name) for dim in dims: - data.append(self.get_data_from_dim(dim, deepcopy=deepcopy)) + data.append(self.get_data_from_dim(dim, deepcopy=deepcopy, sort_name=sort_name)) return data def get_data_from_sig_axes(self, Naxes: int, deepcopy: bool = False) -> DataToExport: diff --git a/tests/data_test.py b/tests/data_test.py index 64828b99..96c23c17 100644 --- a/tests/data_test.py +++ b/tests/data_test.py @@ -1224,20 +1224,22 @@ def test_get_data_from_missing_attribute(self): def test_get_data_by_dim(self, ini_data_to_export): dat1, dat2, data = ini_data_to_export - assert len(data.get_data_from_dim(data_mod.DataDim['Data0D'])) == 0 + assert len(data.get_data_from_dim(data_mod.DataDim.Data0D)) == 0 dat3 = init_data(data=DATA0D, Ndata=1, name='data0D') data.append(dat3) assert isinstance(data.get_data_from_dim('Data0D'), data_mod.DataToExport) assert data.get_data_from_dim('Data0D').data == [dat3] - assert data.get_data_from_dim(data_mod.DataDim['Data0D']).data == [dat3] + assert data.get_data_from_dim(data_mod.DataDim.Data0D).data == [dat3] - assert data.get_data_from_dim(data_mod.DataDim['Data1D']).data == [dat2] - assert data.get_data_from_dim(data_mod.DataDim['Data2D']).data == [dat1] + assert data.get_data_from_dim(data_mod.DataDim.Data1D).data == [dat2] + assert data.get_data_from_dim(data_mod.DataDim.Data2D).data == [dat1] - dat4 = init_data(data=DATA2D, Ndata=1, name='data2Dbis') + dat4 = init_data(data=DATA2D, Ndata=1, name='0_data2Dbis') + # '0_data2Dbis' 0 is meant for the name to show first if filtered data are sorted data.append(dat4) - assert data.get_data_from_dim(data_mod.DataDim['Data2D']).data == [dat1, dat4] + assert data.get_data_from_dim(data_mod.DataDim.Data2D).data == [dat1, dat4] + assert data.get_data_from_dim(data_mod.DataDim.Data2D, sort_name=True).data == [dat4, dat1] def test_get_data_from_name(self, ini_data_to_export): dat1, dat2, data = ini_data_to_export From 5230974b902f0d8aa200b43f708b8298c4d5aae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Wed, 16 Jul 2025 11:57:08 +0200 Subject: [PATCH 2/2] change named attribute to sort_by_name to make it more boolean --- src/pymodaq_data/data.py | 18 +++++++++--------- tests/data_test.py | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/pymodaq_data/data.py b/src/pymodaq_data/data.py index e83555c6..6e0f492f 100644 --- a/src/pymodaq_data/data.py +++ b/src/pymodaq_data/data.py @@ -3210,7 +3210,7 @@ def get_dim_presents(self) -> List[str]: return dims - def get_data_from_source(self, source: DataSource, deepcopy=False, sort_name=False) -> DataToExport: + def get_data_from_source(self, source: DataSource, deepcopy=False, sort_by_name=False) -> DataToExport: """Get the data matching the given DataSource Returns @@ -3218,7 +3218,7 @@ def get_data_from_source(self, source: DataSource, deepcopy=False, sort_name=Fal DataToExport: filtered with data matching the dimensionality """ source = enum_checker(DataSource, source) - return self.get_data_from_attribute('source', source, deepcopy=deepcopy, sort_name=sort_name) + return self.get_data_from_attribute('source', source, deepcopy=deepcopy, sort_by_name=sort_by_name) def get_data_from_missing_attribute(self, attribute: str, deepcopy=False) -> DataToExport: """ Get the data matching a given attribute value @@ -3241,7 +3241,7 @@ def get_data_from_missing_attribute(self, attribute: str, deepcopy=False) -> Dat def get_data_from_attribute(self, attribute: str, attribute_value: Any, deepcopy=False, - sort_name=False) -> DataToExport: + sort_by_name=False) -> DataToExport: """Get the data matching a given attribute value Parameters @@ -3252,7 +3252,7 @@ def get_data_from_attribute(self, attribute: str, The value of the attribute deepcopy: bool If True, the returned data are deepcopied from the original - sort_name: bool + sort_by_name: bool If True the returned data are sorted alphabetically using their name, default is False Returns @@ -3261,7 +3261,7 @@ def get_data_from_attribute(self, attribute: str, """ selection = find_objects_in_list_from_attr_name_val(self.data, attribute, attribute_value, return_first=False) - if sort_name: + if sort_by_name: selection.sort(key=lambda elt: elt[0].name) if deepcopy: data = [sel[0].deepcopy() for sel in selection] @@ -3269,7 +3269,7 @@ def get_data_from_attribute(self, attribute: str, data = [sel[0] for sel in selection] return DataToExport(name=self.name, data=data) - def get_data_from_dim(self, dim: DataDim, deepcopy=False, sort_name=False) -> DataToExport: + def get_data_from_dim(self, dim: DataDim, deepcopy=False, sort_by_name=False) -> DataToExport: """Get the data matching the given DataDim Returns @@ -3277,9 +3277,9 @@ def get_data_from_dim(self, dim: DataDim, deepcopy=False, sort_name=False) -> Da DataToExport: filtered with data matching the dimensionality """ dim = enum_checker(DataDim, dim) - return self.get_data_from_attribute('dim', dim, deepcopy=deepcopy, sort_name=sort_name) + return self.get_data_from_attribute('dim', dim, deepcopy=deepcopy, sort_by_name=sort_by_name) - def get_data_from_dims(self, dims: List[DataDim], deepcopy=False, sort_name=False) -> DataToExport: + def get_data_from_dims(self, dims: List[DataDim], deepcopy=False, sort_by_name=False) -> DataToExport: """Get the data matching the given DataDim Returns @@ -3288,7 +3288,7 @@ def get_data_from_dims(self, dims: List[DataDim], deepcopy=False, sort_name=Fals """ data = DataToExport(name=self.name) for dim in dims: - data.append(self.get_data_from_dim(dim, deepcopy=deepcopy, sort_name=sort_name)) + data.append(self.get_data_from_dim(dim, deepcopy=deepcopy, sort_by_name=sort_by_name)) return data def get_data_from_sig_axes(self, Naxes: int, deepcopy: bool = False) -> DataToExport: diff --git a/tests/data_test.py b/tests/data_test.py index 96c23c17..a2d1bff2 100644 --- a/tests/data_test.py +++ b/tests/data_test.py @@ -1239,7 +1239,7 @@ def test_get_data_by_dim(self, ini_data_to_export): # '0_data2Dbis' 0 is meant for the name to show first if filtered data are sorted data.append(dat4) assert data.get_data_from_dim(data_mod.DataDim.Data2D).data == [dat1, dat4] - assert data.get_data_from_dim(data_mod.DataDim.Data2D, sort_name=True).data == [dat4, dat1] + assert data.get_data_from_dim(data_mod.DataDim.Data2D, sort_by_name=True).data == [dat4, dat1] def test_get_data_from_name(self, ini_data_to_export): dat1, dat2, data = ini_data_to_export