def encode_dimension( dimension: Dimension) -> OptimizerService_pb2.Dimension: if isinstance(dimension, EmptyDimension): return OptimizerService_pb2.Dimension( EmptyDimension=OptimizerServiceEncoder.encode_empty_dimension( dimension)) if isinstance(dimension, ContinuousDimension): return OptimizerService_pb2.Dimension( ContinuousDimension=OptimizerServiceEncoder. encode_continuous_dimension(dimension)) if isinstance(dimension, DiscreteDimension): return OptimizerService_pb2.Dimension( DiscreteDimension=OptimizerServiceEncoder. encode_discrete_dimension(dimension)) if isinstance(dimension, OrdinalDimension): return OptimizerService_pb2.Dimension( OrdinalDimension=OptimizerServiceEncoder. encode_ordinal_dimension(dimension)) if isinstance(dimension, CategoricalDimension): return OptimizerService_pb2.Dimension( CategoricalDimension=OptimizerServiceEncoder. encode_categorical_dimension(dimension)) if isinstance(dimension, CompositeDimension): return OptimizerService_pb2.Dimension( CompositeDimension=OptimizerServiceEncoder. encode_composite_dimension(dimension)) raise TypeError(f"Unsupported dimension type: {type(dimension)}")
def encode_composite_dimension(dimension: CompositeDimension) -> OptimizerService_pb2.CompositeDimension: assert isinstance(dimension, CompositeDimension) encoded_chunks = [] for chunk in dimension.enumerate_chunks(): if dimension.chunks_type is ContinuousDimension: encoded_chunks.append(OptimizerService_pb2.Dimension(ContinuousDimension=OptimizerServiceEncoder.encode_continuous_dimension(chunk))) elif dimension.chunks_type is DiscreteDimension: encoded_chunks.append(OptimizerService_pb2.Dimension(DiscreteDimension=OptimizerServiceEncoder.encode_discrete_dimension(chunk))) elif dimension.chunks_type is OrdinalDimension: encoded_chunks.append(OptimizerService_pb2.Dimension(OrdinalDimension=OptimizerServiceEncoder.encode_ordinal_dimension(chunk))) elif dimension.chunks_type is CategoricalDimension: encoded_chunks.append( OptimizerService_pb2.Dimension(CategoricalDimension=OptimizerServiceEncoder.encode_categorical_dimension(chunk)) ) else: raise TypeError(f"Unsupported chunk type: {dimension.chunks_type.__name__}") return OptimizerService_pb2.CompositeDimension( Name=dimension.name, ChunkType=OptimizerServiceEncoder.dimension_types_to_pb2_types[dimension.chunks_type], Chunks=encoded_chunks )