All files / extensions/cornerstone/src/utils generateSegmentationCSVReport.ts

0% Statements 0/61
0% Branches 0/41
0% Functions 0/6
0% Lines 0/61

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                                                                                                                                                                                                                                                                                       
export function generateSegmentationCSVReport(
  segmentationData,
  info: {
    reference: {
      SeriesNumber: string;
      SeriesInstanceUID: string;
      StudyInstanceUID: string;
      SeriesDate: string;
      SeriesTime: string;
      SeriesDescription: string;
    };
  }
) {
  // Initialize the rows for our CSV
  const csvRows = [];
 
  // Add segmentation-level information
  csvRows.push(['Segmentation ID', segmentationData.segmentationId || '']);
  csvRows.push(['Segmentation Label', segmentationData.label || '']);
 
  csvRows.push([]);
 
  const additionalInfo = info.reference;
  // Add reference information
  const referenceKeys = [
    ['Series Number', additionalInfo.SeriesNumber],
    ['Series Instance UID', additionalInfo.SeriesInstanceUID],
    ['Study Instance UID', additionalInfo.StudyInstanceUID],
    ['Series Date', additionalInfo.SeriesDate],
    ['Series Time', additionalInfo.SeriesTime],
    ['Series Description', additionalInfo.SeriesDescription],
  ];
 
  referenceKeys.forEach(([key, value]) => {
    Iif (value) {
      csvRows.push([`reference ${key}`, value]);
    }
  });
 
  // Add a blank row for separation
  csvRows.push([]);
 
  csvRows.push(['Segments Statistics']);
 
  // Add segment information in columns
  Iif (segmentationData.segments) {
    // First row: Segment headers
    const segmentHeaderRow = ['Label'];
    for (const segmentId in segmentationData.segments) {
      const segment = segmentationData.segments[segmentId];
      segmentHeaderRow.push(`${segment.label || ''}`);
    }
    csvRows.push(segmentHeaderRow);
 
    // Add segment properties
    csvRows.push([
      'Segment Index',
      ...Object.values(segmentationData.segments).map(s => s.segmentIndex || ''),
    ]);
    csvRows.push([
      'Locked',
      ...Object.values(segmentationData.segments).map(s => (s.locked ? 'Yes' : 'No')),
    ]);
    csvRows.push([
      'Active',
      ...Object.values(segmentationData.segments).map(s => (s.active ? 'Yes' : 'No')),
    ]);
 
    // Add segment statistics
    // First, collect all unique statistics across all segments
    const allStats = new Set();
    for (const segment of Object.values(segmentationData.segments)) {
      Iif (segment.cachedStats && segment.cachedStats.namedStats) {
        for (const statKey in segment.cachedStats.namedStats) {
          const stat = segment.cachedStats.namedStats[statKey];
          const statLabel = stat.label || stat.name;
          const statUnit = stat.unit ? ` (${stat.unit})` : '';
          allStats.add(`${statLabel}${statUnit}`);
        }
      }
    }
 
    // Then create a row for each statistic
    for (const statName of allStats) {
      const statRow = [statName];
 
      for (const segment of Object.values(segmentationData.segments)) {
        let statValue = '';
 
        Iif (segment.cachedStats && segment.cachedStats.namedStats) {
          for (const statKey in segment.cachedStats.namedStats) {
            const stat = segment.cachedStats.namedStats[statKey];
            const currentStatName = `${stat.label || stat.name}${stat.unit ? ` (${stat.unit})` : ''}`;
 
            Iif (currentStatName === statName) {
              statValue = stat.value !== undefined ? stat.value : '';
              break;
            }
          }
        }
 
        statRow.push(statValue);
      }
 
      csvRows.push(statRow);
    }
  }
 
  // Convert to CSV string
  let csvString = '';
  for (const row of csvRows) {
    const formattedRow = row.map(cell => {
      // Handle values that need to be quoted (contain commas, quotes, or newlines)
      const cellValue = cell !== undefined && cell !== null ? cell.toString() : '';
      Iif (cellValue.includes(',') || cellValue.includes('"') || cellValue.includes('\n')) {
        // Escape quotes and wrap in quotes
        return '"' + cellValue.replace(/"/g, '""') + '"';
      }
      return cellValue;
    });
    csvString += formattedRow.join(',') + '\n';
  }
 
  // Create a download link and trigger the download
  const blob = new Blob([csvString], { type: 'text/csv;charset=utf-8;' });
  const url = URL.createObjectURL(blob);
  const link = document.createElement('a');
 
  link.setAttribute('href', url);
  link.setAttribute(
    'download',
    `${segmentationData.label || 'Segmentation'}_Report_${new Date().toISOString().split('T')[0]}.csv`
  );
  link.style.visibility = 'hidden';
 
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}