-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFormatting.tsx
More file actions
158 lines (135 loc) · 5.21 KB
/
Copy pathFormatting.tsx
File metadata and controls
158 lines (135 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { SimulationResults } from "@/dto/SimulationResults";
import { ChartDataSet, TabulatedResultRow } from "@/dto/ChartData";
import { ExperimentResult } from "@/dto/ExperimentResult";
export const convertToSubscripts = (chemicalFormula: string): React.ReactNode => {
const regex = /(?<=\p{L})\d|(?=\p{L})\d/gu;
const matches = [...chemicalFormula.matchAll(regex)];
const subscriptsRemoved = chemicalFormula.split(regex);
const result = subscriptsRemoved.flatMap((part, index) =>
index < matches.length ? [part, <sub key={index}>{matches[index][0]}</sub>] : [part]
);
return <p>{result}</p>;
};
export const extractPlotData = (simulationResults: SimulationResults) => {
const { modelInput, finalConcentrations } = simulationResults;
const keys = Object.keys(finalConcentrations).filter(
(key) => (modelInput.concentrations[key] ?? 0) >= 0.001 || (finalConcentrations[key] ?? 0) >= 0.001
);
const initial = keys.map((key) => ({ x: key, y: modelInput.concentrations[key] }));
const final = keys.map((key) => ({ x: key, y: finalConcentrations[key] }));
const change = keys.map((key) => ({
x: key,
y: finalConcentrations[key] - (modelInput.concentrations[key] ?? 0),
}));
return [
{
label: "Change",
data: change,
hidden: false,
},
{
label: "Initial",
data: initial,
hidden: true,
},
{
label: "Final",
data: final,
hidden: true,
},
];
};
export const ISODate_to_UIDate = (date: Date) => {
const day = date.getDate();
const month = date.toLocaleString("default", { month: "short" });
const year = date.getFullYear();
return `${day}. ${month} ${year}`;
};
export const convertSimulationToChartData = (simulation: SimulationResults, experimentName: string): ChartDataSet => {
return {
label: `${simulation.modelInput.modelId} - ${experimentName}`,
data: Object.entries(simulation.finalConcentrations)
.filter(([, y]) => y !== 0)
.map(([x, y]) => ({ x, y })),
};
};
const buildTabulatedRow = (
label: string,
inputsConcentrations: Record<string, number>,
finalConcentration: Record<string, number>,
parameters: Record<string, number | string>
): TabulatedResultRow => {
const row: TabulatedResultRow = {};
row["label"] = label;
Object.entries(inputsConcentrations).forEach(([component, value]) => {
row[`In_${component}`] = value;
});
Object.entries(finalConcentration).forEach(([component, value]) => {
row[`Out_${component}`] = value;
});
Object.entries(parameters).forEach(([param, value]) => {
row[param] = value;
});
return row;
};
export const convertSimulationQueriesResultToTabulatedData = (
simulationResultsPerExperiment: Record<string, SimulationResults[]>
): TabulatedResultRow[] => {
const tabulatedData: TabulatedResultRow[] = [];
Object.entries(simulationResultsPerExperiment).forEach(([experimentName, simulations]) => {
simulations.forEach((simulation) => {
tabulatedData.push(
buildTabulatedRow(
`${simulation.modelInput.modelId || "Unknown"} - ${experimentName}`,
simulation.modelInput.concentrations,
simulation.finalConcentrations,
simulation.modelInput.parameters
)
);
});
});
return tabulatedData;
};
export const convertExperimentResultsToTabulatedData = (
experimentResults: ExperimentResult[]
): TabulatedResultRow[] => {
return experimentResults.map((result) =>
buildTabulatedRow(result.name ?? "", result.initialConcentrations, result.finalConcentrations, {
pressure: result.pressure ?? 0,
temperature: (result.temperature ?? 0) + 273, // Convert to Kelvin
time: result.time ?? 0,
})
);
};
export function convertTabulatedDataToCSVFormat(tabulatedData: TabulatedResultRow[]): string {
if (tabulatedData.length === 0) return "";
const allKeys = Array.from(
tabulatedData.reduce((set, row) => {
Object.keys(row).forEach((key) => set.add(key));
return set;
}, new Set<string>())
);
const filteredKeys = allKeys.filter((key) =>
tabulatedData.some((row) => {
const val = row[key];
return !(val === 0 || val === "" || val === undefined);
})
);
const header = filteredKeys;
const rows = tabulatedData.map((row) => header.map((key) => (row[key] !== undefined ? row[key] : "")));
const csvContent = [header, ...rows]
.map((row) => row.map((val) => `"${String(val).replace(/"/g, '""')}"`).join(","))
.join("\r\n");
return csvContent;
}
export const downloadTabulatedDataAsCSV = (csvContent: string, fileName: string) => {
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
};