def update_accuracy_in_paramters_on_end_then_save_to_json(parameters, history):

    # append function name to the call sequence
    calling_sequence.append("[update_paramters]==>>")

    print(" ==============================================")
    print(" [INFO] Entering function[update_paramters]  in core.py")
    # save parameters in result sheet

    # get max validation accuracy and the train accuracy according to it.
    max_validation_acc = np.max(history.history['val_acc'])
    index = history.history['val_acc'].index(np.max(
        history.history['val_acc']))
    train_acc_according_to_max_validation_acc = history.history['acc'][index]

    parameters['train_acc_according_to_max_validation_acc'] = round(
        train_acc_according_to_max_validation_acc, 4)
    parameters['max_validation_acc'] = round(max_validation_acc, 4)
    parameters['best_accuracy_epoch'] = index + 1

    parameters['overall_train_accuracy'] = round(history.history['acc'][-1], 4)
    parameters['overall_validation_accuracy'] = round(
        history.history['val_acc'][-1], 4)

    #     print(parameters)
    write_json_parameters(parameters)

    print(" [INFO] Leaving function[update_paramters]  in core.py")
    print(" ==============================================")
示例#2
0
def save_train_result(history, parameters, initiate_new_result_sheet=True):

    # append function name to the call sequence
    calling_sequence.append("[save_train_result]==>>")

    print(" ==============================================")
    print(
        " [INFO] Entering function[save_train_result]  in core.py INTENT TO CREATE NEW RESULT SHEET = ",
        initiate_new_result_sheet)
    # save parameters in result sheet

    # get max validation accuracy and the train accuracy according to it.
    max_validation_acc = np.max(history.history['val_acc'])
    index = history.history['val_acc'].index(np.max(
        history.history['val_acc']))
    train_acc_according_to_max_validation_acc = history.history['acc'][index]

    parameters['train_acc_according_to_max_validation_acc'] = round(
        train_acc_according_to_max_validation_acc, 4)
    parameters['max_validation_acc'] = round(max_validation_acc, 4)
    parameters['best_accuracy_epoch'] = index + 1

    parameters['overall_train_accuracy'] = round(
        history.history['acc'][len(history.history['acc']) - 1], 4)
    parameters['overall_validation_accuracy'] = round(
        history.history['val_acc'][len(history.history['val_acc']) - 1], 4)

    # get the paramters format that will be written in csv file
    result_sheet = get_result_parameters(parameters)
    # convert python dict to pandas data frame
    result_sheet = pd.DataFrame([result_sheet])

    # CHECK IF CREATING NEW .CSV FILE DESIRED OR APPEND TO CURENT ONE
    if (initiate_new_result_sheet):
        result_sheet.to_csv(parameters['result_sheet'], index=False)
    else:
        previous_result_sheet = pd.read_csv(parameters['result_sheet'])
        result_sheet = pd.concat([previous_result_sheet, result_sheet],
                                 sort=False,
                                 ignore_index=True)
        result_sheet.to_csv(parameters['result_sheet'], index=False)

    print('===============================================')
    print(" [INFO] save result at " + str(parameters['model_save_path']))
    print('===================SAVED=======================')
    print(" [INFO] Leaving function[save_train_result]  in core.py")
    print(" ==============================================")
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')
unique, counts = np.unique(past_ref1, return_counts=True)
counts_dict = dict(zip(unique, counts))
print('=' * 50)
print(counts_dict)
past_ref = past_ref1[:1700, :1440]

#  Creation of buffer
示例#4
0
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