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

Commit

Permalink
Merge pull request #54 from georgianpartners/issue_53_concrete_serial…
Browse files Browse the repository at this point in the history
…ization

Revise smart transformer serialization (#53)
  • Loading branch information
alexrallen committed May 17, 2019
2 parents 061c43a + 3cb9a76 commit c7db7f4
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 422 deletions.
1 change: 1 addition & 0 deletions foreshadow/optimizers/param_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def _param_mapping(pipeline, X_df, y_df):
# One parameter is from_json (the configuration dict we calculated)
# Other parameters are pulled from config_dict using
# extract_config_params()

explicit_params = {
"{}__from_json".format(k): [param[k].serialize()]
for k, v in task.items()
Expand Down
64 changes: 54 additions & 10 deletions foreshadow/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from foreshadow.intents import GenericIntent
from foreshadow.intents.registry import registry_eval
from foreshadow.transformers.base import ParallelProcessor
from foreshadow.transformers.base import ParallelProcessor, SmartTransformer
from foreshadow.utils import PipelineStep, check_df


Expand Down Expand Up @@ -397,7 +397,10 @@ def serialize(self):

# Serialize intent multi processors
json_intents = {
k: {l: serialize_pipeline(j) for l, j in v.items()}
k: {
l: serialize_pipeline(j, include_smart=True)
for l, j in v.items()
}
for k, v in self._intent_pipelines.items()
}

Expand Down Expand Up @@ -485,7 +488,7 @@ def inverse_transform(self, X):
return self.pipeline.inverse_transform(X)


def serialize_pipeline(pipeline):
def serialize_pipeline(pipeline, include_smart=False):
"""Serializes sklearn Pipeline object into JSON object for reconstruction.
Args:
Expand All @@ -495,14 +498,55 @@ def serialize_pipeline(pipeline):
Returns:
list: JSON serializable object of form ``[cls, name, {**params}]``
"""
return [
{
"transformer": type(step[PipelineStep["CLASS"]]).__name__,
"name": step[PipelineStep["NAME"]],
"parameters": step[PipelineStep["CLASS"]].get_params(deep=False),

def ser_params(trans):
from foreshadow.transformers.externals import no_serialize_params

bad_params = [
"name",
*no_serialize_params.get(type(trans).__name__, []),
]
return {
k: v
for k, v in trans.get_params(deep=False).items()
if k not in bad_params
}
for step in pipeline.steps
if pipeline.steps[0][PipelineStep["NAME"]] != "null"

return [
p
for s in [
(
[
{
"transformer": type(
step[PipelineStep["CLASS"]].transformer
).__name__,
"name": step[PipelineStep["NAME"]],
"parameters": ser_params(
step[PipelineStep["CLASS"]].transformer
),
}
]
if not isinstance(
step[PipelineStep["CLASS"]].transformer, Pipeline
)
else serialize_pipeline(
step[PipelineStep["CLASS"]].transformer
)
)
if isinstance(step[PipelineStep["CLASS"]], SmartTransformer)
and not include_smart
else [
{
"transformer": type(step[PipelineStep["CLASS"]]).__name__,
"name": step[PipelineStep["NAME"]],
"parameters": ser_params(step[PipelineStep["CLASS"]]),
}
]
for step in pipeline.steps
if pipeline.steps[0][PipelineStep["NAME"]] != "null"
]
for p in s
]


Expand Down

0 comments on commit c7db7f4

Please sign in to comment.