# Path: ifcb/data/adc.py
# class SCHEMA_VERSION_1(object):
#     """
#     IFCB revision 1 schema.
#     """
#     _name = 'v1'
#     _cols = range(15)
#     TRIGGER = 0
#     PROCESSING_END_TIME = 1
#     FLUORESCENCE_LOW = 2
#     FLUORESCENCE_HIGH = 3
#     SCATTERING_LOW = 4
#     SCATTERING_HIGH = 5
#     COMPARATOR_PULSE = 6
#     TRIGGER_OPEN_TIME = 7
#     FRAME_GRAB_TIME = 8
#     ROI_X = 9
#     ROI_Y = 10
#     ROI_WIDTH = 11
#     ROI_HEIGHT = 12
#     START_BYTE = 13
#     VALVE_STATUS = 14
# 
# class SCHEMA_VERSION_2(object):
#     """
#     IFCB revision 2 schema.
#     """
#     _name = 'v2'
#     _cols = range(24)
#     TRIGGER = 0
#     ADC_TIME = 1
#     PMT_A = 2
#     PMT_B = 3
#     PMT_C = 4
#     PMT_D = 5
#     PEAK_A = 6
#     PEAK_B = 7
#     PEAK_C = 8
#     PEAK_D = 9
#     TIME_OF_FLIGHT = 10
#     GRAB_TIME_START = 11
#     GRAB_TIME_END = 12
#     ROI_X = 13
#     ROI_Y = 14
#     ROI_WIDTH = 15
#     ROI_HEIGHT = 16
#     START_BYTE = 17
#     COMPARATOR_OUT = 18
#     START_POINT = 19
#     SIGNAL_LENGTH = 20
#     STATUS = 21
#     RUN_TIME = 22
#     INHIBIT_TIME = 23


import numpy as np
import pandas as pd
import scipy.stats as stats
from ifcb.data.adc import SCHEMA_VERSION_1, SCHEMA_VERSION_2
    from scipy.io import loadmat



def read_ml_analyzed(path):
    """read from the legacy matlab files"""
    mat = loadmat(path, squeeze_me=True)
    # ignore variables other than the following
    cols = ['filelist_all', 'looktime', 'minproctime', 'ml_analyzed', 'runtime']
    # convert to dataframe
    df = pd.DataFrame({ c: mat[c] for c in cols }, columns=cols)
    df.index = df.pop('filelist_all') # index by bin LID
    return df

def compute_ml_analyzed_s1_adc(adc):
    """compute ml_analyzed for an old instrument"""
    # first, make sure this isn't an empty bin
    if len(adc) == 0:
        return np.nan, np.nan, np.nan
    # we have targets, can proceed
    MIN_PROC_TIME = 0.073
    STEPS_PER_SEC = 40.
    ML_PER_STEP = 5./48000.
    FLOW_RATE = ML_PER_STEP * STEPS_PER_SEC # ml/s

# Path: ifcb/data/adc.py
# class SCHEMA_VERSION_1(object):
#     """
#     IFCB revision 1 schema.
#     """
#     _name = 'v1'
#     _cols = range(15)
#     TRIGGER = 0
#     PROCESSING_END_TIME = 1
#     FLUORESCENCE_LOW = 2
#     FLUORESCENCE_HIGH = 3
#     SCATTERING_LOW = 4
#     SCATTERING_HIGH = 5
#     COMPARATOR_PULSE = 6
#     TRIGGER_OPEN_TIME = 7
#     FRAME_GRAB_TIME = 8
#     ROI_X = 9
#     ROI_Y = 10
#     ROI_WIDTH = 11
#     ROI_HEIGHT = 12
#     START_BYTE = 13
#     VALVE_STATUS = 14
# 
# class SCHEMA_VERSION_2(object):
#     """
#     IFCB revision 2 schema.
#     """
#     _name = 'v2'
#     _cols = range(24)
#     TRIGGER = 0
#     ADC_TIME = 1
#     PMT_A = 2
#     PMT_B = 3
#     PMT_C = 4
#     PMT_D = 5
#     PEAK_A = 6
#     PEAK_B = 7
#     PEAK_C = 8
#     PEAK_D = 9
#     TIME_OF_FLIGHT = 10
#     GRAB_TIME_START = 11
#     GRAB_TIME_END = 12
#     ROI_X = 13
#     ROI_Y = 14
#     ROI_WIDTH = 15
#     ROI_HEIGHT = 16
#     START_BYTE = 17
#     COMPARATOR_OUT = 18
#     START_POINT = 19
#     SIGNAL_LENGTH = 20
#     STATUS = 21
#     RUN_TIME = 22
#     INHIBIT_TIME = 23


# Path: ifcb/metrics/ml_analyzed.py
import numpy as np
import pandas as pd
import scipy.stats as stats
from ifcb.data.adc import SCHEMA_VERSION_1, SCHEMA_VERSION_2
    from scipy.io import loadmat




def read_ml_analyzed(path):
    """read from the legacy matlab files"""
    mat = loadmat(path, squeeze_me=True)
    # ignore variables other than the following
    cols = ['filelist_all', 'looktime', 'minproctime', 'ml_analyzed', 'runtime']
    # convert to dataframe
    df = pd.DataFrame({ c: mat[c] for c in cols }, columns=cols)
    df.index = df.pop('filelist_all') # index by bin LID
    return df

def compute_ml_analyzed_s1_adc(adc):
    """compute ml_analyzed for an old instrument"""
    # first, make sure this isn't an empty bin
    if len(adc) == 0:
        return np.nan, np.nan, np.nan
    # we have targets, can proceed
    MIN_PROC_TIME = 0.073
    STEPS_PER_SEC = 40.
    ML_PER_STEP = 5./48000.
    FLOW_RATE = ML_PER_STEP * STEPS_PER_SEC # ml/s

    s = SCHEMA_VERSION_1