def test_roundtime(self):
        """
        A testing function for rounTimeArray
        """
        obj_test1 = data_import.ImportData('./smallData/cgm_small.csv')
        output = data_import.roundTimeArray(obj_test1, 30, linear=True)
        for (time, val) in output:
            self.assertEqual(time.minute, 0)
            self.assertEqual(val, (131 + 138 + 144) / 3)
            break

        output = data_import.roundTimeArray(obj_test1, 30, linear=False)
        for (time, val) in output:
            self.assertEqual(time.minute, 0)
            self.assertEqual(val, (131 + 138 + 144) / 3)
            break

        obj_test2 = data_import.ImportData('./smallData/activity_small.csv')
        output = data_import.roundTimeArray(obj_test2, 40, linear=True)
        for (time, val) in output:
            self.assertEqual(time.minute, 0)
            self.assertEqual(val, (3 + 7 + 76 + 6 + 7))
            break

        output = data_import.roundTimeArray(obj_test2, 40, linear=False)
        for (time, val) in output:
            self.assertEqual(time.minute, 0)
            self.assertEqual(val, (3 + 7 + 76 + 6 + 7))
            break
コード例 #2
0
 def test_printarray_bolus_cgm(self):
     bolus_data = data_import.ImportData('smallData/bolus_small.csv')
     cgm_data = data_import.ImportData('smallData/cgm_small.csv')
     cgm_zip = data_import.roundTimeArray(cgm_data, 60, 'average')
     bolus_zip = data_import.roundTimeArray(bolus_data, 60, 'sum')
     data_list = [cgm_zip, bolus_zip]
     ann_list = ['cgm_small', 'bolus_small']
     base_name = 'test_printarray.csv'
     key_file = 'cgm_small'
     data_import.printArray(data_list, ann_list, base_name, key_file)
     assert os.path.exists(base_name)
     with open(base_name, 'r') as f:
         assert 'time,cgm,bolus' in f.readline()
         assert '2018-03-16 00:00:00,144.5,0.7,' in f.readline()
コード例 #3
0
 def test_roundtimearray_test_file_sum(self):
     csv_reader = data_import.ImportData('test_timeround.csv')
     zip_obj = data_import.roundTimeArray(csv_reader, 60, 'sum')
     for time_round, value_sum in zip_obj:
         assert value_sum == 435.0
         assert time_round == data_import.datetime.datetime(
             2018, 3, 16, 0, 0)
コード例 #4
0
 def test_roundtimearray_test_file_average(self):
     csv_reader = data_import.ImportData('test_timeround.csv')
     zip_obj = data_import.roundTimeArray(csv_reader, 60, 'average')
     for time_round, value_avg in zip_obj:
         assert value_avg == 145.0
         assert time_round == data_import.datetime.datetime(
             2018, 3, 16, 0, 0)
コード例 #5
0
 def test_linear_search_double_index(self):
     # To make sure it's handling double digits okay, I choese
     # to return a double digit entry
     r = data_import.ImportData('./smallData/activity_small.csv')
     data_import.roundTimeArray(r, 1)
     w = r.linear_search_value(r._time[14])
     self.assertEqual([r._value[14]], w)
コード例 #6
0
 def test_linear_search_roundtime(self):
     # Linear search of the rounded table should return 2 entries for the first one
     r = data_import.ImportData('./smallData/activity_small.csv')
     data_import.roundTimeArray(r, 3)
     w = r.linear_search_value(r._time[0])
     # For activity_small, should return a sum of 0
     self.assertEqual(w, [0.0])
