예제 #1
0
def test_read_csv():
    filename = './test_data/test_data1.csv'
    time, voltage = file_io.read_csv(filename)
    assert float(time[0]) == 0
    assert float(voltage[0]) == -0.145
    assert float(time[1]) == 0.003
    assert float(voltage[1]) == -0.145

    filename = './test_data/test_data2.csv'
    time, voltage = file_io.read_csv(filename)
    assert float(time[0]) == 0
    assert float(voltage[0]) == -0.345
    assert float(time[1]) == 0.003
    assert float(voltage[1]) == -0.345
예제 #2
0
def process(test_file, index, reverse, metrics):
    test_data = file_io.read_csv('data', test_file)
    header = file_io.get_csv_header('data', test_file)
    print "Test result of feature %s Reverse %s" % (header[index]  , str(reverse))

    for metric in metrics.split(','):
        methodToCall = getattr(sys.modules[__name__], metric.rstrip())    
        methodToCall(test_data, index, reverse, test_file)
예제 #3
0
def main():
    dir = './test_data/*.csv'
    filenames_list = file_io.collect_csv_filenames(dir)

    filename_ref = filenames_list[0]
    time, voltage_ref = file_io.read_csv(filename_ref)

    time_unit = input("Time is in min or sec (default = sec)? ")
    if time_unit != "sec" and time_unit != "min":
        print("Warning: Input was neither 'sec' or 'min'. Data saved as "
              "seconds.")

    for filename in filenames_list:
        time, voltage = file_io.read_csv(filename)
        hrm = process_ecg_data.HeartRateMonitor(time, voltage, voltage_ref,
                                                time_unit, filename)

        file_io.write_json(filename, hrm.output())

    print("Finished.")
def plot_multi_pressure(dir, filename, format):
    """ Gathers multiple filenames, extracts data, facilitates range selection
    and plots data sets onto a single axes

    :param dir: Directory containing the data files
    :param filename: List containing all filenames of data files
    :param format: File type for exported graphs
    :return:
    """

    temp = []
    new_pressure = []
    new_time = []

    for count, file in enumerate(filename):
        raw_time, pressure, units, start, end, min, max = file_io.read_csv(
            dir + file)

        start = normalize_single_time(start)
        end = normalize_single_time(end) - start

        time = normalize_time_list(raw_time, start)

        temp.append(plot_single_pressure(pressure, time, units))
        temp[count].show()

        try:
            new_pressure, new_time = \
                handle_inputs.handle_interval_selection(
                pressure,time,end,new_pressure,new_time)
        except TypeError:
            return

    save_dir = str("./Exported/" + datetime.now().strftime("%Y-%m-%d "
                                                           "%H-%M-%S"))
    os.mkdir(save_dir)

    if format != "":
        for count, i in enumerate(new_pressure):
            single = (plot_single_pressure(i, new_time[count], units))
            file_io.render_figure(single, [filename[count]],
                                  save_dir + "/" + filename[count], format,
                                  False)
            single.clf()

    for count, i in enumerate(new_pressure):
        graph = plot_single_pressure(i, new_time[count], units, int(count))
        prepare_output_data(filename[count], save_dir, i, new_time[count])

    export_filename = save_dir + "/" + "combined_plots"

    file_io.render_figure(graph, filename, export_filename, format)

    return
def handle_single(dir):
    """ Handles case of when only one file is to be analyzed, executing
    functions for data range selection, plotting and export

    :param dir: Directory of where the data file is located
    :return:
    """

    filename = input("Filename? ")

    try:
        raw_time, pressure, units, start, end, min, max = file_io.read_csv(
            dir + filename)
    except FileNotFoundError:
        print("Error: File not found in specified directory.")
        return

    start = pressure_processor.normalize_single_time(start)
    end = pressure_processor.normalize_single_time(end) - start

    time = pressure_processor.normalize_time_list(raw_time, start)

    temp = pressure_processor.plot_single_pressure(pressure, time, units)
    temp.show()

    try:
        pressure, time = handle_interval_selection(pressure, time, end)
    except TypeError:
        return

    format = input("Export filetype ('pdf' or 'png'; other input for view "
                   "only) ")

    save_dir = str("./Exported/" + datetime.now().strftime("%Y-%m-%d "
                                                           "%H-%M-%S"))
    os.mkdir(save_dir)

    export_filename = save_dir + '/' + filename + ' ' + '.' + format

    graph = pressure_processor.plot_single_pressure(pressure[0], time[0],
                                                    units)

    file_io.render_figure(graph, [filename], export_filename, format)
    pressure_processor.prepare_output_data(filename, save_dir, pressure[0],
                                           time[0])

    return
예제 #6
0
def _get_row_description(file_name: str):
    """ Generator providing description for each row

    :param file_name: complete path of file containing the description of the
    contents of a flat file
    :returns: a RowDescription object containing row description
    """

    start_index = 0
    for field_name, char_length, data_type in read_csv(file_name):

        if data_type not in DataTypes.__members__:
            raise DataDescriptorParseError(f"Unexpected datatype {data_type}")

        char_length = int(char_length)
        yield RowDescription(field_name, start_index,
                             start_index + char_length, DataTypes[data_type])
        start_index += char_length
예제 #7
0
        params['n_log_n_fit']          = bool(args.n_log_n_fit)

    if args.linear_fit == 'True':
        params['linear_fit']           = bool(args.linear_fit)

    if args.logarithmic_fit == 'True':
        params['logarithmic_fit']      = bool(args.logarithmic_fit)

    if args.upper_bound == 'True':
        params['upper_bound']          = bool(args.upper_bound)

    if args.lower_bound == 'True':
        params['lower_bound']          = bool(args.lower_bound)

    # Read the x and y components from the .csv file
    x_data, y_data = read_csv(params['csv_directory'] + params['input_file_name'] + '.csv')

    # Plot the data with the updated configuration parameters
    plot_data(output_file_path     = params['png_directory'] + params['input_file_name'] + '.png',
              plot_title           = params['plot_title'],
              input_data_label     = params['input_data_label'],
              x_label              = params['x_axis_label'],
              y_label              = params['y_axis_label'],
              x_data               = x_data,
              y_data               = y_data,
              dependent_variable   = params['dependent_variable'],
              independent_variable = params['independent_variable'],
              plot_original_data   = params['original_data'],
              desired_fits         = {
                  "exponential": params['exponential_fit'],
                  "cubic":       params['cubic_fit'],
예제 #8
0
def main():

    test_1 = process_ecg_data.HeartRateMonitor()

    # Testing is_float
    output_1 = test_1.is_float("abc")
    assert output_1 is False

    output_2 = test_1.is_float(4)
    assert output_2 is True

    # Testing process_input

    with pytest.raises(ValueError):
        test_1.voltage = [
            0.1, -0.2, math.sqrt(2),
            math.sqrt(-5), -(math.sqrt(8))
        ]
        test_1.process_input()

    with pytest.raises(ValueError):
        test_1.time = [6, 7, math.sqrt(-2)]
        test_1.process_input()

    test_1.voltage = []
    test_1.time = []
    test_1.time_input = ["6", 7, 8, 9]
    test_1.voltage_input = [5, 7, 3, 4]
    output_3 = test_1.process_input()
    assert output_3 is True

    with pytest.raises(TypeError):
        test_1.time_input = tuple("abc", 5, 7, 1)
        test_1.process_input()

    with pytest.raises(TypeError):
        test_1.voltage_input = tuple("g", 5.2, 8.7, 1.0)
        test_1.process_input()

    test_1.time_input = ["6", 7, "bad data", 9]
    output_4 = test_1.process_input()
    assert output_4 is True

    test_1.voltage = []
    test_1.time = []
    test_1.voltage_input = [2.3, 5.2, 8.7, 1.0]
    test_1.time_input = [6.7, 2.6, 2.3]
    assert test_1.process_input() is False

    # Testing mean_hr_bpm

    test_1.voltage = []
    test_1.time = []
    test_1.time_input = [0, 10, 20, 30, 40, 50, 60, 70, 80]
    test_1.voltage_input = [2, 3, 4, 5, 6, 7, 8, 9, 10]
    test_1.process_input()
    output_5 = test_1.mean_hr_bpm(130)
    assert output_5 == 97.5

    # Testing voltage_extremes

    test_1.voltage = []
    test_1.time = []
    test_1.voltage_input = [-5.0, -2.0, 0.0, 1.0, 7.0]
    test_1.time_input = [1, 2, 3, 4, 5]
    test_1.process_input()
    output_6 = test_1.voltage_extremes()
    assert output_6[0] == -5.0
    assert output_6[1] == 7.0

    # Testing duration

    test_1.time = []
    test_1.time_input = [0, 10, 20, 30, 40, 50, 60, 70, 80]
    test_1.process_input()
    output_7 = test_1.duration()
    assert output_7 == 80

    # Testing num_beats

    dir = "./test_data/test_data1.csv"
    time, voltage_ref = file_io.read_csv("./test_data/test_data1.csv")

    time, voltage = file_io.read_csv("./test_data/test_data1.csv")
    test_2 = process_ecg_data.HeartRateMonitor(time, voltage, voltage_ref,
                                               "sec",
                                               "./test_data/test_data1.csv")
    output_8 = test_2.num_beats()
    assert output_8 == 37

    # Testing beats

    output_9 = test_2.beats()
    assert round(output_9[0], 5) == [1.91919]
    assert round(output_9[1], 5) == [7.27613]
예제 #9
0
파일: test_file_io.py 프로젝트: vighy/fffc
    def test_read_csv_content(self):
        """ test for reading contents of a csv """

        for line in read_csv(self.temp_csv_path):
            self.assertEqual(line, self.expected_csv_content)