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

proximity tool to convert the 0's and 1's in the polygon #432

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
73 changes: 73 additions & 0 deletions eis_toolkit/vector_processing/proximity_tool.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this file to proximity_computation.py (and related files accordingly) to follow the naming schema of distance computation.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import numpy as np
import geopandas as gpd
from numbers import Number
from beartype import beartype
from beartype.typing import Union
from rasterio import profiles, transform
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transform is unused import

from eis_toolkit.vector_processing.distance_computation import distance_computation

@beartype
def calculate_proximity(geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict], maximum_distance: Number) -> np.ndarray:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line, as some other lines, exceeds the allowed limit. If you use pre-commit locally, it will fix/point out these kind of cosmetic issues.

Also, perhaps it is better if the tool is named similarly as distance_computation is right now, so proximity_computation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had an issue with the pre-commit hooke
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

However, is now resolved. I ran the precommit hooke and cleared the cosmetic problems
image


""" Interpolates the distance values calculated by the distance_computation function between 0 and 1.
1 denots the value inside the polygon and 0 at the maximum distance.
If maximum_distance value is not provided, the program sets this value to the maximum value
in the provided distance matrix.
Uses linear interpolation to calculate the distance from the polygon.
Comment on lines +12 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format of the docstring should follow that of other tools. I believe the line "If maximum_distance value is not provided, the program sets this value to the maximum value " is also outdated as maximum_distance is required


Args:
geodataframe: The GeoDataFrame with geometries to determine distance to.
raster_profile: The raster profile of the raster in which the distances
to the nearest geometry are determined.
max_distance: The maximum distance in the output array.

Returns:
A 2D numpy array with the the distance values inverted.
"""

out_matrix = distance_computation(geodataframe, raster_profile, maximum_distance)

minimum = np.min(out_matrix)
difference = maximum_distance - minimum
out_matrix = maximum_distance - out_matrix
out_matrix = out_matrix/difference
Comment on lines +30 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you could utilize _min_max_scaling from EIS Toolkit although I am not 100% sure. Please check and if it fits, we could replace this piece of code with a call to that function to have the operation centralized in one place only.

There's also the point that if we extend the scaling options in the future, it is good to use our own transformations module wherever we can

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried using the _min_max_scaling, using this tool I could not achieve the inversion we intend to achieve. Moreover, I cannot see the scaling happening as well. Below I have attached the pictures.

image


return out_matrix

@beartype
def calculate_logarithmic_proximity(geodataframe: gpd.GeoDataFrame, raster_profile: Union[profiles.Profile, dict], maximum_distance: Number) -> np.ndarray:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if the choice of scaling should be parameterized, not formulated as another public function. So, let's expose only one proximity_computation to the users with parameter scaling. It could be literal Literal['linear', 'log'] with linear as default.


""" Logarithmically interpolates the distance values calculated by the distance_computation function between 0 and 1.
1 denots the value inside the polygon and 0 at the maximum distance.
If maximum_distance value is not provided, the program sets this value to the maximum value
in the provided distance matrix.
Uses linear interpolation to calculate the distance from the polygon.

Args:
geodataframe: The GeoDataFrame with geometries to determine distance to.
raster_profile: The raster profile of the raster in which the distances
to the nearest geometry are determined.
max_distance: The maximum distance in the output array.

Returns:
A 2D numpy array with the the distance values inverted.
"""

distance_array = distance_computation(geodataframe, raster_profile,maximum_distance)

modified_distance_array = np.where(distance_array==0.0,np.nan,distance_array)
out_matrix = np.log(modified_distance_array)

log_maximum = np.log(maximum_distance)
minimum = np.min(distance_array)
if(minimum != 0):
log_minimum = np.log(minimum)
else :
log_minimum = minimum
difference = log_maximum - log_minimum
out_matrix = log_maximum - out_matrix
out_matrix = out_matrix/difference

out_matrix = np.nan_to_num(out_matrix,nan=log_maximum)
Comment on lines +58 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check also if the transformations module would be of use to perform the log transform.


return out_matrix
172 changes: 172 additions & 0 deletions notebooks/testing_proximity_tool.ipynb

Large diffs are not rendered by default.

Loading