Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | 5130x 50x 5080x 5080x 5080x 5080x 384x | /**
* Typed provider for scalingModule. Expects instance or natural data in the
* chain's data field (via dataLookup). Data is assumed to use upper camel case
* for all tags (DICOM keyword style). Uses NATURAL in the default setup
* (multiframe, no per-frame scaling). PT: builds InstanceMetadata from data
* and uses @cornerstonejs/calculate-suv; RTDOSE: returns DoseGridScaling etc.
*/
import { addTypedProvider } from '../../metaData';
import type { TypedProvider } from '../../metaData';
import { MetadataModules } from '../../enums';
import { calculateSUVScalingFactors } from '@cornerstonejs/calculate-suv';
import type { InstanceMetadata } from '@cornerstonejs/calculate-suv';
function timeToString(v: unknown): string {
if (typeof v === 'string') return v;
if (v && typeof v === 'object' && !Array.isArray(v)) {
const t = v as {
hours?: number;
minutes?: number;
seconds?: number;
fractionalSeconds?: number;
};
const hours = `${t?.hours ?? '00'}`.padStart(2, '0');
const minutes = `${t?.minutes ?? '00'}`.padStart(2, '0');
const seconds = `${t?.seconds ?? '00'}`.padStart(2, '0');
const fractionalSeconds = `${t?.fractionalSeconds ?? '000000'}`.padEnd(
6,
'0'
);
return `${hours}${minutes}${seconds}.${fractionalSeconds}`;
}
return v as string;
}
function dateToString(v: unknown): string {
if (typeof v === 'string') return v;
if (v && typeof v === 'object' && !Array.isArray(v) && 'year' in v) {
const d = v as { year: number; month: number; day: number };
const month = `${d.month}`.padStart(2, '0');
const day = `${d.day}`.padStart(2, '0');
return `${d.year}${month}${day}`;
}
return v as string;
}
function parseNumber(v: unknown): number | undefined {
if (typeof v === 'number' && !Number.isNaN(v)) return v;
if (typeof v === 'string') {
const n = Number(v);
return Number.isNaN(n) ? undefined : n;
}
return undefined;
}
/** First item of RadiopharmaceuticalInfo (sequence); supports array or array-like (e.g. makeArrayLike). */
function getRadiopharmaceuticalInfo(
data: Record<string, unknown>
): Record<string, unknown> | undefined {
const ri = data.RadiopharmaceuticalInfo;
if (ri == null || typeof ri !== 'object') return undefined;
const first = Array.isArray(ri)
? ri[0]
: ((ri as { 0?: unknown; length?: number })[0] ?? ri);
return first && typeof first === 'object' && !Array.isArray(first)
? (first as Record<string, unknown>)
: undefined;
}
/**
* Build InstanceMetadata for calculate-suv from data. Data is assumed to use
* upper camel case for all tags (Modality, SeriesDate, PatientWeight, etc.).
*/
function buildPTInstanceMetadataFromData(
data: Record<string, unknown>
): InstanceMetadata | null {
if (data.Modality !== 'PT') return null;
const {
SeriesDate: seriesDate,
SeriesTime: seriesTime,
AcquisitionDate: acquisitionDate,
AcquisitionTime: acquisitionTime,
PatientWeight: patientWeight,
CorrectedImage: correctedImage,
Units: units,
DecayCorrection: decayCorrection,
} = data;
const ri = getRadiopharmaceuticalInfo(data);
if (!ri) return null;
const radionuclideTotalDose = parseNumber(ri.RadionuclideTotalDose);
const radionuclideHalfLife = parseNumber(ri.RadionuclideHalfLife);
const radiopharmaceuticalStartDateTime =
ri.RadiopharmaceuticalStartDateTime as string | undefined;
const radiopharmaceuticalStartTime = ri.RadiopharmaceuticalStartTime as
| string
| undefined;
const patientWeightNum = parseNumber(patientWeight);
if (
seriesDate === undefined ||
seriesTime === undefined ||
patientWeightNum === undefined ||
acquisitionDate === undefined ||
acquisitionTime === undefined ||
correctedImage === undefined ||
units === undefined ||
decayCorrection === undefined ||
radionuclideTotalDose === undefined ||
radionuclideHalfLife === undefined
) {
return null;
}
const toDate = (v: unknown): string =>
typeof v === 'string'
? v
: v && typeof v === 'object' && 'year' in v
? dateToString(v as { year: number; month: number; day: number })
: (v as string);
const toTime = (v: unknown): string =>
typeof v === 'string'
? v
: v && typeof v === 'object'
? timeToString(v as Parameters<typeof timeToString>[0])
: (v as string);
const correctedImageValue =
typeof correctedImage === 'string'
? correctedImage.split('\\')
: Array.isArray(correctedImage)
? correctedImage
: correctedImage;
const instanceMetadata: InstanceMetadata = {
CorrectedImage: correctedImageValue as string | string[],
Units: units as string,
RadionuclideHalfLife: radionuclideHalfLife,
RadionuclideTotalDose: radionuclideTotalDose,
DecayCorrection: decayCorrection as string,
PatientWeight: patientWeightNum,
SeriesDate: dateToString(seriesDate),
SeriesTime: timeToString(seriesTime),
AcquisitionDate: dateToString(acquisitionDate),
AcquisitionTime: timeToString(acquisitionTime),
};
if (radiopharmaceuticalStartDateTime !== undefined) {
instanceMetadata.RadiopharmaceuticalStartDateTime = dateToString(
radiopharmaceuticalStartDateTime
);
}
if (radiopharmaceuticalStartTime !== undefined) {
instanceMetadata.RadiopharmaceuticalStartTime = timeToString(
radiopharmaceuticalStartTime
);
}
if (data.PatientSex !== undefined)
instanceMetadata.PatientSex = data.PatientSex as string;
if (data.PatientSize !== undefined)
instanceMetadata.PatientSize = data.PatientSize as number;
return instanceMetadata;
}
function scalingFromInstanceProvider(
next: (query: string, data?: unknown, options?: unknown) => unknown,
query: string,
data?: unknown,
options?: unknown
): unknown {
if (data == null || typeof data !== 'object') {
return next(query, data, options);
}
const d = data as Record<string, unknown>;
Iif (d.Modality === 'RTDOSE') {
const doseGridScaling = parseNumber(d.DoseGridScaling);
const { DoseSummation, DoseType, DoseUnit } = d;
if (
doseGridScaling !== undefined ||
DoseSummation !== undefined ||
DoseType !== undefined ||
DoseUnit !== undefined
) {
return {
DoseGridScaling: doseGridScaling,
DoseSummation,
DoseType,
DoseUnit,
};
}
}
Iif (d.Modality === 'PT') {
const instanceMetadata = buildPTInstanceMetadataFromData(d);
if (!instanceMetadata) {
return next(query, undefined, options);
}
try {
const factors = calculateSUVScalingFactors([instanceMetadata]);
return factors[0] ?? next(query, undefined, options);
} catch {
return next(query, undefined, options);
}
}
return next(query, undefined, options);
}
export function registerScalingFromInstanceProvider(): void {
addTypedProvider(
MetadataModules.SCALING,
scalingFromInstanceProvider as TypedProvider,
{ priority: 0, isDefault: true }
);
}
|