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

Conversation

okolekar
Copy link

@okolekar okolekar commented Sep 25, 2024

As discussed during the meetings, where it was requested to add a new tool that can convert the 0's and 1's in the polygon has been created.
I have added the new proximity tool to invert the 0's and 1's in the polygon we get from the distance computation.
I have just used only linear interpolation as of now.

image

@nmaarnio
Copy link
Collaborator

Hey, a quick review: The tool should not take in an already computed distance array, but instead perform the distance computation within this proximity tool. It is already possible to first run distance computation and then apply whatever transformation/scaling to the output, but now we want convenience (=user runs just one tool to produce the proximity raster). Also, maximum distance parameter should not be optional here, since the output raster does not really make sense if the values scale up to the boundaries (unless the user specifically sets the max distance so large that it reaches the edges).

@nmaarnio
Copy link
Collaborator

Also see issue #430 which was made for this feature

@okolekar
Copy link
Author

Hey, a quick review: The tool should not take in an already computed distance array, but instead perform the distance computation within this proximity tool. It is already possible to first run distance computation and then apply whatever transformation/scaling to the output, but now we want convenience (=user runs just one tool to produce the proximity raster). Also, maximum distance parameter should not be optional here, since the output raster does not really make sense if the values scale up to the boundaries (unless the user specifically sets the max distance so large that it reaches the edges).

Okay, I get it now.

@okolekar
Copy link
Author

Also see issue #430 which was made for this feature

So should I make a commit there or should it be here (I am referring to the pull request here should it be done to #430 or here)?

@nmaarnio
Copy link
Collaborator

No need to rename branch or anything, the issue you based this feature on was a little misleading (should have been closed already probably) and can be closed when this PR is merged. Additionally #430 will be closed when this is merged.

…ance as inputs and performs the inversion. Logarithmic scaling is also introduced as result of previous discussions
@okolekar
Copy link
Author

okolekar commented Oct 1, 2024

The tool now takes in a GeoDataFrame, raster profile and maximum distance as inputs and performs the inversion by calling distance computation function inside the tool itself. Logarithmic scaling is also introduced as result of previous.

image

The logarithmic scaling results are shown below

image

Copy link
Collaborator

@nmaarnio nmaarnio left a comment

Choose a reason for hiding this comment

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

Hi Omkar, did a full review now. Apologies for the strict format rules of EIS Toolkit, but we try to keep the toolkit harmonized and tidy.

Besides the comments I wrote, please add a .md file in the docs folder and unit tests (see some other unit tests in EIS Toolkit for examples, no need to include many).

Additionally, you could add a CLI function in cli.py for proximity_computation, but I can take care of that after this PR has been merged if you want.

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.

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

Comment on lines +12 to +16
""" 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.
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

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.

Comment on lines +30 to +33
minimum = np.min(out_matrix)
difference = maximum_distance - minimum
out_matrix = maximum_distance - out_matrix
out_matrix = out_matrix/difference
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

Comment on lines +58 to +71
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)
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants