Skip to content

Commit

Permalink
cleanup errors and todos for cohort_creation_experiments branch
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwolfmayr committed Sep 19, 2023
1 parent 5a5c7d2 commit f263738
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 163 deletions.
22 changes: 3 additions & 19 deletions src/Cohort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
dataDBCohortWithEqualsFilter,
dataDBCohortWithNumFilter,
getCohortData,
getCohortSize, getDBCohortData, recommendSplit,
getCohortSize,
sizeDBCohortDepletionScoreFilter,
sizeDBCohortGeneWithEqualsFilter,
sizeDBCohortGeneWithNumFilter,
Expand Down Expand Up @@ -133,14 +133,6 @@ function getLegacyRangeFilter(parentFilters: IAllFilters, attribute: string, ran
return mergeTwoAllFilters(parentFilters, thisFilter); // merge the existing with the new filter
}

// TODO implement correctly, or remove if different solution is better
function getAutoSplitFilter(parentFilters: IAllFilters, attribute: string) {
const thisFilter: IAllFilters = { normal: {}, lt: {}, lte: {}, gt: {}, gte: {} };
thisFilter['gte'][attribute] = "label1"; // TODO: remove, this is only for now for development of autosplit
thisFilter['lt'][attribute] = "label2";
return mergeTwoAllFilters(parentFilters, thisFilter); // merge the existing with the new filter
}

export async function createCohortWithEqualsFilter(
parentCohort: ICohort,
labelOne: string,
Expand Down Expand Up @@ -268,14 +260,8 @@ export async function createCohortAutoSplit(
attribute,
};
log.debug('try new cohort num filter: ', params);
// const dbId = await cohortCreationDBHandler(createDBCohortAutomatically, params); // TODO: NOT needed. The ids are already known.
const dbId = newCohortId;

// let dummyrange = [this.getGeneralNumericalFilter(0 , 50 , NumRangeOperators.gte, NumRangeOperators.lte)]; // TODO: remove, just for development of autosplit


// const newFilter = getAutoSplitFilter(parentCohort.filters, attribute);
const newFilter = null; // not needed
const dbId = newCohortId;

// ATTENTION : database != databaseName
const cht = new Cohort(
Expand All @@ -289,9 +275,7 @@ export async function createCohortAutoSplit(
idType: parentCohort.idType,
idColumn: parentCohort.idColumn,
},
newFilter,
);
log.debug('new cohort with num filter: ', cht);
return cht;
}

Expand Down Expand Up @@ -485,7 +469,7 @@ async function cohortCreationDBHandler<T>(
return dbId;
}

