Skip to content

Commit fede90f

Browse files
authored
fix bug in FilterObjects_StringMatch, add more settings (#265)
1 parent 088d8ff commit fede90f

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

active_plugins/filterobjects_stringmatch.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121

2222
LOGGER = logging.getLogger(__name__)
2323

24-
METHOD_EXACT = "Exact match"
25-
METHOD_CONTAINS = "String contains"
24+
METHOD_EXACT = "Filter out strings matching"
25+
METHOD_CONTAINS = "Filter out strings containing"
26+
METHOD_KEEP_EXACT = "Keep only strings matching"
27+
METHOD_KEEP_CONTAINS = "Keep only strings containing"
2628

2729
class FilterObjects_StringMatch(ObjectProcessing):
2830
module_name = "FilterObjects_StringMatch"
2931

30-
variable_revision_number = 1
32+
variable_revision_number = 2
3133

3234
def __init__(self):
3335
self.rules = Rules()
@@ -48,18 +50,21 @@ def create_settings(self):
4850
self.spacer_1 = Divider(line=False)
4951

5052
self.filter_out = Alphanumeric(
51-
"What string to filter out",
53+
"String to use for filter",
5254
"AAAA",
5355
doc="""Enter the string that should be used to filter objects.""",
5456
)
5557

5658
self.filter_method = Choice(
5759
"Filter method",
58-
[METHOD_EXACT, METHOD_CONTAINS],
59-
doc="""Select whether to only filter objects that are an exact match for the string entered
60+
[METHOD_EXACT, METHOD_CONTAINS, METHOD_KEEP_EXACT, METHOD_KEEP_CONTAINS],
61+
doc="""Select whether to filter out objects that are an exact match for the string entered
6062
(e.g. Object 'AAAAB' will NOT be filtered by string 'AAAA')
61-
or to filter any object that contains the string entered
62-
(e.g. Object 'AAAAB' will be filtered by string 'AAAA').""",
63+
to filter any object that contains the string entered
64+
(e.g. Object 'AAAAB' will be filtered by string 'AAAA'), to keep only objects that
65+
are an exact match for the string entered (e.g. Only 'AAAA' objects will be kept by string
66+
'AAAA'), or keep only objects that contain the string entered (e.g. 'AAAAB' and 'AAAAA' objects
67+
but not 'AAAB' objects will be kept by string 'AAAA').""",
6368
)
6469

6570
self.filter_column = Measurement("Measurement",
@@ -186,18 +191,25 @@ def keep_by_string(self, workspace, src_objects):
186191
src_name = self.x_name.value
187192
m = workspace.measurements
188193
values = m.get_current_measurement(src_name, self.filter_column.value)
194+
# keep hits
189195
if self.filter_method == METHOD_EXACT:
190-
# Is this structure still necessary or is it an artifact?
191-
# Could be just values == self.filter_out.value
192-
# Make an array of True
193-
hits = numpy.ones(len(values), bool)
194-
# Fill with False for those where we want to filter out
195-
hits[values == self.filter_out.value] = False
196+
hits = [self.filter_out.value != x for x in values]
197+
elif self.filter_method == METHOD_KEEP_EXACT:
198+
hits = [self.filter_out.value == x for x in values]
196199
elif self.filter_method == METHOD_CONTAINS:
200+
hits = [self.filter_out.value not in x for x in values]
201+
elif self.filter_method == METHOD_KEEP_CONTAINS:
197202
hits = [self.filter_out.value in x for x in values]
198203
# Get object numbers for things that are True
199204
indexes = numpy.argwhere(hits)[:, 0]
200205
# Objects are 1 counted, Python is 0 counted
201206
indexes = indexes + 1
202207

203208
return indexes
209+
210+
def upgrade_settings(self, setting_values, variable_revision_number, module_name):
211+
if variable_revision_number == 1:
212+
setting_values = [value.replace('Exact match','Filter out strings matching') for value in setting_values]
213+
setting_values = [value.replace('String contains','Filter out strings containing') for value in setting_values]
214+
variable_revision_number = 2
215+
return setting_values, variable_revision_number

0 commit comments

Comments
 (0)