Skip to content
This repository has been archived by the owner on Aug 25, 2024. It is now read-only.

model: Rename directory property to location #1155

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
656434b
model : fixed some typos
programmer290399 Jul 3, 2021
46a9039
model : added experimental archive support
programmer290399 Jul 10, 2021
2843836
base : removed unused import
programmer290399 Jul 10, 2021
e459380
model : slr : renamed directory to location
programmer290399 Jul 10, 2021
16cf66c
Merge branch 'master' into model_baseclass_update
programmer290399 Jul 10, 2021
a24def9
Revert "model : slr : renamed directory to location"
programmer290399 Jul 11, 2021
1b82ff2
Revert "model : added experimental archive support"
programmer290399 Jul 11, 2021
298241f
model : Renamed Directory property to Location
programmer290399 Jul 11, 2021
af1cd43
examples : Renamed Directory property to Location
programmer290399 Jul 11, 2021
9f3d55e
tests : Renamed Directory property to Location
programmer290399 Jul 11, 2021
aea8d18
CHANGELOG : Updated
programmer290399 Jul 11, 2021
3ac402e
service : http : docs : Renamed Directory property to Location
programmer290399 Jul 11, 2021
89bf24f
skel : model : Renamed Directory property to Location
programmer290399 Jul 11, 2021
f77990f
docs : Renamed Directory property to Location
programmer290399 Jul 11, 2021
cf15e3d
examples : Renamed more occurences of Directory property to Location
programmer290399 Jul 11, 2021
07b6d51
model : Renamed more occurences of Directory property to Location
programmer290399 Jul 11, 2021
124d119
operations : Renamed Directory property to Location
programmer290399 Jul 11, 2021
3b84783
model : Renamed more occurances of directory property to location
programmer290399 Jul 11, 2021
afc26b3
high_level : Renamed directory property to location
programmer290399 Jul 11, 2021
c6abde3
operation : Renamed directory property to location
programmer290399 Jul 11, 2021
50cf8be
skel : Renamed directory property to location
programmer290399 Jul 11, 2021
ae83d61
examples : Renamed directory property to location
programmer290399 Jul 11, 2021
8d7b67c
tests : test_docstrings : Renamed directory property to location
programmer290399 Jul 11, 2021
e5784e3
service : http : tests : Renamed directory property to location
programmer290399 Jul 11, 2021
6142e96
tests : cli : Renamed directory property to location
programmer290399 Jul 11, 2021
5a68b26
skel : renamed more occurences of directory to location
programmer290399 Jul 11, 2021
11fb921
model : pytorch : tests : renamed more occurences of directory to lo…
programmer290399 Jul 11, 2021
1ddc480
service :http : renamed more occurences of directory to location
programmer290399 Jul 11, 2021
6a40429
https : tests : cli : TestServer : test_models : Renamed directory pr…
programmer290399 Jul 12, 2021
10661c1
Merge branch 'master' into model_baseclass_update
programmer290399 Jul 16, 2021
6ed9996
Merge branch 'master' into model_baseclass_update
programmer290399 Jul 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Calls to hashlib now go through helper functions
- Build docs using `dffml service dev docs`
- `cached_download/unpack_archive()` are now functions
- Directory property to Location

### Fixed
- Record object key properties are now always strings

Expand Down
1 change: 0 additions & 1 deletion dffml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
they follow a similar API for instantiation and usage.
"""
import abc
import ast
import copy
import inspect
import argparse
Expand Down
6 changes: 3 additions & 3 deletions dffml/high_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ async def train(model, *args: Union[BaseSource, Record, Dict[str, Any]]):
... Feature("Years", int, 1),
... ),
... predict=Feature("Salary", int, 1),
... directory="tempdir",
... location="tempdir",
... )
>>>
>>> async def main():
Expand Down Expand Up @@ -443,7 +443,7 @@ async def accuracy(
... Feature("Years", int, 1),
... ),
... predict=Feature("Salary", int, 1),
... directory="tempdir",
... location="tempdir",
... )
>>>
>>> async def main():
Expand Down Expand Up @@ -513,7 +513,7 @@ async def predict(
... Feature("Years", int, 1),
... ),
... predict=Feature("Salary", int, 1),
... directory="tempdir",
... location="tempdir",
... )
>>>
>>> async def main():
Expand Down
48 changes: 24 additions & 24 deletions dffml/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) 2019 Intel Corporation
"""
Model subclasses are responsible for training themselves on records, making
predictions about the value of a feature in the record, and assessing thei
predictions about the value of a feature in the record, and assessing their
programmer290399 marked this conversation as resolved.
Show resolved Hide resolved
prediction accuracy.
"""
import abc
Expand All @@ -29,13 +29,13 @@ class ModelNotTrained(Exception):

@config
class ModelConfig:
directory: str
location: str
features: Features


class ModelContext(abc.ABC, BaseDataFlowFacilitatorObjectContext):
"""
Abstract base class which should be derived from and implmented using
Abstract base class which should be derived from and implemented using
various machine learning frameworks or concepts.
"""

Expand Down Expand Up @@ -80,28 +80,28 @@ def __init__(self, config):
# TODO Just in case its a string. We should make it so that on
# instantiation of an @config we convert properties to their correct
# types.
directory = getattr(self.config, "directory", None)
if isinstance(directory, str):
directory = pathlib.Path(directory)
if isinstance(directory, pathlib.Path):
# to treat "~" as the the home directory rather than a literal
directory = directory.expanduser().resolve()
self.config.directory = directory
location = getattr(self.config, "location", None)
if isinstance(location, str):
location = pathlib.Path(location)
if isinstance(location, pathlib.Path):
# to treat "~" as the the home location rather than a literal
location = location.expanduser().resolve()
self.config.location = location

def __call__(self) -> ModelContext:
self._make_config_directory()
self._make_config_location()
return self.CONTEXT(self)

def _make_config_directory(self):
def _make_config_location(self):
"""
If the config object for this model contains the directory property
If the config object for this model contains the location property
then create it if it does not exist.
"""
directory = getattr(self.config, "directory", None)
if directory is not None:
directory = pathlib.Path(directory)
if not directory.is_dir():
directory.mkdir(mode=MODE_BITS_SECURE, parents=True)
location = getattr(self.config, "location", None)
if location is not None:
location = pathlib.Path(location)
if not location.is_dir():
location.mkdir(mode=MODE_BITS_SECURE, parents=True)


class SimpleModelNoContext:
Expand Down Expand Up @@ -131,7 +131,7 @@ async def __aenter__(self) -> Model:
# If we've already entered the model's context once, don't reload
if self._in_context > 1:
return self
self._make_config_directory()
self._make_config_location()
self.open()
return self

Expand Down Expand Up @@ -171,20 +171,20 @@ def close(self):
def disk_path(self, extention: Optional[str] = None):
"""
We do this for convenience of the user so they can usually just use the
default directory and if they train models with different parameters
default location and if they train models with different parameters
this method transparently to the user creates a filename unique the that
configuration of the model where data is saved and loaded.
"""
# Export the config to a dictionary
exported = self.config._asdict()
# Remove the directory from the exported dict
if "directory" in exported:
del exported["directory"]
# Remove the location from the exported dict
if "location" in exported:
del exported["location"]
# Replace features with the sorted list of features
if "features" in exported:
exported["features"] = dict(sorted(exported["features"].items()))
# Hash the exported config
return pathlib.Path(self.config.directory, "Model",)
return pathlib.Path(self.config.location, "Model",)

def applicable_features(self, features):
usable = []
Expand Down
8 changes: 4 additions & 4 deletions dffml/model/slr.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def best_fit_line(x, y):
class SLRModelConfig:
predict: Feature = field("Label or the value to be predicted")
features: Features = field("Features to train on. For SLR only 1 allowed")
directory: pathlib.Path = field("Directory where state should be saved")
location: pathlib.Path = field("Location where state should be saved")


@entrypoint("slr")
Expand Down Expand Up @@ -82,7 +82,7 @@ class SLRModel(SimpleModel):
-model slr \
-model-features f1:float:1 \
-model-predict ans:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename dataset.csv

Expand All @@ -95,7 +95,7 @@ class SLRModel(SimpleModel):
-model slr \
-model-features f1:float:1 \
-model-predict ans:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename dataset.csv
1.0
Expand All @@ -118,7 +118,7 @@ class SLRModel(SimpleModel):
-model slr \
-model-features f1:float:1 \
-model-predict ans:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename predict.csv
[
Expand Down
2 changes: 1 addition & 1 deletion dffml/operation/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def model_predict(self, features: Dict[str, Any]) -> Dict[str, Any]:
>>> slr_model = SLRModel(
... features=Features(Feature("Years", int, 1)),
... predict=Feature("Salary", int, 1),
... directory="tempdir",
... location="tempdir",
... )
>>> dataflow = DataFlow(
... operations={
Expand Down
6 changes: 3 additions & 3 deletions dffml/skel/model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sed -i 's/.*setosa,versicolor,virginica/SepalLength,SepalWidth,PetalLength,Petal
head iris_training.csv
dffml train \
-model model_name \
-model-directory tempdir \
-model-location tempdir \
-sources csv=iris_training.csv \
-classifications 0 1 2 \
-model-features \
Expand All @@ -31,7 +31,7 @@ dffml train \
-log debug
dffml accuracy \
-model model_name \
-model-directory tempdir \
-model-location tempdir \
-sources csv=iris_training.csv \
-classifications 0 1 2 \
-model-features \
Expand All @@ -42,7 +42,7 @@ dffml accuracy \
-log critical
dffml predict all \
-model model_name \
-model-directory tempdir \
-model-location tempdir \
-sources csv=iris_test.csv \
-classifications 0 1 2 \
-model-features \
Expand Down
8 changes: 4 additions & 4 deletions dffml/skel/model/REPLACE_IMPORT_PACKAGE_NAME/myslr.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MySLRModelConfig:
"Features to train on (myslr only supports one)"
)
predict: Feature = field("Label or the value to be predicted")
directory: pathlib.Path = field("Directory where state should be saved")
location: pathlib.Path = field("Location where state should be saved")


@entrypoint("myslr")
Expand Down Expand Up @@ -105,7 +105,7 @@ class MySLRModel(SimpleModel):
-model myslr \
-model-features x:float:1 \
-model-predict y:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename train.csv

Expand All @@ -118,7 +118,7 @@ class MySLRModel(SimpleModel):
-model myslr \
-model-features x:float:1 \
-model-predict y:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename test.csv
1.0
Expand All @@ -141,7 +141,7 @@ class MySLRModel(SimpleModel):
-model myslr \
-model-features x:float:1 \
-model-predict y:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename predict.csv
[
Expand Down
2 changes: 1 addition & 1 deletion dffml/skel/model/examples/example_myslr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
model = MySLRModel(
features=Features(Feature("x", float, 1)),
predict=Feature("y", int, 1),
directory="tempdir",
location="tempdir",
)

# Train the model
Expand Down
2 changes: 1 addition & 1 deletion dffml/skel/model/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def setUpClass(cls):
cls.model = MySLRModel(
features=Features(Feature("X", float, 1)),
predict=Feature("Y", float, 1),
directory=cls.model_dir.name,
location=cls.model_dir.name,
)

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/icecream_sales.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ and the output being sales.
-model-hidden 9 18 9 \
-model-features population:int:1 temperature:float:1 \
-model-predict sales:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename preprocessed.csv \
-log debug
Expand All @@ -163,7 +163,7 @@ using the accuracy method.
-model-hidden 9 18 9 \
-model-features population:int:1 temperature:float:1 \
-model-predict sales:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources f=csv \
-source-filename preprocessed.csv \
-log debug
Expand All @@ -188,7 +188,7 @@ for the prediction of sales.
-model-hidden 9 18 9 \
-model-features population:int:1 temperature:float:1 \
-model-predict sales:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources preprocess=df \
-source-preprocess-dataflow preprocess_ops.json \
-source-preprocess-features city:str:1 state:str:1 month:int:1 \
Expand Down
12 changes: 6 additions & 6 deletions docs/tutorials/dataflows/nlp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ We can now use this dataflow to preprocess the data and make it ready to be fed
-model-clstype int \
-model-predict sentiment:int:1 \
-model-classifications 0 1 \
-model-directory tempdir \
-model-location tempdir \
-model-features embedding:float:[1,10,96] \
-sources text=df \
-source-text-dataflow nlp_ops_dataflow.json \
Expand All @@ -132,7 +132,7 @@ Assess accuracy:
-model-clstype int \
-model-predict sentiment:int:1 \
-model-classifications 0 1 \
-model-directory tempdir \
-model-location tempdir \
-model-features embedding:float:[1,10,96] \
-sources text=df \
-source-text-dataflow nlp_ops_dataflow.json \
Expand Down Expand Up @@ -165,7 +165,7 @@ Make prediction on test data:
-model-clstype int \
-model-predict sentiment:int:1 \
-model-classifications 0 1 \
-model-directory tempdir \
-model-location tempdir \
-model-features embedding:float:[1,10,96] \
-sources text=df \
-source-text-dataflow nlp_ops_dataflow.json \
Expand Down Expand Up @@ -273,7 +273,7 @@ We can now use this dataflow to preprocess the data and make it ready to be fed
-model scikitgnb \
-model-features extract_array_from_matrix.outputs.result:float:1 \
-model-predict sentiment:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources text=df \
-source-text-dataflow nlp_ops_dataflow.json \
-source-text-features sentence:str:1 \
Expand All @@ -290,7 +290,7 @@ Assess accuracy:
-model scikitgnb \
-model-features extract_array_from_matrix.outputs.result:float:1 \
-model-predict sentiment:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources text=df \
-source-text-dataflow nlp_ops_dataflow.json \
-source-text-features sentence:str:1 \
Expand Down Expand Up @@ -348,7 +348,7 @@ Now we can make prediction on test data:
-model scikitgnb \
-model-features extract_array_from_matrix.outputs.result:float:1 \
-model-predict sentiment:int:1 \
-model-directory tempdir \
-model-location tempdir \
-sources temp=json \
-source-temp-filename test_data_preprocessed.json \
-pretty
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/models/load.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ dataclass using the :py:func:`dataclasses.fields` function.
<class 'dffml.model.slr.SLRModel'>
predict: Label or the value to be predicted
features: Features to train on. For SLR only 1 allowed
directory: Directory where state should be saved
location: Location where state should be saved
2 changes: 1 addition & 1 deletion docs/tutorials/models/package.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ reference it by it's short name.
-model myslr \
-model-features Years:int:1 \
-model-predict Salary:float:1 \
-model-directory modeldir \
-model-location modeldir \
-sources f=csv \
-source-filename train.csv

Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/models/slr.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ We do the same steps we did with Python, only using the command line interface.
-model myslr:MySLRModel \
-model-features Years:int:1 \
-model-predict Salary:float:1 \
-model-directory modeldir \
-model-location modeldir \
-sources f=csv \
-source-filename train.csv

Expand All @@ -260,7 +260,7 @@ Now let's make predictions
-model myslr:MySLRModel \
-model-features Years:int:1 \
-model-predict Salary:float:1 \
-model-directory modeldir \
-model-location modeldir \
-sources f=csv \
-source-filename predict.csv
[
Expand Down Expand Up @@ -311,7 +311,7 @@ via the HTTP :ref:`plugin_service_http_api_model` API.
-models mymodel=myslr:MySLRModel \
-model-features Years:int:1 \
-model-predict Salary:float:1 \
-model-directory modeldir
-model-location modeldir

We can then ask the HTTP service to make predictions, or do training or accuracy
assessment.
Expand Down
Loading