Skip to content

Commit

Permalink
feat!: New flag 'basic_stats' instead of 'omit_stat'
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The new flag `basic_stats` changes the
  funcitonality of the CLI because it reverses the behaviour of
  old `all_stats` flag and it skips quantiles and top values.
  • Loading branch information
rantolin committed Dec 4, 2024
1 parent d2369f7 commit 2cb89cc
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions raster_loader/cli/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def bigquery(args=None):
is_flag=True,
)
@click.option(
"--omit_stats",
help="Omit quantiles and most frequent values statistics.",
"--basic_stats",
help="Compute basic stats and omit quantiles and most frequent values.",
required=False,
is_flag=True,
)
Expand All @@ -104,7 +104,7 @@ def upload(
append=False,
cleanup_on_failure=False,
exact_stats=False,
omit_stats=False,
basic_stats=False,
):
from raster_loader.io.common import (
get_number_of_blocks,
Expand Down Expand Up @@ -176,7 +176,7 @@ def upload(
append=append,
cleanup_on_failure=cleanup_on_failure,
exact_stats=exact_stats,
omit_stats=omit_stats,
basic_stats=basic_stats,
)

click.echo("Raster file uploaded to Google BigQuery")
Expand Down
8 changes: 4 additions & 4 deletions raster_loader/cli/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def snowflake(args=None):
is_flag=True,
)
@click.option(
"--omit_stats",
help="Omit quantiles and most frequent values statistics.",
"--basic_stats",
help="Compute basic stats and omit quantiles and most frequent values.",
required=False,
is_flag=True,
)
Expand All @@ -117,7 +117,7 @@ def upload(
append=False,
cleanup_on_failure=False,
exact_stats=False,
omit_stats=False,
basic_stats=False,
):
from raster_loader.io.common import (
get_number_of_blocks,
Expand Down Expand Up @@ -200,7 +200,7 @@ def upload(
append=append,
cleanup_on_failure=cleanup_on_failure,
exact_stats=exact_stats,
omit_stats=omit_stats,
basic_stats=basic_stats,
)

click.echo("Raster file uploaded to Snowflake")
Expand Down
4 changes: 2 additions & 2 deletions raster_loader/io/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def upload_raster(
append: bool = False,
cleanup_on_failure: bool = False,
exact_stats: bool = False,
omit_stats: bool = False,
basic_stats: bool = False,
):
"""Write a raster file to a BigQuery table."""
print("Loading raster file to BigQuery...")
Expand All @@ -131,7 +131,7 @@ def upload_raster(
exit()

metadata = rasterio_metadata(
file_path, bands_info, self.band_rename_function, exact_stats, omit_stats
file_path, bands_info, self.band_rename_function, exact_stats, basic_stats
)

overviews_records_gen = rasterio_overview_to_records(
Expand Down
16 changes: 8 additions & 8 deletions raster_loader/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def rasterio_metadata(
bands_info: List[Tuple[int, str]],
band_rename_function: Callable,
exact_stats: bool = False,
omit_stats: bool = False,
basic_stats: bool = False,
):
"""Open a raster file with rasterio."""
raster_info = rio_cogeo.cog_info(file_path).dict()
Expand Down Expand Up @@ -243,11 +243,11 @@ def rasterio_metadata(
"User is encourage to compute approximate statistics instead.",
UserWarning,
)
stats = raster_band_stats(raster_dataset, band, omit_stats)
stats = raster_band_stats(raster_dataset, band, basic_stats)
else:
print("Computing approximate stats...")
stats = raster_band_approx_stats(
raster_dataset, samples, band, omit_stats
raster_dataset, samples, band, basic_stats
)

band_colorinterp = get_color_name(raster_dataset, band)
Expand Down Expand Up @@ -483,7 +483,7 @@ def raster_band_approx_stats(
raster_dataset: rasterio.io.DatasetReader,
samples: Samples,
band: int,
omit_stats: bool,
basic_stats: bool,
) -> dict:
"""Get approximate statistics for a raster band."""

Expand All @@ -498,7 +498,7 @@ def raster_band_approx_stats(
_sum = int(np.sum(samples_band))
sum_squares = int(np.sum(np.array(samples_band) ** 2))

if omit_stats:
if basic_stats:
quantiles = None
most_common = None
else:
Expand Down Expand Up @@ -564,7 +564,7 @@ def read_raster_band(raster_dataset: rasterio.io.DatasetReader, band: int) -> np


def raster_band_stats(
raster_dataset: rasterio.io.DatasetReader, band: int, omit_stats: bool
raster_dataset: rasterio.io.DatasetReader, band: int, basic_stats: bool
) -> dict:
"""Get statistics for a raster band."""

Expand All @@ -588,7 +588,7 @@ def raster_band_stats(
print("Removing masked data...")
qdata = raster_band.compressed()

if omit_stats:
if basic_stats:
quantiles = None
most_common = None
else:
Expand All @@ -603,7 +603,7 @@ def raster_band_stats(
warnings.warn(
"Most common values are meant for categorical data. "
"Computing them for float bands can be meaningless.\n"
"Please, consider to use the --omit_stats option.",
"Please, consider to use the --basic_stats option.",
)
most_common = Counter(qdata).most_common(100)
most_common.sort(key=lambda x: x[1], reverse=True)
Expand Down
4 changes: 2 additions & 2 deletions raster_loader/io/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def upload_raster(
append: bool = False,
cleanup_on_failure: bool = False,
exact_stats: bool = False,
omit_stats: bool = False,
basic_stats: bool = False,
) -> bool:
def band_rename_function(x):
return x.upper()
Expand Down Expand Up @@ -207,7 +207,7 @@ def band_rename_function(x):
exit()

metadata = rasterio_metadata(
file_path, bands_info, band_rename_function, exact_stats, omit_stats
file_path, bands_info, band_rename_function, exact_stats, basic_stats
)

overviews_records_gen = rasterio_overview_to_records(
Expand Down
4 changes: 2 additions & 2 deletions raster_loader/tests/bigquery/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_bigquery_upload(*args, **kwargs):

@patch("raster_loader.cli.bigquery.BigQueryConnection.upload_raster", return_value=None)
@patch("raster_loader.cli.bigquery.BigQueryConnection.__init__", return_value=None)
def test_bigquery_upload_with_omit_stats(*args, **kwargs):
def test_bigquery_upload_with_basic_stats(*args, **kwargs):
runner = CliRunner()
result = runner.invoke(
main,
Expand All @@ -59,7 +59,7 @@ def test_bigquery_upload_with_omit_stats(*args, **kwargs):
1,
"--band",
1,
"--omit_stats",
"--basic_stats",
],
)
assert result.exit_code == 0
Expand Down
4 changes: 2 additions & 2 deletions raster_loader/tests/snowflake/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_snowflake_upload(*args, **kwargs):
"raster_loader.io.snowflake.SnowflakeConnection.upload_raster", return_value=None
)
@patch("raster_loader.io.snowflake.SnowflakeConnection.__init__", return_value=None)
def test_snowflake_upload_with_omit_stats(*args, **kwargs):
def test_snowflake_upload_with_basic_stats(*args, **kwargs):
runner = CliRunner()
result = runner.invoke(
main,
Expand All @@ -75,7 +75,7 @@ def test_snowflake_upload_with_omit_stats(*args, **kwargs):
1,
"--band",
1,
"--omit_stats",
"--basic_stats",
],
)
assert result.exit_code == 0
Expand Down

0 comments on commit 2cb89cc

Please sign in to comment.