Skip to content

Commit

Permalink
Merge pull request #395 from inowas/t15-more-fixes
Browse files Browse the repository at this point in the history
T15 more fixes
  • Loading branch information
Roschl committed Jun 4, 2021
2 parents e6ec165 + 46d92db commit 2de5987
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 121 deletions.
19 changes: 0 additions & 19 deletions src/core/model/qmra/DoseResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,6 @@ class DoseResponse extends GenericObject<IDoseResponse> {
});
}

public static fromDefaults(pathogenId: number) {
return new DoseResponse({
id: uuid.v4(),
pathogenId: pathogenId,
pathogenName: '',
pathogenGroup: '',
bestFitModel: '',
hostType: '',
doseUnits: '',
k: null,
alpha: null,
n50: null,
route: '',
response: '',
reference: '',
link: '',
});
}

public static fromObject(obj: IDoseResponse) {
return new DoseResponse(obj);
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/model/qmra/DoseResponse.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ export default interface IDoseResponse {
pathogenName: string;
pathogenGroup: string;
bestFitModel: string;
k: number;
alpha: number;
n50: number;
k: number | null;
alpha: number | null;
n50: number | null;
hostType: string;
doseUnits: string;
route: string;
Expand All @@ -20,9 +20,9 @@ export interface IDoseResponsePayload {
PathogenName: string;
PathogenGroup: string;
'Best fit model*': string;
alpha: number;
k: number;
N50: number;
alpha?: number | null;
k?: number | null;
N50?: number | null;
'Host type': string;
'Dose units': string;
Route: string;
Expand Down
34 changes: 16 additions & 18 deletions src/core/model/qmra/Qmra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ class Qmra extends GenericObject<IQmra> {
}

public fromPayload(obj: IQmraRequest) {
const eventsPerYear = obj.config.exposure.filter((e) => e.name === 'number_of_exposures');
const litresPerEvent = obj.config.exposure.filter((e) => e.name === 'volume_perEvent');
const numberOfRepeatings = obj.config.exposure.filter((e) => e.name === 'number_of_repeatings')
const eventsPerYear = obj.exposure.filter((e) => e.name === 'number_of_exposures');
const litresPerEvent = obj.exposure.filter((e) => e.name === 'volume_perEvent');
const numberOfRepeatings = obj.exposure.filter((e) => e.name === 'number_of_repeatings')

let litresPerEventValue: IValue = {type: 'value', min: 0, max: 1, mode: 1, value: 365};

Expand Down Expand Up @@ -275,26 +275,24 @@ class Qmra extends GenericObject<IQmra> {
litresPerEvent: litresPerEventValue
})
];
this.inflow = obj.config.inflow.map((p) => Pathogen.fromPayload(p));
this.treatmentProcesses = obj.config.treatment.processes.map((tp) => TreatmentProcess.fromPayload(tp));
this.treatmentSchemes = obj.config.treatment.schemes.map((ts) => TreatmentScheme.fromPayload(ts));
this.health = obj.config.health.map((h) => Health.fromPayload(h));
this.doseResponse = obj.config.doseresponse.map((dr) => DoseResponse.fromPayload(dr));
this.inflow = obj.inflow.map((p) => Pathogen.fromPayload(p));
this.treatmentProcesses = obj.treatment.processes.map((tp) => TreatmentProcess.fromPayload(tp));
this.treatmentSchemes = obj.treatment.schemes.map((ts) => TreatmentScheme.fromPayload(ts));
this.health = obj.health.map((h) => Health.fromPayload(h));
this.doseResponse = obj.doseresponse.map((dr) => DoseResponse.fromPayload(dr));
return this;
}

public toPayload() {
return {
config: {
exposure: this.generateExposure(),
inflow: this.inflow.map((p) => p.toPayload()),
treatment: {
processes: this.treatmentProcesses.map((p) => p.toPayload()),
schemes: this.treatmentSchemes.map((s) => s.toPayload())
},
doseresponse: this.doseResponse.map((r) => r.toPayload()),
health: this.health.map((h) => h.toPayload())
}
exposure: this.generateExposure(),
inflow: this.inflow.map((p) => p.toPayload()),
treatment: {
processes: this.treatmentProcesses.map((p) => p.toPayload()),
schemes: this.treatmentSchemes.map((s) => s.toPayload())
},
doseresponse: this.doseResponse.map((r) => r.toPayload()),
health: this.health.map((h) => h.toPayload())
};
}

Expand Down
16 changes: 7 additions & 9 deletions src/core/model/qmra/Qmra.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ interface IQmraData {
}

export interface IQmraRequest {
config: {
exposure: IExposure[];
inflow: IPathogenPayload[];
treatment: {
processes: ITreatmentProcessPayload[];
schemes: ITreatmentSchemePayload[];
};
doseresponse: IDoseResponsePayload[];
health: IHealthPayload[];
exposure: IExposure[];
inflow: IPathogenPayload[];
treatment: {
processes: ITreatmentProcessPayload[];
schemes: ITreatmentSchemePayload[];
};
doseresponse: IDoseResponsePayload[];
health: IHealthPayload[];
}

export default IQmra;
12 changes: 6 additions & 6 deletions src/scenes/t15/components/DoseResponse/DoseResponseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ const DoseResponseForm = ({onChange, readOnly, selectedDoseResponse}: IProps) =>

if (value === 'beta-Poisson') {
cItem.bestFitModel = 'beta-Poisson';
cItem.k = NaN;
cItem.k = null;
cItem.n50 = 0;
cItem.alpha = 0;
}

if (value === 'exponential') {
cItem.bestFitModel = 'exponential';
cItem.k = 0;
cItem.n50 = NaN;
cItem.alpha = NaN;
cItem.n50 = null;
cItem.alpha = null;
}

setElement(cItem);
Expand Down Expand Up @@ -100,7 +100,7 @@ const DoseResponseForm = ({onChange, readOnly, selectedDoseResponse}: IProps) =>
placeholder="alpha"
readOnly={readOnly}
type="number"
value={activeInput === 'alpha' ? activeValue : element.alpha}
value={activeInput === 'alpha' ? activeValue : element.alpha || 0}
/>
</Form.Field>
<Form.Field>
Expand All @@ -112,7 +112,7 @@ const DoseResponseForm = ({onChange, readOnly, selectedDoseResponse}: IProps) =>
placeholder="N50"
readOnly={readOnly}
type="number"
value={activeInput === 'n50' ? activeValue : element.n50}
value={activeInput === 'n50' ? activeValue : element.n50 || 0}
/>
</Form.Field>
</Form.Group>
Expand All @@ -127,7 +127,7 @@ const DoseResponseForm = ({onChange, readOnly, selectedDoseResponse}: IProps) =>
placeholder="k"
readOnly={readOnly}
type="number"
value={activeInput === 'k' ? activeValue : element.k}
value={activeInput === 'k' ? activeValue : element.k || 0}
/>
</Form.Field>
}
Expand Down
55 changes: 32 additions & 23 deletions src/scenes/t15/components/JsonUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Button, Icon, Message, Modal } from 'semantic-ui-react';
import { JSON_SCHEMA_URL } from '../../../services/api';
import {Button, Icon, Message, Modal} from 'semantic-ui-react';
import {JSON_SCHEMA_URL} from '../../../services/api';
import {validate as jsonSchemaValidate} from '../../../services/jsonSchemaValidator';
import Qmra from '../../../core/model/qmra/Qmra';
import React, { ChangeEvent, useState } from 'react';
import React, {ChangeEvent, useState} from 'react';

