Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data io for generic PandasTransformComponent #721

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/fondant/component/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,11 @@ def optional_fondant_arguments() -> t.List[str]:
return ["input_manifest_path", "input_partition_rows"]

@staticmethod
def wrap_transform(transform: t.Callable, *, spec: ComponentSpec) -> t.Callable:
def wrap_transform(
transform: t.Callable,
*,
operation_spec: OperationSpec,
) -> t.Callable:
"""Factory that creates a function to wrap the component transform function. The wrapper:
- Removes extra columns from the returned dataframe which are not defined in the component
spec `produces` section
Expand All @@ -524,15 +528,15 @@ def wrap_transform(transform: t.Callable, *, spec: ComponentSpec) -> t.Callable:

Args:
transform: Transform method to wrap
spec: Component specification to base behavior on
operation_spec: Operation specification to base behavior on
"""

def wrapped_transform(dataframe: pd.DataFrame) -> pd.DataFrame:
# Call transform method
dataframe = transform(dataframe)

# Drop columns not in specification
columns = [name for name, field in spec.produces.items()]
columns = [name for name, field in operation_spec.inner_produces.items()]

return dataframe[columns]

Expand Down Expand Up @@ -560,11 +564,14 @@ def _execute_component(

# Create meta dataframe with expected format
meta_dict = {"id": pd.Series(dtype="object")}
for field_name, field in self.spec.produces.items():
for field_name, field in self.operation_spec.inner_produces.items():
meta_dict[field_name] = pd.Series(dtype=pd.ArrowDtype(field.type.value))
meta_df = pd.DataFrame(meta_dict).set_index("id")

wrapped_transform = self.wrap_transform(component.transform, spec=self.spec)
wrapped_transform = self.wrap_transform(
component.transform,
operation_spec=self.operation_spec,
)

# Call the component transform method for each partition
dataframe = dataframe.map_partitions(
Expand Down
13 changes: 11 additions & 2 deletions tests/component/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ExecutorFactory,
PandasTransformExecutor,
)
from fondant.core.component_spec import ComponentSpec
from fondant.core.component_spec import ComponentSpec, OperationSpec
from fondant.core.manifest import Manifest, Metadata
from fondant.pipeline import ComponentOp

Expand Down Expand Up @@ -403,6 +403,7 @@ def test_wrap_transform():
},
},
"produces": {
"additionalProperties": True,
"caption_text": {
"type": "string",
},
Expand Down Expand Up @@ -430,7 +431,15 @@ def transform(dataframe: pd.DataFrame) -> pd.DataFrame:
]
return dataframe

wrapped_transform = PandasTransformExecutor.wrap_transform(transform, spec=spec)
overwrite_produces = {
"caption_text": pa.string(),
"image_height": pa.int16(),
}

wrapped_transform = PandasTransformExecutor.wrap_transform(
transform,
operation_spec=OperationSpec(spec, produces=overwrite_produces),
)
output_df = wrapped_transform(input_df)

# Check column flattening, trimming, and ordering
Expand Down
Loading