Commit 49f4a7dc authored by Mitar's avatar Mitar
Browse files

Updated to DataFrame.

parent 615bff79
Pipeline #20492132 failed with stage
in 3 minutes and 16 seconds
......@@ -14,8 +14,8 @@ __all__ = ('IncrementPrimitive',)
# It is useful to define these names, so that you can reuse it both
# for class type arguments and method signatures.
Inputs = container.ndarray
Outputs = container.ndarray
Inputs = container.DataFrame
Outputs = container.DataFrame
class Hyperparams(hyperparams.Hyperparams):
......@@ -85,12 +85,11 @@ class IncrementPrimitive(transformer.TransformerPrimitiveBase[Inputs, Outputs, H
})
def produce(self, *, inputs: Inputs, timeout: float = None, iterations: int = None) -> base.CallResult[Outputs]:
# It "inputs" is container.ndarray, then also result is.
outputs = numpy.add(inputs, self.hyperparams['amount'])
# If "inputs" is container.DataFrame, then also result is.
outputs = inputs + self.hyperparams['amount']
# In the case of container.ndarray, operations automatically clear old metadata
# (but keep history and link to previous metadata). We update with required metadata.
# "source" tells which primitive generated this metadata.
# In the case of container.DataFrame, operations metadata is not preserved.
# We update with required metadata. "source" tells which primitive generated this metadata.
outputs.metadata = outputs.metadata.update((), {
'schema': metadata_base.CONTAINER_SCHEMA_VERSION,
'structural_type': type(outputs),
......@@ -107,9 +106,15 @@ class IncrementPrimitive(transformer.TransformerPrimitiveBase[Inputs, Outputs, H
assert dimension_index is not None
outputs.metadata = outputs.metadata.update((metadata_base.ALL_ELEMENTS,) * (dimension_index + 1), {
'structural_type': outputs.dtype.type,
}, source=self)
if len(set(inputs.dtypes)) == 1:
outputs.metadata = outputs.metadata.update((metadata_base.ALL_ELEMENTS,) * (dimension_index + 1), {
'structural_type': inputs.dtypes[0].type,
}, source=self)
else:
for i, dtype in enumerate(outputs.dtypes):
outputs.metadata = outputs.metadata.update((metadata_base.ALL_ELEMENTS,) * dimension_index + (i,), {
'structural_type': dtype.type,
}, source=self)
# Wrap it into default "CallResult" object: we are not doing any iterations.
return base.CallResult(outputs)
......@@ -141,6 +146,7 @@ class IncrementPrimitive(transformer.TransformerPrimitiveBase[Inputs, Outputs, H
inputs_value_structural_type = metadata.get('structural_type', None)
if inputs_value_structural_type is None:
# TODO: Check if every element individually is a numeric type.
return None
# Not a perfect way to check for a numeric type but will do for this example.
......
import os.path
import typing
import numpy # type: ignore
......@@ -12,7 +11,7 @@ from . import __author__, __version__
__all__ = ('RandomPrimitive',)
Outputs = container.ndarray
Outputs = container.DataFrame
class Hyperparams(hyperparams.Hyperparams):
......@@ -88,8 +87,8 @@ class RandomPrimitive(generator.GeneratorPrimitiveBase[Outputs, None, Hyperparam
def produce(self, *, inputs: container.List[None], timeout: float = None, iterations: int = None) -> base.CallResult[Outputs]:
result = self._random_state.normal(self.hyperparams['mu'], self.hyperparams['sigma'], len(inputs))
# We convert a regular ndarray to a container ndarray which supports metadata attribute.
outputs = container.ndarray(result)
# We convert a regular ndarray to a container DataFrame which supports metadata attribute.
outputs = container.DataFrame(result)
# We clear old metadata (but which keeps history and link to inputs metadata) and set new metadata.
# "for_value" tells that this new metadata will be associated with "outputs",
......
......@@ -22,7 +22,7 @@ DOCKER_KEY = 'summing'
# which is in fact more restrictive than what the primitive can really handle.
# One could probably just use "typing.Container" in this case, if accepting
# a wide range of input types.
Inputs = typing.Union[container.ndarray, container.List[float], container.List[container.List[float]]]
Inputs = typing.Union[container.ndarray, container.DataFrame, container.List[float], container.List[container.List[float]]]
Outputs = container.List[float]
......@@ -74,7 +74,7 @@ class SumPrimitive(transformer.TransformerPrimitiveBase[Inputs, Outputs, Hyperpa
'image_name': 'registry.gitlab.com/datadrivendiscovery/tests-data/summing',
# Instead of a label, an exact hash of the image is required. This assures reproducibility.
# You can see digests using "docker images --digests".
'image_digest': 'sha256:07db5fef262c1172de5c1db5334944b2f58a679e4bb9ea6232234d71239deb64',
'image_digest': 'sha256:f75e21720e44cfa29d8a8e239b5746c715aa7cf99f9fde7916623fabc30d3364',
}],
# URIs at which one can obtain code for the primitive, if available.
'location_uris': [
......@@ -184,6 +184,7 @@ class SumPrimitive(transformer.TransformerPrimitiveBase[Inputs, Outputs, Hyperpa
inputs_value_structural_type = metadata.get('structural_type', None)
if inputs_value_structural_type is None:
# TODO: Check if every element individually is a numeric type.
return None
# Not a perfect way to check for a numeric type but will do for this example.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment