Skip to content

Commit 6c1a47c

Browse files
Very cool bug fix for img callback not updating
1 parent dc676d8 commit 6c1a47c

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

Diff for: app/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def create_app(config_class=Config):
140140

141141

142142
def register_dashapps(app):
143-
"""Function to regeister the dash application to the flask app.
143+
"""Function to register the dash application to the flask app.
144144
145145
Args:
146146
app (Flask Application Object): Our flask application object.

Diff for: app/dashapp/callbacks.py

+33-31
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@
3737

3838

3939
onto_tree_imgannot = common_func.load_onto()
40+
class_label_colormap = {
41+
int(term["id"].split(":")[1]): term["data"]["hex_color"]
42+
for term in onto_tree_imgannot
43+
if term["data"]["image_annotation"] is True
44+
}
45+
class_labels = [
46+
int(term["id"].split(":")[1])
47+
for term in onto_tree_imgannot
48+
if term["data"]["image_annotation"] is True
49+
]
50+
button_list = [
51+
int(i["id"].split(":")[1])
52+
for i in onto_tree_imgannot
53+
if i["data"]["image_annotation"] is True
54+
]
55+
NUM_LABEL_CLASSES = len(class_label_colormap)
56+
DEFAULT_LABEL_CLASS = class_labels[0]
57+
58+
# we can't have less colors than classes
59+
assert NUM_LABEL_CLASSES <= len(class_label_colormap) # nosec
4060

4161

4262
def class_to_color(ontology, class_number):
@@ -51,13 +71,13 @@ def color_to_class(ontology, color_hex):
5171
return int(term["id"].split(":")[1])
5272

5373

54-
# def class_to_file(seg_matrix, onto_tree, save_path):
74+
# def class_to_file(seg_matrix, onto_tree_imgannot, save_path):
5575
# unique_classes = np.unique(seg_matrix)
5676
# col_df = {"class": [], "color": [], "onto_id": []}
5777
# for class_img in unique_classes:
5878
# col_df["class"].append(class_img)
59-
# col_df["color"].append(onto_tree[class_img - 1]["data"]["hex_color"])
60-
# col_df["onto_id"].append(onto_tree[class_img - 1]["id"])
79+
# col_df["color"].append(onto_tree_imgannot[class_img - 1]["data"]["hex_color"])
80+
# col_df["onto_id"].append(onto_tree_imgannot[class_img - 1]["id"])
6181
# df = pd.DataFrame.from_dict(col_df)
6282
# df.to_csv(save_path, index=False)
6383

@@ -100,7 +120,12 @@ def save_img_classifier(clf, label_to_colors_args, segmenter_args):
100120

101121

102122
def show_segmentation(
103-
image_path, mask_shapes, features, segmenter_args, class_label_colormap, onto_tree
123+
image_path,
124+
mask_shapes,
125+
features,
126+
segmenter_args,
127+
class_label_colormap,
128+
onto_tree_imgannot,
104129
):
105130
"""adds an image showing segmentations to a figure's layout"""
106131
# add 1 because classifier takes 0 to mean no mask
@@ -109,7 +134,8 @@ def show_segmentation(
109134
# for shape in mask_shapes
110135
# ]
111136
shape_layers = [
112-
color_to_class(onto_tree, shape["line"]["color"]) for shape in mask_shapes
137+
color_to_class(onto_tree_imgannot, shape["line"]["color"])
138+
for shape in mask_shapes
113139
]
114140
label_to_colors_args = {
115141
"colormap": class_label_colormap,
@@ -166,30 +192,6 @@ def annotation_react(
166192
sigma_range_slider_value,
167193
masks_data,
168194
):
169-
with open(os.path.join("data/ontology", "ontology.json"), "r") as fp:
170-
onto_tree = json.load(fp)
171-
class_label_colormap = {
172-
int(term["id"].split(":")[1]): term["data"]["hex_color"]
173-
for term in onto_tree_imgannot
174-
if term["data"]["image_annotation"] is True
175-
}
176-
class_labels = [
177-
int(term["id"].split(":")[1])
178-
for term in onto_tree
179-
if term["data"]["image_annotation"] is True
180-
]
181-
button_list = [
182-
int(i["id"].split(":")[1])
183-
for i in onto_tree_imgannot
184-
if i["data"]["image_annotation"] is True
185-
]
186-
NUM_LABEL_CLASSES = len(class_label_colormap)
187-
DEFAULT_LABEL_CLASS = class_labels[0]
188-
DEFAULT_STROKE_WIDTH = 3 # gives line width of 2^3 = 8
189-
190-
# we can't have less colors than classes
191-
assert NUM_LABEL_CLASSES <= len(class_label_colormap) # nosec
192-
193195
classified_image_store_data = dash.no_update
194196
classifier_store_data = dash.no_update
195197
alertbox = html.Div()
@@ -254,7 +256,7 @@ def annotation_react(
254256
# label_class_value = class_labels[label_class_value]
255257
fig = make_default_figure(
256258
images=[image.image_path],
257-
stroke_color=class_to_color(onto_tree, label_class_value),
259+
stroke_color=class_to_color(onto_tree_imgannot, label_class_value),
258260
stroke_width=stroke_width,
259261
shapes=masks_data["shapes"],
260262
source_img=source,
@@ -277,7 +279,7 @@ def annotation_react(
277279
features,
278280
feature_opts,
279281
class_label_colormap,
280-
onto_tree,
282+
onto_tree_imgannot,
281283
)
282284
if cbcontext == "download-button.n_clicks":
283285
classifier_store_data = clf

Diff for: app/ontocreate/routes.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ def modify_onto():
9595
db.session.commit()
9696

9797
# Update The DashApp Callback & layout
98-
# By Force reloading the layout code
98+
# By Force reloading the layout code & callbacks
9999
dashapp = current_app.config["DASHAPP"]
100100
with current_app.app_context():
101101
import importlib
102+
import sys
102103

104+
importlib.reload(sys.modules["app.dashapp.callbacks"])
103105
import app.dashapp.layout
104106

105107
importlib.reload(app.dashapp.layout)
@@ -165,11 +167,13 @@ def invert_lang():
165167
generate_stat_per(df, features_col, onto)
166168

167169
# Update The DashApp Callback & layout
168-
# By Force reloading the layout code
170+
# By Force reloading the layout code & callbacks
169171
dashapp = current_app.config["DASHAPP"]
170172
with current_app.app_context():
171173
import importlib
174+
import sys
172175

176+
importlib.reload(sys.modules["app.dashapp.callbacks"])
173177
import app.dashapp.layout
174178

175179
importlib.reload(app.dashapp.layout)

0 commit comments

Comments
 (0)