<commit_before>import { Meteor } from 'meteor/meteor';
import { parsingUtils } from './parsingUtils';

const metaDataLookup = {};

/**
 * Cornerstone MetaData provider to store image meta data
 * Data from instances, series, and studies are associated with
 * imageIds to facilitate usage of this information by Cornerstone's Tools
 *
 * e.g. the imagePlane metaData object contains instance information about
 * row/column pixel spacing, patient position, and patient orientation. It
 * is used in CornerstoneTools to position reference lines and orientation markers.
 *
 * @param {String} imageId The Cornerstone ImageId
 * @param {Object} data An object containing instance, series, and study metaData
 */
const addMetaData = (imageId, data) => {
    let instanceMetaData = data.instance;
    let seriesMetaData = data.series;
    let studyMetaData = data.study;
    let imageIndex = data.imageIndex;
    let numImages = data.numImages;

    let metaData = {};

    metaData.study = {
        patientId: studyMetaData.patientId,
        studyInstanceUid: studyMetaData.studyInstanceUid,
        studyDate: studyMetaData.studyDate,
        studyTime: studyMetaData.studyTime,
        studyDescription: studyMetaData.studyDescription,
        institutionName: studyMetaData.institutionName,
        patientHistory: studyMetaData.patientHistory
    };

    metaData.series = {
        seriesDescription: seriesMetaData.seriesDescription,
        seriesNumber: seriesMetaData.seriesNumber,
        modality: seriesMetaData.modality,
        seriesInstanceUid: seriesMetaData.seriesInstanceUid,
        numImages: numImages
    };

    metaData.instance = instanceMetaData;

    metaData.patient = {
        name: studyMetaData.patientName,
        id: studyMetaData.patientId,
        birthDate: studyMetaData.patientBirthDate,
        sex: studyMetaData.patientSex
    };

    // If there is sufficient information, populate
    // the imagePlane object for easier use in the Viewer
    metaData.imagePlane = getImagePlane(instanceMetaData);

    // Add the metaData to the imageId lookup object
    metaDataLookup[imageId] = metaData;
};

/**
 * Adds a set of metaData to the Cornerstone metaData provider given a specific
 * imageId, type, and dataset
 *
 * @param imageId
 * @param type (e.g. series, instance, tagDisplay)
 * @param data
 */
const addSpecificMetadata = (imageId, type, data) => {
    let metaData = {};
    metaData[type] = data;

    metaDataLookup[imageId] = $.extend(metaDataLookup[imageId], metaData);
};


function getFromDataSet(dataSet, type, tag) {
    if (!dataSet) {
        return;
    }

    const fn = dataSet[type];
    if (!fn) {
        return;
    }

    return fn.call(dataSet, tag);
}

/**
 * Updates the related metaData for missing fields given a specified image
 *
 * @param image
 */
const updateMetaData = image => {
    const imageMetaData = metaDataLookup[image.imageId];
    if (!imageMetaData) {
        return;
    }

    imageMetaData.instance.rows = imageMetaData.instance.rows || image.rows;
    imageMetaData.instance.columns = imageMetaData.instance.columns || image.columns;

    imageMetaData.instance.sopClassUid = imageMetaData.instance.sopClassUid || getFromDataSet(image.data, 'string', 'x00080016');
    imageMetaData.instance.sopInstanceUid = imageMetaData.instance.sopInstanceUid || getFromDataSet(image.data, 'string', 'x00080018');

    imageMetaData.instance.pixelSpacing = imageMetaData.instance.pixelSpacing || getFromDataSet(image.data, 'string', 'x00280030');
    imageMetaData.instance.frameOfReferenceUID = imageMetaData.instance.frameOfReferenceUID || getFromDataSet(image.data, 'string', 'x00200052');
    imageMetaData.instance.imageOrientationPatient = imageMetaData.instance.imageOrientationPatient || getFromDataSet(image.data, 'string', 'x00200037');
    imageMetaData.instance.imagePositionPatient = imageMetaData.instance.imagePositionPatient || getFromDataSet(image.data, 'string', 'x00200032');

    imageMetaData.instance.sliceThickness = imageMetaData.instance.sliceThickness || getFromDataSet(image.data, 'string', 'x00180050');
    imageMetaData.instance.sliceLocation = imageMetaData.instance.sliceLocation || getFromDataSet(image.data, 'string', 'x00201041');
    imageMetaData.instance.tablePosition = imageMetaData.instance.tablePosition || getFromDataSet(image.data, 'string', 'x00189327');
    imageMetaData.instance.spacingBetweenSlices = imageMetaData.instance.spacingBetweenSlices || getFromDataSet(image.data, 'string', 'x00180088');

    imageMetaData.instance.lossyImageCompression = imageMetaData.instance.lossyImageCompression || getFromDataSet(image.data, 'string', 'x00282110');
    imageMetaData.instance.lossyImageCompressionRatio = imageMetaData.instance.lossyImageCompressionRatio || getFromDataSet(image.data, 'string', 'x00282112');

    imageMetaData.instance.frameIncrementPointer = imageMetaData.instance.frameIncrementPointer || getFromDataSet(image.data, 'string', 'x00280009');
    imageMetaData.instance.frameTime = imageMetaData.instance.frameTime || getFromDataSet(image.data, 'string', 'x00181063');
    imageMetaData.instance.frameTimeVector = imageMetaData.instance.frameTimeVector || getFromDataSet(image.data, 'string', 'x00181065');

    if (image.data && !imageMetaData.instance.multiframeMetadata) {
        imageMetaData.instance.multiframeMetadata = getMultiframeModuleMetaData(image.data);
    }

    imageMetaData.imagePlane = imageMetaData.imagePlane || getImagePlane(imageMetaData.instance);
};

