-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
173 lines (144 loc) · 5.19 KB
/
Copy path__init__.py
File metadata and controls
173 lines (144 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""Double Band Filter plugin.
This plugin provides an operator to filter a float-valued field on two ranges
simultaneously.
|
"""
import fiftyone as fo
import fiftyone.operators as foo
from fiftyone.operators import types
from fiftyone import ViewField as F
def _get_float_fields(dataset):
return list(dataset.get_field_schema(ftype=fo.FloatField).keys())
def _filter_view(view, left_min, left_max, right_min, right_max, field):
left_cond = (F(field) > left_min) & (F(field) < left_max)
right_cond = (F(field) > right_min) & (F(field) < right_max)
return view.match(left_cond | right_cond)
class MatchEitherBand(foo.Operator):
@property
def config(self):
_config = foo.OperatorConfig(
name="match_either_band",
label="Double Band Filter: filter a float-valued field on two ranges simultaneously",
dynamic=True,
)
_config.icon = "/assets/icon.svg"
return _config
def resolve_input(self, ctx):
inputs = types.Object()
form_view = types.View(
label="Double Band Filter: filter a float-valued field on two ranges simultaneously"
)
float_fields = _get_float_fields(ctx.dataset)
if len(float_fields) == 0:
inputs.view(
"warning",
types.Warning(
label="No float-valued fields found in dataset",
description="Message description",
),
)
return types.Property(inputs, view=form_view)
field_choices = types.RadioGroup()
for ff in float_fields:
field_choices.add_choice(ff, label=ff)
inputs.enum(
"field",
field_choices.values(),
label="Float-valued field to filter",
description="Select a float-valued field to filter",
view=types.DropdownView(),
required=True,
)
field = ctx.params.get("field", None)
if field:
min, max = ctx.view.bounds(field)
else:
min, max = 0, 1
obj = types.Object()
obj.float(
"left_min",
label="Left Min",
description="Minimum value for left band",
view=types.FieldView(space=3),
)
obj.float(
"left_max",
label="Left Max",
description="Maximum value for left band",
required=True,
view=types.FieldView(space=3),
)
obj.float(
"right_min",
label="Right Min",
description="Minimum value for right band",
required=True,
view=types.FieldView(space=3),
)
obj.float(
"right_max",
label="Right Max",
description="Maximum value for right band",
required=True,
view=types.FieldView(space=3),
)
inputs.define_property("range_bounds", obj)
range_bounds = ctx.params.get("range_bounds", {})
left_min = range_bounds.get("left_min", None)
left_max = range_bounds.get("left_max", None)
right_min = range_bounds.get("right_min", None)
right_max = range_bounds.get("right_max", None)
if left_min and min and left_min < min:
inputs.view(
"error",
types.Error(
label="Out of bounds: Left",
description="Left Min is out of bounds",
),
)
elif left_min and left_max and left_min > left_max:
inputs.view(
"error",
types.Error(
label="Empty Left Band",
description="Left Min is greater than Left Max",
),
)
elif right_max and max and right_max > max:
inputs.view(
"error",
types.Error(
label="Out of bounds: Right",
description="Right Max is out of bounds",
),
)
elif right_min and right_max and right_min > right_max:
inputs.view(
"error",
types.Error(
label="Empty Right Band",
description="Right Min is greater than Right Max",
),
)
elif left_max and right_min and left_max > right_min:
inputs.view(
"error",
types.Error(
label="Overlapping Bands",
description="Left Max is greater than Right Min",
),
)
return types.Property(inputs, view=form_view)
def execute(self, ctx):
field = ctx.params.get("field", None)
range_bounds = ctx.params.get("range_bounds", {})
left_min = range_bounds.get("left_min", None)
left_max = range_bounds.get("left_max", None)
right_min = range_bounds.get("right_min", None)
right_max = range_bounds.get("right_max", None)
view = _filter_view(
ctx.view, left_min, left_max, right_min, right_max, field
)
ctx.ops.set_view(view=view)
def register(plugin):
plugin.register(MatchEitherBand)