-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IsValidPython descriptor (#1351)
* Add feature and descriptor for IsValidPython * Add tests for IsValidPython descriptor feature * Update documentation to include IsValidPython descriptor --------- Co-authored-by: Emeli Dral <[email protected]>
- Loading branch information
1 parent
08d1502
commit 421630f
Showing
7 changed files
with
106 additions
and
0 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
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,11 @@ | ||
from evidently.features import is_valid_python_feature | ||
from evidently.features.generated_features import FeatureDescriptor | ||
from evidently.features.generated_features import GeneratedFeature | ||
|
||
|
||
class IsValidPython(FeatureDescriptor): | ||
class Config: | ||
type_alias = "evidently:descriptor:IsValidPython" | ||
|
||
def feature(self, column_name: str) -> GeneratedFeature: | ||
return is_valid_python_feature.IsValidPython(column_name, self.display_name) |
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,28 @@ | ||
import ast | ||
from typing import Any | ||
from typing import ClassVar | ||
from typing import Optional | ||
|
||
from evidently import ColumnType | ||
from evidently.features.generated_features import ApplyColumnGeneratedFeature | ||
|
||
|
||
class IsValidPython(ApplyColumnGeneratedFeature): | ||
class Config: | ||
type_alias = "evidently:feature:IsValidPython" | ||
|
||
__feature_type__: ClassVar = ColumnType.Categorical | ||
display_name_template: ClassVar = "Valid Python for {column_name}" | ||
column_name: str | ||
|
||
def __init__(self, column_name: str, display_name: Optional[str] = None): | ||
self.column_name = column_name | ||
self.display_name = display_name | ||
super().__init__() | ||
|
||
def apply(self, value: Any) -> bool: | ||
try: | ||
ast.parse(value) | ||
return True | ||
except (SyntaxError, TypeError): | ||
return False |
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,54 @@ | ||
from typing import Any | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from evidently.features.is_valid_python_feature import IsValidPython | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("column_value", "expected"), | ||
[ | ||
("print('Hello')", True), | ||
("x = 5 + 3", True), | ||
("def foo():\n return 'bar'", True), | ||
("for i in range(10): print(i)", True), | ||
("print('Hello'", False), | ||
("for i in range(5) print(i)", False), | ||
("def foo(\n return 'bar'", False), | ||
("if True print('yes')", False), | ||
(None, False), | ||
("12", True), | ||
("Sorry I can't answer this", False), | ||
("{'name': 'test', 'age': 13}", True), | ||
], | ||
) | ||
def test_is_valid_python_apply(column_value: Any, expected: bool): | ||
is_python = IsValidPython("TestColumnName") | ||
actual = is_python.apply(column_value) | ||
assert actual == expected | ||
|
||
|
||
test_data = pd.DataFrame( | ||
{ | ||
"TestColumnName": [ | ||
"print('Hello')", | ||
"def foo():\n return 'bar'", | ||
"def foo(\n return 'bar'", | ||
None, | ||
"{'name': 'test', 'age': 13}", | ||
np.nan, | ||
] | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("expected"), | ||
[[True, True, False, False, True, False]], | ||
) | ||
def test_is_valid_python(expected: bool): | ||
is_python = IsValidPython("TestColumnName") | ||
actual = is_python.generate_feature(test_data, None) | ||
assert actual[is_python._feature_column_name()].tolist() == expected |