function combineLabelsForDB(labelOne: string, labelTwo: string): string {
export function combineLabelsForDB(labelOne: string, labelTwo: string): string {
return `${labelOne}${valueListDelimiter}${labelTwo}`;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Taskview/tasks/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { KaplanMeierPlot } from '../visualizations/KaplanMeierPlot';
import { Scatterplot } from '../visualizations/Scatterplot';
import { ATask } from './ATask';
import tippy from "tippy.js";
import {createDBCohortAutomatically, ICohortMultiAttrDBDataParams, attributesJSON} from "../../base";
import {createDBCohortAutomatically, ICohortMultiAttrDBDataParams} from "../../base";
import {AutoSplitEvent} from "../../base/events";

export class Filter extends ATask {
Expand Down Expand Up @@ -149,7 +149,7 @@ export class Filter extends ATask {

private async showTsne(attributes: IAttribute[], cohorts: ICohort[]) {
this.$visContainer.innerHTML = 'Currently, we only support the visualization of up to two attributes.';
// this.addControls(); // TODO: why does it not add if I add here, only if I add it in show()?
// this.addControls(); // why does it not add if I add here, only if I add it in show()?
this.attributes = attributes;
// this.$visContainer.innerHTML += '<button type="button" class="btn createAutomaticallyBtn btn-coral-prime" title="Calculate meaningful splits.">Create cohorts automatically</button>';
// TODO #647 fix tsne implementation
Expand Down Expand Up @@ -186,7 +186,7 @@ export class Filter extends ATask {
});
}

async createAutomatically(useNumberOfClusters: boolean = false) {
async createAutomatically(useNumberOfClusters = false) {
console.log("createAutomatically 3 or more attributes");
console.log("cohorts ", this.cohorts);
console.log("attributes ", this.attributes);
Expand Down
14 changes: 7 additions & 7 deletions src/Taskview/visualizations/AVegaVisualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { DATA_LABEL } from './constants';
import { IVisualization } from './IVisualization';
import { IAttribute, IdValuePair } from '../../data/IAttribute';
import {
ICohortDBDataParams, ICohortDBDataRecommendSplitParams,
ICohortDBWithNumFilterParams, ICohortMultiAttrDBDataParams,
ICohortDBDataRecommendSplitParams,
ICohortMultiAttrDBDataParams,
IEqualsList,
INumRange,
NumRangeOperators
Expand Down Expand Up @@ -255,8 +255,8 @@ export abstract class AVegaVisualization implements IVegaVisualization {
abstract show(container: HTMLDivElement, attributes: IAttribute[], cohorts: ICohort[]);
abstract filter(): void;
abstract split(): void;
abstract createAutomatically?(useNumberOfClusters: boolean = false): void;
abstract recommendSplit?(useNumberOfClusters: boolean = false): void;
abstract createAutomatically?(useNumberOfClusters: boolean): void;
abstract recommendSplit?(useNumberOfClusters: boolean): void;
abstract showImpl(chart: HTMLDivElement, data: Array<IdValuePair>); // probably the method impl from SingleAttributeVisualization can be moved here

destroy() {
Expand Down Expand Up @@ -461,7 +461,7 @@ export abstract class SingleAttributeVisualization extends AVegaVisualization {


/** Calls the recommendSplit webservice and sets the bins according to the returned results */
async recommendSplit(useNumberOfClusters: boolean = false) {
async recommendSplit(useNumberOfClusters = false) {
console.log("recommendSplit");

let numberOfClusters = 0;
Expand All @@ -481,7 +481,7 @@ export abstract class SingleAttributeVisualization extends AVegaVisualization {
// recommendSplit recommends splits that are used for ALL cohorts, so it would not make sense to use it on multiple cohorts

// 1 cohort, 1 category
let filter: INumRange[] | IEqualsList = [];
let filter: INumRange[] = [];

filterDesc.push({
cohort: cohorts[0],
Expand Down Expand Up @@ -665,7 +665,7 @@ export abstract class SingleAttributeVisualization extends AVegaVisualization {
}

/** Calls the createAutomatically webservice and creates cohorts according to the returned results */
async createAutomatically(useNumberOfClusters: boolean = false) {
async createAutomatically(useNumberOfClusters = false) {
console.log("createAutomatically");
// get the information on HOW MANY new cohorts to create here already
// call the webservice that creates the cohorts, then I have the number of cohorts and can create the filters
Expand Down
8 changes: 5 additions & 3 deletions src/Taskview/visualizations/AreaChart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ export class AreaChart extends MultiAttributeVisualization {
super(vegaLiteOptions);
}

async createAutomatically(useNumberOfClusters: boolean = false) {
async createAutomatically(useNumberOfClusters = false) {
console.log("createAutomatically scatterplot");

// AttributeType = 'categorical' | 'number' | 'string'; TODO send it with the data

let numberOfClusters = 0;
if (useNumberOfClusters) {
// select the bins field
Expand Down Expand Up @@ -145,6 +143,10 @@ export class AreaChart extends MultiAttributeVisualization {
log.error('split is not implemented');
}

async recommendSplit(useNumberOfClusters = false) {
log.error('recommendSplit is not implemented');
}

addControls() {
// log.info('no controls for Area Chart yet');
this.controls.insertAdjacentHTML(
Expand Down
6 changes: 5 additions & 1 deletion src/Taskview/visualizations/GroupedBoxplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export class GroupedBoxplot extends MultiAttributeVisualization {
this.container.dispatchEvent(new FilterEvent(filterDescs));
}

async recommendSplit(useNumberOfClusters = false) {
log.error('recommendSplit is not implemented');
}

split() {
const categories = this.vegaView.data('row_domain').map((row) => row[this.catAttribute.dataKey]);
const [minX, maxX] = this.vegaView.scale('x').domain();
Expand Down Expand Up @@ -242,7 +246,7 @@ export class GroupedBoxplot extends MultiAttributeVisualization {
this.container.dispatchEvent(new SplitEvent(filterDescs));
}

async createAutomatically(useNumberOfClusters: boolean = false) {
async createAutomatically(useNumberOfClusters = false) {
console.log("createAutomatically GroupedBoxplot");

let numberOfClusters = 0;
Expand Down
193 changes: 97 additions & 96 deletions src/Taskview/visualizations/Scatterplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,107 +880,104 @@ export class Scatterplot extends MultiAttributeVisualization {
}


// unused, because it does not make sense for more than 1 attribute
/** Calls the recommendSplit webservice and sets the bins according to the returned results */
async recommendSplit(useNumberOfClusters: boolean = false) {
console.log("recommendSplit scatterplot");

let numberOfClusters = 0;
if (useNumberOfClusters) {
// select the bins field
// binsCount0 = (this.controls.querySelector(`#split input.bins[data-axis=x]`) as HTMLInputElement).valueAsNumber;
// console.log("binsCountX", binsCount0);
// binsCount1 = (this.controls.querySelector(`#split input.bins[data-axis=y]`) as HTMLInputElement).valueAsNumber;
// console.log("binsCountY", binsCount1);
numberOfClusters = (this.controls.querySelector(`#split #recommendSplitControls input.clusters`) as HTMLInputElement).valueAsNumber;
console.log("useNumberOfClusters", useNumberOfClusters);
}

const cohorts = this.cohorts;

let filterDesc: IFilterDesc[] = [];
if (cohorts.length === 1) {
// it does not make sense to do a recommendSplit on multiple cohorts at once
// createAutomatically looks at the data of one cohort and creates new cohorts based on that
// recommendSplit recommends splits that are used for ALL cohorts, so it would not make sense to use it on multiple cohorts

// 1 cohort, 1 category
let filter: INumRange[] | IEqualsList = [];

filterDesc.push({
cohort: cohorts[0],
filter: [
{
attr: this.attributes[0],
range: filter,
},
],
});

filterDesc.push({
cohort: cohorts[0],
filter: [
{
attr: this.attributes[1],
range: filter,
},
],
});

const params: ICohortMultiAttrDBDataRecommendSplitParams = {
cohortId: filterDesc[0].cohort.dbId,
attribute0: this.attributes[0].dataKey,
attribute1: this.attributes[1].dataKey,
numberOfClusters: numberOfClusters,
};

//TODO show results
const data = await recommendSplitDB(params);
console.log("recommendSplit", data);

// set the bins for x-axis
let splitValues = [];
for (let i = 0; i < data[this.attributes[0].dataKey].length; i++) {
// get the int val of the data[i]
const binBorder = Number(data[this.attributes[0].dataKey][i]);
splitValues.push(binBorder);
}
// this.vegaView.data(AVegaVisualization.SPLITVALUE_DATA_STORE, cloneDeep(this.splitValues)); // set a defensive copy
// TODO this is different for the scatterplot
this.vegaView.data(`splitvalues_x`, splitValues.slice()); // set a defensive copy
console.log("split values x", splitValues.slice());
// this.vegaView.runAsync(); // update the view
this.vegaView.runAsync().then(
(
vegaView, // defer adding signallistener until the new data is set internally
) => vegaView.addDataListener(`splitvalues_x`, this.vegaSplitListener), // add listener again
);


// set the bins for y-axis
splitValues = [];
for (let i = 0; i < data[this.attributes[1].dataKey].length; i++) {
// get the int val of the data[i]
const binBorder = Number(data[this.attributes[1].dataKey][i]);
splitValues.push(binBorder);
}
// this.vegaView.data(AVegaVisualization.SPLITVALUE_DATA_STORE, cloneDeep(this.splitValues)); // set a defensive copy
// TODO this is different for the scatterplot
this.vegaView.data(`splitvalues_y`, splitValues.slice()); // set a defensive copy
console.log("split values y", splitValues.slice());
// this.vegaView.runAsync(); // update the view
this.vegaView.runAsync().then(
(
vegaView, // defer adding signallistener until the new data is set internally
) => vegaView.addDataListener(`splitvalues_y`, this.vegaSplitListener), // add listener again
);
}
async recommendSplit(useNumberOfClusters = false) {
console.log("does not make sense for more than 1 attribute");
// console.log("recommendSplit scatterplot");
//
// let numberOfClusters = 0;
// if (useNumberOfClusters) {
// // select the bins field
// // binsCount0 = (this.controls.querySelector(`#split input.bins[data-axis=x]`) as HTMLInputElement).valueAsNumber;
// // console.log("binsCountX", binsCount0);
// // binsCount1 = (this.controls.querySelector(`#split input.bins[data-axis=y]`) as HTMLInputElement).valueAsNumber;
// // console.log("binsCountY", binsCount1);
// numberOfClusters = (this.controls.querySelector(`#split #recommendSplitControls input.clusters`) as HTMLInputElement).valueAsNumber;
// console.log("useNumberOfClusters", useNumberOfClusters);
// }
//
// const cohorts = this.cohorts;
//
// let filterDesc: IFilterDesc[] = [];
// if (cohorts.length === 1) {
// // it does not make sense to do a recommendSplit on multiple cohorts at once
// // createAutomatically looks at the data of one cohort and creates new cohorts based on that
// // recommendSplit recommends splits that are used for ALL cohorts, so it would not make sense to use it on multiple cohorts
//
// // 1 cohort, 1 category
// let filter: INumRange[] | IEqualsList = [];
//
// filterDesc.push({
// cohort: cohorts[0],
// filter: [
// {
// attr: this.attributes[0],
// range: filter,
// },
// ],
// });
//
// filterDesc.push({
// cohort: cohorts[0],
// filter: [
// {
// attr: this.attributes[1],
// range: filter,
// },
// ],
// });
//
// const params: ICohortMultiAttrDBDataRecommendSplitParams = {
// cohortId: filterDesc[0].cohort.dbId,
// attribute0: this.attributes[0].dataKey,
// attribute1: this.attributes[1].dataKey,
// numberOfClusters: numberOfClusters,
// };
//
// const data = await recommendSplitDB(params);
// console.log("recommendSplit", data);
//
// // set the bins for x-axis
// let splitValues = [];
// for (let i = 0; i < data[this.attributes[0].dataKey].length; i++) {
// // get the int val of the data[i]
// const binBorder = Number(data[this.attributes[0].dataKey][i]);
// splitValues.push(binBorder);
// }
//
// this.vegaView.data(`splitvalues_x`, splitValues.slice()); // set a defensive copy
// console.log("split values x", splitValues.slice());
// // this.vegaView.runAsync(); // update the view
// this.vegaView.runAsync().then(
// (
// vegaView, // defer adding signallistener until the new data is set internally
// ) => vegaView.addDataListener(`splitvalues_x`, this.vegaSplitListener), // add listener again
// );
//
//
// // set the bins for y-axis
// splitValues = [];
// for (let i = 0; i < data[this.attributes[1].dataKey].length; i++) {
// // get the int val of the data[i]
// const binBorder = Number(data[this.attributes[1].dataKey][i]);
// splitValues.push(binBorder);
// }
//
// this.vegaView.data(`splitvalues_y`, splitValues.slice()); // set a defensive copy
// console.log("split values y", splitValues.slice());
// // this.vegaView.runAsync(); // update the view
// this.vegaView.runAsync().then(
// (
// vegaView, // defer adding signallistener until the new data is set internally
// ) => vegaView.addDataListener(`splitvalues_y`, this.vegaSplitListener), // add listener again
// );
// }
}

async createAutomatically(useNumberOfClusters: boolean = false) {
async createAutomatically(useNumberOfClusters = false) {
console.log("createAutomatically scatterplot");

// AttributeType = 'categorical' | 'number' | 'string'; TODO send it with the data

let numberOfClusters = 0;
if (useNumberOfClusters) {
// select the bins field
Expand Down Expand Up @@ -1379,6 +1376,10 @@ export class TsneScatterplot extends Scatterplot {
this.container.dispatchEvent(new FilterEvent(filterDescs));
}

async recommendSplit(useNumberOfClusters = false) {
console.log("does not make sense for more than 1 attribute");
}

split() {
const filterDescs: IFilterDesc[] = [];
for (const cohort of this.cohorts) {
Expand Down
Loading

0 comments on commit f263738

Please sign in to comment.