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

Template transition matrices #5141

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ import Dropdown from 'primevue/dropdown';
import Button from 'primevue/button';
import { StratifiedMatrix } from '@/types/Model';
import type { MiraMatrix, MiraModel, MiraTemplateParams } from '@/model-representation/mira/mira-common';
import { createParameterMatrix, createInitialMatrix, collapseTemplates } from '@/model-representation/mira/mira';
import {
createParameterMatrix,
createDiagramTemplateMatrix,
createInitialMatrix,
collapseTemplates
} from '@/model-representation/mira/mira';
import { getVariable } from '@/model-representation/service';
import { extractTemplateMatrix } from '@/model-representation/mira/mira-util';
import { logger } from '@/utils/logger';
Expand Down Expand Up @@ -221,8 +226,28 @@ async function getMatrixValue(variableName: string) {
return (await pythonInstance.parseExpression(expressionBase)).pmathml;
}

function setupMatrixOptions() {
// Find a default
if (currentMatrixtype.value) {
matrix.value = matrixMap.value[currentMatrixtype.value];
matrixType.value = currentMatrixtype.value;
return;
}

for (let i = 0; i < matrixTypes.length; i++) {
const typeStr = matrixTypes[i];

if (matrixMap.value[typeStr] && matrixMap.value[typeStr].length > 0) {
matrix.value = matrixMap.value[typeStr];
matrixType.value = typeStr;
break;
}
}
}

function generateMatrix() {
const stratifiedType = props.stratifiedMatrixType;
matrixMap.value = {};

if (stratifiedType === StratifiedMatrix.Initials) {
matrix.value = createInitialMatrix(props.mmt, props.id);
Expand All @@ -236,31 +261,25 @@ function generateMatrix() {
other: matrices.other
};

// Find a default
if (currentMatrixtype.value) {
matrix.value = matrixMap.value[currentMatrixtype.value];
matrixType.value = currentMatrixtype.value;
return;
}

for (let i = 0; i < matrixTypes.length; i++) {
const typeStr = matrixTypes[i];

if (matrixMap.value[typeStr] && matrixMap.value[typeStr].length > 0) {
matrix.value = matrixMap.value[typeStr];
matrixType.value = typeStr;
break;
}
}
setupMatrixOptions();
} else if (stratifiedType === StratifiedMatrix.Rates) {
const templatesMap = collapseTemplates(props.mmt).matrixMap;
const matrices = createDiagramTemplateMatrix(props.mmt, props.mmtParams, props.id);

const transitionMatrix = templatesMap.get(props.id);
if (!transitionMatrix) {
logger.error('Failed to generate transition matrix');
return;
}
matrix.value = extractTemplateMatrix(transitionMatrix).matrix;

matrixMap.value = {
subjectOutcome: extractTemplateMatrix(transitionMatrix).matrix,
subjectControllers: matrices.subjectControllers.matrix,
outcomeControllers: matrices.outcomeControllers.matrix,
other: matrices.other
};

setupMatrixOptions();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,75 @@ export const createInitialMatrix = (miraModel: MiraModel, key: string) => {
return m2;
};

/**
* For Template Matrix only
* Assumes one-to-one with cells
*
*/
export const createDiagramTemplateMatrix = (
miraModel: MiraModel,
miraTemplateParams: MiraTemplateParams,
param: string
) => {
const paramMap = collapseTemplates(miraModel).matrixMap;
const childrenParams = paramMap.get(param)?.map((p) => p.name);
if (!childrenParams) throw new Error(`Cannot map ${param}`);

// Create map for mapping params to row/col of matrix
// param => [ [subject, outcome, controllers], [subject, outcome, controllers] ... ]
const paramLocationMap = new Map<string, TemplateSummary[]>();
const templateParams = Object.values(miraTemplateParams);
templateParams.forEach((templateParam) => {
if (!paramLocationMap.has(templateParam.name)) paramLocationMap.set(templateParam.name, []);

paramLocationMap.get(templateParam.name)?.push({
name: templateParam.name,
subject: templateParam.subject,
outcome: templateParam.outcome,
controllers: templateParam.controllers
});
});

// Create map for param values
// param => value
const tempValueMap = new Map<string, any>();
Object.values(miraModel.templates).forEach((tempObj) => {
tempValueMap.set(tempObj.name, tempObj);
});

// Find templates with expressions that contains one or more of the params
const templates = miraModel.templates.filter((t) => {
const miraTemplateParam = miraTemplateParams[t.name];
const intersection = _.intersection(childrenParams, [miraTemplateParam.name]);
return intersection.length > 0;
});
const subjectControllers = extractSubjectControllersMatrix(templates, childrenParams, tempValueMap, paramLocationMap);
const outcomeControllers = extractOutcomeControllersMatrix(templates, childrenParams, tempValueMap, paramLocationMap);

// Others, may be initial, maybe no in use ...
const other: MiraMatrix = [];
childrenParams.forEach((name, idx) => {
const row: MiraMatrixEntry[] = [];
row.push({
row: idx,
col: 0,
rowCriteria: name,
colCriteria: '',
content: {
id: name,
value: miraModel.templates[name]
}
});
other.push(row);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculation of 'others' seems slightly odd as it was an escape-hatch we made specifically for parameters, note templates - which should have regular-looking matrices.

Can we provide some additional background here on what this task is about?


return {
subjectControllers,
outcomeControllers,
other
};
};

/**
* Assumes one-to-one with cells
*
Expand Down