Skip to content

Commit a2908a7

Browse files
committed
#389 Set experiment parameters, fetch param on create, simulate, create&simulate and test with indesign experiment
1 parent 41a73a0 commit a2908a7

4 files changed

Lines changed: 103 additions & 9 deletions

File tree

webapp/components/experiments/ExperimentEdit.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import ParameterMenu from './ParameterMenu';
2626
import useStyles from './ExperimentEditStyle';
2727
import * as ExperimentHelper from './ExperimentHelper';
2828
import DialogBox from '../general/DialogBox';
29-
29+
// import { useDispatch } from 'react-redux';
30+
// import { setExperimentParameters } from 'root/redux/actions/experiments';
3031
const RANGE_VALUE = 0;
3132
const SUPPORTED_TYPES = [REAL_TYPE.INT, REAL_TYPE.FLOAT, REAL_TYPE.STR, REAL_TYPE.BOOL];
3233
const MAX_TRIALS = 100;
@@ -198,7 +199,7 @@ const ExperimentEdit = (props) => {
198199
const experiments = useSelector((state) => state.experiments.experiments);
199200

200201
let numberOfTrials = 1;
201-
202+
// const dispatch = useDispatch();
202203
const validateParameter = (param) => {
203204
let updatedParam = param;
204205
if (param.type === LIST) {
@@ -267,6 +268,9 @@ const ExperimentEdit = (props) => {
267268
});
268269

269270
console.debug(`Size before ${paramKeys.length}, after: ${filteredKeys.length}`);
271+
// dispatch(setExperimentParameters({
272+
// parameters: filteredKeys,
273+
// }));
270274
setSelectionParams(filteredKeys);
271275
});
272276
};

webapp/redux/actions/experiments.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const VIEW_EXPERIMENTS_RESULTS = 'VIEW_EXPERIMENT_RESULTS';
55
export const TRIAL_LOAD_MODEL_SPEC = 'TRIAL_LOAD_MODEL_SPEC';
66
export const OPEN_LAUNCH_DIALOG = 'OPEN_LAUNCH_DIALOG';
77
export const CLOSE_LAUNCH_DIALOG = 'CLOSE_LAUNCH_DIALOG';
8+
export const SET_EXPERIMENT_PARAMETERS = 'SET_EXPERIMENT_PARAMETERS';
89

910
/**
1011
* Triggers fetching the Experiments from the backend.
@@ -21,6 +22,14 @@ export const setExperiments = (payload) => ({
2122
payload,
2223
});
2324

25+
/**
26+
* Set fetched experiments.
27+
*/
28+
export const setExperimentParameters = (payload) => ({
29+
type: SET_EXPERIMENT_PARAMETERS,
30+
payload,
31+
});
32+
2433
/**
2534
* View results of selected experiment/trial.
2635
*/

webapp/redux/middleware/middleware.js

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import { closeDrawerDialogBox } from '../actions/drawer';
2626
import Utils from '../../Utils';
2727
import { downloadJsonResponse, downloadPythonResponse } from './utils';
2828
import * as Constants from '../../constants';
29-
29+
import * as ExperimentsApi from 'root/api/experiments';
30+
import { setExperimentParameters } from 'root/redux/actions/experiments';
31+
const SUPPORTED_TYPES = [Constants.REAL_TYPE.INT, Constants.REAL_TYPE.FLOAT, Constants.REAL_TYPE.STR, Constants.REAL_TYPE.BOOL];
3032
let previousLayout = {
3133
edit: undefined,
3234
network: undefined,
@@ -161,18 +163,90 @@ export default (store) => (next) => (action) => {
161163
break;
162164
}
163165
case CREATE_NETWORK: {
164-
instantiateNetwork({})
165-
.then(toNetworkCallback(false), pythonErrorCallback);
166+
ExperimentsApi.getParameters()
167+
.then((params) => {
168+
const flattened = Utils.flatten(params);
169+
const paramKeys = Object.keys(flattened);
170+
171+
const filteredKeys = paramKeys.filter((key) => {
172+
// TODO: avoid to fetch field twice!
173+
const field = Utils.getMetadataField(`netParams.${key}`);
174+
if (field && SUPPORTED_TYPES.includes(field.type)) {
175+
return true;
176+
}
177+
return false;
178+
});
179+
next(setExperimentParameters({
180+
parameters: filteredKeys,
181+
}));
182+
const expData = store.getState().experiments;
183+
expData?.inDesign?.params?.map((param) => {
184+
if(filteredKeys.includes(param.mapsTo)) {
185+
instantiateNetwork({})
186+
.then(toNetworkCallback(false), pythonErrorCallback);
187+
} else {
188+
pythonErrorCallback({errorDetails: 'Missing Parameters', errorMessage: 'Error'})
189+
}
190+
})
191+
});
166192
break;
167193
}
168194
case CREATE_SIMULATE_NETWORK: {
169-
simulateNetwork({ allTrials: false })
170-
.then(toNetworkCallback(false), pythonErrorCallback);
195+
ExperimentsApi.getParameters()
196+
.then((params) => {
197+
const flattened = Utils.flatten(params);
198+
const paramKeys = Object.keys(flattened);
199+
200+
const filteredKeys = paramKeys.filter((key) => {
201+
// TODO: avoid to fetch field twice!
202+
const field = Utils.getMetadataField(`netParams.${key}`);
203+
if (field && SUPPORTED_TYPES.includes(field.type)) {
204+
return true;
205+
}
206+
return false;
207+
});
208+
next(setExperimentParameters({
209+
parameters: filteredKeys,
210+
}));
211+
const expData = store.getState().experiments;
212+
expData?.inDesign?.params?.map((param) => {
213+
if(filteredKeys.includes(param.mapsTo)) {
214+
simulateNetwork({ allTrials: false })
215+
.then(toNetworkCallback(false), pythonErrorCallback);
216+
} else {
217+
pythonErrorCallback({errorDetails: 'Missing Parameters', errorMessage: 'Error'})
218+
}
219+
})
220+
});
171221
break;
172222
}
173223
case SIMULATE_NETWORK:
174-
simulateNetwork({ allTrials: action.payload, usePrevInst: false })
175-
.then(toNetworkCallback(false), pythonErrorCallback);
224+
ExperimentsApi.getParameters()
225+
.then((params) => {
226+
const flattened = Utils.flatten(params);
227+
const paramKeys = Object.keys(flattened);
228+
229+
const filteredKeys = paramKeys.filter((key) => {
230+
// TODO: avoid to fetch field twice!
231+
const field = Utils.getMetadataField(`netParams.${key}`);
232+
if (field && SUPPORTED_TYPES.includes(field.type)) {
233+
return true;
234+
}
235+
return false;
236+
});
237+
next(setExperimentParameters({
238+
parameters: filteredKeys,
239+
}));
240+
const expData = store.getState().experiments;
241+
expData?.inDesign?.params?.map((param) => {
242+
if(filteredKeys.includes(param.mapsTo)) {
243+
simulateNetwork({ allTrials: action.payload, usePrevInst: false })
244+
.then(toNetworkCallback(false), pythonErrorCallback);
245+
} else {
246+
pythonErrorCallback({errorDetails: 'Missing Parameters', errorMessage: 'Error'})
247+
}
248+
})
249+
});
176250
break;
177251
case PYTHON_CALL: {
178252
const callback = (response) => {

webapp/redux/reducers/experiments.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {
33
OPEN_LAUNCH_DIALOG,
44
SET_EXPERIMENTS,
55
CLOSE_LAUNCH_DIALOG,
6+
SET_EXPERIMENT_PARAMETERS,
67
} from 'root/redux/actions/experiments';
78
import { EXPERIMENT_STATE } from 'root/constants';
89

910
export const EXPERIMENTS_DEFAULT_STATE = {
1011
experiments: [],
1112
inDesign: null,
1213
openLaunchDialog: false,
14+
experimentParams: [],
1315
};
1416

1517
export default (state = EXPERIMENTS_DEFAULT_STATE, action) => {
@@ -20,6 +22,11 @@ export default (state = EXPERIMENTS_DEFAULT_STATE, action) => {
2022
experiments: action.payload,
2123
inDesign: action.payload.find((el) => el.state === EXPERIMENT_STATE.DESIGN),
2224
};
25+
case SET_EXPERIMENT_PARAMETERS:
26+
return {
27+
...state,
28+
experimentParams: action.payload?.parameters,
29+
};
2330
case OPEN_LAUNCH_DIALOG:
2431
return {
2532
...state,

0 commit comments

Comments
 (0)