コード例 #7
0
    def test_import_data_timereader(self):
        # We should be able to read the time in the proper
        # format from the file activity_small.csv
        # To test this, we will observe a known dateutil parser

        r = data_import.ImportData('./smallData/activity_small.csv')
        self.assertEqual((r._time[0]), dateutil.parser.parse('3/12/18 0:00'))
 def test_linear_search_double(self):
     with open('test_file.csv', "w") as file_handle:
         file_writer = csv.writer(file_handle,
                                  delimiter=',',
                                  quotechar='"',
                                  quoting=csv.QUOTE_MINIMAL)
         file_writer.writerow(['Id', 'time', 'value', 'no useful', 'info'])
         Id = 0
         for i in range(rdm.randint(1, 20)):
             time = str(random_date())
             Id += i
             value = rdm.randint(100, 1000)
             file_writer.writerow([Id, time, value])
         time = str(datetime.datetime(1800, 10, 20, 22, 38))
         Id += 1
         value = rdm.randint(100, 1000)
         marked_time = time
         marked_value_1 = value
         file_writer.writerow([Id, time, value])
         for i in range(rdm.randint(1, 20)):
             time = random_date()
             Id += i
             value = rdm.randint(100, 1000)
             file_writer.writerow([Id, time, value])
         time = str(datetime.datetime(1800, 10, 20, 22, 38))
         Id += 1
         value = rdm.randint(100, 1000)
         marked_value_2 = value
         file_writer.writerow([Id, time, value])
         file_handle.close()
         imported_data = d_i.ImportData('test_file.csv')
         self.assertEqual(imported_data.linear_search_value(marked_time),
                          [marked_value_1, marked_value_2])
     os.remove('test_file.csv')
 def test_entry_empty(self):
     with open('test_file.csv', "w") as file_handle:
         file_writer = csv.writer(file_handle,
                                  delimiter=',',
                                  quotechar='"',
                                  quoting=csv.QUOTE_MINIMAL)
         file_writer.writerow(['Id', 'time', 'value'])
         Id = 1
         check_vals = [[], [], []]
         for i in range(5):
             time = random_date()
             Id += i
             value = rdm.randint(100, 1000)
             check_vals[0].append(Id)
             check_vals[1].append(time)
             check_vals[2].append(value)
             file_writer.writerow([Id, time, value])
         time = None
         value = None
         Id = None
         file_writer.writerow([Id, time, value])
         file_handle.close()
         imported_data = d_i.ImportData('test_file.csv')
         self.assertEqual(imported_data._time, check_vals[1])
         self.assertEqual(imported_data._value, check_vals[2])
     os.remove('test_file.csv')
    def test_bad_file(self):
        file_name = 'test.csv'

        test_data = data_import.ImportData(file_name)

        self.assertEqual(len(test_data._value), 0)
        self.assertEqual(len(test_data._time), 0)
 def test_binary_search(self):
     filename = './smallData/smbg_small.csv'
     obj = data_import.ImportData(filename)
     obj.binary_sort()
     d = datetime.datetime(2018, 3, 16, 12, 38)
     index = obj.binary_search_value(d)
     self.assertEqual(index, 0)
    def test_print_array(self):
        folder_path = 'smallData'
        resolution = 5
        output_file = 'tmp.csv'
        key_file = 'cgm_small.csv'

        # pull all the folders in the file
        files_lst = [
            f for f in listdir(folder_path) if isfile(join(folder_path, f))
        ]

        # import all the files into a list of ImportData objects
        data_lst = []
        for f in files_lst:
            data_lst.append(data_import.ImportData(folder_path + '/' + f))

        # convert to zipped, rounded data
        zip_data = []
        for data in data_lst:
            zip_data.append(data_import.roundTimeArray(data, resolution))

        # print to a csv file
        data_import.printArray(zip_data, files_lst, output_file, key_file)

        self.assertTrue(os.path.exists(output_file))

        os.remove(output_file)
コード例 #13
0
 def test_ImportData_class(self):
     '''
     Test to see if both time and value are imported correctly
     '''
     file = './smallData/activity_small.csv'
     instance = di.ImportData(file)
     self.assertEqual(len(instance._time), len(instance._value))
    def test_file_exists(self):
        with open('test_file1.csv', "w") as file_handle:
            file_writer = csv.writer(file_handle,
                                     delimiter=',',
                                     quotechar='"',
                                     quoting=csv.QUOTE_MINIMAL)
            file_writer.writerow(['Id', 'time', 'value', 'no useful', 'info'])
            Id = 1
            check_vals = [[], [], []]
            for i in range(100):
                time = random_date()
                Id += i
                value = rdm.randint(100, 1000)
                check_vals[0].append(Id)
                check_vals[1].append(time)
                check_vals[2].append(value)
                file_writer.writerow([Id, time, value])
            file_handle.close()

        with open('test_file2.csv', "w") as file_handle:
            file_writer = csv.writer(file_handle,
                                     delimiter=',',
                                     quotechar='"',
                                     quoting=csv.QUOTE_MINIMAL)
            file_writer.writerow(['Id', 'time', 'value', 'no useful', 'info'])
            Id = 1
            check_vals = [[], [], []]
            for i in range(100):
                time = random_date()
                Id += i
                value = rdm.randint(100, 1000)
                check_vals[0].append(Id)
                check_vals[1].append(time)
                check_vals[2].append(value)
                file_writer.writerow([Id, time, value])
            file_handle.close()
        imported_data1 = d_i.ImportData('test_file1.csv')
        imported_data2 = d_i.ImportData('test_file2.csv')
        zipp = [
            imported_data1.roundTimeArray(5),
            imported_data2.roundTimeArray(5)
        ]
        d_i.printArray(zipp, ['test_file1.csv', 'test_file2.csv'],
                       'output_file', 'test_file1.csv')
        self.assertTrue(path.exists('output_file.csv'))
        os.remove('test_file1.csv')
        os.remove('test_file2.csv')
 def test_Init(self):
     file = open('test.csv', 'w')
     file.write('time,value\n')
     file.write('1/29/18 1:29,25\n')
     file.close()
     obj = data_import.ImportData('test.csv')
     self.assertEqual(obj._value[0], 25)
     os.remove('test.csv')
コード例 #16
0
 def test_import_data_highlow(self):
     csv_reader = data_import.ImportData('test_highlow.csv', highlow=True)
     time_high = data_import.datetime.datetime(2018, 3, 16, 0, 20)
     time_low = data_import.datetime.datetime(2018, 3, 16, 0, 21)
     time_normal = data_import.datetime.datetime(2018, 3, 16, 0, 22)
     assert csv_reader.linear_search_value(time_high) == [300.0]
     assert csv_reader.linear_search_value(time_low) == [40.0]
     assert csv_reader.linear_search_value(time_normal) == [150.0]
コード例 #17
0
 def test_round_time(self):
     filename = './smallData/smbg_small.csv'
     obj = data_import.ImportData(filename)
     rounded = data_import.roundTimeArray(obj, 5)
     for (time, value) in rounded:
         self.assertEqual(value, 254.0)
         d = datetime.datetime(2018, 3, 16, 8, 45)
         self.assertEqual(time, d)
         break
    def test_rounding_resolution(self):
        file_name = 'smallData/activity_small.csv'
        resolution = 5

        test_data = data_import.ImportData(file_name)
        rounded_data = data_import.roundTimeArray(test_data, resolution)
        # make sure the rounded times correct resolution
        for t, v in rounded_data:
            self.assertTrue(t.minute % resolution == 0)
 def setUp(self):
     file = open('search.csv', 'w')
     file.write('time,value\n')
     file.write('1/29/18 1:29,1.0\n')
     file.write('2/12/18 1:29,2.0\n')
     file.close()
     self.obj = data_import.ImportData('search.csv')
     self.obj._rounded_time.append(datetime.datetime(2018, 1, 29, 1, 29))
     self.obj._rounded_time.append(datetime.datetime(2018, 2, 12, 1, 29))
コード例 #20
0
 def test_low_high_handling(self):
     f = open('low_high.csv', 'w')
     f.write('time,value\n')
     f.write('3/16/19 4:16,low\n')
     f.write('3/16/19 5:23,high')
     f.close()
     obj = data_import.ImportData('low_high.csv')
     self.assertEqual(obj._value[0], 40)
     self.assertEqual(obj._value[1], 300)
     os.remove('low_high.csv')