/**
 * Constructs and returns the imagePlane given the metadata instance
 *
 * @param metadataInstance The metadata instance (InstanceMetadata class) containing information to construct imagePlane
 * @returns imagePlane The constructed imagePlane to be used in viewer easily
 */
function getImagePlane(metadataInstance) {
    const instance = metadataInstance.getData();
    if (!instance.rows || !instance.columns || !instance.pixelSpacing ||
        !instance.frameOfReferenceUID || !instance.imageOrientationPatient ||
        !instance.imagePositionPatient) {
        return;
    }

    const imageOrientation = instance.imageOrientationPatient.split('\\');
    const imagePosition = instance.imagePositionPatient.split('\\');

    let columnPixelSpacing = 1.0;
    let rowPixelSpacing = 1.0;
    if (instance.pixelSpacing) {<commit_msg>Fixing rebase issue: restoring getImagePlane behaviour...<commit_after>import { Meteor } from 'meteor/meteor';
import { parsingUtils } from './parsingUtils';

const metaDataLookup = {};

/**
 * Cornerstone MetaData provider to store image meta data
 * Data from instances, series, and studies are associated with
 * imageIds to facilitate usage of this information by Cornerstone's Tools
 *
 * e.g. the imagePlane metaData object contains instance information about
 * row/column pixel spacing, patient position, and patient orientation. It
 * is used in CornerstoneTools to position reference lines and orientation markers.
 *
 * @param {String} imageId The Cornerstone ImageId
 * @param {Object} data An object containing instance, series, and study metaData
 */
const addMetaData = (imageId, data) => {
    let instanceMetaData = data.instance;
    let seriesMetaData = data.series;
    let studyMetaData = data.study;
    let imageIndex = data.imageIndex;
    let numImages = data.numImages;

    let metaData = {};

    metaData.study = {
        patientId: studyMetaData.patientId,
        studyInstanceUid: studyMetaData.studyInstanceUid,
        studyDate: studyMetaData.studyDate,
        studyTime: studyMetaData.studyTime,
        studyDescription: studyMetaData.studyDescription,
        institutionName: studyMetaData.institutionName,
        patientHistory: studyMetaData.patientHistory
    };

    metaData.series = {
        seriesDescription: seriesMetaData.seriesDescription,
        seriesNumber: seriesMetaData.seriesNumber,
        modality: seriesMetaData.modality,
        seriesInstanceUid: seriesMetaData.seriesInstanceUid,
        numImages: numImages
    };

    metaData.instance = instanceMetaData;

    metaData.patient = {
        name: studyMetaData.patientName,
        id: studyMetaData.patientId,
        birthDate: studyMetaData.patientBirthDate,
        sex: studyMetaData.patientSex
    };

    // If there is sufficient information, populate
    // the imagePlane object for easier use in the Viewer
    metaData.imagePlane = getImagePlane(instanceMetaData);

    // Add the metaData to the imageId lookup object
    metaDataLookup[imageId] = metaData;
};

/**
 * Adds a set of metaData to the Cornerstone metaData provider given a specific
 * imageId, type, and dataset
 *
 * @param imageId
 * @param type (e.g. series, instance, tagDisplay)
 * @param data
 */
const addSpecificMetadata = (imageId, type, data) => {
    let metaData = {};
    metaData[type] = data;

    metaDataLookup[imageId] = $.extend(metaDataLookup[imageId], metaData);
};


function getFromDataSet(dataSet, type, tag) {
    if (!dataSet) {
        return;
    }

    const fn = dataSet[type];
    if (!fn) {
        return;
    }

    return fn.call(dataSet, tag);
}

/**
 * Updates the related metaData for missing fields given a specified image
 *
 * @param image
 */
const updateMetaData = image => {
    const imageMetaData = metaDataLookup[image.imageId];
    if (!imageMetaData) {
        return;
    }

    imageMetaData.instance.rows = imageMetaData.instance.rows || image.rows;
    imageMetaData.instance.columns = imageMetaData.instance.columns || image.columns;

    imageMetaData.instance.sopClassUid = imageMetaData.instance.sopClassUid || getFromDataSet(image.data, 'string', 'x00080016');
    imageMetaData.instance.sopInstanceUid = imageMetaData.instance.sopInstanceUid || getFromDataSet(image.data, 'string', 'x00080018');

    imageMetaData.instance.pixelSpacing = imageMetaData.instance.pixelSpacing || getFromDataSet(image.data, 'string', 'x00280030');
    imageMetaData.instance.frameOfReferenceUID = imageMetaData.instance.frameOfReferenceUID || getFromDataSet(image.data, 'string', 'x00200052');
    imageMetaData.instance.imageOrientationPatient = imageMetaData.instance.imageOrientationPatient || getFromDataSet(image.data, 'string', 'x00200037');
    imageMetaData.instance.imagePositionPatient = imageMetaData.instance.imagePositionPatient || getFromDataSet(image.data, 'string', 'x00200032');

    imageMetaData.instance.sliceThickness = imageMetaData.instance.sliceThickness || getFromDataSet(image.data, 'string', 'x00180050');
    imageMetaData.instance.sliceLocation = imageMetaData.instance.sliceLocation || getFromDataSet(image.data, 'string', 'x00201041');
    imageMetaData.instance.tablePosition = imageMetaData.instance.tablePosition || getFromDataSet(image.data, 'string', 'x00189327');
    imageMetaData.instance.spacingBetweenSlices = imageMetaData.instance.spacingBetweenSlices || getFromDataSet(image.data, 'string', 'x00180088');

    imageMetaData.instance.lossyImageCompression = imageMetaData.instance.lossyImageCompression || getFromDataSet(image.data, 'string', 'x00282110');
    imageMetaData.instance.lossyImageCompressionRatio = imageMetaData.instance.lossyImageCompressionRatio || getFromDataSet(image.data, 'string', 'x00282112');

    imageMetaData.instance.frameIncrementPointer = imageMetaData.instance.frameIncrementPointer || getFromDataSet(image.data, 'string', 'x00280009');
    imageMetaData.instance.frameTime = imageMetaData.instance.frameTime || getFromDataSet(image.data, 'string', 'x00181063');
    imageMetaData.instance.frameTimeVector = imageMetaData.instance.frameTimeVector || getFromDataSet(image.data, 'string', 'x00181065');

    if (image.data && !imageMetaData.instance.multiframeMetadata) {
        imageMetaData.instance.multiframeMetadata = getMultiframeModuleMetaData(image.data);
    }

    imageMetaData.imagePlane = imageMetaData.imagePlane || getImagePlane(imageMetaData.instance);
};

/**
 * Constructs and returns the imagePlane given the metadata instance
 *
 * @param metadataInstance The metadata instance (InstanceMetadata class) containing information to construct imagePlane
 * @returns imagePlane The constructed imagePlane to be used in viewer easily
 */
function getImagePlane(instance) {
    if (!instance.rows || !instance.columns || !instance.pixelSpacing ||
        !instance.frameOfReferenceUID || !instance.imageOrientationPatient ||
        !instance.imagePositionPatient) {
        return;
    }

    const imageOrientation = instance.imageOrientationPatient.split('\\');
    const imagePosition = instance.imagePositionPatient.split('\\');

    let columnPixelSpacing = 1.0;
    let rowPixelSpacing = 1.0;
    if (instance.pixelSpacing) {
{'fraction_non_alphanumeric': 0.10051080550098232, 'fraction_numerical': 0.019489194499017683, 'mean_word_length': 4.4618025751072965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '976264', 'n_tokens_mistral': 3641, 'n_tokens_neox': 3510, 'n_words': 788}