def test_num_passed_to_Tensor_creates_scalar_tensor( num: Union[primitive_types, numpy_types], expected_java_class: str) -> None: t = Tensor(num) assert_java_class(t, expected_java_class) assert t.is_scalar() assert t.scalar() == num
def test_dataframe_passed_to_Tensor_creates_tensor(data: List[List[primitive_types]]) -> None: dataframe = pd.DataFrame(columns=['A', 'B'], data=data) t = Tensor(dataframe) tensor_value = Tensor._to_ndarray(t.unwrap()) dataframe_value = dataframe.values assert np.array_equal(tensor_value, dataframe_value)
def test_series_passed_to_Tensor_creates_tensor(data: List[primitive_types]) -> None: series = pd.Series(data) t = Tensor(series) tensor_value = Tensor._to_ndarray(t.unwrap()) series_value = series.values assert len(tensor_value) == len(series_value) assert tensor_value.shape == (len(series_value),) assert series_value.shape == (len(series_value),) assert np.array_equal(tensor_value.flatten(), series_value.flatten())
def test_cannot_pass_generic_ndarray_to_Tensor(generic) -> None: with pytest.raises( NotImplementedError, match= r"Generic types in an ndarray are not supported. Was given object" ): Tensor(np.array([generic, generic]))
def _samples_generator( sample_iterator: JavaObject, vertices_unwrapped: JavaList, live_plot: bool, refresh_every: int, ax: Any, all_scalar: bool, id_to_label: Dict[Tuple[int, ...], str]) -> sample_generator_types: traces = [] x0 = 0 while (True): network_sample = sample_iterator.next() if all_scalar: sample: sample_generator_dict_type = { id_to_label[Vertex._get_python_id(vertex_unwrapped)]: Tensor._to_ndarray( network_sample.get(vertex_unwrapped)).item() for vertex_unwrapped in vertices_unwrapped } else: sample = __create_multi_indexed_samples_generated( vertices_unwrapped, network_sample, id_to_label) if live_plot: traces.append(sample) if len(traces) % refresh_every == 0: joined_trace: sample_types = { k: [t[k] for t in traces] for k in sample.keys() } if ax is None: ax = traceplot(joined_trace, x0=x0) else: traceplot(joined_trace, ax=ax, x0=x0) x0 += refresh_every traces = [] yield sample
def _samples_generator(sample_iterator: JavaObject, vertices_unwrapped: JavaList, live_plot: bool, refresh_every: int, ax: Any) -> sample_generator_types: traces = [] x0 = 0 while (True): network_sample = sample_iterator.next() sample = { Vertex._get_python_label(vertex_unwrapped): Tensor._to_ndarray(network_sample.get(vertex_unwrapped)) for vertex_unwrapped in vertices_unwrapped } if live_plot: traces.append(sample) if len(traces) % refresh_every == 0: joined_trace = { k: [t[k] for t in traces] for k in sample.keys() } if ax is None: ax = traceplot(joined_trace, x0=x0) else: traceplot(joined_trace, ax=ax, x0=x0) x0 += refresh_every traces = [] yield sample
def _samples_generator(sample_iterator: JavaObject, vertices_unwrapped: JavaList) -> sample_generator_types: while (True): network_sample = sample_iterator.next() sample = { Vertex._get_python_id(vertex_unwrapped): Tensor._to_ndarray(network_sample.get(vertex_unwrapped)) for vertex_unwrapped in vertices_unwrapped } yield sample
def __init__(self, type_: str, latents: List[Vertex] = None, sigma: Union[tensor_arg_types, List[tensor_arg_types]] = None, listeners: List[Any] = []) -> None: ctor = proposal_distribution_types[type_] args: List[Union[JavaMap, JavaList, JavaObject]] = [] if type_ == "gaussian": if latents is None or len(latents) <= 0: raise TypeError( "Gaussian Proposal Distribution requires values for latents" ) if isinstance(sigma, runtime_tensor_arg_types): sigma_as_tensor = Tensor(sigma).unwrap() args.append(k.to_java_object_list(latents)) args.append(sigma_as_tensor) elif isinstance( sigma, list) and len(sigma) == len(latents) and isinstance( sigma[0], runtime_tensor_arg_types): sigma_as_tensors = [Tensor(s) for s in sigma] args.append(k.to_java_map(dict(zip(latents, sigma_as_tensors)))) else: raise TypeError( "Gaussian Proposal Distribution requires a sigma or a list of sigmas for each latent" ) elif sigma is not None: raise TypeError( 'Parameter sigma is not valid unless type is "gaussian"') if type_ == "prior": if latents is None: raise TypeError( "Prior Proposal Distribution requires latent variables") if len(listeners) > 0: args.append(k.to_java_object_list(listeners)) super(ProposalDistribution, self).__init__(ctor(*args))
def Const(t: tensor_arg_types) -> Vertex: if isinstance(t, runtime_numpy_types): ctor = __infer_const_ctor_from_ndarray(t) val = t elif isinstance(t, runtime_pandas_types): val = t.values ctor = __infer_const_ctor_from_ndarray(val) elif isinstance(t, runtime_primitive_types): ctor = __infer_const_ctor_from_scalar(t) val = t else: raise NotImplementedError( "Argument t must be either an ndarray or an instance of numbers.Number. Was given {} instead" .format(type(t))) return ctor(Tensor(val))
def __create_multi_indexed_samples_generated( vertices_unwrapped: JavaList, network_samples: JavaObject, id_to_label: Dict[Tuple[int, ...], str]) -> sample_generator_dict_type: vertex_samples_multi: Dict = {} for vertex in vertices_unwrapped: vertex_label = id_to_label[Vertex._get_python_id(vertex)] vertex_samples_multi[vertex_label] = defaultdict(list) sample = Tensor._to_ndarray(network_samples.get(vertex)) __add_sample_to_dict(sample, vertex_samples_multi[vertex_label]) tuple_hierarchy: Dict = { (vertex_label, tensor_index): values for vertex_label, tensor_index in vertex_samples_multi.items() for tensor_index, values in tensor_index.items() } return tuple_hierarchy
def __init__(self, type_: str, sigma: numpy_types = None, listeners: List[Any] = []) -> None: ctor = proposal_distribution_types[type_] args = [] if type_ == "gaussian": if sigma is None: raise TypeError( "Gaussian Proposal Distribution requires a value for sigma" ) args.append(Tensor(sigma).unwrap()) else: if sigma is not None: raise TypeError( 'Parameter sigma is not valid unless type is "gaussian"') if len(listeners) > 0: args.append(k.to_java_object_list(listeners)) super(ProposalDistribution, self).__init__(ctor(*args))
def test_cannot_pass_generic_to_Tensor(generic) -> None: with pytest.raises(NotImplementedError) as excinfo: Tensor(generic) assert str(excinfo.value) == "Generic types in an ndarray are not supported. Was given {}".format(type(generic))
def test_you_can_apply_a_function_to_a_tensor(value, expected_result): t = Tensor(value) result = t.apply(lambda x: x + 10) ndarray = Tensor._to_ndarray(result) assert (ndarray == expected_result).all()
def test_you_can_create_boolean_tensor(value): bools = np.array(value) t = Tensor(bools) ndarray = Tensor._to_ndarray(t.unwrap()) assert (bools == ndarray).all()
def test_can_pass_empty_ndarray_to_Tensor() -> None: with pytest.raises(ValueError, match=r"Cannot infer type because array is empty"): Tensor(np.array([]))
def test_can_pass_empty_ndarray_to_Tensor() -> None: with pytest.raises(ValueError) as excinfo: Tensor(np.array([])) assert str(excinfo.value) == "Cannot infer type because array is empty"
def set_and_cascade(self, v: tensor_arg_types) -> None: self.unwrap().setAndCascade(Tensor(self.cast(v)).unwrap())
def test_ndarray_passed_to_Tensor_creates_nonscalar_tensor(arr: primitive_types) -> None: ndarray = np.array(arr) t = Tensor(ndarray) assert not t.is_scalar()
def test_num_passed_to_Tensor_creates_scalar_tensor(num: Union[primitive_types, numpy_types]) -> None: t = Tensor(num) assert t.is_scalar() assert t.scalar() == num
def test_you_can_create_tensors(dtype): ones = np.ones((1, 1), dtype) t = Tensor(ones) ndarray = Tensor._to_ndarray(t.unwrap()) assert (ones == ndarray).all()
def __get_vertex_samples(network_samples, vertex) -> ndarray: samples_for_vertex = network_samples.get(vertex).asTensor() return Tensor._to_ndarray(samples_for_vertex)
def observe(self, v: tensor_arg_types) -> None: self.unwrap().observe(Tensor(self.cast(v)).unwrap())
def get_value(self) -> numpy_types: return Tensor._to_ndarray(self.unwrap().getValue())
def sample(self) -> numpy_types: return Tensor._to_ndarray(self.unwrap().sample())
def test_ndarray_passed_to_Tensor_creates_nonscalar_tensor(arr: primitive_types, expected_java_class: str) -> None: ndarray = np.array(arr) t = Tensor(ndarray) assert_java_class(t, expected_java_class) assert not t.is_scalar()
def test_fails_when_long_is_too_long() -> None: ones = np.ones((1, 1), np.int64) ones[0, 0] = sys.maxsize with pytest.raises(Py4JJavaError): Tensor(ones)
def test_cannot_pass_generic_ndarray_to_Tensor(generic) -> None: with pytest.raises(NotImplementedError) as excinfo: Tensor(np.array([generic, generic])) assert str(excinfo.value) == "Generic types in an ndarray are not supported. Was given object"
def test_cannot_pass_generic_to_Tensor(generic) -> None: with pytest.raises( NotImplementedError, match=r"Generic types in an ndarray are not supported. Was given {}" .format(type(generic))): Tensor(generic)
def test_convert_java_tensor_to_ndarray(value: numpy_types) -> None: t = Tensor(value) ndarray = Tensor._to_ndarray(t.unwrap()) assert type(ndarray) == np.ndarray assert (value == ndarray).all()
def set_value(self, v: tensor_arg_types) -> None: self.unwrap().setValue(Tensor(self.cast(v)).unwrap())