<commit_before>import { PatientSurvival } from '../../../shared/model/PatientSurvival';
import { tsvFormat } from 'd3-dsv';
import jStat from 'jStat';
import * as _ from 'lodash';

export type ScatterData = {
    x: number;
    y: number;
    patientId: string;
    uniquePatientKey: string;
    studyId: string;
    status: boolean;
    opacity?: number;
    group?: string;
};

export type DownSamplingOpts = {
    xDenominator: number;
    yDenominator: number;
    threshold: number;
};

export type GroupedScatterData = {
    [key: string]: SurvivalCurveData;
};

export type SurvivalCurveData = {
    numOfCases: number;
    line: any[];
    scatterWithOpacity: ScatterData[];
    scatter: ScatterData[];
};

export type SurvivalPlotFilters = {
    x: [number, number];
    y: [number, number];
};

export function getEstimates(patientSurvivals: PatientSurvival[]): number[] {
    let estimates: number[] = [];
    let previousEstimate: number = 1;
    patientSurvivals.forEach((patientSurvival, index) => {
        if (patientSurvival.status) {
            const atRisk = patientSurvivals.length - index;
            const estimate = previousEstimate * ((atRisk - 1) / atRisk);
            previousEstimate = estimate;
            estimates.push(estimate);
        } else {
            estimates.push(previousEstimate);
        }
    });
    return estimates;
}

export function getMedian(
    patientSurvivals: PatientSurvival[],
    estimates: number[]
): string {
    let median: string = 'NA';
    for (let i = 0; i < estimates.length; i++) {
        if (estimates[i] <= 0.5) {
            median = patientSurvivals[i].months.toString();
            break;
        }
    }
    return median;
}

export function getLineData(
    patientSurvivals: PatientSurvival[],
    estimates: number[]
): any[] {
    let chartData: any[] = [];
<commit_msg>survival - use only 2 digit precision for median months<commit_after>import { PatientSurvival } from '../../../shared/model/PatientSurvival';
import { tsvFormat } from 'd3-dsv';
import jStat from 'jStat';
import * as _ from 'lodash';

export type ScatterData = {
    x: number;
    y: number;
    patientId: string;
    uniquePatientKey: string;
    studyId: string;
    status: boolean;
    opacity?: number;
    group?: string;
};

export type DownSamplingOpts = {
    xDenominator: number;
    yDenominator: number;
    threshold: number;
};

export type GroupedScatterData = {
    [key: string]: SurvivalCurveData;
};

export type SurvivalCurveData = {
    numOfCases: number;
    line: any[];
    scatterWithOpacity: ScatterData[];
    scatter: ScatterData[];
};

export type SurvivalPlotFilters = {
    x: [number, number];
    y: [number, number];
};

export function getEstimates(patientSurvivals: PatientSurvival[]): number[] {
    let estimates: number[] = [];
    let previousEstimate: number = 1;
    patientSurvivals.forEach((patientSurvival, index) => {
        if (patientSurvival.status) {
            const atRisk = patientSurvivals.length - index;
            const estimate = previousEstimate * ((atRisk - 1) / atRisk);
            previousEstimate = estimate;
            estimates.push(estimate);
        } else {
            estimates.push(previousEstimate);
        }
    });
    return estimates;
}

export function getMedian(
    patientSurvivals: PatientSurvival[],
    estimates: number[]
): string {
    let median: string = 'NA';
    for (let i = 0; i < estimates.length; i++) {
        if (estimates[i] <= 0.5) {
            median = patientSurvivals[i].months.toFixed(2);
            break;
        }
    }
    return median;
}

export function getLineData(
    patientSurvivals: PatientSurvival[],
    estimates: number[]
): any[] {
    let chartData: any[] = [];

{'fraction_non_alphanumeric': 0.12506666666666666, 'fraction_numerical': 0.0037333333333333333, 'mean_word_length': 2.855087358684481, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3513452', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1143, 'n_words': 290}