Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recreate order of estimators #572

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ThreeEditor/examples/ex1_result.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ThreeEditor/js/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Loader } from './Loader.js';
import { Storage as _Storage } from './Storage.js';
import hash from 'object-hash';

var _DEFAULT_CAMERA = new THREE.PerspectiveCamera(50, 1, 0.01, 1000);
const _DEFAULT_CAMERA = new THREE.PerspectiveCamera(50, 1, 0.01, 1000);
_DEFAULT_CAMERA.name = 'Camera';
_DEFAULT_CAMERA.position.set(0, 5, 10);
_DEFAULT_CAMERA.lookAt(new THREE.Vector3());
Expand Down
3 changes: 3 additions & 0 deletions src/ThreeEditor/js/EditorJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Editor } from "./Editor";

export type EditorJson = ReturnType<typeof Editor.prototype.toJSON>;
14 changes: 12 additions & 2 deletions src/WrapperApp/components/ResultsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Box, Button, Card, CardContent, Grid, Tab, Tabs, Typography, useMediaQuery } from '@mui/material';
import {
Box,
Button,
Card,
CardContent,
Grid,
Tab,
Tabs,
Typography,
useMediaQuery
} from '@mui/material';
import React, { SyntheticEvent, useEffect, useState } from 'react';
import { generateGraphs } from '../../JsRoot/GraphData';
import { TabPanel } from './TabPanel';
Expand Down Expand Up @@ -33,7 +43,7 @@ function ResultsPanel() {
padding: '0.5rem',
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
alignItems: 'center'
}}>
<Typography
gutterBottom
Expand Down
26 changes: 25 additions & 1 deletion src/services/ShSimulatorService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { createGenericContext } from '../util/GenericContext';
import { useAuth } from './AuthService';
import { IResponseMsg } from './ResponseTypes';
import { Estimator } from '../JsRoot/GraphData';
import { EditorJson } from '../ThreeEditor/js/EditorJson';
import { ScoringManagerJSON } from '../ThreeEditor/util/Scoring/ScoringManager';
import { orderAccordingToList } from '../util/Sort';

export interface ShSimulationProps {
children: ReactNode;
Expand Down Expand Up @@ -103,6 +106,7 @@ interface ResShStatusSuccess extends IResponseMsg {
result: {
estimators: Estimator[];
};
input?: EditorJson | { input_files: InputFiles };
};
}

Expand All @@ -125,8 +129,16 @@ export interface SimulationStatusData {
result?: {
estimators: Estimator[];
};
editor?: EditorJson;
}

export const recreateOrderInEstimators = (
estimators: Estimator[],
scoringManagerJSON: ScoringManagerJSON
): Estimator[] => {
return orderAccordingToList(estimators, scoringManagerJSON.scoringOutputs, 'name');
};

const [useShSimulation, ShSimulationContextProvider] = createGenericContext<IShSimulation>();

const ShSimulation = (props: ShSimulationProps) => {
Expand Down Expand Up @@ -180,7 +192,6 @@ const ShSimulation = (props: ShSimulationProps) => {
[authKy]
);


const getStatus = useCallback(
(
simulation: SimulationInfo,
Expand Down Expand Up @@ -231,6 +242,19 @@ const ShSimulation = (props: ShSimulationProps) => {

case StatusState.SUCCESS:
data.result = content.result;

// remove trailing underscores from estimators names (#530)
for (const estimator of data.result.estimators) {
estimator.name = estimator.name.replace(/_$/, '');
}

if (content.input && 'metadata' in content.input) {
data.editor = content.input;
data.result.estimators = recreateOrderInEstimators(
data.result.estimators,
data.editor.scoringManager
);
}
break;
}

Expand Down
23 changes: 23 additions & 0 deletions src/util/Sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { orderAccordingToList } from "./Sort";

test('ordering according to full list', () => {
const list = [{ id: '1', name: 'a' }, { id: '2', name: 'b' }, { id: '3', name: 'c' }, { id: '4', name: 'd' }];
const orderList = [{ id: '1' }, { id: '3' }, { id: '4' }, { id: '2' }];

const listExpected = [{ id: '1', name: 'a' }, { id: '3', name: 'c' }, { id: '4', name: 'd' }, { id: '2', name: 'b' }];

const listOrdered = orderAccordingToList(list, orderList, 'id');

expect(listOrdered).toEqual(listExpected);
});

test('ordering according to list with duplicates', () => {
const list = [{ id: '1', name: 'a' }, { id: '2', name: 'b' }, { id: '3', name: 'c2' }, { id: '3', name: 'c1' }, { id: '4', name: 'd' }];
const orderList = [{ id: '1' }, { id: '3' }, { id: '4' }, { id: '2' },{ id: '3' }];

const listExpected = [{ id: '1', name: 'a' }, { id: '3', name: 'c2' }, { id: '4', name: 'd' }, { id: '2', name: 'b' },{ id: '3', name: 'c1' }];

const listOrdered = orderAccordingToList(list, orderList, 'id');

expect(listOrdered).toEqual(listExpected);
});
25 changes: 25 additions & 0 deletions src/util/Sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Common } from "./Types";


export const orderAccordingToList = <A extends C, B extends C, C extends Common<A, B>>(list: A[], orderList: B[], property: keyof C) => {

const listCopy = [...list];
const listOrdered = [];

for (const orderElement of orderList) {
const index = listCopy.findIndex(
obj => obj[property] === orderElement[property]
);

if (index === -1) {
console.warn(`OrderElement ${orderElement[property]} not found`);
continue;
}

listOrdered.push(listCopy[index]);
listCopy.splice(index, 1);
}

return listOrdered;

}
3 changes: 3 additions & 0 deletions src/util/Types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Common<A, B> = {
[P in keyof A & keyof B]: A[P] | B[P];
}