1
1
from jlab_datascience_toolkit .data_parser import make
2
2
import unittest
3
+ import logging
3
4
import matplotlib .pyplot as plt
4
5
import pandas as pd
5
6
import numpy as np
7
+ import argparse
8
+ import shutil
9
+ import sys
6
10
import os
7
11
12
+ test_log = logging .Logger ('test_logger' )
13
+
8
14
rng = np .random .default_rng (seed = 42 )
15
+ parser_id = 'CSVParser_v0'
9
16
10
17
11
- class TestPandasParserv0 (unittest .TestCase ):
18
+ class TestCSVParserv0 (unittest .TestCase ):
12
19
13
20
# Initialize:
14
21
# *****************************************
15
22
def __init__ (self , * args , ** kwargs ):
16
- super (TestPandasParserv0 , self ).__init__ (* args , ** kwargs )
23
+ super (TestCSVParserv0 , self ).__init__ (* args , ** kwargs )
17
24
18
25
@classmethod
19
26
def setUpClass (self ) -> None :
20
27
print ('Setting up all tests...' )
21
28
self .columns = ['R121GMES' , 'R122GMES' ,
22
29
'R123GMES' , 'R121GSET' , 'R122GSET' , 'R123GSET' ]
23
- self .path = './pandas_parser_utest .csv'
30
+ self .path = './csv_parser_utest .csv'
24
31
self .samples = 100
25
32
data = rng .normal (loc = 5 , scale = 1 , size = (
26
33
self .samples , len (self .columns )))
@@ -34,7 +41,7 @@ def setUpClass(self) -> None:
34
41
test_data
35
42
test_data .to_csv (self .path )
36
43
37
- self .path2 = './pandas_parser_utest2 .csv'
44
+ self .path2 = './csv_parser_utest2 .csv'
38
45
data = rng .normal (loc = 9 , scale = 2 , size = (
39
46
self .samples , len (self .columns )))
40
47
dates = []
@@ -64,14 +71,14 @@ def tearDown(self) -> None:
64
71
65
72
def test_no_config (self ):
66
73
print ('*****No Config Test*****\n ' )
67
- parser = make ('PandasParser_v0' )
74
+ parser = make (parser_id )
68
75
output = parser .load_data ()
69
76
self .assertIsNone (output )
70
77
71
78
def test_string_filepaths (self ):
72
79
print ('*****String Filepaths Test*****\n ' )
73
80
74
- parser = make ('PandasParser_v0' , config = dict (filepaths = self .path ))
81
+ parser = make (parser_id , config = dict (filepaths = self .path ))
75
82
output = parser .load_data ()
76
83
print ('Output Head:\n ' , output .head ())
77
84
@@ -80,14 +87,14 @@ def test_string_filepaths(self):
80
87
def test_one_item_list_filepaths (self ):
81
88
print ('*****One Item List Test*****\n ' )
82
89
83
- parser = make ('PandasParser_v0' , config = dict (filepaths = [self .path ]))
90
+ parser = make (parser_id , config = dict (filepaths = [self .path ]))
84
91
output = parser .load_data ()
85
92
print ('Output Head:\n ' , output .head ())
86
93
self .assertEqual (output .shape , (self .samples , len (self .columns )+ 1 ))
87
94
88
95
def test_two_filepaths (self ):
89
96
print ('*****Two Filepaths Test*****\n ' )
90
- parser = make ('PandasParser_v0' , config = dict (filepaths = [self .path , self .path2 ]))
97
+ parser = make (parser_id , config = dict (filepaths = [self .path , self .path2 ]))
91
98
output = parser .load_data ()
92
99
print ('Output Head:\n ' , output .head ())
93
100
print ('Output shape:' , output .shape )
@@ -97,7 +104,7 @@ def test_usecols_read_arg(self):
97
104
print ('*****Usecols Read Arg Test*****\n ' )
98
105
99
106
two_columns = ['R121GMES' , 'R121GSET' ]
100
- parser = make ('PandasParser_v0' , config = dict (
107
+ parser = make (parser_id , config = dict (
101
108
filepaths = self .path , read_kwargs = dict (usecols = two_columns )))
102
109
output = parser .load_data ()
103
110
print ('Output Head:\n ' , output .head ())
@@ -110,7 +117,7 @@ def test_use_datetime_index(self):
110
117
def column_lambda (x ): return ('GMES' in x ) or (x == 'Date' )
111
118
read_kwargs = dict (usecols = column_lambda ,
112
119
index_col = 'Date' , parse_dates = True )
113
- parser = make ('PandasParser_v0' ,
120
+ parser = make (parser_id ,
114
121
config = dict (
115
122
filepaths = self .path , read_kwargs = read_kwargs )
116
123
)
@@ -121,7 +128,27 @@ def column_lambda(x): return ('GMES' in x) or (x == 'Date')
121
128
self .assertTrue ('GMES' in column )
122
129
self .assertIsInstance (output .index , pd .DatetimeIndex )
123
130
131
+ def test_save_load (self ):
132
+ print ('*****Save/Load Test*****\n ' )
124
133
125
- # Run this file via: python utest_pandas_parser_v0.py
134
+ parser = make (parser_id , config = dict (filepaths = self .path , read_kwargs = {'usecols' : self .columns }))
135
+ output = parser .load_data ()
136
+ save_path = './temp_parser'
137
+ try :
138
+ parser .save (save_path )
139
+ new_parser = make (parser_id )
140
+ new_parser .load (save_path )
141
+ new_output = new_parser .load_data ()
142
+ for col in output .columns :
143
+ with self .subTest (col = col ):
144
+ self .assertTrue (np .allclose (output [col ], new_output [col ]))
145
+ finally :
146
+ shutil .rmtree (save_path )
147
+ pass
148
+
149
+ # Run this file via: python utest_csv_parser_v0.py
126
150
if __name__ == "__main__" :
151
+ argv = len (sys .argv ) > 1 and sys .argv [1 ]
152
+ loglevel = logging .DEBUG if argv == '-v' else logging .WARNING
153
+ logging .basicConfig (stream = sys .stdout , level = loglevel )
127
154
unittest .main ()
0 commit comments