Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Forked from isentropic/histogram2d.py
Last active October 28, 2021 20:28
Show Gist options
  • Select an option

  • Save dmadisetti/ea7b46aa0457d757a4b52057b5b9f5ea to your computer and use it in GitHub Desktop.

Select an option

Save dmadisetti/ea7b46aa0457d757a4b52057b5b9f5ea to your computer and use it in GitHub Desktop.

Revisions

  1. dmadisetti revised this gist Oct 28, 2021. 1 changed file with 7 additions and 8 deletions.
    15 changes: 7 additions & 8 deletions histogram2d.py
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,9 @@
    import tensorflow as tf
    @tf.function
    def histogram(x, y,
    value_range,
    nbins=100,
    weights=None,
    dtype=tf.dtypes.int32):
    bin_dtype=tf.dtypes.int32):
    """
    Bins x, y coordinates of points onto simple square 2d histogram
    @@ -18,7 +17,7 @@ def histogram(x, y,
    Args:
    x: Numeric `Tensor`.
    x: Numeric `Tensor`.
    y: Numeric `Tensor`.
    value_range[0] lims for x
    value_range[1] lims for y
    @@ -39,12 +38,12 @@ def histogram(x, y,
    y_range = value_range[1]

    if weights is None:
    hist_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=dtype)
    hist_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=bin_dtype)
    return tf.map_fn(lambda i: tf.histogram_fixed_width(x[hist_bins == i], x_range, nbins=nbins),
    tf.range(nbins))

    x_bins = tf.histogram_fixed_width_bins(x, x_range, nbins=nbins, dtype=tf.dtypes.int32)
    y_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=tf.dtypes.int32)
    hist = tf.zeros((nbins, nbins), dtype=dtype)
    indices = tf.transpose(tf.stack([x_bins, y_bins]))
    x_bins = tf.histogram_fixed_width_bins(x, x_range, nbins=nbins, dtype=bin_dtype)
    y_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=bin_dtype)
    hist = tf.zeros((nbins, nbins), dtype=weights.dtype)
    indices = tf.transpose(tf.stack([y_bins, x_bins]))
    return tf.tensor_scatter_nd_add(hist, indices, weights)
  2. dmadisetti revised this gist Sep 11, 2021. 1 changed file with 15 additions and 8 deletions.
    23 changes: 15 additions & 8 deletions histogram2d.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    import tensorflow as tf
    @tf.function
    def get2dHistogram(x, y,
    value_range,
    nbins=100,
    dtype=tf.dtypes.int32):
    def histogram(x, y,
    value_range,
    nbins=100,
    weights=None,
    dtype=tf.dtypes.int32):
    """
    Bins x, y coordinates of points onto simple square 2d histogram
    @@ -23,6 +24,7 @@ def get2dHistogram(x, y,
    value_range[1] lims for y
    nbins: Scalar `int32 Tensor`. Number of histogram bins.
    weights: The value to scale
    dtype: dtype for returned histogram.
    Example:
    @@ -36,8 +38,13 @@ def get2dHistogram(x, y,
    x_range = value_range[0]
    y_range = value_range[1]

    histy_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=dtype)

    H = tf.map_fn(lambda i: tf.histogram_fixed_width(x[histy_bins == i], x_range, nbins=nbins),
    if weights is None:
    hist_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=dtype)
    return tf.map_fn(lambda i: tf.histogram_fixed_width(x[hist_bins == i], x_range, nbins=nbins),
    tf.range(nbins))
    return H # Matrix!

    x_bins = tf.histogram_fixed_width_bins(x, x_range, nbins=nbins, dtype=tf.dtypes.int32)
    y_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=tf.dtypes.int32)
    hist = tf.zeros((nbins, nbins), dtype=dtype)
    indices = tf.transpose(tf.stack([x_bins, y_bins]))
    return tf.tensor_scatter_nd_add(hist, indices, weights)
  3. @isentropic isentropic revised this gist May 5, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions histogram2d.py
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,11 @@ def get2dHistogram(x, y,
    nbins: Scalar `int32 Tensor`. Number of histogram bins.
    dtype: dtype for returned histogram.
    Example:
    N = 1000
    xs = tf.random.normal([N])
    ys = tf.random.normal([N])
    get2dHistogram(xs, ys, ([-5.0, 5.0], [-5.0, 5.0]), 50)
    """
  4. @isentropic isentropic revised this gist Nov 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion histogram2d.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    def get2dHistogram(x, y,
    value_range,
    nbins=100,
    dtype=dtypes.int32):
    dtype=tf.dtypes.int32):
    """
    Bins x, y coordinates of points onto simple square 2d histogram
  5. @isentropic isentropic created this gist Nov 14, 2019.
    38 changes: 38 additions & 0 deletions histogram2d.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import tensorflow as tf
    @tf.function
    def get2dHistogram(x, y,
    value_range,
    nbins=100,
    dtype=dtypes.int32):
    """
    Bins x, y coordinates of points onto simple square 2d histogram
    Given the tensor x and y:
    x: x coordinates of points
    y: y coordinates of points
    this operation returns a rank 2 `Tensor`
    representing the indices of a histogram into which each element
    of `values` would be binned. The bins are equal width and
    determined by the arguments `value_range` and `nbins`.
    Args:
    x: Numeric `Tensor`.
    y: Numeric `Tensor`.
    value_range[0] lims for x
    value_range[1] lims for y
    nbins: Scalar `int32 Tensor`. Number of histogram bins.
    dtype: dtype for returned histogram.
    """
    x_range = value_range[0]
    y_range = value_range[1]

    histy_bins = tf.histogram_fixed_width_bins(y, y_range, nbins=nbins, dtype=dtype)

    H = tf.map_fn(lambda i: tf.histogram_fixed_width(x[histy_bins == i], x_range, nbins=nbins),
    tf.range(nbins))
    return H # Matrix!