Exemplo n.º 1
0
 def add_boolean_proxy_for(self, label: str, shape: Collection[int] = None) -> Vertex:
     """
     Creates a proxy vertex for the given label and adds to the sequence item
     """
     if shape is None:
         return Vertex._from_java_vertex(self.unwrap().addBooleanProxyFor(_VertexLabel(label).unwrap()))
     else:
         return Vertex._from_java_vertex(self.unwrap().addBooleanProxyFor(_VertexLabel(label).unwrap(), shape))
Exemplo n.º 2
0
 def proxy_label_for(label: str) -> str:
     """
     >>> Sequence.proxy_label_for("foo")
     'proxy_for.foo'
     """
     label_java = _VertexLabel(label).unwrap()
     proxy_label_java = k.jvm_view().SequenceBuilder.proxyLabelFor(label_java)
     return proxy_label_java.getQualifiedName()
Exemplo n.º 3
0
def __check_output_equals(sequence: Sequence, label: str,
                          desired_output: float) -> None:
    sequence_unwrapped = sequence.unwrap()
    x1_result = sequence_unwrapped.getLastItem().get(
        _VertexLabel(label).unwrap())
    assert len(x1_result.getShape()) == 0

    print(x1_result.getValue())
    print(x1_result.getValue().scalar())
    assert x1_result.getValue().scalar() == desired_output
Exemplo n.º 4
0
    def __init__(self,
                 factories: Union[Callable[..., None], Iterable[Callable[..., None]]] = None,
                 count: int = None,
                 name: str = None,
                 data_generator: Iterator[Dict[str, Any]] = None,
                 initial_state: Dict[str, vertex_constructor_param_types] = None):

        builder = k.jvm_view().SequenceBuilder().named(name)

        if initial_state is not None:
            initial_state_java = k.to_java_map(
                {_VertexLabel(k): cast_to_double_vertex(v).unwrap() for (k, v) in initial_state.items()})
            vertex_dictionary = k.jvm_view().SimpleVertexDictionary.backedBy(initial_state_java)
            builder = builder.withInitialState(vertex_dictionary)

        if count is None and data_generator is None:
            raise ValueError(
                "Cannot create a sequence of an unknown size: you must specify either a count of a data_generator")
        elif count is not None and data_generator is not None:
            raise ValueError("If you pass in a data_generator you cannot also pass in a count")
        elif factories is None:
            raise ValueError("You must provide a value for the 'factories' input")

        if not isinstance(factories, CollectionsIterable):
            factories = [factories]

        if count is not None:
            functions = [Consumer(partial(lambda f, p: f(SequenceItem(p)), f)) for f in factories]
            java_functions = ListConverter().convert(functions, k._gateway._gateway_client)
            builder = builder.count(count).withFactories(java_functions)

        if data_generator is not None:
            bifunctions = [BiConsumer(partial(lambda f, p, data: f(SequenceItem(p), data), f)) for f in factories]
            java_bifunctions = ListConverter().convert(bifunctions, k._gateway._gateway_client)
            data_generator_java = (k.to_java_map(m) for m in data_generator)
            builder = builder.fromIterator(JavaIterator(data_generator_java)).withFactories(java_bifunctions)

        sequence = builder.build()
        super().__init__(sequence)
Exemplo n.º 5
0
 def get_double_model_output_vertex(self, label: str) -> Vertex:
     label_unwrapped = _VertexLabel(label).unwrap()
     result = self.unwrap().getDoubleModelOutputVertex(label_unwrapped)
     return Vertex._from_java_vertex(result)
Exemplo n.º 6
0
 def __update_unwrapped_vertices(vertices_wrapped: Dict[str, Vertex],
                                 vertices_unwrapped: JavaMap) -> None:
     for k, v in vertices_wrapped.items():
         vertices_unwrapped[_VertexLabel(k).unwrap()] = v.unwrap()
Exemplo n.º 7
0
 def __to_java_map(inputs: Dict[str, Vertex]) -> JavaMap:
     inputs_with_wrapped_keys = {
         _VertexLabel(k): v
         for k, v in inputs.items()
     }
     return context.to_java_map(inputs_with_wrapped_keys)
Exemplo n.º 8
0
def IntegerProxy(shape: Collection[int], label: str) -> Vertex:
    return Integer(context.jvm_view().IntegerProxyVertex, label, cast_to_long_array(shape), _VertexLabel(label))
Exemplo n.º 9
0
def DoubleProxy(shape: Collection[int], label: str) -> Vertex:
    return Double(context.jvm_view().DoubleProxyVertex, label, cast_to_long_array(shape), _VertexLabel(label))
Exemplo n.º 10
0
def BooleanProxy(shape: Collection[int], label: str) -> Vertex:
    return Boolean(context.jvm_view().BooleanProxyVertex, label, cast_to_long_array(shape), _VertexLabel(label))
Exemplo n.º 11
0
 def get(self, label: str) -> Vertex:
     return Vertex._from_java_vertex(self.unwrap().get(_VertexLabel(label).unwrap()))
Exemplo n.º 12
0
 def add(self, vertex: Vertex, label: Optional[str] = None) -> None:
     if label is None:
         self.unwrap().add(vertex.unwrap())
     else:
         self.unwrap().add(_VertexLabel(label).unwrap(), vertex.unwrap())
Exemplo n.º 13
0
 def set_label(self, label: Optional[str]) -> None:
     if label is None:
         raise ValueError("label cannot be None.")
     self.unwrap().setLabel(_VertexLabel(label).unwrap())