4
4
import importlib
5
5
6
6
from dffml .record import Record
7
- from dffml import train , accuracy , predict
7
+ from dffml import train , accuracy , predict , save , load
8
8
from dffml .source .csv import CSVSource
9
9
from dffml .feature .feature import Features , DefFeature
10
10
from dffml .util .asynctestcase import IntegrationCLITestCase
@@ -23,6 +23,8 @@ async def populate_source(self, source_cls, *records, **kwargs):
23
23
24
24
async def setUp (self ):
25
25
await super ().setUp ()
26
+ save_and_load_file = self .mktempfile () + ".csv"
27
+ setattr (self , "save_and_load" , save_and_load_file )
26
28
self .train_data = [
27
29
[0 , 1 , 0.2 , 10 ],
28
30
[1 , 3 , 0.4 , 20 ],
@@ -43,6 +45,61 @@ async def setUp(self):
43
45
setattr (self , f"{ use } _filename" , filename )
44
46
await self .populate_source (CSVSource , * records , filename = filename )
45
47
48
+ async def test_save_and_load (self ):
49
+ source = CSVSource (
50
+ filename = self .save_and_load , allowempty = True , readwrite = True
51
+ )
52
+ await save (
53
+ source ,
54
+ Record (
55
+ "1" ,
56
+ data = {
57
+ "features" : {"A" : 0 , "B" : 1 },
58
+ "prediction" : {"C" : {"value" : 1 , "confidence" : 1.0 }},
59
+ },
60
+ ),
61
+ Record (
62
+ "2" ,
63
+ data = {
64
+ "features" : {"A" : 3 , "B" : 4 },
65
+ "prediction" : {"C" : {"value" : 2 , "confidence" : 1.0 }},
66
+ },
67
+ ),
68
+ )
69
+ # All records in source
70
+ results = [record .export () async for record in load (source )]
71
+ self .assertEqual (
72
+ results ,
73
+ [
74
+ {
75
+ "key" : "1" ,
76
+ "features" : {"A" : 0 , "B" : 1 },
77
+ "prediction" : {"C" : {"confidence" : 1.0 , "value" : "1" }},
78
+ "extra" : {},
79
+ },
80
+ {
81
+ "key" : "2" ,
82
+ "features" : {"A" : 3 , "B" : 4 },
83
+ "prediction" : {"C" : {"confidence" : 1.0 , "value" : "2" }},
84
+ "extra" : {},
85
+ },
86
+ ],
87
+ )
88
+
89
+ # For specific records in a source
90
+ results = [record .export () async for record in load (source , "1" )]
91
+ self .assertEqual (
92
+ results ,
93
+ [
94
+ {
95
+ "key" : "1" ,
96
+ "features" : {"A" : 0 , "B" : 1 },
97
+ "prediction" : {"C" : {"confidence" : 1.0 , "value" : "1" }},
98
+ "extra" : {},
99
+ }
100
+ ],
101
+ )
102
+
46
103
async def test_predict (self ):
47
104
self .required_plugins ("dffml-model-scikit" )
48
105
# Import SciKit modules
0 commit comments