From 69f21be70f3481f5fe74376b4d3b55ec3f8b693b Mon Sep 17 00:00:00 2001 From: Niko Aarnio <113038549+nmaarnio@users.noreply.github.com> Date: Mon, 15 Jan 2024 13:37:27 +0200 Subject: [PATCH] Added Coda functions to CLI (#277) --- eis_toolkit/cli.py | 215 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) diff --git a/eis_toolkit/cli.py b/eis_toolkit/cli.py index 33746034..7f88a10a 100644 --- a/eis_toolkit/cli.py +++ b/eis_toolkit/cli.py @@ -1423,6 +1423,221 @@ def gamme_overlay_cli( # --- TRANSFORMATIONS --- +# CODA - ALR TRANSFORM +@app.command() +def alr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], + output_vector: Annotated[Path, OUTPUT_FILE_OPTION], + column: str = None, + keep_denominator_column: bool = False, +): + """Perform an additive logratio transformation on the data.""" + from eis_toolkit.transformations.coda.alr import alr_transform + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_df = alr_transform(df=df, column=column, keep_denominator_column=keep_denominator_column) + typer.echo("Progess 75%") + + out_gdf = gpd.GeoDataFrame(out_df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"ALR transform completed, output saved to {output_vector}") + + +# CODA - INVERSE ALR TRANSFORM +@app.command() +def inverse_alr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], + output_vector: Annotated[Path, OUTPUT_FILE_OPTION], + denominator_column: str = typer.Option(), + scale: float = 1.0, +): + """Perform the inverse transformation for a set of ALR transformed data.""" + from eis_toolkit.transformations.coda.alr import inverse_alr + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_df = inverse_alr(df=df, denominator_column=denominator_column, scale=scale) + typer.echo("Progess 75%") + + out_gdf = gpd.GeoDataFrame(out_df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"Inverse ALR transform completed, output saved to {output_vector}") + + +# CODA - CLR TRANSFORM +@app.command() +def clr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], output_vector: Annotated[Path, OUTPUT_FILE_OPTION] +): + """Perform a centered logratio transformation on the data.""" + from eis_toolkit.transformations.coda.clr import clr_transform + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_df = clr_transform(df=df) + typer.echo("Progess 75%") + + out_gdf = gpd.GeoDataFrame(out_df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"CLR transform completed, output saved to {output_vector}") + + +# CODA - INVERSE CLR TRANSFORM +@app.command() +def inverse_clr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], + output_vector: Annotated[Path, OUTPUT_FILE_OPTION], + colnames: Annotated[List[str], typer.Option()] = None, + scale: float = 1.0, +): + """Perform the inverse transformation for a set of CLR transformed data.""" + from eis_toolkit.transformations.coda.clr import inverse_clr + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_df = inverse_clr(df=df, colnames=colnames, scale=scale) + typer.echo("Progess 75%") + + out_gdf = gpd.GeoDataFrame(out_df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"Inverse CLR transform completed, output saved to {output_vector}") + + +# CODA - SINGLE ILR TRANSFORM +@app.command() +def single_ilr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], + output_vector: Annotated[Path, OUTPUT_FILE_OPTION], + subcomposition_1: Annotated[List[str], typer.Option()], + subcomposition_2: Annotated[List[str], typer.Option()], +): + """Perform a single isometric logratio transformation on the provided subcompositions.""" + from eis_toolkit.transformations.coda.ilr import single_ilr_transform + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_series = single_ilr_transform(df=df, subcomposition_1=subcomposition_1, subcomposition_2=subcomposition_2) + typer.echo("Progess 75%") + + # NOTE: Output of pairwise_logratio might be changed to DF in the future, to automatically do the following + df["single_ilr"] = out_series + out_gdf = gpd.GeoDataFrame(df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"Single ILR transform completed, output saved to {output_vector}") + + +# CODA - PAIRWISE LOGRATIO TRANSFORM +@app.command() +def pairwise_logratio_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], + output_vector: Annotated[Path, OUTPUT_FILE_OPTION], + numerator_column: str = typer.Option(), + denominator_column: str = typer.Option(), +): + """Perform a pairwise logratio transformation on the given columns.""" + from eis_toolkit.transformations.coda.pairwise import pairwise_logratio + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_series = pairwise_logratio(df=df, numerator_column=numerator_column, denominator_column=denominator_column) + typer.echo("Progess 75%") + + # NOTE: Output of pairwise_logratio might be changed to DF in the future, to automatically do the following + df["pairwise_logratio"] = out_series + out_gdf = gpd.GeoDataFrame(df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"Pairwise logratio transform completed, output saved to {output_vector}") + + +# CODA - SINGLE PLR TRANSFORM +@app.command() +def single_plr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], + output_vector: Annotated[Path, OUTPUT_FILE_OPTION], + column: str = typer.Option(), +): + """Perform a pivot logratio transformation on the selected column.""" + from eis_toolkit.transformations.coda.plr import single_plr_transform + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_series = single_plr_transform(df=df, column=column) + typer.echo("Progess 75%") + + # NOTE: Output of single_plr_transform might be changed to DF in the future, to automatically do the following + df["single_plr"] = out_series + out_gdf = gpd.GeoDataFrame(df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"Single PLR transform completed, output saved to {output_vector}") + + +# CODA - PLR TRANSFORM +@app.command() +def plr_transform_cli( + input_vector: Annotated[Path, INPUT_FILE_OPTION], output_vector: Annotated[Path, OUTPUT_FILE_OPTION] +): + """Perform a pivot logratio transformation on the dataframe, returning the full set of transforms.""" + from eis_toolkit.transformations.coda.plr import plr_transform + + typer.echo("Progress: 10%") + + gdf = gpd.read_file(input_vector) + geometries = gdf["geometry"] + df = pd.DataFrame(gdf.drop(columns="geometry")) + typer.echo("Progress: 25%") + + out_df = plr_transform(df=df) + typer.echo("Progess 75%") + + out_gdf = gpd.GeoDataFrame(out_df, geometry=geometries) + out_gdf.to_file(output_vector) + typer.echo("Progress: 100%") + typer.echo(f"PLR transform completed, output saved to {output_vector}") + + # BINARIZE @app.command() def binarize_cli(