Skip to content

Commit 3da8f49

Browse files
WIP: add Form.useWatch to update value whenever form changes
1 parent 420f648 commit 3da8f49

File tree

1 file changed

+39
-70
lines changed

1 file changed

+39
-70
lines changed

frontend/javascripts/oxalis/view/jobs/train_ai_model.tsx

Lines changed: 39 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ export function TrainAiModelFromAnnotationTab({ onClose }: { onClose: () => void
158158
);
159159
}
160160

161-
type MagInfoWithAnnotationId = { annotationId: string; magInfo: MagInfo };
161+
type TrainingAnnotation = {
162+
annotationId: string;
163+
imageDataLayer: string;
164+
layerName: string;
165+
mag: Vector3;
166+
};
162167

163168
export function TrainAiModelTab<GenericAnnotation extends APIAnnotation | HybridTracing>({
164169
getMagsForSegmentationLayer,
@@ -174,8 +179,28 @@ export function TrainAiModelTab<GenericAnnotation extends APIAnnotation | Hybrid
174179
onAddAnnotationsInfos?: (newItems: Array<AnnotationInfoForAIJob<APIAnnotation>>) => void;
175180
}) {
176181
const [form] = Form.useForm();
182+
183+
const magInfoPerLayer: Array<MagInfo> | null = Form.useWatch(() => {
184+
const getIntersectingMags = (idx: number, annotationId: string, dataset: APIDataset) => {
185+
console.log("getIntersectingMags");
186+
const segmentationLayerName = form.getFieldValue(["trainingAnnotations", idx, "layerName"]);
187+
const imageDataLayerName = form.getFieldValue(["trainingAnnotations", idx, "imageDataLayer"]);
188+
if (segmentationLayerName != null && imageDataLayerName != null) {
189+
return new MagInfo(
190+
getIntersectingMagList(annotationId, dataset, segmentationLayerName, imageDataLayerName),
191+
);
192+
}
193+
return new MagInfo([]);
194+
};
195+
196+
return form.getFieldValue("trainingAnnotations").map((_: TrainingAnnotation, idx: number) => {
197+
const annotation = annotationInfos[idx].annotation;
198+
const annotationId = "id" in annotation ? annotation.id : annotation.annotationId;
199+
return getIntersectingMags(idx, annotationId, annotationInfos[idx].dataset);
200+
});
201+
}, form);
202+
177203
const [useCustomWorkflow, setUseCustomWorkflow] = React.useState(false);
178-
const [mags, setMags] = useState<MagInfoWithAnnotationId[]>();
179204

180205
const getIntersectingMagList = (
181206
annotationId: string,
@@ -195,60 +220,22 @@ export function TrainAiModelTab<GenericAnnotation extends APIAnnotation | Hybrid
195220
);
196221
};
197222

198-
const getIntersectingMags = (
199-
annotationId: string,
200-
dataset: APIDataset,
201-
groundTruthLayerName: string,
202-
imageDataLayerName: string,
203-
) => {
204-
const intersectingMags = getIntersectingMagList(
205-
annotationId,
206-
dataset,
207-
groundTruthLayerName,
208-
imageDataLayerName,
209-
);
210-
if (mags == null) {
211-
return [{ annotationId, magInfo: new MagInfo(intersectingMags) }];
212-
} else {
213-
return mags.map((mag) =>
214-
mag.annotationId === annotationId
215-
? { annotationId, magInfo: new MagInfo(intersectingMags) }
216-
: mag,
217-
);
218-
}
219-
};
220-
221-
const setIntersectingMags = (
222-
annotationId: string,
223-
dataset: APIDataset,
224-
groundTruthLayerName: string,
225-
imageDataLayerName: string,
226-
) =>
227-
setMags(getIntersectingMags(annotationId, dataset, groundTruthLayerName, imageDataLayerName));
228-
229223
const getMagsForColorLayer = (colorLayers: APIDataLayer[], layerName: string) => {
230224
const colorLayer = colorLayers.find((layer) => layer.name === layerName);
231225
return colorLayer != null ? getMagInfo(colorLayer.resolutions).getMagList() : null;
232226
};
233227

234228
const getTrainingAnnotations = async (values: any) => {
235229
return Promise.all(
236-
values.trainingAnnotations.map(
237-
async (trainingAnnotation: {
238-
annotationId: string;
239-
imageDataLayer: string;
240-
layerName: string;
241-
mag: Vector3;
242-
}) => {
243-
const { annotationId, imageDataLayer, layerName, mag } = trainingAnnotation;
244-
return {
245-
annotationId,
246-
colorLayerName: imageDataLayer,
247-
segmentationLayerName: layerName,
248-
mag,
249-
};
250-
},
251-
),
230+
values.trainingAnnotations.map(async (trainingAnnotation: TrainingAnnotation) => {
231+
const { annotationId, imageDataLayer, layerName, mag } = trainingAnnotation;
232+
return {
233+
annotationId,
234+
colorLayerName: imageDataLayer,
235+
segmentationLayerName: layerName,
236+
mag,
237+
};
238+
}),
252239
);
253240
};
254241

@@ -347,24 +334,7 @@ export function TrainAiModelTab<GenericAnnotation extends APIAnnotation | Hybrid
347334
const fixedSelectedColorLayer = colorLayers.length === 1 ? colorLayers[0] : null;
348335
const annotationId = "id" in annotation ? annotation.id : annotation.annotationId;
349336

350-
const initialMags =
351-
fixedSelectedColorLayer != null && fixedSelectedSegmentationLayer != null
352-
? getIntersectingMagList(
353-
annotationId,
354-
dataset,
355-
fixedSelectedSegmentationLayer.name,
356-
fixedSelectedColorLayer.name,
357-
)
358-
: [];
359-
const initialMagInfo = new MagInfo(initialMags);
360-
361337
const onChangeLayer = () => {
362-
setIntersectingMags(
363-
annotationId,
364-
dataset,
365-
form.getFieldValue(["trainingAnnotations", idx, "layerName"]),
366-
form.getFieldValue(["trainingAnnotations", idx, "imageDataLayer"]),
367-
);
368338
form.setFieldValue(["trainingAnnotations", idx, "mag"], undefined);
369339
};
370340

@@ -421,10 +391,9 @@ export function TrainAiModelTab<GenericAnnotation extends APIAnnotation | Hybrid
421391
<MagSelectionFormItem
422392
name={["trainingAnnotations", idx, "mag"]}
423393
magInfo={
424-
mags != null
425-
? mags.find((magInfoForAnno) => magInfoForAnno.annotationId === annotationId)
426-
?.magInfo
427-
: initialMagInfo
394+
magInfoPerLayer != null && magInfoPerLayer[idx] != null
395+
? magInfoPerLayer[idx]
396+
: new MagInfo([])
428397
}
429398
/>
430399
</Col>

0 commit comments

Comments
 (0)