feat(HangingProtocol): add protocol generator back (#3128)

This commit is contained in:
Matthis Duclos authored and GitHub committed 2023-01-25 10:11:08 -05:00
1 parent f7c886878d
commit 514ea3ccea
5 files changed
+139 -37

No files matched your search

@@ -79,6 +79,14 @@ const testProtocol = {
numberOfPriorsReferenced: -1,
};
function testProtocolGenerator({ servicesManager }) {
servicesManager.services.TestService.toCall();
return {
protocol: testProtocol,
};
}
const studyMatch = {
StudyInstanceUID: 'studyMatch',
StudyDescription: 'A PETCT study type',
@@ -107,41 +115,80 @@ const displaySet3 = {
const studyMatchDisplaySets = [displaySet3, displaySet2, displaySet1];
function checkHpsBestMatch(hps) {
hps.run({ studies: [studyMatch], displaySets: studyMatchDisplaySets });
const { hpAlreadyApplied, viewportMatchDetails } = hps.getMatchDetails();
expect(hpAlreadyApplied).toMatchObject(new Map([[0, false]]));
expect(viewportMatchDetails.size).toBe(1);
expect(viewportMatchDetails.get(0)).toMatchObject({
viewportOptions: {
viewportId: 'ctAXIAL',
viewportType: 'volume',
orientation: 'axial',
toolGroupId: 'ctToolGroup',
},
// Matches ds1 because it matches 2 rules, a required and an optional
// ds2 fails to match required and ds3 fails to match an optional.
displaySetsInfo: [
{
SeriesInstanceUID: 'ds1',
displaySetInstanceUID: 'displaySet1',
displaySetOptions: {},
},
],
});
}
describe('HangingProtocolService', () => {
const mockedFunction = jest.fn();
const commandsManager = {};
const hps = new HangingProtocolServiceClass(commandsManager);
const servicesManager = {
services: {
TestService: {
toCall: mockedFunction,
},
},
};
const hps = new HangingProtocolServiceClass(commandsManager, servicesManager);
let initialScaling;
beforeAll(() => {
hps.addProtocol(testProtocol.id, testProtocol);
afterEach(() => {
mockedFunction.mockClear();
});
it('has one protocol', () => {
expect(hps.getProtocols().length).toBe(1);
describe('with a static protocol', () => {
beforeAll(() => {
hps.addProtocol(testProtocol.id, testProtocol);
});
it('has one protocol', () => {
expect(hps.getProtocols().length).toBe(1);
});
describe('run', () => {
it('matches best image match', () => {
checkHpsBestMatch(hps);
});
});
});
describe('run', () => {
it('matches best image match', () => {
hps.run({ studies: [studyMatch], displaySets: studyMatchDisplaySets });
const { hpAlreadyApplied, viewportMatchDetails } = hps.getMatchDetails();
expect(hpAlreadyApplied).toMatchObject(new Map([[0, false]]));
expect(viewportMatchDetails.size).toBe(1);
expect(viewportMatchDetails.get(0)).toMatchObject({
viewportOptions: {
viewportId: 'ctAXIAL',
viewportType: 'volume',
orientation: 'axial',
toolGroupId: 'ctToolGroup',
},
// Matches ds1 because it matches 2 rules, a required and an optional
// ds2 fails to match required and ds3 fails to match an optional.
displaySetsInfo: [
{
SeriesInstanceUID: 'ds1',
displaySetInstanceUID: 'displaySet1',
displaySetOptions: {},
},
],
describe('with protocol generator', () => {
beforeAll(() => {
hps.addProtocol(testProtocol.id, testProtocolGenerator);
});
it('has one protocol', () => {
expect(hps.getProtocols().length).toBe(1);
});
describe('run', () => {
it('matches best image match', () => {
checkHpsBestMatch(hps);
});
it('uses services manager', () => {
hps.run({ studies: [studyMatch], displaySets: studyMatchDisplaySets });
expect(mockedFunction).toHaveBeenCalledTimes(1);
});
});
});
@@ -16,7 +16,7 @@ const EVENTS = {
'event::hanging_protocol_applied_for_viewport',
};
type Protocol = HangingProtocol.Protocol;
type Protocol = HangingProtocol.Protocol | HangingProtocol.ProtocolGenerator;
class HangingProtocolService {
studies: StudyMetadata[];
@@ -180,7 +180,21 @@ class HangingProtocolService {
public getProtocolById(id: string): HangingProtocol.Protocol {
const protocol = this.protocols.get(id);
return protocol;
if (protocol instanceof Function) {
try {
const { protocol: generatedProtocol } = this._getProtocolFromGenerator(
protocol
);
return generatedProtocol;
} catch (error) {
console.warn(
`Error while executing protocol generator for protocol ${id}: ${error}`
);
}
} else {
return this._validateProtocol(protocol);
}
}
/**
@@ -434,6 +448,23 @@ class HangingProtocolService {
return protocol;
}
private _getProtocolFromGenerator(
protocolGenerator: HangingProtocol.ProtocolGenerator
): {
protocol: HangingProtocol.Protocol;
} {
const { protocol } = protocolGenerator({
servicesManager: this._servicesManager,
commandsManager: this._commandsManager,
});
const validatedProtocol = this._validateProtocol(protocol);
return {
protocol: validatedProtocol,
};
}
getViewportsRequireUpdate(viewportIndex, displaySetInstanceUID) {
const newDisplaySetInstanceUID = displaySetInstanceUID;
const protocol = this.protocol;
+5 -2
View File
@@ -151,6 +151,9 @@ type Protocol = {
syncDataForViewports?: boolean;
};
type ProtocolGenerator = ({ servicesManager: any, commandsManager: any }) => {
protocol: Protocol;
};
export type {
SetProtocolOptions,
@@ -171,6 +174,6 @@ export type {
DisplaySetInfo,
GlobalProtocolOptions,
ViewportSpecificProtocolOptions,
DisplaySetAndViewportOptions
DisplaySetAndViewportOptions,
ProtocolGenerator,
};