interface IProps {
onChange: (response: Qmra) => void;
qmra: Qmra;
}

const JsonUpload = ({ onChange, qmra }: IProps) => {
const JsonUpload = ({onChange, qmra}: IProps) => {
const [data, setData] = useState<any>();
const [errors, setErrors] = useState<string[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand All @@ -22,17 +22,23 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
}
};

const checkFile = (data: any) => Array.isArray(data.inflow) && Array.isArray(data.exposure) &&
Array.isArray(data.doseresponse) && Array.isArray(data.health) && data.treatment &&
Array.isArray(data.treatment.processes) && Array.isArray(data.treatment.schemes);

const handleConfirm = () => {
let q = Qmra.fromDefaults();
if ('config' in data) {
q = qmra.fromPayload(data);

if ('config' in data && checkFile(data.config)) {
q = qmra.fromPayload(data.config);
}
if ('data' in data) {
if ('data' in data && checkFile(data.data)) {
q = Qmra.fromObject(data);
}
if ('inflow' in data) {
if ('inflow' in data && checkFile(data)) {
q = qmra.fromPayload(data);
}

setShowModal(false);
onChange(q);
};
Expand All @@ -54,27 +60,30 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {

if (!checkPassed) {
setData(undefined);
setShowModal(true);
e.push('Invalid JSON');
setErrors(e);
}

const d = JSON.parse(text);

// TODO: JSON SCHEME VALIDATION

jsonSchemaValidate(
d,
JSON_SCHEMA_URL + '/qmra/qmra.payload.json'
).then((r) => {
console.log(r);
if (r[0]) {
setErrors([]);
setIsLoading(false);
setShowModal(true);
setData(d);
} else {
if (Array.isArray(r[1]) && 'message' in r[1][0]) {
setErrors(r[1].map((e) => e.message));
}
setIsLoading(false);
setShowModal(true);
}
});

if (checkPassed && e.length === 0) {
setData(d);
}

setErrors(e);
setIsLoading(false);
setShowModal(true);
};

const handleUploadFile = (e: ChangeEvent<HTMLInputElement>) => {
Expand All @@ -97,7 +106,7 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
labelPosition="left"
loading={isLoading}
/>
<input hidden={true} type="file" id="inputField" onChange={handleUploadFile} />
<input hidden={true} type="file" id="inputField" onChange={handleUploadFile}/>
<Modal onClose={handleTriggerModal} open={showModal} size="small">
<Modal.Header>Upload Json</Modal.Header>
<Modal.Content>
Expand All @@ -111,17 +120,17 @@ const JsonUpload = ({ onChange, qmra }: IProps) => {
{errors.length > 0 && (
<Modal.Actions>
<Button color="grey" onClick={handleTriggerModal}>
<Icon name="remove" /> Close
<Icon name="remove"/> Close
</Button>
</Modal.Actions>
)}
{errors.length < 1 && (
<Modal.Actions>
<Button color="red" onClick={handleTriggerModal}>
<Icon name="remove" /> No
<Icon name="remove"/> No
</Button>
<Button color="green" onClick={handleConfirm}>
<Icon name="checkmark" /> Yes
<Icon name="checkmark"/> Yes
</Button>
</Modal.Actions>
)}
Expand Down
40 changes: 0 additions & 40 deletions src/scenes/t15/components/shared/CsvUpload.tsx

This file was deleted.

0 comments on commit 2de5987

Please sign in to comment.