-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert group dosing data and group observations to a single CSV file for all subject groups. CSV headers are taken from a standardised list used by the Django app. Remove the previous download button, on the last step of the data stepper, and replace it with a download button in the Data tab.
- Loading branch information
1 parent
9d84cf1
commit 81ec69f
Showing
4 changed files
with
166 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import Papa from 'papaparse'; | ||
import { | ||
DatasetRead, | ||
ProtocolRead, | ||
SubjectGroupListApiResponse, | ||
UnitListApiResponse, | ||
UnitRead | ||
} from '../../app/backendApi'; | ||
|
||
type Row = { [key: string]: any }; | ||
type Data = Row[]; | ||
type SubjectBiomarker = { | ||
subjectId: any; | ||
subjectDatasetId: number | undefined; | ||
time: any; | ||
timeUnit: UnitRead | undefined; | ||
value: any; | ||
unit: UnitRead | undefined; | ||
qname: string | undefined; | ||
label: string; | ||
id: number; | ||
}; | ||
|
||
const HEADERS: string[] = [ | ||
'ID', 'Group', 'Time', 'Time_unit', | ||
'Observation', 'Observation_unit', 'Observation_id', 'Observation Variable', | ||
'Administration ID', 'Amount', 'Amt_unit', 'Amount Variable', 'Infusion_time', | ||
'II', 'ADDL' | ||
]; | ||
|
||
function parseDosingRow( | ||
protocol: ProtocolRead, | ||
units: UnitListApiResponse | undefined, | ||
groupId: string | undefined, | ||
adminId: number | ||
) { | ||
const amountUnit = units?.find(unit => unit.id === protocol.amount_unit)?.symbol || ''; | ||
const timeUnit = units?.find(unit => unit.id === protocol.time_unit)?.symbol || ''; | ||
const qname = protocol.mapped_qname; | ||
const doseType = protocol.dose_type; | ||
return protocol.doses.map(dose => ({ | ||
'Administration ID': adminId, | ||
Group: groupId, | ||
Amount: dose.amount.toString(), | ||
'Amt_unit': amountUnit, | ||
Time: dose.start_time.toString(), | ||
'Time_unit': timeUnit, | ||
'Infusion_time': dose.duration, | ||
'ADDL': (dose?.repeats || 1) - 1, | ||
'II': dose.repeat_interval, | ||
'Amount Variable': qname, | ||
Observation: '.' | ||
})) | ||
} | ||
|
||
function parseBiomarkerRow(row: SubjectBiomarker, groupId: string | undefined): Row { | ||
return ({ | ||
'ID': row.subjectDatasetId, | ||
'Time': row.time.toString(), | ||
'Time_unit': row.timeUnit?.symbol, | ||
'Observation': row.value.toString(), | ||
'Observation_unit': row.unit?.symbol, | ||
'Observation_id': row.label, | ||
'Observation Variable': row.qname, | ||
Group: groupId, | ||
Amount: '.' | ||
}); | ||
} | ||
|
||
export default function generateCSV( | ||
dataset: DatasetRead | undefined, | ||
groups: SubjectGroupListApiResponse, | ||
subjectBiomarkers: SubjectBiomarker[][] | undefined, | ||
units: UnitListApiResponse | undefined | ||
) { | ||
const rows: Data = []; | ||
groups.forEach((group, groupIndex) => { | ||
const groupId = group?.id_in_dataset || group?.name; | ||
|
||
const dosingRows: Row[] = group.protocols.flatMap((protocol, protocolIndex) => { | ||
const adminId = groupIndex + 1 + protocolIndex; | ||
return parseDosingRow(protocol, units, groupId, adminId); | ||
}); | ||
|
||
const observationRows: Row[] = (subjectBiomarkers || []).flatMap(biomarkerRows => biomarkerRows.map(row => { | ||
const group = dataset?.groups?.find(group => group.subjects.includes(row.subjectId)); | ||
const groupId = group?.id_in_dataset || group?.name; | ||
return parseBiomarkerRow(row, groupId); | ||
})) | ||
.filter((row: Row) => row.Group === groupId); | ||
|
||
const subjects = new Set(observationRows?.map((row: Row) => row['ID'])); | ||
subjects.forEach(subjectId => { | ||
const subjectObservations = observationRows ? | ||
observationRows.filter((row: Row) => row['ID'] === subjectId) : | ||
[]; | ||
const subjectDosing: Row[] = dosingRows | ||
.filter(row => row.Group === groupId) | ||
.map(row => ({ ...row, 'ID': subjectId })); | ||
subjectDosing.concat(subjectObservations) | ||
.forEach((observation: Row) => { | ||
const dataRow: Row = {}; | ||
HEADERS.forEach(header => { | ||
dataRow[header] = observation[header]; | ||
}); | ||
rows.push(dataRow); | ||
}); | ||
}); | ||
}); | ||
return Papa.unparse(rows); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters