Skip to content

Commit

Permalink
Added histogramdd function to statistical modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dhanush-2501 committed Sep 13, 2023
1 parent 476d591 commit 5d4842e
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/demos
Submodule demos updated from 23d8da to 1ba2b6
55 changes: 55 additions & 0 deletions ivy/data_classes/array/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,61 @@ def histogram(
density=density,
out=out,
)

def histogramdd(
self: Union[ivy.Array, ivy.NativeArray],
/,
*,
bins: Optional[Union[int, ivy.Array, ivy.NativeArray]] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype]] = None,
range: Optional[Tuple[float]] = None,
weights: Optional[Union[ivy.Array, ivy.NativeArray]] = None,
density: Optional[bool] = False,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
ivy.Array instance method variant of ivy.histogramdd. This method simply wraps
the function, and so the docstring for ivy.histogramdd also applies to this
method with minimal changes.
Parameters
----------
self :
Input array, must be at least 2 dimensions. If input has shape (M, N), each of its M rows defines a point in N-dimensional space.
bins :
If ``bins`` is a sequence of N 1D tensors, it explicitly specifies the N sequences of bin edges.
If ``bins`` is a sequence of N ints, it specifies the number of equal-width bins in each dimension.
If ``bins`` is an int, it specifies the number of equal-width bins for all dimensions.
range :
the lower and upper range of the bins. The first element of the range must be
less than or equal to the second.
weights :
each value in ``sample`` only contributes its associated weight towards the bin count
(instead of 1). Must be of the same shape as ``sample``.
out:
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to
Returns
-------
H :
N-dimensional array containing the values of the histogram.
bin_edges:
List of N 1D Tensors containing the bin edges.
Both the description and the type hints above assumes an array input for simplicity,
but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
instances in place of any of the arguments.
"""
return ivy.histogramdd(
self._data,
bins=bins,
dtype=dtype,
range=range,
weights=weights,
density=density,
out=out,
)

def median(
self: ivy.Array,
Expand Down
154 changes: 154 additions & 0 deletions ivy/data_classes/container/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,160 @@ def histogram(
map_sequences=map_sequences,
out=out,
)

@staticmethod
def static_histogramdd(
input: Union[ivy.Array, ivy.NativeArray, ivy.Container],
/,
*,
bins: Optional[
Union[int, ivy.Array, ivy.NativeArray, ivy.Container, str]
] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype, ivy.Container]] = None,
range: Optional[Tuple[Union[bool, ivy.Container]]] = None,
weights: Optional[Union[ivy.Array, ivy.NativeArray, ivy.Container]] = None,
density: Optional[Union[bool, ivy.Container]] = False,
key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None,
to_apply: Union[bool, ivy.Container] = True,
prune_unapplied: Union[bool, ivy.Container] = False,
map_sequences: Union[bool, ivy.Container] = False,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container static method variant of ivy.histogramdd. This method simply wraps
the function, and so the docstring for ivy.histogramdd also applies to this
method with minimal changes.
Parameters
----------
input :
``input`` must be a array with at least 2 dimensions. If input has shape (M, N), each of its M rows defines a point in N-dimensional space.
bins :
If ``bins`` is a sequence of N 1D tensors, it explicitly specifies the N sequences of bin edges.
If ``bins`` is a sequence of N ints, it specifies the number of equal-width bins in each dimension.
If ``bins`` is an int, it specifies the number of equal-width bins for all dimensions.
range :
the lower and upper range of the bins. The first element of the range must be
less than or equal to the second.
weights :
each value in ``sample`` only contributes its associated weight towards the bin count
(instead of 1). Must be of the same shape as ``sample``.
out:
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
key_chains
The key-chains to apply or not apply the method to. Default is ``None``.
to_apply
If True, the method will be applied to key_chains, otherwise key_chains
will be skipped. Default is ``True``.
prune_unapplied
Whether to prune key_chains for which the function was not applied.
Default is ``False``.
map_sequences
Whether to also map method to sequences (lists, tuples).
Default is ``False``.
Returns
-------
H :
N-dimensional array containing the values of the histogram.
bin_edges:
List of N 1D Tensors containing the bin edges.
Both the description and the type hints above assumes an array input for simplicity,
but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
instances in place of any of the arguments.
"""
return ContainerBase.cont_multi_map_in_function(
"histogramdd",
input,
bins=bins,
dtype=dtype,
range=range,
weights=weights,
density=density,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

def histogramdd(
self: ivy.Container,
/,
*,
bins: Optional[
Union[int, ivy.Array, ivy.NativeArray, ivy.Container, str]
] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype, ivy.Container]] = None,
range: Optional[Tuple[Union[bool, ivy.Container]]] = None,
weights: Optional[Union[ivy.Array, ivy.NativeArray, ivy.Container]] = None,
density: Optional[Union[bool, ivy.Container]] = False,
key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None,
to_apply: Union[bool, ivy.Container] = True,
prune_unapplied: Union[bool, ivy.Container] = False,
map_sequences: Union[bool, ivy.Container] = False,
out: Optional[ivy.Container] = None,
) -> ivy.Container:
"""
ivy.Container static method variant of ivy.histogramdd. This method simply wraps
the function, and so the docstring for ivy.histogramdd also applies to this
method with minimal changes.
Parameters
----------
input :
``input`` must be a array with at least 2 dimensions. If input has shape (M, N), each of its M rows defines a point in N-dimensional space.
bins :
If ``bins`` is a sequence of N 1D tensors, it explicitly specifies the N sequences of bin edges.
If ``bins`` is a sequence of N ints, it specifies the number of equal-width bins in each dimension.
If ``bins`` is an int, it specifies the number of equal-width bins for all dimensions.
range :
the lower and upper range of the bins. The first element of the range must be
less than or equal to the second.
weights :
each value in ``sample`` only contributes its associated weight towards the bin count
(instead of 1). Must be of the same shape as ``sample``.
out:
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
key_chains
The key-chains to apply or not apply the method to. Default is ``None``.
to_apply
If True, the method will be applied to key_chains, otherwise key_chains
will be skipped. Default is ``True``.
prune_unapplied
Whether to prune key_chains for which the function was not applied.
Default is ``False``.
map_sequences
Whether to also map method to sequences (lists, tuples).
Default is ``False``.
Returns
-------
H :
N-dimensional array containing the values of the histogram.
bin_edges:
List of N 1D Tensors containing the bin edges.
Both the description and the type hints above assumes an array input for simplicity,
but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
instances in place of any of the arguments.
"""
return self.static_histogramdd(
self,
bins=bins,
dtype=dtype,
range=range,
weights=weights,
density=density,
key_chains=key_chains,
to_apply=to_apply,
prune_unapplied=prune_unapplied,
map_sequences=map_sequences,
out=out,
)

@staticmethod
def static_median(
Expand Down
21 changes: 21 additions & 0 deletions ivy/functional/backends/jax/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,27 @@ def histogram(
return ret


def histogramdd(
input: jnp.ndarray,
/,
*,
bins: Optional[Union[int, jnp.ndarray]] = 10,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype]] = None,
range: Optional[Tuple[float]] = None,
weights: Optional[jnp.ndarray] = None,
density: Optional[bool] = False,
out: Optional[jnp.ndarray] = None,
) -> Tuple[jnp.ndarray]:
if isinstance(range, list):
range = tuple(range)
ret = jnp.histogramdd(
input, bins=bins, range=range, weights=weights, density=density
)
if dtype:
ret = ret.astype(dtype)
return ret


@with_unsupported_dtypes(
{"0.4.14 and below": ("complex64", "complex128")}, backend_version
)
Expand Down
25 changes: 25 additions & 0 deletions ivy/functional/backends/numpy/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ def histogram(
return ret


def histogramdd(
input: np.ndarray,
/,
*,
bins: Union[int, Tuple[int]] = 10,
range: Optional[Tuple[Tuple[float]]] = None,
weights: Optional[np.ndarray] = None,
density: Optional[bool] = False,
dtype: Optional[np.dtype] = None,
out: Optional[np.ndarray] = None,
):
if isinstance(range, list):
range = tuple(range)
ret = np.histogramdd(
input,
bins=bins,
range=range,
weights=weights,
density=density,
)
if dtype:
ret = ret.astype(dtype)
return ret


def median(
input: np.ndarray,
/,
Expand Down
28 changes: 28 additions & 0 deletions ivy/functional/backends/torch/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,34 @@ def histogram(
histogram.support_native_out = True


def histogramdd(
input: torch.Tensor,
/,
*,
bins: Optional[Union[int, torch.Tensor]] = None,
dtype: Optional[torch.dtype] = None,
range: Optional[Tuple[float]] = None,
weights: Optional[torch.Tensor] = None,
density: Optional[bool] = False,
out: Optional[torch.Tensor] = None,
) -> Tuple[torch.Tensor]:
if isinstance(range, list):
range = tuple(range)
ret = torch.histogramdd(
input,
bins=bins,
range=range,
weights=weights,
density=density,
)
if dtype:
ret = ret.astype(dtype)
return ret


histogramdd.support_native_out = True


@with_unsupported_dtypes({"2.0.1 and below": ("float16", "bool")}, backend_version)
def median(
input: torch.Tensor,
Expand Down
62 changes: 62 additions & 0 deletions ivy/functional/ivy/experimental/statistical.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,68 @@ def histogram(
)


@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_out_argument
@to_native_arrays_and_back
@handle_device_shifting
def histogramdd(
input: Union[ivy.Array, ivy.NativeArray],
/,
*,
bins: Optional[Union[int, ivy.Array, ivy.NativeArray]] = None,
dtype: Optional[Union[ivy.Dtype, ivy.NativeDtype]] = None,
range: Optional[Tuple[float]] = None,
weights: Optional[Union[ivy.Array, ivy.NativeArray]] = None,
density: Optional[bool] = False,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""
Compute a multi-dimensional histogram of the values in a array.
Parameters
----------
input :
``input`` must be a array with at least 2 dimensions. If input has shape (M, N), each of its M rows defines a point in N-dimensional space.
bins :
If ``bins`` is a sequence of N 1D tensors, it explicitly specifies the N sequences of bin edges.
If ``bins`` is a sequence of N ints, it specifies the number of equal-width bins in each dimension.
If ``bins`` is an int, it specifies the number of equal-width bins for all dimensions.
range :
the lower and upper range of the bins. The first element of the range must be
less than or equal to the second.
weights :
each value in ``sample`` only contributes its associated weight towards the bin count
(instead of 1). Must be of the same shape as ``sample``.
dtype
the output type.
out:
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to
Returns
-------
H :
N-dimensional array containing the values of the histogram.
bin_edges:
List of N 1D Tensors containing the bin edges.
Both the description and the type hints above assumes an array input for simplicity,
but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
instances in place of any of the arguments.
"""
return ivy.current_backend(input).histogramdd(
input,
bins=bins,
dtype=dtype,
range=range,
weights=weights,
density=density,
out=out,
)


@handle_exceptions
@handle_backend_invalid
@handle_nestable
Expand Down

0 comments on commit 5d4842e

Please sign in to comment.