11// Copyright (C) 2022-2025 Intel Corporation
22// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33
4- import { FC , ReactNode , useState } from 'react' ;
4+ import { FC , ReactNode } from 'react' ;
55
66import { Grid , minmax , Text , View } from '@geti/ui' ;
7- import { noop } from 'lodash-es' ;
87
9- import { ConfigurationParameter } from '../../../../../../../../core/configurable-parameters/services/configuration.interface' ;
8+ import {
9+ ConfigurationParameter ,
10+ TrainingConfiguration ,
11+ } from '../../../../../../../../core/configurable-parameters/services/configuration.interface' ;
12+ import { isBoolParameter } from '../../../../../../../../core/configurable-parameters/utils' ;
1013import { Accordion } from '../../ui/accordion/accordion.component' ;
1114import { Parameters } from '../../ui/parameters.component' ;
1215import { TILING_MODES , TilingModes } from './tiling-modes.component' ;
@@ -15,29 +18,105 @@ import styles from './tiling.module.scss';
1518
1619interface TilingProps {
1720 tilingParameters : ConfigurationParameter [ ] ;
21+ onUpdateTrainingConfiguration : (
22+ updateFunction : ( config : TrainingConfiguration | undefined ) => TrainingConfiguration | undefined
23+ ) => void ;
1824}
1925
20- const getTilingMode = ( tilingParameters : ConfigurationParameter [ ] ) : TILING_MODES => {
21- const adaptive = tilingParameters . find ( ( parameter ) => parameter . key === 'enable_adaptive_params' ) ;
22- const enablingTiling = tilingParameters . find ( ( parameter ) => parameter . key === 'enable_tiling' ) ;
26+ const ADAPTIVE_TILING_PARAMETER = 'adaptive_tiling' ;
27+ const ENABLE_TILING_PARAMETER = 'enable' ;
28+
29+ const getAdaptiveTilingParameter = ( tilingParameters : ConfigurationParameter [ ] ) => {
30+ const parameter = tilingParameters . find ( ( { key } ) => key === ADAPTIVE_TILING_PARAMETER ) ;
31+
32+ if ( parameter === undefined || ! isBoolParameter ( parameter ) ) {
33+ return undefined ;
34+ }
35+
36+ return parameter ;
37+ } ;
2338
24- if ( adaptive !== undefined && adaptive . value === true ) {
25- return TILING_MODES . Adaptive ;
39+ const getEnableTilingParameter = ( tilingParameters : ConfigurationParameter [ ] ) => {
40+ const parameter = tilingParameters . find ( ( { key } ) => key === ENABLE_TILING_PARAMETER ) ;
41+
42+ if ( parameter === undefined || ! isBoolParameter ( parameter ) ) {
43+ return undefined ;
2644 }
2745
28- if ( enablingTiling !== undefined && enablingTiling . value === false ) {
46+ return parameter ;
47+ } ;
48+
49+ const getTilingMode = ( tilingParameters : ConfigurationParameter [ ] ) : TILING_MODES => {
50+ const adaptive = getAdaptiveTilingParameter ( tilingParameters ) ;
51+ const enablingTiling = getEnableTilingParameter ( tilingParameters ) ;
52+
53+ if ( enablingTiling ?. value === false ) {
2954 return TILING_MODES . OFF ;
3055 }
3156
32- return TILING_MODES . Manual ;
57+ if ( adaptive ?. value === true ) {
58+ return TILING_MODES . ADAPTIVE ;
59+ }
60+
61+ return TILING_MODES . MANUAL ;
3362} ;
3463
35- export const Tiling : FC < TilingProps > = ( { tilingParameters } ) => {
36- const [ selectedTilingMode , setSelectedTilingMode ] = useState < TILING_MODES > ( ( ) => getTilingMode ( tilingParameters ) ) ;
64+ export const Tiling : FC < TilingProps > = ( { tilingParameters, onUpdateTrainingConfiguration } ) => {
65+ const selectedTilingMode = getTilingMode ( tilingParameters ) ;
66+
67+ const manualTilingParameters = tilingParameters . filter (
68+ ( parameter ) => ! [ ADAPTIVE_TILING_PARAMETER , ENABLE_TILING_PARAMETER ] . includes ( parameter . key )
69+ ) ;
70+
71+ const handleUpdateTilingParameter = ( inputParameter : ConfigurationParameter | ConfigurationParameter [ ] ) => {
72+ onUpdateTrainingConfiguration ( ( config ) => {
73+ if ( config === undefined ) return ;
74+
75+ const updatedTilingParameters = tilingParameters . map ( ( parameter ) => {
76+ if ( Array . isArray ( inputParameter ) ) {
77+ const parameterToUpdate = inputParameter . find ( ( p ) => p . key === parameter . key ) ;
78+
79+ return parameterToUpdate ?? parameter ;
80+ }
3781
38- const manualTilingParameters =
39- tilingParameters ?. filter ( ( parameter ) => ! [ 'enable_adaptive_params' , 'enable_tiling' ] . includes ( parameter . key ) ) ??
40- [ ] ;
82+ return parameter . key === inputParameter . key ? inputParameter : parameter ;
83+ } ) ;
84+
85+ return {
86+ ...config ,
87+ datasetPreparation : {
88+ ...config . datasetPreparation ,
89+ augmentation : {
90+ ...config . datasetPreparation . augmentation ,
91+ tiling : updatedTilingParameters ,
92+ } ,
93+ } ,
94+ } ;
95+ } ) ;
96+ } ;
97+
98+ const handleTilingModeChange = ( tilingMode : TILING_MODES ) => {
99+ const adaptiveParameter = getAdaptiveTilingParameter ( tilingParameters ) ;
100+ const enableParameter = getEnableTilingParameter ( tilingParameters ) ;
101+
102+ if ( adaptiveParameter === undefined || enableParameter === undefined ) {
103+ return ;
104+ }
105+
106+ if ( tilingMode === TILING_MODES . ADAPTIVE ) {
107+ handleUpdateTilingParameter ( [
108+ { ...enableParameter , value : true } ,
109+ { ...adaptiveParameter , value : true } ,
110+ ] ) ;
111+ } else if ( tilingMode === TILING_MODES . OFF ) {
112+ handleUpdateTilingParameter ( { ...enableParameter , value : false } ) ;
113+ } else {
114+ handleUpdateTilingParameter ( [
115+ { ...enableParameter , value : true } ,
116+ { ...adaptiveParameter , value : false } ,
117+ ] ) ;
118+ }
119+ } ;
41120
42121 const TILING_MODE_COMPONENTS : Record < TILING_MODES , ReactNode > = {
43122 [ TILING_MODES . OFF ] : (
@@ -48,15 +127,15 @@ export const Tiling: FC<TilingProps> = ({ tilingParameters }) => {
48127 </ Text >
49128 ) ,
50129
51- [ TILING_MODES . Adaptive ] : (
130+ [ TILING_MODES . ADAPTIVE ] : (
52131 < View UNSAFE_className = { styles . tilingModeDescription } gridColumn = { '2/3' } >
53132 Adaptive means that the system will automatically set the parameters based on the images resolution and
54133 annotations size.
55134 </ View >
56135 ) ,
57- [ TILING_MODES . Manual ] : (
136+ [ TILING_MODES . MANUAL ] : (
58137 < View gridColumn = { '1/-1' } >
59- < Parameters parameters = { manualTilingParameters } onChange = { noop } />
138+ < Parameters parameters = { manualTilingParameters } onChange = { handleUpdateTilingParameter } />
60139 </ View >
61140 ) ,
62141 } ;
@@ -77,7 +156,7 @@ export const Tiling: FC<TilingProps> = ({ tilingParameters }) => {
77156 gap = { 'size-300' }
78157 alignItems = { 'center' }
79158 >
80- < TilingModes selectedTilingMode = { selectedTilingMode } onTilingModeChange = { setSelectedTilingMode } />
159+ < TilingModes selectedTilingMode = { selectedTilingMode } onTilingModeChange = { handleTilingModeChange } />
81160 { TILING_MODE_COMPONENTS [ selectedTilingMode ] }
82161 </ Grid >
83162 </ Accordion . Content >
0 commit comments