# Path: lib/channelunits.py
# def getCleanTierName(sTierName):
# def createChannelUnitXMLFile(visitID, workbenchDB, xmlFilePath):
# def createChannelUnitJSONFile(visitID, workbenchDB, jsonFilePath):
# def loadChannelUnitsFromSQLite(visitID, workbenchDB):
# def writeChannelUnitsToXML(visitID, dUnits, xmlFilePath):
# def loadChannelUnitsFromAPI(vid):
# def loadChannelUnitsFromJSON(jsonFilePath):
# def writeChannelUnitsToJSON(jsonFilePath, dUnits):
# def loadChannelUnitsFromXML(xmlFilePath):
# 
# Path: lib/channelunits.py
# def getCleanTierName(sTierName):
#     try:
#         return sTierName.replace(' ', '').replace('/', '').replace('-', '')
#     except Exception, e:
#         raise DataException("Invalid or null tiername passed to 'getCleanTierName()'")


from lib.channelunits import dUnitDefs
from lib.channelunits import getCleanTierName
import numpy as np
    # Retrieve channel unit areas and tier 1 type from channel unit metrics
    dChannelUnits = {}
    for unit in channelUnitMetrics:
        unitNumber = int(unit['ChUnitNumber'])
        unitID = next(u['value']['ChannelUnitID'] for u in channelUnitMeasurements['values'] if u['value']['ChannelUnitNumber'] == unitNumber)
        dChannelUnits[unitNumber] = {}
        dChannelUnits[unitNumber]['Area'] = unit['AreaTotal']
        dChannelUnits[unitNumber]['Tier1'] = unit['Tier1']

        # Loop over each metric.
        # See the metric definitions dict in each calling function.
        # Some metrics group several measurement classes into one metric
        for metricName, subClasses in metricDict.iteritems():
            dChannelUnits[unitNumber][metricName] = 0.0

            # Loop over the API measurements that are used by this metric
            for subClass in subClasses[0]:
                # Sum the proportions of the measurement for this class
                vals = [val[subClass] for val in apiValues if val['ChannelUnitID'] == unitID and val[subClass] != None]
                if len(vals) == 0:
                    dChannelUnits[unitNumber][metricName] = None
                else:
                    if dChannelUnits[unitNumber][metricName] is None:
                        dChannelUnits[unitNumber][metricName] = 0
                    dChannelUnits[unitNumber][metricName] += np.sum(vals)


    cuMetrics = {}
    for t1Type in dUnitDefs:
        # The Tier 1 types from the API need to be sanitized for the metric XML file

# Path: lib/channelunits.py
# def getCleanTierName(sTierName):
# def createChannelUnitXMLFile(visitID, workbenchDB, xmlFilePath):
# def createChannelUnitJSONFile(visitID, workbenchDB, jsonFilePath):
# def loadChannelUnitsFromSQLite(visitID, workbenchDB):
# def writeChannelUnitsToXML(visitID, dUnits, xmlFilePath):
# def loadChannelUnitsFromAPI(vid):
# def loadChannelUnitsFromJSON(jsonFilePath):
# def writeChannelUnitsToJSON(jsonFilePath, dUnits):
# def loadChannelUnitsFromXML(xmlFilePath):
# 
# Path: lib/channelunits.py
# def getCleanTierName(sTierName):
#     try:
#         return sTierName.replace(' ', '').replace('/', '').replace('-', '')
#     except Exception, e:
#         raise DataException("Invalid or null tiername passed to 'getCleanTierName()'")


# Path: tools/topoauxmetrics/methods/metricsbychannelunit.py
from lib.channelunits import dUnitDefs
from lib.channelunits import getCleanTierName
import numpy as np

    # Retrieve channel unit areas and tier 1 type from channel unit metrics
    dChannelUnits = {}
    for unit in channelUnitMetrics:
        unitNumber = int(unit['ChUnitNumber'])
        unitID = next(u['value']['ChannelUnitID'] for u in channelUnitMeasurements['values'] if u['value']['ChannelUnitNumber'] == unitNumber)
        dChannelUnits[unitNumber] = {}
        dChannelUnits[unitNumber]['Area'] = unit['AreaTotal']
        dChannelUnits[unitNumber]['Tier1'] = unit['Tier1']

        # Loop over each metric.
        # See the metric definitions dict in each calling function.
        # Some metrics group several measurement classes into one metric
        for metricName, subClasses in metricDict.iteritems():
            dChannelUnits[unitNumber][metricName] = 0.0

            # Loop over the API measurements that are used by this metric
            for subClass in subClasses[0]:
                # Sum the proportions of the measurement for this class
                vals = [val[subClass] for val in apiValues if val['ChannelUnitID'] == unitID and val[subClass] != None]
                if len(vals) == 0:
                    dChannelUnits[unitNumber][metricName] = None
                else:
                    if dChannelUnits[unitNumber][metricName] is None:
                        dChannelUnits[unitNumber][metricName] = 0
                    dChannelUnits[unitNumber][metricName] += np.sum(vals)


    cuMetrics = {}
    for t1Type in dUnitDefs:
        # The Tier 1 types from the API need to be sanitized for the metric XML file

        safet1Type = getCleanTierName(t1Type)