-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
20 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
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,85 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from whylogs.app.config import load_config | ||
from whylogs.app.session import session_from_config | ||
from whylogs.core.statistics.constraints import ( | ||
DatasetConstraints, | ||
Op, | ||
SummaryConstraint, | ||
ValueConstraint, | ||
_summary_funcs1, | ||
_value_funcs, | ||
) | ||
from whylogs.proto import Op | ||
from whylogs.util.protobuf import message_to_json | ||
|
||
|
||
def test_value_summary_serialization(): | ||
|
||
for each_op, _ in _value_funcs.items(): | ||
|
||
value = ValueConstraint(each_op, 3.6) | ||
msg_value = value.to_protobuf() | ||
json_value = json.loads(message_to_json(msg_value)) | ||
assert json_value["name"] == "value " + Op.Name(each_op) + " 3.6" | ||
assert pytest.approx(json_value["value"], 0.001) == 3.6 | ||
assert json_value["op"] == Op.Name(each_op) | ||
assert json_value["verbose"] == False | ||
|
||
for each_op, _ in _summary_funcs1.items(): | ||
# constraints may have an optional name | ||
sum_constraint = SummaryConstraint("min", each_op, 300000, name="< 30K") | ||
msg_sum_const = sum_constraint.to_protobuf() | ||
json_summary = json.loads(message_to_json(msg_sum_const)) | ||
|
||
assert json_summary["name"] == "< 30K" | ||
assert pytest.approx(json_summary["value"], 0.1) == 300000 | ||
assert json_summary["firstField"] == "min" | ||
assert json_summary["op"] == str(Op.Name(each_op)) | ||
assert json_summary["verbose"] == False | ||
|
||
|
||
def test_value_constraints(df_lending_club, local_config_path): | ||
|
||
conforming_loan = ValueConstraint(Op.LT, 548250) | ||
smallest_loan = ValueConstraint(Op.GT, 2500.0, verbose=True) | ||
|
||
high_fico = ValueConstraint(Op.GT, 4000) | ||
|
||
dc = DatasetConstraints(None, value_constraints={"loan_amnt": [conforming_loan, smallest_loan], "fico_range_high": [high_fico]}) | ||
|
||
config = load_config(local_config_path) | ||
session = session_from_config(config) | ||
|
||
profile = session.log_dataframe(df_lending_club, "test.data", constraints=dc) | ||
session.close() | ||
report = dc.report() | ||
|
||
assert len(report) == 2 | ||
print(report) | ||
# make sure it checked every value | ||
for each_feat in report: | ||
for each_constraint in each_feat[1]: | ||
assert each_constraint[1] == 50 | ||
|
||
assert report[1][1][0][2] == 50 | ||
|
||
|
||
def test_summary_constraints(df_lending_club, local_config_path): | ||
|
||
non_negative = SummaryConstraint("min", Op.GE, 0) | ||
|
||
dc = DatasetConstraints(None, summary_constraints={"annual_inc": [non_negative]}) | ||
config = load_config(local_config_path) | ||
session = session_from_config(config) | ||
profile = session.log_dataframe(df_lending_club, "test.data", constraints=dc) | ||
session.close() | ||
report = r = profile.apply_summary_constraints() | ||
|
||
assert len(report) == 1 | ||
# make sure it checked every value | ||
for each_feat in report: | ||
for each_constraint in each_feat[1]: | ||
assert each_constraint[1] == 1 |
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,24 @@ | ||
import datetime | ||
import os | ||
|
||
import numpy as np | ||
|
||
from whylogs.app.config import load_config | ||
from whylogs.app.session import session_from_config | ||
from whylogs.viz import profile_viewer | ||
|
||
|
||
def test_profile_viewer(tmpdir, local_config_path): | ||
|
||
config = load_config(local_config_path) | ||
session = session_from_config(config) | ||
|
||
with session.logger("mytestytest", dataset_timestamp=datetime.datetime(2021, 6, 2)) as logger: | ||
for _ in range(5): | ||
logger.log({"uniform_integers": np.random.randint(0, 50)}) | ||
logger.log({"nulls": None}) | ||
|
||
profile = logger.profile | ||
result = profile_viewer(profiles=[profile], output_path=tmpdir + "my_test.html") | ||
assert os.path.exists(tmpdir + "my_test.html") | ||
assert result == tmpdir + "my_test.html" |