-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from h1st-ai/aht-graph-hello-world
Add HelloWorld graph example
- Loading branch information
Showing
8 changed files
with
127 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,24 @@ | ||
Best way to quickly see this in action is to run the test suite: | ||
This is a super quick guide to using H1st! | ||
|
||
You can & should examine each file and run each example individually: | ||
|
||
``` | ||
% nose2 | ||
python3 rule_based_model.py | ||
python3 ml_model.py | ||
python3 helloworld_graph.py | ||
``` | ||
|
||
Some of the examples may require that you install various standard frameworks, e.g., `scikitlearn` | ||
To quickly see everything in action at once, run the test suite: | ||
|
||
``` | ||
% nose2 | ||
``` | ||
|
||
Some of the examples may require that you install various standard frameworks, e.g., `scikitlearn`. | ||
|
||
``` | ||
% pip3 install -U scikit-learn | ||
% pip3 install -U scikit-learn nose2 | ||
``` | ||
|
||
For a more complex example that demonstrate the power of H1st graph in a real-world use case, | ||
see the [AutoCyber tutorial](../AutoCyber) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
This is an example of a very simple graph which prints hello for each even number x in the input stream, | ||
using a conditional RuleBasedModel node and a HelloPrinter h1.Action. | ||
""" | ||
|
||
import h1st as h1 | ||
from rule_based_model import RuleBasedModel | ||
|
||
class HelloPrinter(h1.Action): | ||
"""Print hello to the inputs value""" | ||
def call(self, command, inputs): | ||
# Note that H1st does the conditional/filtering orchestration already. | ||
# All we need to do here is just to print. | ||
for d in inputs["predictions"]: | ||
print("Hello world {}!".format(d["value"])) | ||
|
||
|
||
def create_graph(): | ||
"""Create a graph which prints hello for each even number x in the input stream, | ||
using a conditional RuleBasedModel node and a HelloPrinter h1.Action.""" | ||
graph = h1.Graph() | ||
graph.start()\ | ||
.add(h1.Decision(RuleBasedModel(), result_field="predictions"))\ | ||
.add(yes=HelloPrinter(), no=h1.NoOp()) | ||
graph.end() | ||
return graph | ||
|
||
if __name__ == "__main__": | ||
graph = create_graph() | ||
results = graph.predict({"values": range(6)}) | ||
# Should get: | ||
# Hello world 0! | ||
# Hello world 2! | ||
# Hello world 4! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,39 @@ | ||
import unittest | ||
import h1st as h1 | ||
|
||
from HelloWorld.rule_based_model import RuleBasedModel | ||
from HelloWorld.ml_model import MLModel | ||
from HelloWorld.helloworld_graph import create_graph | ||
|
||
|
||
class TestHellloWorld(unittest.TestCase): | ||
|
||
def test_rule_based_model(self): | ||
m = RuleBasedModel() | ||
for value in range(6): | ||
prediction = m.predict({"value": value}) | ||
print("Prediction for " + str(value) + " is " + str(m.predict({"value": value}))) | ||
self.assertTrue(prediction["result"] == (value % 2 == 0)) | ||
|
||
xs = list(range(6)) | ||
results = m.predict({"values": xs}) | ||
print(f"RuleBasedModel's predictions for {xs} is {results}") | ||
self.assertTrue(results["predictions"] == [ | ||
{'prediction': True, 'value': 0}, {'prediction': False, 'value': 1}, {'prediction': True, 'value': 2}, {'prediction': False, 'value': 3}, {'prediction': True, 'value': 4}, {'prediction': False, 'value': 5}]) | ||
|
||
def test_ml_model(self): | ||
h1.init(MODEL_REPO_PATH=".models") | ||
m = MLModel() | ||
raw_data = m.get_data() | ||
prepared_data = m.prep(raw_data) | ||
|
||
m.train(prepared_data) | ||
metric = m.evaluate(prepared_data) | ||
print("metric = ", str(metric)) | ||
self.assertGreaterEqual(metric, 0.9) | ||
|
||
version_id = m.persist() | ||
print("Persisted to version_id = %s" % version_id) | ||
m = MLModel().load(version_id) | ||
self.assertGreaterEqual(m.metrics, 0.9) | ||
|
||
def test_graph(self): | ||
graph = create_graph() | ||
results = graph.predict({"values": range(6)}) | ||
print(results) | ||
self.assertEqual(results, {'predictions': [{'prediction': True, 'value': 0}, {'prediction': False, 'value': 1}, {'prediction': True, 'value': 2}, {'prediction': False, 'value': 3}, {'prediction': True, 'value': 4}, {'prediction': False, 'value': 5}]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters