# Path: util/measurementReportUtils/measurementGroup.py
# class MeasurementGroup(object):
#   """
#   Data structure plus convenience methods to create measurment groups following
#   the required format to be processed by the DCMQI tid1500writer tool. Use this
#   to populate the Measurements list in :class:`MeasurementReport`.
#   """
# 
#   def __init__(self, 
#                trackingIdentifier, trackingUniqueIdentifier, referencedSegment, 
#                sourceSeriesInstanceUID, segmentationSOPInstanceUID,
#                finding, findingSite):
#     self.TrackingIdentifier = trackingIdentifier
#     self.TrackingUniqueIdentifier = trackingUniqueIdentifier
#     self.ReferencedSegment = referencedSegment
#     self.SourceSeriesForImageSegmentation = sourceSeriesInstanceUID
#     self.segmentationSOPInstanceUID = segmentationSOPInstanceUID
#     self.Finding = finding
#     self.FindingSite = findingSite
#     self.measurementItems = []
# 
#   def addMeasurementItem(self, measurementItem):
#     self.measurementItems.append(measurementItem)
# 
# Path: util/measurementReportUtils/measurementItem.py
# class MeasurementItem(object):
# 
#   def __init__(self, value):
#     self.value = self.convertNumericToDcmtkFittingString(value)
# 
#   def convertNumericToDcmtkFittingString(self, value):
#     if isinstance(value, float) or isinstance(value, int):
#       s = str(value)
#       if (len(s) <= 16):
#         return s
#       elif (s.find(".") >= 0) and (s.find(".") < 15):
#         return s[:16]
#       else:
#         raise ValueError("Value cannot be converted to 16 digits without loosing too much precision!")
#     else:
#       raise TypeError("Value to convert is not of type float or int")
# 
# Path: util/measurementReportUtils/codeSequences.py
# class CodeSequence(object):
# 
#   def __init__(self, codeMeaning, codingSchemeDesignator, codeValue):
#     self.CodeValue = codeValue
#     self.CodingSchemeDesignator = codingSchemeDesignator
#     self.CodeMeaning = codeMeaning


import json
from .measurementGroup import MeasurementGroup
from .measurementItem import MeasurementItem
from .codeSequences import CodeSequence

  def addMeasurementGroup(self, measurementGroup):
    self.Measurements.append(measurementGroup)

  
  def exportToJson(self, fileName):
    with open(fileName, 'w') as fp:
      json.dump(self._getAsDict(), fp, indent = 2)


  def getJsonStr(self):
    return json.dumps(self._getAsDict(), indent = 2)


  def _getAsDict(self):
    # This is a bit of a hack to get the "@schema" in there, didn't figure out how to 
    # do this otherwise with json.dumps. If this wasn't needed I could just dump
    # the json directly with my custom encoder.
    jsonStr = json.dumps(self, indent = 2, cls = self._MyJSONEncoder)
    tempDict = json.loads(jsonStr)
    outDict = {}
    outDict["@schema"] = "https://raw.githubusercontent.com/qiicr/dcmqi/master/doc/schemas/sr-tid1500-schema.json#"
    outDict.update(tempDict)
    return outDict

  # Inner private class to define a custom JSON encoder for serializing MeasurmentReport
  class _MyJSONEncoder(json.JSONEncoder):
    def default(self, obj):
      if (isinstance(obj, MeasurementReport) or 
          isinstance(obj, MeasurementGroup) or

# Path: util/measurementReportUtils/measurementGroup.py
# class MeasurementGroup(object):
#   """
#   Data structure plus convenience methods to create measurment groups following
#   the required format to be processed by the DCMQI tid1500writer tool. Use this
#   to populate the Measurements list in :class:`MeasurementReport`.
#   """
# 
#   def __init__(self, 
#                trackingIdentifier, trackingUniqueIdentifier, referencedSegment, 
#                sourceSeriesInstanceUID, segmentationSOPInstanceUID,
#                finding, findingSite):
#     self.TrackingIdentifier = trackingIdentifier
#     self.TrackingUniqueIdentifier = trackingUniqueIdentifier
#     self.ReferencedSegment = referencedSegment
#     self.SourceSeriesForImageSegmentation = sourceSeriesInstanceUID
#     self.segmentationSOPInstanceUID = segmentationSOPInstanceUID
#     self.Finding = finding
#     self.FindingSite = findingSite
#     self.measurementItems = []
# 
#   def addMeasurementItem(self, measurementItem):
#     self.measurementItems.append(measurementItem)
# 
# Path: util/measurementReportUtils/measurementItem.py
# class MeasurementItem(object):
# 
#   def __init__(self, value):
#     self.value = self.convertNumericToDcmtkFittingString(value)
# 
#   def convertNumericToDcmtkFittingString(self, value):
#     if isinstance(value, float) or isinstance(value, int):
#       s = str(value)
#       if (len(s) <= 16):
#         return s
#       elif (s.find(".") >= 0) and (s.find(".") < 15):
#         return s[:16]
#       else:
#         raise ValueError("Value cannot be converted to 16 digits without loosing too much precision!")
#     else:
#       raise TypeError("Value to convert is not of type float or int")
# 
# Path: util/measurementReportUtils/codeSequences.py
# class CodeSequence(object):
# 
#   def __init__(self, codeMeaning, codingSchemeDesignator, codeValue):
#     self.CodeValue = codeValue
#     self.CodingSchemeDesignator = codingSchemeDesignator
#     self.CodeMeaning = codeMeaning


# Path: util/measurementReportUtils/measurementReport.py
import json
from .measurementGroup import MeasurementGroup
from .measurementItem import MeasurementItem
from .codeSequences import CodeSequence


  def addMeasurementGroup(self, measurementGroup):
    self.Measurements.append(measurementGroup)

  
  def exportToJson(self, fileName):
    with open(fileName, 'w') as fp:
      json.dump(self._getAsDict(), fp, indent = 2)


  def getJsonStr(self):
    return json.dumps(self._getAsDict(), indent = 2)


  def _getAsDict(self):
    # This is a bit of a hack to get the "@schema" in there, didn't figure out how to 
    # do this otherwise with json.dumps. If this wasn't needed I could just dump
    # the json directly with my custom encoder.
    jsonStr = json.dumps(self, indent = 2, cls = self._MyJSONEncoder)
    tempDict = json.loads(jsonStr)
    outDict = {}
    outDict["@schema"] = "https://raw.githubusercontent.com/qiicr/dcmqi/master/doc/schemas/sr-tid1500-schema.json#"
    outDict.update(tempDict)
    return outDict

  # Inner private class to define a custom JSON encoder for serializing MeasurmentReport
  class _MyJSONEncoder(json.JSONEncoder):
    def default(self, obj):
      if (isinstance(obj, MeasurementReport) or 
          isinstance(obj, MeasurementGroup) or

          isinstance(obj, MeasurementItem) or