-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature extraction - wrapper around schema.apply_udf (#198)
* extract feature * raise invalid data type error --------- Co-authored-by: felipe207 <felipe@whylabs.ai> Co-authored-by: Jamie Broomall <88007022+jamie256@users.noreply.github.com>
- Loading branch information
1 parent
80a7ca7
commit 23497fa
Showing
4 changed files
with
76 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pandas as pd | ||
from typing import Any, Dict, Optional, Union | ||
from whylogs.experimental.core.udf_schema import udf_schema, UdfSchema | ||
|
||
|
||
def extract( | ||
data: Union[pd.DataFrame, Dict[str, Any]], | ||
schema: Optional[UdfSchema] = None, | ||
): | ||
if schema is None: | ||
schema = udf_schema() | ||
if isinstance(data, pd.DataFrame): | ||
df_enhanced, _ = schema.apply_udfs(pandas=data) | ||
return df_enhanced | ||
elif isinstance(data, dict): | ||
_, row_enhanced = schema.apply_udfs(row=data) | ||
return row_enhanced | ||
raise ValueError( | ||
f"Extract: data of type {type(data)} is invalid: supported input types are pandas dataframe or dictionary" | ||
) |
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,48 @@ | ||
import langkit | ||
import pandas as pd | ||
from whylogs.experimental.core.udf_schema import UdfSchema, UdfSpec | ||
|
||
|
||
def test_extract_pandas(): | ||
from langkit import textstat | ||
|
||
textstat.init() | ||
df = pd.DataFrame({"prompt": ["I love you", "I hate you"]}) | ||
enhanced_df = langkit.extract(data=df) | ||
assert "prompt.flesch_reading_ease" in enhanced_df.columns | ||
|
||
|
||
def test_extract_row(): | ||
from langkit import regexes | ||
|
||
regexes.init() | ||
row = {"prompt": "I love you", "response": "address: 123 Main St."} | ||
enhanced_row = langkit.extract(data=row) | ||
assert enhanced_row.get("response.has_patterns") == "mailing address" | ||
assert not enhanced_row.get("prompt.has_patterns") | ||
|
||
|
||
def test_extract_light_metrics(): | ||
from langkit import light_metrics | ||
|
||
light_metrics.init() | ||
|
||
row = {"prompt": "I love you", "response": "address: 123 Main St."} | ||
enhanced_row = langkit.extract(row) | ||
assert enhanced_row.get("response.has_patterns") == "mailing address" | ||
assert not enhanced_row.get("prompt.has_patterns") | ||
assert "prompt.flesch_reading_ease" in enhanced_row.keys() | ||
|
||
|
||
def test_extract_with_custom_schema(): | ||
schema = UdfSchema( | ||
udf_specs=[ | ||
UdfSpec( | ||
column_names=["prompt"], | ||
udfs={"prompt.customfeature": lambda x: x["prompt"]}, | ||
) | ||
], | ||
) | ||
row = {"prompt": "I love you", "response": "address: 123 Main St."} | ||
enhanced_row = langkit.extract(row, schema=schema) | ||
assert enhanced_row.get("prompt.customfeature") == "I love you" |