def generate_cycle(length,*args,peep=None):
    
    startpos  = np.random.randint(0,len(args[0])) 
    delta     = (length - startpos)
    nzeroes   = delta//len(args[0])+1
    
    zpos      = np.random.randint(2,delta//10,size=nzeroes)
    
    res       = [None]*len(args)

    vrandom   = [np.random.uniform(0.95,1.05) if np.random.random() > 0.01 else np.random.uniform(0.1,0.2) for i in range(nzeroes+1)]

    for i in range(len(args)):
        rarr = args[i][-startpos:]
        # mode = stats.mode(args[i])[0]
        mode = peep if (not peep is None) and (i==len(args)-1) else 0
        for j in range(nzeroes):
            aux = vrandom[j+1]*args[i] + (1-vrandom[j+1])*mode
            # aux = 0.0
            # if vrandom[j+1] < 0.9: aux = np.array([ vrandom[j+1]*value if value > mode/vrandom[j+1] else value for value in args[i]])
            # else: aux = np.array([vrandom[j+1]*value if value > mode/vrandom[j+1] else value for value in args[i]])
            rarr = np.concatenate((rarr,np.ones(zpos[j])*mode,aux))

        res[i] = rarr[:length]
    
    return res
def pmus_ingmar(fs, rr, pp, tp, tf):
    """
    Sinusoidal profile
    :param fs: sample frequency
    :param rr: respiratory rate
    :param pp: peak pressure
    :param tp: peak time
    :param tf: end of effort
    :return: pmus profile
    """

    ntp = np.floor(tp * fs)
    ntf = np.floor(tf * fs)
    ntN = np.floor(60.0 * fs / rr)

    pmus1 = np.sin(np.pi * np.arange(0, ntp + 1, 1) / fs / 2.0 / tp)
    pmus2 = np.sin(np.pi / 2.0 / (tf - tp) *
                   (np.arange(ntp + 1, ntf + 1, 1) / fs + tf - 2.0 * tp))
    pmus3 = 0 * np.arange(ntf + 1, ntN + 1, 1) / fs
    pmus = pp * np.concatenate((pmus1, pmus2, pmus3))

    return pmus
def pmus_parexp(fs, rr, pp, tp, tf):
    """
    Parabolic-exponential profile
    :param fs: sample frequency
    :param rr: respiratory rate
    :param pp: peak pressure
    :param tp: peak time
    :param tf: end of effort
    :return: pmus profile
    """

    ntp = np.floor(tp * fs)
    ntN = np.floor(60.0 / rr * fs)
    taur = abs(tf - tp) / 4.0

    pmus1 = pp * (60.0 * rr - np.arange(0, ntp + 1, 1) / fs) * (
        np.arange(0, ntp + 1, 1) / fs) / (tp * (60.0 * rr - tp))
    pmus2 = pp * (np.exp(-(np.arange(ntp + 1, ntN + 1, 1) / fs - tp) / taur) -
                  np.exp(-(60.0 * rr - tp) / taur)) / (
                      1.0 - np.exp(-(60.0 * rr - tp) / taur))
    pmus = np.concatenate((pmus1, pmus2))

    return pmus
RGB_image, extract_patches, patch_tiles, bal_aug_patches, extrac_patch2, test_FCN, pred_recostruction, \
weighted_categorical_crossentropy, mask_no_considered, tf, Adam, prediction, load_model, confusion_matrix, \
EarlyStopping, ModelCheckpoint, identity_block, ResNet50, color_map

root_path = './DATASETS/'

# Load images
img_t1 = load_tiff_image(root_path + 'images/18_08_2017_image' +
                         '.tif').astype(np.float32)
img_t1 = img_t1.transpose((1, 2, 0))
img_t2 = load_tiff_image(root_path + 'images/21_08_2018_image' +
                         '.tif').astype(np.float32)
img_t2 = img_t2.transpose((1, 2, 0))

# Concatenation of images
image_array1 = np.concatenate((img_t1, img_t2), axis=-1).astype(np.float32)
h_, w_, channels = image_array1.shape
print(image_array1.shape)

# Normalization
type_norm = 1
image_array = normalization(image_array1, type_norm)
print(np.min(image_array), np.max(image_array))

# Load reference
image_ref1 = load_tiff_image(root_path + 'images/REFERENCE_2018_EPSG4674' +
                             '.tif')
image_ref = image_ref1[:1700, :1440]

past_ref1 = load_tiff_image(root_path +
                            'images/PAST_REFERENCE_FOR_2018_EPSG4674' + '.tif')
def solve_model(header_params,params,header_features,features,debugmsg):
    #Extracts each parameter
    fs = params[header_params.index('Fs')]
    rvent = params[header_params.index('Rvent')]
    c = params[header_params.index('C')]
    rins = params[header_params.index('Rins')]
    rexp = rins  # params[4]
    peep = params[header_params.index('PEEP')]
    sp = params[header_params.index('SP')]
    trigger_type = features[header_features.index('Triggertype')]
    trigger_arg = params[header_params.index('Triggerarg')]
    rise_type = features[header_features.index('Risetype')]
    rise_time = params[header_params.index('Risetime')]
    cycle_off = params[header_params.index('Cycleoff')]
    rr = params[header_params.index('RR')]
    pmus_type = features[header_features.index('Pmustype')]
    pp = params[header_params.index('Pp')]
    tp = params[header_params.index('Tp')]
    tf = params[header_params.index('Tf')]
    noise = params[header_params.index('Noise')]
    e2 = params[header_params.index('E2')]
    model = features[header_features.index('Model')]

    expected_len = int(np.floor(180.0 / np.min(RR) * np.max(Fs)) + 1)
    
    #Assings pmus profile
    pmus = pmus_profile(fs, rr, pmus_type, pp, tp, tf)
    pmus = pmus + peep #adjusts PEEP
    pmus = np.concatenate((np.array([0]), pmus)) #sets the first value to zero

    
    #Unit conversion from cmH2O.s/L to cmH2O.s/mL
    rins = rins / 1000.0
    rexp = rexp / 1000.0
    rvent = rvent / 1000.0


    #Generates time, flow, volume, insex and paw waveforms
    time = np.arange(0, np.floor(60.0 / rr * fs) + 1, 1) / fs
    time = np.concatenate((np.array([0]), time))
    flow = np.zeros(len(time))
    volume = np.zeros(len(time))
    insex = np.zeros(len(time))
    paw = np.zeros(len(time)) + peep #adjusts PEEP
    len_time = len(time)

    #Peak flow detection
    peak_flow = flow[0]
    detect_peak_flow = False

    #Support detection
    detect_support = False
    time_support = -1

    #Expiration detection
    detect_exp = False
    time_exp = -1

    if trigger_type == 'flow':
        # units conversion from L/min to mL/s
        trigger_arg = trigger_arg / 60.0 * 1000.0

    for i in range(1, len(time)):
        # period until the respiratory effort beginning
        if (((trigger_type == 'flow' and flow[i] < trigger_arg) or
             (trigger_type == 'pressure' and paw[i] > trigger_arg + peep) or
             (trigger_type == 'delay' and time[i] < trigger_arg)) and
                (not detect_support) and (not detect_exp)):
            paw[i] = peep
            y0 = volume[i - 1]
            tspan = [time[i - 1], time[i]]
            args = (paw[i], pmus[i], model, c, e2, rins)
            sol = odeint(flow_model, y0, tspan, args=args)
            volume[i] = sol[-1]
            flow[i] = flow_model(volume[i], time[i], paw[i], pmus[i], model, c, e2, rins)
            if debugmsg:
                print('volume[i]= {:.2f}, flow[i]= {:.2f}, paw[i]= {:.2f}, waiting'.format(volume[i], flow[i], paw[i]))

            if (((trigger_type == 'flow' and flow[i] >= trigger_arg) or
                 (trigger_type == 'pressure' and paw[i] <= trigger_arg + peep) or
                 (trigger_type == 'delay' and time[i] >= trigger_arg))):
                detect_support = True
                time_support = time[i+1]
                continue

        # detection of inspiratory effort
        # ventilator starts to support the patient
        elif (detect_support and (not detect_exp)):
            if rise_type == 'step':
                paw[i] = sp + peep
            elif rise_type == 'exp':
                rise_type = rise_type if np.random.random() > 0.01 else 'linear'
                if paw[i] < sp + peep:
                    paw[i] = (1.0 - np.exp(-(time[i] - time_support) / rise_time )) * sp + peep
                if paw[i] >= sp + peep:
                    paw[i] = sp + peep
            elif rise_type == 'linear':
                rise_type = rise_type if np.random.random() > 0.01 else 'exp'
                if paw[i] < sp + peep:
                    paw[i] = (time[i] - time_support) / rise_time * sp + peep
                if paw[i] >= sp + peep:
                    paw[i] = sp + peep

            y0 = volume[i - 1]
            tspan = [time[i - 1], time[i]]
            args = (paw[i], pmus[i], model, c, e2, rins)
            sol = odeint(flow_model, y0, tspan, args=args)
            volume[i] = sol[-1]
            flow[i] = flow_model(volume[i], time[i], paw[i], pmus[i], model, c, e2, rins)
            if debugmsg:
                print('volume[i]= {:.2f}, flow[i]= {:.2f}, paw[i]= {:.2f}, supporting'.format(volume[i], flow[i], paw[i]))

            if flow[i] >= flow[i - 1]:
                peak_flow = flow[i]
                detect_peak_flow = False
            elif flow[i] < flow[i - 1]:
                detect_peak_flow = True

            if (flow[i] <= cycle_off * peak_flow) and detect_peak_flow and i<len_time:
                detect_exp = True
                time_exp = i+1    
                try:
                    paw[i + 1] = paw[i]
                except IndexError:
                    pass

        elif detect_exp:
            if rise_type == 'step':
                paw[i] = peep
            elif rise_type == 'exp':
                if paw[i - 1] > peep:
                    paw[i] = sp * (np.exp(-(time[i] - time[time_exp-1]) / rise_time )) + peep
                if paw[i - 1] <= peep:
                    paw[i] = peep
            elif rise_type == 'linear':
                rise_type = rise_type if np.random.random() > 0.01 else 'exp'
                if paw[i - 1] > peep:
                    paw[i] = sp * (1 - (time[i] - time[time_exp-1]) / rise_time) + peep
                if paw[i - 1] <= peep:
                    paw[i] = peep

            y0 = volume[i - 1]
            tspan = [time[i - 1], time[i]]
            args = (paw[i], pmus[i], model, c, e2, rexp + rvent)
            sol = odeint(flow_model, y0, tspan, args=args)
            volume[i] = sol[-1]
            flow[i] = flow_model(volume[i], time[i], paw[i], pmus[i], model, c, e2, rexp + rvent)
            if debugmsg:
                print('volume[i]= {:.2f}, flow[i]= {:.2f}, paw[i]= {:.2f}, exhaling'.format(volume[i], flow[i], paw[i]))

    #Generates InsEx trace
    if time_exp > -1:
        insex = np.concatenate((np.ones(time_exp), np.zeros(len(time) - time_exp)))

    #Drops the first element
    flow = flow[1:] / 1000.0 * 60.0  # converts back to L/min
    volume = volume[1:]
    paw = paw[1:]
    pmus = pmus[1:] - peep #reajust peep again
    insex = insex[1:]

    flow,volume,pmus,insex,paw = generate_cycle(expected_len,flow,volume,pmus,insex,paw,peep=peep)

    # paw = generate_cycle(expected_len,paw,peep=peep)[0]
    
    flow,volume,paw,pmus,insex = generate_noise(noise,flow,volume,paw,pmus,insex)

    # plt.plot(flow)
    # plt.plot(volume)
    # plt.plot(paw)
    # plt.plot(pmus)
    # plt.show()

    return flow, volume, paw, pmus, insex, rins,rexp, c
parser.add_argument("--dataset",
    help="dataset path", type=str, default='dataset')
args = parser.parse_args()

root_path = args.dataset
img_t1_path = 'clipped_raster_004_66_2018.tif'
img_t2_path = 'clipped_raster_004_66_2019.tif'

# Load images
img_t1 = load_tiff_image(os.path.join(root_path,img_t1_path)).astype(np.float32)
img_t1 = img_t1.transpose((1,2,0))
img_t2 = load_tiff_image(os.path.join(root_path,img_t2_path)).astype(np.float32)
img_t2 = img_t2.transpose((1,2,0))

# Concatenation of images
image_array1 = np.concatenate((img_t1, img_t2), axis = -1).astype(np.float32)
image_array1 = image_array1[:6100,:6600]
h_, w_, channels = image_array1.shape
print(f"Input image shape: {image_array1.shape}")

# Normalization
type_norm = 1
image_array = normalization(image_array1, type_norm)
print(np.min(image_array), np.max(image_array))

# Load Mask area
img_mask_ref_path = 'mask_ref.tif'
img_mask_ref = load_tiff_image(os.path.join(root_path, img_mask_ref_path))
img_mask_ref = img_mask_ref[:6100,:6600]
print(f"Mask area reference shape: {img_mask_ref.shape}")
num_examples = flow.shape[0]
num_samples = flow.shape[1]

(min_flow, max_flow, flow) = normalize_data(flow)
(min_volume, max_volume, volume) = normalize_data(volume)
(min_paw, max_paw, paw) = normalize_data(paw)
(min_resistance, max_resistance, resistances) = normalize_data(resistances)
(min_capacitance, max_capacitance, capacitances) = normalize_data(capacitances)

print("normalized data")

input_data = np.zeros((num_examples, num_samples, 3))
input_data[:, :, 0] = flow
input_data[:, :, 1] = volume
input_data[:, :, 2] = paw
output_data = np.concatenate((resistances, capacitances), axis=1)
indices = np.arange(num_examples)

print("input created")


input_train, input_test, output_train, output_test, indices_train, indices_test = \
    train_test_split(input_data, output_data, indices, test_size=0.3, shuffle=False)

input_validation, input_test, output_validation, output_test, indices_validation, indices_test = \
    train_test_split(input_test, output_test, indices_test, test_size=0.5, shuffle=False)

np.save('./data/input_test.npy', input_test)
np.save('./data/output_test.npy', output_test)

print("before CNN")