async def test_config(self): config = self.model.__class__.config( parse_unknown( "--model-predict", "feature_name:int:1", "--model-classifications", "0", "1", "2", "--model-clstype", "int", "--model-features", "starts_with_a:int:1", )) self.assertEqual( config.directory, os.path.join(os.path.expanduser("~"), ".cache", "dffml", "tensorflow"), ) self.assertEqual(config.steps, 3000) self.assertEqual(config.epochs, 30) self.assertEqual(config.hidden, [12, 40, 15]) self.assertEqual(config.predict.NAME, "feature_name") self.assertEqual(config.classifications, [0, 1, 2]) self.assertEqual(config.clstype, int)
async def test_model(self): with tempfile.TemporaryDirectory() as tempdir, patch.object( Model, "load", new=model_load): config = parse_unknown( "--model-directory", tempdir, "--model-features", "Years:int:1", "Experiance:int:1", "--model-predict", "Salary:float:1", ) async with self.post("/configure/model/fake/salary", json=config) as r: self.assertEqual(await r.json(), OK) self.assertIn("salary", self.cli.app["models"]) self.assertEqual( self.cli.app["models"]["salary"].config, FakeModelConfig( directory=pathlib.Path(tempdir), features=Features( Feature("Years", int, 1), Feature("Experiance", int, 1), ), predict=Feature("Salary", float, 1), ), ) with self.subTest(context="salaryctx"): # Create the context async with self.get( "/context/model/salary/salaryctx") as r: self.assertEqual(await r.json(), OK) self.assertIn("salaryctx", self.cli.app["model_contexts"])
def test_config_readonly_default(self): config = FileSource.config( parse_unknown("--source-file-filename", "feedface")) self.assertEqual(config.filename, "feedface") self.assertEqual(config.tag, "untagged") self.assertFalse(config.readwrite) self.assertFalse(config.allowempty)
def test_config_readonly_default(self): config = CSVSource.config( parse_unknown("--source-csv-filename", "feedface")) self.assertEqual(config.filename, "feedface") self.assertEqual(config.label, "unlabeled") self.assertEqual(config.key, None) self.assertFalse(config.readonly)
async def test_model(self): with tempfile.TemporaryDirectory() as tempdir, patch.object( Model, "load", new=model_load): config = parse_unknown("--model-directory", tempdir) async with self.post("/configure/model/fake/salary", json=config) as r: self.assertEqual(await r.json(), OK) self.assertIn("salary", self.cli.app["models"]) self.assertEqual( self.cli.app["models"]["salary"].config, FakeModelConfig(directory=tempdir), ) with self.subTest(context="salaryctx"): # Define the features features = Features( DefFeature("Years", int, 1), DefFeature("Experiance", int, 1), ) exported_features = features.export() # Check that we can send shorthand version of feature_def for name, feature_def in exported_features.items(): del feature_def["name"] # Create the context async with self.post( "/context/model/salary/salaryctx", json=exported_features, ) as r: self.assertEqual(await r.json(), OK) self.assertIn("salaryctx", self.cli.app["model_contexts"]) self.assertEqual( self.cli.app["model_contexts"] ["salaryctx"].features.export(), features.export(), )
async def test_model_config_error(self): # Should be directory, not folder config = parse_unknown("--model-directory", "mymodel_dir") with patch.object(Model, "load", new=model_load): with self.assertRaisesRegex(ServerException, "missing.*features"): async with self.post("/configure/model/fake/salary", json=config): pass # pramga: no cov
def test_config_default(self): config = CSVSource.config( parse_unknown("--source-csv-filename", "feedface")) self.assertEqual(config.filename, "feedface") self.assertEqual(config.label, "unlabeled") self.assertEqual(config.labelcol, "label") self.assertEqual(config.key, "src_url") self.assertFalse(config.readwrite) self.assertFalse(config.allowempty)
def test_config_default(self): config = CSVSource.config( parse_unknown("--source-csv-filename", "feedface")) self.assertEqual(config.filename, "feedface") self.assertEqual(config.tag, "untagged") self.assertEqual(config.tagcol, "tag") self.assertEqual(config.key, "key") self.assertFalse(config.readwrite) self.assertFalse(config.allowempty)
def test_config_readonly_set(self): config = FileSource.config( parse_unknown( "--source-file-filename", "feedface", "--source-file-label", "default-label", "--source-file-readonly", )) self.assertEqual(config.filename, "feedface") self.assertEqual(config.label, "default-label") self.assertTrue(config.readonly)
async def test_model_bad_features(self): with tempfile.TemporaryDirectory() as tempdir, patch.object( Model, "load", new=model_load): config = parse_unknown("--model-directory", tempdir) async with self.post("/configure/model/fake/salary", json=config) as r: self.assertEqual(await r.json(), OK) with self.assertRaisesRegex(ServerException, "Incorrect format for features"): async with self.post("/context/model/salary/salaryctx", json=[]) as r: pass # pramga: no cov
def test_config_default_label(self): was = MemoryRedundancyChecker.config( parse_unknown( "--rchecker-memory-kvstore", "withargs", "--rchecker-memory-kvstore-withargs-filename", "somefile", )) self.assertEqual(type(was), BaseRedundancyCheckerConfig) self.assertEqual(type(was.key_value_store), KeyValueStoreWithArguments) self.assertEqual(type(was.key_value_store.config), KeyValueStoreWithArgumentsConfig) self.assertEqual(was.key_value_store.config.filename, "somefile")
def test_config_readonly_set(self): config = FileSource.config( parse_unknown( "--source-file-filename", "feedface", "--source-file-tag", "default-tag", "--source-file-readwrite", "--source-file-allowempty", )) self.assertEqual(config.filename, "feedface") self.assertEqual(config.tag, "default-tag") self.assertTrue(config.readwrite) self.assertTrue(config.allowempty)
def test_config_readonly_set(self): config = CSVSource.config( parse_unknown( "--source-csv-filename", "feedface", "--source-csv-label", "default-label", "--source-csv-key", "SourceURLColumn", "--source-csv-readonly", )) self.assertEqual(config.filename, "feedface") self.assertEqual(config.label, "default-label") self.assertEqual(config.key, "SourceURLColumn") self.assertTrue(config.readonly)
async def test_config(self): # Setting up configuration for model config = self.model.__class__.config( parse_unknown("--model-predict", "TARGET") ) self.assertEqual( config.directory, os.path.join( os.path.expanduser("~"), ".cache", "dffml", "tensorflow" ), ) self.assertEqual(config.steps, 3000) self.assertEqual(config.epochs, 30) self.assertEqual(config.hidden, [12, 40, 15]) self.assertEqual(config.predict, "TARGET")
async def test_source(self): config = parse_unknown("--source-filename", "dataset.csv", "--source-readonly") async with self.post("/configure/source/csv/salary", json=config) as r: self.assertEqual(await r.json(), OK) self.assertIn("salary", self.cli.app["sources"]) self.assertEqual( self.cli.app["sources"]["salary"].config, CSVSourceConfig( filename="dataset.csv", label="unlabeled", readonly=True, key="src_url", label_column="label", ), )
async def test_config(self): # Setting up configuration for model config = self.model.__class__.config( parse_unknown( "--model-predict", "TARGET:float:1", "--model-features", "feature_1:float:1", "--model-features", "feature_2:float:1", "-model-directory", self.model_dir.name, )) self.assertEqual(config.directory, pathlib.Path(self.model_dir.name)) self.assertEqual(config.steps, 3000) self.assertEqual(config.epochs, 30) self.assertEqual(config.hidden, [12, 40, 15]) self.assertEqual(config.predict.name, "TARGET")
async def test_source(self): config = parse_unknown("--source-filename", "dataset.csv", "-source-allowempty") async with self.post("/configure/source/csv/salary", json=config) as r: self.assertEqual(await r.json(), OK) self.assertIn("salary", self.cli.app["sources"]) self.assertEqual( self.cli.app["sources"]["salary"].config, CSVSourceConfig( filename=pathlib.Path("dataset.csv"), tag="untagged", key="key", tagcol="tag", allowempty=True, ), ) with self.subTest(context="salaryctx"): async with self.get("/context/source/salary/salaryctx") as r: self.assertEqual(await r.json(), OK) self.assertIn("salaryctx", self.cli.app["source_contexts"])
def test_config_set(self): config = CSVSource.config( parse_unknown( "--source-csv-filename", "feedface", "--source-csv-tag", "default-tag", "--source-csv-tagcol", "dffml_tag", "--source-csv-key", "SourceURLColumn", "--source-csv-readwrite", "--source-csv-allowempty", )) self.assertEqual(config.filename, "feedface") self.assertEqual(config.tag, "default-tag") self.assertEqual(config.tagcol, "dffml_tag") self.assertEqual(config.key, "SourceURLColumn") self.assertTrue(config.readwrite) self.assertTrue(config.allowempty)
async def test_config(self): config = self.model.__class__.config( parse_unknown( "--model-predict", "feature_name:int:1", "--model-classifications", "0", "1", "2", "--model-clstype", "int", "--model-features", "starts_with_a:int:1", "-model-directory", self.model_dir.name, )) self.assertEqual(config.directory, pathlib.Path(self.model_dir.name)) self.assertEqual(config.steps, 3000) self.assertEqual(config.epochs, 30) self.assertEqual(config.hidden, [12, 40, 15]) self.assertEqual(config.predict.name, "feature_name") self.assertEqual(config.classifications, [0, 1, 2]) self.assertEqual(config.clstype, int)
def test_parse_unknown(self): parsed = parse_unknown( "-rchecker-memory-kvstore", "withargs", "-rchecker-memory-kvstore-withargs-filename", "somefile", ) self.assertEqual( parsed, { "rchecker": { "plugin": None, "config": { "memory": { "plugin": None, "config": { "kvstore": { "plugin": ["withargs"], "config": { "withargs": { "plugin": None, "config": { "filename": { "plugin": ["somefile"], "config": {}, } }, } }, } }, } }, } }, )
async def test_source_error(self): config = parse_unknown("--source-file", "dataset.csv") with self.assertRaisesRegex(ServerException, "missing.*filename"): async with self.post("/configure/source/csv/salary", json=config): pass # pramga: no cov