Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/sc 458619/allow negative values on most common approx #159

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions raster_loader/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def get_color_table(raster_dataset: rasterio.io.DatasetReader, band: int):
return None



def rasterio_metadata(
file_path: str,
bands_info: List[Tuple[int, str]],
Expand Down Expand Up @@ -424,7 +423,12 @@ def not_enough_samples():
)
if not raster_is_masked:
for band in bands:
not_masked_samples[band].append(sample[band - 1])
band_sample = sample[band - 1]
is_valid_sample = not (
np.isinf(band_sample) or np.isnan(band_sample)
)
if is_valid_sample:
not_masked_samples[band].append(band_sample)

iterations += 1

Expand All @@ -451,10 +455,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 Expand Up @@ -495,8 +511,14 @@ def raster_band_approx_stats(
_sum = 0
sum_squares = 0
if count > 0:
_sum = int(np.sum(samples_band))
sum_squares = int(np.sum(np.array(samples_band) ** 2))
try:
_sum = int(np.sum(samples_band))
except (OverflowError, ValueError):
_sum = 0
try:
sum_squares = int(np.sum(np.array(samples_band) ** 2))
except (OverflowError, ValueError):
sum_squares = 0

if basic_stats:
quantiles = None
Expand Down
Loading