Skip to content

Commit 677fb7a

Browse files
authored
add multiple filters in filterobjects_stringmatch (#266)
1 parent fede90f commit 677fb7a

1 file changed

Lines changed: 56 additions & 4 deletions

File tree

active_plugins/filterobjects_stringmatch.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from cellprofiler_core.module.image_segmentation import ObjectProcessing
2-
from cellprofiler_core.setting import (
3-
Divider,
4-
)
2+
from cellprofiler_core.setting import Divider
53
from cellprofiler_core.setting.text import Alphanumeric
64
from cellprofiler_core.setting.choice import Choice
7-
from cellprofiler_core.setting import Measurement
5+
from cellprofiler_core.setting import Measurement, HiddenCount, SettingsGroup
6+
from cellprofiler_core.setting.do_something import DoSomething, RemoveSettingButton
87

98
__doc__ = ""
109

@@ -73,11 +72,48 @@ def create_settings(self):
7372
doc="""Select the measurement column that will be used for filtering.""",
7473
)
7574

75+
self.additional_strings = []
76+
77+
self.additional_string_count = HiddenCount(
78+
self.additional_strings, "Additional string count"
79+
)
80+
81+
self.spacer_2 = Divider(line=True)
82+
83+
self.additional_string_button = DoSomething(
84+
"Add an additional string to use to filter objects?",
85+
"Add an additional string",
86+
self.add_additional_string,
87+
doc="""\
88+
Click this button to add an additional string to apply to the objects with the same rules.""",
89+
)
90+
7691
self.rules.create_settings()
7792

93+
def add_additional_string(self):
94+
group = SettingsGroup()
95+
group.append(
96+
"additional_string",
97+
Alphanumeric(
98+
"String to use for additional filter",
99+
"AAAA",
100+
doc="""Enter the string that should be used to filter objects.""",
101+
),
102+
)
103+
group.append(
104+
"remover",
105+
RemoveSettingButton(
106+
"", "Remove this additional string", self.additional_strings, group
107+
),
108+
)
109+
group.append("divider", Divider(line=False))
110+
self.additional_strings.append(group)
111+
78112
def settings(self):
79113
settings = super(FilterObjects_StringMatch, self).settings()
80114
settings += [self.filter_out,self.filter_method, self.filter_column]
115+
for x in self.additional_strings:
116+
settings += [self.filter_out]
81117
return settings
82118

83119
def visible_settings(self):
@@ -87,6 +123,10 @@ def visible_settings(self):
87123
self.filter_method,
88124
self.filter_column
89125
]
126+
if self.filter_method != METHOD_KEEP_EXACT:
127+
for x in self.additional_strings:
128+
visible_settings += x.visible_settings()
129+
visible_settings += [self.additional_string_button]
90130
return visible_settings
91131

92132
def run(self, workspace):
@@ -194,12 +234,24 @@ def keep_by_string(self, workspace, src_objects):
194234
# keep hits
195235
if self.filter_method == METHOD_EXACT:
196236
hits = [self.filter_out.value != x for x in values]
237+
if self.additional_strings:
238+
for group in self.additional_strings:
239+
more_hits = [group.additional_string.value != x for x in values]
240+
hits = [a and b for a, b in zip(hits, more_hits)]
197241
elif self.filter_method == METHOD_KEEP_EXACT:
198242
hits = [self.filter_out.value == x for x in values]
199243
elif self.filter_method == METHOD_CONTAINS:
200244
hits = [self.filter_out.value not in x for x in values]
245+
if self.additional_strings:
246+
for group in self.additional_strings:
247+
more_hits = [group.additional_string.value not in x for x in values]
248+
hits = [a and b for a, b in zip(hits, more_hits)]
201249
elif self.filter_method == METHOD_KEEP_CONTAINS:
202250
hits = [self.filter_out.value in x for x in values]
251+
if self.additional_strings:
252+
for group in self.additional_strings:
253+
more_hits = [group.additional_string.value in x for x in values]
254+
hits = [a and b for a, b in zip(hits, more_hits)]
203255
# Get object numbers for things that are True
204256
indexes = numpy.argwhere(hits)[:, 0]
205257
# Objects are 1 counted, Python is 0 counted

0 commit comments

Comments
 (0)