Skip to content
Snippets Groups Projects
Commit 9dd1a945 authored by Steve Reis's avatar Steve Reis
Browse files

fix(exareme2): Descriptive stats error handlings

parent 2278f44c
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,12 @@ import { ...@@ -9,6 +9,12 @@ import {
import { ResultExperiment } from '../../interfaces/experiment/result-experiment.interface'; import { ResultExperiment } from '../../interfaces/experiment/result-experiment.interface';
import BaseHandler from '../base.handler'; import BaseHandler from '../base.handler';
interface Stat {
dataset: string;
variable: string;
data: Record<string, string> | null;
}
export default class DescriptiveHandler extends BaseHandler { export default class DescriptiveHandler extends BaseHandler {
private static readonly headerDescriptive = ` private static readonly headerDescriptive = `
$fnum := function($x) { $type($x) = 'number' ? $round($number($x),3) : $x }; $fnum := function($x) { $type($x) = 'number' ? $round($number($x),3) : $x };
...@@ -114,45 +120,59 @@ $fn := function($o, $prefix) { ...@@ -114,45 +120,59 @@ $fn := function($o, $prefix) {
return result; return result;
} }
static readonly descriptiveToTable = (stats: any[]): TableResult[] => { static readonly descriptiveToTable = (stats: Stat[]): TableResult[] => {
const datasets: string[] = Array.from(new Set(stats.map(d => d.dataset))) const datasets: string[] = Array.from(new Set(stats.map((d) => d.dataset)));
const variables: string[] = Array.from(new Set(stats.map(d => d.variable))) const variables: string[] = Array.from(
new Set(stats.map((d) => d.variable)),
);
const columns = (variable) => { const columns = (variable) => {
const stat = stats.filter((s) => s.variable === variable) const stat = stats.filter((s) => s.variable === variable);
const data = key => stat.map(d => d.data[key] || '') const data = (key) =>
const modalities = Array.from(new Set(data('counts').flatMap(c => Object.keys(c)))) stat.map((d) =>
d.data === null ? 'No Enough Data' : d.data[key] || '',
return ([ );
[variable, ...data('num_total')], const modalities = Array.from(
['Datapoints', ...data('num_dtps')], new Set(data('counts').flatMap((c) => Object.keys(c))),
['NA', ...data('num_na')], );
...(modalities.length > 0 ? const notEnoughData = stat.map((d) => d.data).includes(null);
modalities.map(m => [m, ...stat.map(d => d.data.counts[m] || '')]) :
[['SE', ...data('std')], return (
['mean', ...data('mean')], (notEnoughData && [[variable, ...data('num_total')]]) || [
['min', ...data('num_dtps')], [variable, ...data('num_total')],
['Q1', ...data('q1')], ['Datapoints', ...data('num_dtps')],
['Q2', ...data('q2')], ['NA', ...data('num_na')],
['Q3', ...data('q3')], ...(modalities.length > 0
['max', ...data('max')]] ? modalities.map((m) => [
) m,
]) ...stat.map((d) => d.data.counts[m] || ''),
} ])
: [
return variables.map(variable => ({ ['SE', ...data('std')],
['mean', ...data('mean')],
['min', ...data('num_dtps')],
['Q1', ...data('q1')],
['Q2', ...data('q2')],
['Q3', ...data('q3')],
['max', ...data('max')],
]),
]
);
};
return variables.map((variable) => ({
headers: [ headers: [
{ name: "", type: "string" }, { name: '', type: 'string' },
...datasets.map(d => ({ name: d, type: 'string ' })) ...datasets.map((d) => ({ name: d, type: 'string ' })),
], ],
data: columns(variable), data: columns(variable),
name: '', name: '',
tableStyle: 1 tableStyle: 1,
})) }));
} };
descriptiveDataToTableResult2(data: ResultExperiment): GroupsResult { descriptiveDataToTableResult2(data: ResultExperiment): GroupsResult {
const result = new GroupsResult() const result = new GroupsResult();
result.groups = [ result.groups = [
new GroupResult({ new GroupResult({
...@@ -160,7 +180,7 @@ $fn := function($o, $prefix) { ...@@ -160,7 +180,7 @@ $fn := function($o, $prefix) {
description: 'Descriptive statistics for the variables of interest.', description: 'Descriptive statistics for the variables of interest.',
results: DescriptiveHandler.descriptiveToTable(data['variable_based']), results: DescriptiveHandler.descriptiveToTable(data['variable_based']),
}), }),
] ];
result.groups.push( result.groups.push(
new GroupResult({ new GroupResult({
...@@ -169,9 +189,9 @@ $fn := function($o, $prefix) { ...@@ -169,9 +189,9 @@ $fn := function($o, $prefix) {
'Intersection table for the variables of interest as it appears in the experiment.', 'Intersection table for the variables of interest as it appears in the experiment.',
results: DescriptiveHandler.descriptiveToTable(data['model_based']), results: DescriptiveHandler.descriptiveToTable(data['model_based']),
}), }),
) );
return result return result;
} }
handle(exp: Experiment, data: unknown, domain?: Domain): void { handle(exp: Experiment, data: unknown, domain?: Domain): void {
...@@ -179,16 +199,20 @@ $fn := function($o, $prefix) { ...@@ -179,16 +199,20 @@ $fn := function($o, $prefix) {
return super.handle(exp, data, domain); return super.handle(exp, data, domain);
const inputs = data as ResultExperiment[]; const inputs = data as ResultExperiment[];
console.log(JSON.stringify(inputs, null, 2));
if (inputs && Array.isArray(inputs)) { if (inputs && Array.isArray(inputs)) {
const exareme1 = inputs const exareme1 = inputs.filter(
.filter((input) => input.type === 'application/json') (input) => input.type === 'application/json',
);
if (exareme1.length > 0) if (exareme1.length > 0)
exareme1.map((input) => this.descriptiveDataToTableResult1(input)) exareme1
.map((input) => this.descriptiveDataToTableResult1(input))
.forEach((input) => exp.results.push(input)); .forEach((input) => exp.results.push(input));
else else
inputs.map((input) => this.descriptiveDataToTableResult2(input)) inputs
.map((input) => this.descriptiveDataToTableResult2(input))
.forEach((input) => exp.results.push(input)); .forEach((input) => exp.results.push(input));
} }
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment