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

550 adjust scientific format of generated numbers #622

Merged
merged 5 commits into from
Sep 19, 2022
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
178 changes: 178 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@uiw/react-textarea-code-editor": "^2.0.3",
"comlink": "^4.3.1",
"command-exists": "^1.2.9",
"convert-units": "^3.0.0-beta.4",
"glob": "^8.0.3",
"ky": "^0.31.1",
"notistack": "^2.0.5",
Expand Down Expand Up @@ -92,6 +93,7 @@
"@types/three": "^0.143.0",
"cross-env": "^7.0.3",
"gh-pages": "^4.0.0",
"jest-matcher-deep-close-to": "^3.0.2",
"prettier": "2.7.1",
"react-app-rewired": "^2.2.1",
"use-resize-observer": "^9.0.2",
Expand Down
44 changes: 36 additions & 8 deletions src/WrapperApp/components/Results/ResultsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import { DataGrid, GridColDef, GridValueGetterParams } from '@mui/x-data-grid';
import { Estimator, Page0D } from '../../../JsRoot/GraphData';
import { Button } from '@mui/material';
import { estimatorPage1DToCsv, pages0DToCsv } from '../../../util/csv/Csv';
import { Button, Stack, Switch, Typography } from '@mui/material';
import { pages0DToCsv } from '../../../util/csv/Csv';
import { saveString } from '../../../util/File';
import { EstimatorResults } from './ResultsPanel';

import { useState } from 'react';
import { convertToBestUnit } from '../../../util/convertUnits/Units';
export interface TablePage0DItem {
id: number;
name: string;
Expand All @@ -20,22 +20,39 @@ const columns: GridColDef[] = [
{ field: 'id', headerName: 'ID' },
{ field: 'name', headerName: 'Name' },
{ field: 'quantity', headerName: 'Quantity', flex: 1 },
{ field: 'value', headerName: 'Value', type: 'number', flex: 1 },
{
field: 'value',
headerName: 'Value',
type: 'number',
flex: 1,
valueGetter: (params: GridValueGetterParams) => formatValue(params.row.value)
},
{ field: 'unit', headerName: 'Unit', width: 100 },
{ field: 'filterName', headerName: 'Filter name', flex: 1 },
{ field: 'filterRules', headerName: 'Filter rules', flex: 1 }
];

const formatValue = (value: number) => {
const precision = 6;
if (value >= 0.0001 && value <= 10000) return value.toPrecision(precision);
else return value.toExponential(precision);
};

export default function TablePage0D(props: { estimator: EstimatorResults }) {
const { estimator } = props;
const { tablePages: pages } = estimator;
const tablePages: TablePage0DItem[] = pages.map((page, idx) => {

const [isUnitFixed, setUnitFixed] = useState(false);

const tablePages: TablePage0DItem[] = pages.map((page, idx) => {
let convertedValue = isUnitFixed ? null : convertToBestUnit(page.data.values[0], page.data.unit);

return {
id: idx,
name: page.name ?? '',
quantity: page.data.name,
value: page.data.values[0],
unit: page.data.unit,
value: convertedValue?.val ?? page.data.values[0],
unit: convertedValue?.unit ?? page.data.unit,
filterName: page.filterRef?.name ?? '',
filterRules:
page.filterRef?.rules
Expand All @@ -44,6 +61,12 @@ export default function TablePage0D(props: { estimator: EstimatorResults }) {
};
});



const handleChangeUnitFixed = (event: React.ChangeEvent<HTMLInputElement>) => {
setUnitFixed(event.target.checked);
};

const onClickSaveToFile = (pages: TablePage0DItem[]) => {
saveString(pages0DToCsv(estimator, pages), `table_${estimator.name}.csv`);
};
Expand All @@ -54,6 +77,11 @@ export default function TablePage0D(props: { estimator: EstimatorResults }) {
EXPORT TABLE TO CSV
</Button>

<Stack direction='row' spacing={1} alignItems='center' sx={{ marginLeft: '.5rem' }}>
<Typography>Unit: Auto</Typography>
<Switch checked={isUnitFixed} onChange={handleChangeUnitFixed} color='primary' />
<Typography>Fixed</Typography>
</Stack>
<DataGrid
initialState={{ columns: { columnVisibilityModel: { id: false } } }}
rows={tablePages}
Expand Down
23 changes: 23 additions & 0 deletions src/util/convertUnits/Units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { convertToBestUnit } from "./Units";
import { toBeDeepCloseTo, toMatchCloseTo } from 'jest-matcher-deep-close-to';
expect.extend({ toBeDeepCloseTo, toMatchCloseTo });

test('basic conversion', () => {
expect(convertToBestUnit(100, 'MeV')).toMatchObject({ val: 100, unit: 'MeV' });
expect(convertToBestUnit(1000, 'MeV')).toMatchObject({ val: 1, unit: 'GeV' });
expect(convertToBestUnit(0.1, 'MeV')).toMatchObject({ val: 100, unit: 'keV' });
expect(convertToBestUnit(0.000001, 'MeV')).toMatchObject({ val: 1, unit: 'eV' });
expect(convertToBestUnit(1000000, 'eV')).toMatchObject({ val: 1, unit: 'MeV' });

expect(convertToBestUnit(100, 'cm^-2')).toMatchObject({ val: 100, unit: 'cm^-2' });
expect(convertToBestUnit(100_00, 'cm^-2')).toMatchObject({ val: 1, unit: 'm^-2' });

expect(convertToBestUnit(100, 'cm^3')).toMatchObject({ val: 100, unit: 'cm^3' });
expect(convertToBestUnit(100_00_00, 'cm^3')).toMatchCloseTo({ val: 1, unit: 'm^3' });
});

test('fixed conversion', () => {
expect(convertToBestUnit(100, 'g/cm^3')).toMatchObject({ val: 100, unit: 'g/cm^3' });
expect(convertToBestUnit(1000000000000, 'g/cm^3')).toMatchObject({ val: 1000000000000, unit: 'g/cm^3' });
});

40 changes: 40 additions & 0 deletions src/util/convertUnits/Units.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import configureMeasurements from 'convert-units';
import { baseMeasure, inverseOfSurfaceMeasure, isBaseUnit, volumeMeasure } from "./baseUnit";

const convert = configureMeasurements({ baseMeasure, fluenceMeasure: inverseOfSurfaceMeasure, volumeMeasure });

const fixedUnits = ['g/cm^3', 'MeV/g', 'MeV/cm', 'keV/um', 'MeV cm^2 / g'];

const isFixedUnit = (unit: string) => {
return fixedUnits.includes(unit);
};

const baseUnits = ['m', 'm^-2', 'm^3', 'eV', 'eV/u', 'eV/nucleon', 'Gy', 'Sv'];



export const convertToBestUnit = (value: number, unit: string) => {
if (isFixedUnit(unit)) return { val: value, unit: unit };

if (convert().possibilities().includes(unit))
return convert(value).from(unit).toBest();

let convertedValue = null;
try {
let preUnit = unit[0];
let postUnit = unit.slice(1);
if (baseUnits.includes(unit)) {
preUnit = '';
postUnit = unit;
}

if (isBaseUnit(preUnit)) {
convertedValue = convert(value).from(preUnit).toBest();
if (convertedValue) convertedValue.unit = convertedValue?.unit + postUnit;
}
} catch (e) {
console.error(e);
}
return convertedValue;
};
Loading