Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotly #2

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 94 additions & 8 deletions app/RootContext.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,104 @@
import {createContext} from "react";

export interface State {
history: string
titre_type: string
type PlotDisplay = "facet" | "trace"

export interface PlotOptions {
[index: string]: PlotDisplay
}

export interface PlotConfig {
key: string
displayName: string
type: string,
lineColors?: any
fillColors?: any
}

export interface Model {
key: string
displayName: string
datasets: Dataset[]
regressionModels: Covariate[]
plots: PlotConfig[]
variables: Covariate[]
}

export interface Dataset {
key: string
displayName: string
}

export interface Covariate {
displayName: string
key: string
}

export interface AppState {
models: Model[]
selectedModel: Model
selectedDataset: Dataset
selectedRegressionModel: Covariate
selectedPlotOptions: { [index: string]: PlotOptions }
}

export interface ContextValue {
state: State
export interface AppContext {
state: AppState
dispatch: () => void
}

export const initialState = {history: "Facet", titre_type: "Trace"}
export const RootContext = createContext<ContextValue>({state: initialState, dispatch: () => null})
const biomarkerModel: Model = {
displayName: "Biomarker Kinetics",
key: "biomarker",
datasets: [{
key: "legacy",
displayName: "SARS-CoV2-legacy"
}],
regressionModels: [
{key: "infection_history", displayName: "Infection history"},
{key: "last_exp_type", displayName: "Last exposure type"}
],
plots: [{
key: "pop_fits",
displayName: "Population fits",
type: "line",
lineColors: [
`rgba(204, 102, 119, 1)`,
`rgba(221, 204, 119, 1)`,
`rgba(136, 204, 238, 1)`,
`rgba(136, 34, 85, 1)`,
`rgba(68, 170, 153, 1)`,
`rgba(226, 226, 226, 1)`]
,
fillColors: [
`rgba(204, 102, 119, 0.3)`,
`rgba(221, 204, 119, 0.3)`,
`rgba(136, 204, 238, 0.3)`,
`rgba(136, 34, 85, 0.3)`,
`rgba(68, 170, 153, 0.3)`,
`rgba(226, 226, 226, 0.3)`

]
}],
variables: [{key: "titre_type", displayName: "Titre type"}]
}

export const initialState: AppState = {
models: [biomarkerModel],
selectedModel: biomarkerModel,
selectedDataset: biomarkerModel.datasets[0],
selectedRegressionModel: biomarkerModel.regressionModels[0],
selectedPlotOptions: Object.fromEntries(biomarkerModel.plots.map(p => [p.key,
Object.fromEntries(biomarkerModel.variables.concat([biomarkerModel.regressionModels[0]]).map(
v => [v.key, "trace"]
))
]))
}

export const RootContext = createContext<AppContext>({
state: initialState,
dispatch: () => null
})

export function rootReducer(oldState, newState): State {
export function rootReducer(oldState, newState): AppState {
return {...newState}
}
83 changes: 83 additions & 0 deletions app/components/ConfiguredPlot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {useContext} from "react";
import {AppContext, Covariate, PlotConfig, RootContext} from "~/RootContext";
import {Col, Row} from "react-bootstrap";
import LocalPlot from "~/components/LocalPlot";

function Facet({
data,
facets,
traces,
covariate,
value,
facetVariables,
traceVariables,
plot
}: {
data: any[],

Check failure on line 16 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

Unexpected any. Specify a different type
facets: { [k: string]: string[] }
traces: { [k: string]: string[] }
covariate: Covariate
value: string,
facetVariables: Covariate[],
traceVariables: Covariate[],
plot: PlotConfig
}) {
const filteredData = data.filter(d => d[covariate.key] == value);
const otherFacetVariables = facetVariables.filter(v => v.key != covariate.key);
if (otherFacetVariables.length == 0) {
return <Col><LocalPlot data={filteredData}
traceVariables={traceVariables}
traces={traces}
plot={plot}
value={value}></LocalPlot></Col>
} else {
const nextFacetVariable = otherFacetVariables.pop()!!;

Check failure on line 34 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

Forbidden extra non-null assertion
const facetValues = facets[nextFacetVariable.key];
return facetValues.map(v => [<h5 className={"text-center"}
key={Math.random().toString(36).substring(2, 7)}>{value}</h5>, <Facet
key={Math.random().toString(36).substring(2, 7)}
plot={plot}
data={filteredData}
facets={facets}
traces={traces}
covariate={nextFacetVariable}
value={v}
facetVariables={otherFacetVariables}
traceVariables={traceVariables}></Facet>])
}
}


export default function ConfiguredPlot({plot, data}) {

Check failure on line 51 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

'plot' is missing in props validation

Check failure on line 51 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

'data' is missing in props validation
const {state} = useContext<AppContext>(RootContext);
const variables = [state.selectedRegressionModel].concat(state.selectedModel.variables);
const settings = state.selectedPlotOptions[plot.key];

Check failure on line 54 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

'plot.key' is missing in props validation

const facetVariables = variables.filter(v => settings[v.key] == "facet");
const traceVariables = variables.filter(v => settings[v.key] == "trace");

const facets = Object.fromEntries(facetVariables.map(v => [v.key, [...new Set(data.map(entry => entry[v.key]))]]));

Check failure on line 59 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

'data.map' is missing in props validation
const traces = Object.fromEntries(traceVariables.map(v => [v.key, [...new Set(data.map(entry => entry[v.key]))]]));

Check failure on line 60 in app/components/ConfiguredPlot.tsx

View workflow job for this annotation

GitHub Actions / ⬣ Lint

'data.map' is missing in props validation

if (facetVariables.length > 0) {
const firstFacet = facetVariables[0];
const facetValues = facets[firstFacet.key];

return <Row>
{facetValues.map(v => <Facet key={v}
plot={plot}
data={data}
facets={facets}
traces={traces}
covariate={firstFacet}
value={v}
facetVariables={facetVariables}
traceVariables={traceVariables}></Facet>)}
</Row>
} else {
return <Row><Col><LocalPlot data={data}
traceVariables={traceVariables}
traces={traces} value={""}
plot={plot}></LocalPlot></Col></Row>
}
}
128 changes: 0 additions & 128 deletions app/components/LineChart.tsx

This file was deleted.

Loading