# 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
    if ml_analyzed <= 0 or abs(last_row[s.RUN_TIME] - last_row[s.ADC_TIME]) >= 0.3:
        row = adc.iloc[-2]
        ml_analyzed, look_time, run_time = ma(row)
    if ml_analyzed <= 0:
        row = adc.iloc[-2]
        run_time = row[s.ADC_TIME]
        nz = adc[s.RUN_TIME].to_numpy().nonzero()[0]
        mode_inhibit_time = stats.mode(np.diff(adc[s.INHIBIT_TIME].iloc[nz]))[0][0]
        last_good_inhibit_time = adc[s.INHIBIT_TIME].iloc[nz[-1]]
        inhibit_time = last_good_inhibit_time + (len(adc) - len(nz)) * mode_inhibit_time
        look_time = run_time - inhibit_time
        ml_analyzed = FLOW_RATE * (look_time / 60)
    return ml_analyzed, look_time, run_time

def compute_ml_analyzed_s2(abin):
    """compute ml_analyzed for a new instrument"""
    FLOW_RATE = 0.25 # ml/minute
    # ml analyzed is (run time - inhibit time) * flow rate
    run_time = abin.header('runTime')
    inhibit_time = abin.header('inhibitTime')
    look_time = run_time - inhibit_time
    ml_analyzed = FLOW_RATE * (look_time / 60.)
    if look_time > 0:
        return ml_analyzed, look_time, run_time
    else:
        return compute_ml_analyzed_s2_adc(abin)

def compute_ml_analyzed(abin):
    """returns ml_analyzed, look time, run time"""
    s = abin.schema

# 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

    if ml_analyzed <= 0 or abs(last_row[s.RUN_TIME] - last_row[s.ADC_TIME]) >= 0.3:
        row = adc.iloc[-2]
        ml_analyzed, look_time, run_time = ma(row)
    if ml_analyzed <= 0:
        row = adc.iloc[-2]
        run_time = row[s.ADC_TIME]
        nz = adc[s.RUN_TIME].to_numpy().nonzero()[0]
        mode_inhibit_time = stats.mode(np.diff(adc[s.INHIBIT_TIME].iloc[nz]))[0][0]
        last_good_inhibit_time = adc[s.INHIBIT_TIME].iloc[nz[-1]]
        inhibit_time = last_good_inhibit_time + (len(adc) - len(nz)) * mode_inhibit_time
        look_time = run_time - inhibit_time
        ml_analyzed = FLOW_RATE * (look_time / 60)
    return ml_analyzed, look_time, run_time

def compute_ml_analyzed_s2(abin):
    """compute ml_analyzed for a new instrument"""
    FLOW_RATE = 0.25 # ml/minute
    # ml analyzed is (run time - inhibit time) * flow rate
    run_time = abin.header('runTime')
    inhibit_time = abin.header('inhibitTime')
    look_time = run_time - inhibit_time
    ml_analyzed = FLOW_RATE * (look_time / 60.)
    if look_time > 0:
        return ml_analyzed, look_time, run_time
    else:
        return compute_ml_analyzed_s2_adc(abin)

def compute_ml_analyzed(abin):
    """returns ml_analyzed, look time, run time"""
    s = abin.schema

    if s is SCHEMA_VERSION_1: