Пример #1
0
 async def test_json_label(self):
     await Merge.cli(
         "dest=json",
         "src=json",
         "-source-dest-filename",
         self.temp_filename,
         "-source-dest-label",
         "somelabel",
         "-source-src-filename",
         self.temp_filename,
     )
     # Check the unlabeled source
     with self.subTest(labeled=None):
         async with JSONSource(
                 FileSourceConfig(filename=self.temp_filename)) as source:
             async with source() as sctx:
                 repos = [repo async for repo in sctx.repos()]
                 self.assertEqual(len(repos), len(self.repos))
     # Check the labeled source
     with self.subTest(labeled="somelabel"):
         async with JSONSource(
                 FileSourceConfig(filename=self.temp_filename,
                                  label="somelabel")) as source:
             async with source() as sctx:
                 repos = [repo async for repo in sctx.repos()]
                 self.assertEqual(len(repos), len(self.repos))
Пример #2
0
 async def test_json_tag(self):
     await Merge.cli(
         "src=json",
         "dest=json",
         "-source-src-filename",
         self.temp_filename,
         "-source-dest-filename",
         self.temp_filename,
         "-source-dest-tag",
         "sometag",
         "-source-src-allowempty",
         "-source-dest-allowempty",
         "-source-src-readwrite",
         "-source-dest-readwrite",
     )
     # Check the untagged source
     with self.subTest(tagged=None):
         async with JSONSource(
                 FileSourceConfig(filename=self.temp_filename)) as source:
             async with source() as sctx:
                 records = [record async for record in sctx.records()]
                 self.assertEqual(len(records), len(self.records))
     # Check the tagged source
     with self.subTest(tagged="sometag"):
         async with JSONSource(
                 FileSourceConfig(filename=self.temp_filename,
                                  tag="sometag")) as source:
             async with source() as sctx:
                 records = [record async for record in sctx.records()]
                 self.assertEqual(len(records), len(self.records))
Пример #3
0
    async def test_sources(self):
        with tempfile.TemporaryDirectory() as tempdir:
            # Source the HTTP API will pre-load
            source = JSONSource(
                filename=str(pathlib.Path(tempdir, "source.json")),
                allowempty=True,
                readwrite=True,
            )

            # Record the source will have in it
            myrecord = Record("myrecord", data={"features": {"f1": 0}})
            await save(source, myrecord)

            async with ServerRunner.patch(HTTPService.server) as tserver:
                cli = await tserver.start(
                    HTTPService.server.cli(
                        "-insecure",
                        "-port",
                        "0",
                        "-sources",
                        "mysource=json",
                        "-source-mysource-filename",
                        source.config.filename,
                    )
                )
                async with self.get(
                    cli, "/source/mysource/record/myrecord"
                ) as r:
                    self.assertEqual(await r.json(), myrecord.export())
Пример #4
0
 async def setUp(self):
     await super().setUp()
     self.repos = [Repo(str(random.random())) for _ in range(0, 10)]
     self.temp_filename = self.mktempfile()
     self.sconfig = FileSourceConfig(filename=self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for repo in self.repos:
                 await sctx.update(repo)
     contents = json.loads(Path(self.sconfig.filename).read_text())
     # Ensure there are repos in the file
     self.assertEqual(
         len(contents.get(self.sconfig.label)),
         len(self.repos),
         "ReposTestCase JSON file erroneously initialized as empty",
     )
     # TODO(p3) For some reason patching Model.load doesn't work
     # self._stack.enter_context(patch("dffml.model.model.Model.load",
     #     new=model_load))
     self._stack.enter_context(
         patch.object(
             ModelCMD,
             "arg_model",
             new=ModelCMD.arg_model.modify(type=model_load),
         ))
     self._stack.enter_context(
         patch("dffml.feature.feature.Feature.load", new=feature_load))
     self._stack.enter_context(
         patch("dffml.df.base.OperationImplementation.load",
               new=opimp_load))
     self._stack.enter_context(
         patch("dffml.df.types.Operation.load", new=op_load))
Пример #5
0
 async def setUp(self):
     await super().setUp()
     self.records = [Record(str(random.random())) for _ in range(0, 10)]
     self.temp_filename = self.mktempfile()
     self.sconfig = FileSourceConfig(filename=self.temp_filename,
                                     readwrite=True,
                                     allowempty=True)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for record in self.records:
                 await sctx.update(record)
     contents = json.loads(Path(self.sconfig.filename).read_text())
     # Ensure there are records in the file
     self.assertEqual(
         len(contents.get(self.sconfig.tag)),
         len(self.records),
         "RecordsTestCase JSON file erroneously initialized as empty",
     )
     # TODO(p3) For some reason patching Model.load doesn't work
     self._stack.enter_context(
         patch("dffml.model.model.Model.load", new=model_load))
     self._stack.enter_context(
         patch("dffml.df.base.OperationImplementation.load",
               new=opimp_load))
     self._stack.enter_context(
         patch("dffml.df.types.Operation.load", new=op_load))
Пример #6
0
 async def setUp(self):
     super().setUp()
     self.repos = [Repo(str(random.random())) for _ in range(0, 10)]
     self.__temp_filename = non_existant_tempfile()
     self.temp_filename = self.__temp_filename.__enter__()
     self.sconfig = FileSourceConfig(filename=self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for repo in self.repos:
                 await sctx.update(repo)
 async def test_run(self):
     self.repo_keys = {"add 40 and 2": 42, "multiply 42 and 10": 420}
     self.repos = list(map(Repo, self.repo_keys.keys()))
     os.unlink(self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for repo in self.repos:
                 await sctx.update(repo)
     with patch.object(
         OperationImplementation, "load", opimp_load
     ), patch.object(
         Operation, "load", op_load
     ), tempfile.NamedTemporaryFile(
         suffix=".json"
     ) as dataflow_file:
         dataflow = io.StringIO()
         with contextlib.redirect_stdout(dataflow):
             await Dataflow.cli(
                 "create",
                 "-config",
                 "json",
                 *map(lambda op: op.name, OPERATIONS),
             )
         dataflow_file.write(dataflow.getvalue().encode())
         dataflow_file.seek(0)
         results = await Dataflow.cli(
             "run",
             "repos",
             "all",
             "-dataflow",
             dataflow_file.name,
             "primary=json",
             "-sources",
             "primary=json",
             "-source-filename",
             self.temp_filename,
             "-repo-def",
             "calc_string",
             "-inputs",
             '["result"]=get_single_spec',
         )
         results = {
             result.src_url: result.feature("result") for result in results
         }
         for repo in self.repos:
             self.assertIn(repo.src_url, results)
             self.assertEqual(
                 self.repo_keys[repo.src_url], results[repo.src_url]
             )
Пример #8
0
 async def test_run(self):
     self.record_keys = {"add 40 and 2": 42, "multiply 42 and 10": 420}
     self.records = list(map(Record, self.record_keys.keys()))
     os.unlink(self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for record in self.records:
                 await sctx.update(record)
     tmpdir = tempfile.mkdtemp()
     handle, dataflow_file = tempfile.mkstemp(suffix=".json", dir=tmpdir)
     os.close(handle)
     with open(dataflow_file, mode="w+b") as dataflow_file:
         dataflow = io.StringIO()
         with contextlib.redirect_stdout(dataflow):
             await Dataflow.cli(
                 "create",
                 "-configloader",
                 "json",
                 *map(lambda op: op.name, OPERATIONS),
             )
         dataflow_file.write(dataflow.getvalue().encode())
         dataflow_file.seek(0)
         results = await Dataflow.cli(
             "run",
             "records",
             "all",
             "-dataflow",
             dataflow_file.name,
             "primary=json",
             "-sources",
             "primary=json",
             "-source-filename",
             self.temp_filename,
             "-record-def",
             "calc_string",
             "-inputs",
             '["result"]=get_single_spec',
         )
         results = {
             result.key: result.feature("result")
             for result in results
         }
         for record in self.records:
             self.assertIn(record.key, results)
             self.assertEqual(self.record_keys[record.key],
                              results[record.key])
     shutil.rmtree(tmpdir)
 async def setUp(self):
     super().setUp()
     self.repos = [Repo(str(random.random())) for _ in range(0, 10)]
     self.__stack = ExitStack()
     self._stack = self.__stack.__enter__()
     self.temp_filename = self.mktempfile()
     self.sconfig = FileSourceConfig(filename=self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for repo in self.repos:
                 await sctx.update(repo)
     contents = json.loads(Path(self.sconfig.filename).read_text())
     # Ensure there are repos in the file
     self.assertEqual(
         len(contents.get(self.sconfig.label)),
         len(self.repos),
         "ReposTestCase JSON file erroneously initialized as empty",
     )
Пример #10
0
 async def test_run(self):
     test_key = "multiply 42 and 10"
     self.repo_keys = {"add 40 and 2": 42, "multiply 42 and 10": 420}
     self.repos = list(map(Repo, self.repo_keys.keys()))
     os.unlink(self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for repo in self.repos:
                 await sctx.update(repo)
     with tempfile.NamedTemporaryFile(suffix=".json") as dataflow_file:
         dataflow = io.StringIO()
         with contextlib.redirect_stdout(dataflow):
             await Dataflow.cli(
                 "create",
                 "-config",
                 "json",
                 *map(lambda op: op.name, OPERATIONS),
             )
         dataflow_file.write(dataflow.getvalue().encode())
         dataflow_file.seek(0)
         results = await Dataflow.cli(
             "run",
             "repos",
             "set",
             "-keys",
             test_key,
             "-dataflow",
             dataflow_file.name,
             "primary=json",
             "-sources",
             "primary=json",
             "-source-filename",
             self.temp_filename,
             "-repo-def",
             "calc_string",
             "-inputs",
             '["result"]=get_single_spec',
         )
         self.assertEqual(len(results), 1)
         self.assertEqual(
             self.repo_keys[test_key], results[0].feature("result")
         )
Пример #11
0
 async def test_run(self):
     self.repo_keys = {"add 40 and 2": 42, "multiply 42 and 10": 420}
     self.repos = list(map(Repo, self.repo_keys.keys()))
     os.unlink(self.temp_filename)
     async with JSONSource(self.sconfig) as source:
         async with source() as sctx:
             for repo in self.repos:
                 await sctx.update(repo)
     with patch.object(
         OperationImplementation, "load", opimp_load
     ), patch.object(Operation, "load", op_load):
         results = await OperationsAll.cli(
             "-sources",
             "primary=json",
             "-source-filename",
             self.temp_filename,
             "-repo-def",
             "calc_string",
             "-remap",
             "get_single.result=string_calculator",
             "-output-specs",
             '["result"]=get_single_spec',
             "-dff-memory-operation-network-ops",
             *map(lambda op: op.name, OPERATIONS),
             "-dff-memory-opimp-network-opimps",
             *map(lambda imp: imp.op.name, OPIMPS),
         )
         results = {
             result.src_url: result.features(["string_calculator"])[
                 "string_calculator"
             ]
             for result in results
         }
         for repo in self.repos:
             self.assertIn(repo.src_url, results)
             self.assertEqual(
                 self.repo_keys[repo.src_url], results[repo.src_url]
             )
Пример #12
0
 async def setUpSource(self):
     return JSONSource(FileSourceConfig(filename=self.testfile))
Пример #13
0
    async def test_scorer(self):
        with tempfile.TemporaryDirectory() as tempdir:
            model = SLRModel(
                features=Features(Feature("f1", float, 1)),
                predict=Feature("ans", int, 1),
                location=tempdir,
            )
            # y = m * x + b for equation SLR is solving for
            m = 5
            b = 3

            # Train the model
            await train(model, *[{
                "f1": x,
                "ans": m * x + b
            } for x in range(0, 10)])

            source = JSONSource(
                filename=pathlib.Path(tempdir, "source.json"),
                allowempty=True,
                readwrite=True,
            )

            # Record the source will have in it
            await save(
                source,
                *[
                    Record(
                        str(i),
                        data={"features": {
                            "f1": x,
                            "ans": (m * x) + b
                        }},
                    ) for i, x in enumerate(range(10, 20))
                ],
            )

            async with ServerRunner.patch(HTTPService.server) as tserver:
                cli = await tserver.start(
                    HTTPService.server.cli(
                        "-insecure",
                        "-port",
                        "0",
                        "-models",
                        "mymodel=slr",
                        "-model-mymodel-location",
                        tempdir,
                        "-model-mymodel-features",
                        "f1:float:1",
                        "-model-mymodel-predict",
                        "ans:int:1",
                        "-features",
                        "ans:int:1",
                        "-sources",
                        "mysource=json",
                        "-source-mysource-filename",
                        str(source.config.filename),
                        "-scorers",
                        "myscorer=mse",
                    ))
                async with self.post(cli,
                                     "/scorer/myscorer/mymodel/score",
                                     json=["mysource"]) as r:
                    self.assertEqual(await r.json(), {"accuracy": 0.0})
Пример #14
0
 async def setUpSource(self):
     return JSONSource(
         JSONSourceConfig(
             filename=self.testfile, allowempty=True, readwrite=True
         )
     )