コード例 #21
0
 def test_Init_replace_high_low(self):
     file = open('test.csv', 'w')
     file.write('time,value\n')
     file.write('4/20/20 4:20,low\n')
     file.write('4/21/20 4:20,high')
     file.close()
     instance = di.ImportData('test.csv')
     self.assertEqual(instance._value[0], 40.0)
     self.assertEqual(instance._value[1], 300.0)
     os.remove('test.csv')
 def test_replacement(self):
     f = open('replace.csv', 'w')
     f.write('time,value\n')
     f.write('3/15/18 3:06,low\n')
     f.write('3/15/18 4:06,high')
     f.close()
     obj = data_import.ImportData('replace.csv')
     self.assertEqual(obj._value[0], 40)
     self.assertEqual(obj._value[1], 300)
     os.remove('replace.csv')
コード例 #23
0
 def test_Init(self):
     file = open('test.csv', 'w')
     file.write('time,value\n')
     file.write('4/20/20 4:20,20.0\n')
     file.write('4/21/20 4:20,10.0')
     file.close()
     instance = di.ImportData('test.csv')
     self.assertEqual(instance._value[0], 20.0)
     self.assertEqual(instance._value[1], 10.0)
     os.remove('test.csv')
コード例 #24
0
 def test_low_high_replace(self):
     f = open('test1.csv', 'w')
     f.write('time,value\n')
     f.write('3/15/19 1:00,low\n')
     f.write('3/16/19 2:00,high')
     f.close()
     obj = data_import.ImportData('test1.csv')
     self.assertEqual(obj._value[0], 40)
     self.assertEqual(obj._value[1], 300)
     os.remove('test1.csv')
 def test_InitWithHighLow(self):
     file = open('./cgm_small.csv', 'w')
     file.write('time,value\n')
     file.write('1/29/18 1:29,low\n')
     file.write('2/12/18 1:29,high\n')
     file.close()
     obj = data_import.ImportData('cgm_small.csv')
     self.assertEqual(obj._value[0], 40.0)
     self.assertEqual(obj._value[1], 300.0)
     os.remove('cgm_small.csv')
 def test_init_replace(self):
     """
     A function to check if the replacement of the strings "low" and
     "high" are handled properly
     """
     f = open('test.csv', 'w')
     f.write("Id,time,value\n1134,3/19/18 22:18,low")
     f.close()
     obj_init = data_import.ImportData('test.csv', replace=True)
     self.assertEqual(40, obj_init._value[0])
     os.remove('test.csv')
    def test_rounding_unique(self):
        file_name = 'smallData/activity_small.csv'
        resolution = 5

        test_data = data_import.ImportData(file_name)
        rounded_data = data_import.roundTimeArray(test_data, resolution)

        times = []
        for t, v in rounded_data:
            times.append(t)
        # make sure the rounded times are unique
        self.assertFalse(len(times) > len(times))
コード例 #28
0
 def test_roundtimearray_wrong_inputs(self):
     csv_reader = data_import.ImportData('smallData/bolus_small.csv')
     self.assertRaises(TypeError, data_import.roundTimeArray, 'string!', 10,
                       'average', False)
     self.assertRaises(TypeError, data_import.roundTimeArray, csv_reader,
                       'string!', 'average', False)
     self.assertRaises(TypeError, data_import.roundTimeArray, csv_reader,
                       10, 10, False)
     self.assertRaises(NotImplementedError, data_import.roundTimeArray,
                       csv_reader, 10, 'divide', False)
     self.assertRaises(TypeError, data_import.roundTimeArray, 'string!', 10,
                       'average', 'string!')
コード例 #29
0
 def test_print_array_round_to_15(self):
     data = []
     for f in os.listdir('smallData'):
         data.append(di.ImportData('smallData/' + f))
     rdata_15 = []
     for i in data:
         rdata_15.append(di.roundTimeArray(i, 15))
     self.assertNotEqual(
         di.printArray(rdata_15, os.listdir('smallData'), 'out_15',
                       'meal_small.csv'), -1)
     self.assertTrue(os.path.exists('out_15.csv'))
     os.remove('out_15.csv')
コード例 #30
0
 def test_linear_search_notfound(self):
     in_file = './smallData/bolus_small.csv'
     dt = datetime.datetime(
         2018,
         3,
         12,
         0,
         0,
     )
     instance = di.ImportData(in_file)
     output = instance.linear_search_value(dt)
     self.assertEqual(output, -1)