Skip to content

Commit

Permalink
Fix: Compute approximate most common negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
rantolin committed Jan 8, 2025
1 parent e4cb051 commit f9f5ff5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions raster_loader/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,22 @@ def not_enough_samples():

def most_common_approx(samples: List[Union[int, float]]) -> Dict[int, int]:
"""Compute the most common values in a list of int samples."""
counts = np.bincount(samples)
print("Computing most common values...")

samples_array = np.array(samples)
min_val = int(np.floor(samples_array.min()))
max_val = int(np.ceil(samples_array.max()))

# +2 allows to include max_val in the last bin
bins = np.arange(min_val, max_val + 2)

counts, bin_edges = np.histogram(samples_array, bins=bins)

nth = min(DEFAULT_MAX_MOST_COMMON, len(counts))
counts = np.bincount(samples)
idx = np.argpartition(counts, -nth)[-nth:]
return dict([(int(i), int(counts[i])) for i in idx if counts[i] > 0])

return {int(bin_edges[i]): int(counts[i]) for i in idx if counts[i] > 0}


def compute_quantiles(data: List[Union[int, float]], cast_function: Callable) -> dict:
Expand Down

0 comments on commit f9f5ff5

Please sign in to comment.