kvos / CoastSat

Global shoreline mapping tool from satellite imagery
http://coastsat.space
GNU General Public License v3.0
696 stars 252 forks source link

SDS_preprocess reference shoreline numpy error #515

Closed climeyG closed 4 months ago

climeyG commented 4 months ago

When I run the reference shoreline process, I end up with the error: AttributeError: module 'numpy' has no attribute 'math'. Assistance will be appreciated.

Numpy math

thekester commented 4 months ago

Hello, you could add import math on SDS_preprocess.py and replace phi = np.pi/2 - np.math.atan2(deltax, deltay) by phi = np.pi/2 - math.atan2(deltax, deltay)

climeyG commented 4 months ago

Thanks. That sorts it out. Is it indicative of an install issue on my side?

thekester commented 4 months ago

Since numpy doesn't often have an attribute called "math," I'm honestly not sure why np.math works for me. It's a mystery of Python!

kvos commented 4 months ago

numpy released version 2.0 a few weeks ago, that's probably what's causing the issue. Will check what has changed and adjust the toolbox.

kvos commented 4 months ago

@climeyG can you please check which version of numpy you are running. In Anaconda Prompt type: conda activate coastsat conda list numpy

climeyG commented 4 months ago

version 2.0.0

kvos commented 4 months ago

yep that makes sense. can you check where np.math.atan2 has ended in numpy 2.0.0?

climeyG commented 4 months ago

Hi Kilian

Sorry, you're dealing with someone with no coding background, bumbling along. How would I do that? I can trace this back to the init.py module, but that's as far as my limited knowledge goes.

Gerard

On Fri, 12 Jul 2024 at 04:32, Kilian Vos @.***> wrote:

yep that makes sense. can you check where np.math.atan2 has ended in numpy 2.0.0?

— Reply to this email directly, view it on GitHub https://github.com/kvos/CoastSat/issues/515#issuecomment-2224314277, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUX4A2K5WFU67SIUGOMXZ63ZL455TAVCNFSM6AAAAABKSQK4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRUGMYTIMRXG4 . You are receiving this because you were mentioned.Message ID: @.***>

thekester commented 4 months ago

Hello, i check where np.math.atan2 has ended in numpy 2.0.0 with that AI generated code

import math

import numpy as np

Scalar values

x = 3 y = 4

Using math.atan2 via np.math

result_scalar = np.math.atan2(y, x) print("Result for scalars with np.math.atan2:", result_scalar)

Array values

x_array = np.array([3, 1, -1, -3]) y_array = np.array([4, 2, -2, -4])

Using numpy.arctan2 for arrays

result_array = np.arctan2(y_array, x_array) print("Result for arrays with np.arctan2:", result_array)

When i try to execute the code, i got:

DeprecationWarning: np.math is a deprecated alias for the standard library math module (Deprecated Numpy 1.25). Replace usages of np.math with math

depreciationwarningnpmath

By the way, if you try np.atan2 you got an AttributeError.

File "C:\Users\Avenel\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy__init.py", line 347, in getattr__ raise AttributeError("module {!r} has no attribute " AttributeError: module 'numpy' has no attribute 'atan2'. Did you mean: 'arctan2'?

To conclude, if x and y are scalar values you should use math.atan2(x,y) but if x and y are arrays you should use np.arctan2(x,y).

Hope this will help.

thekester commented 4 months ago

When i execute the script i got the following error Traceback (most recent call last): File "/home/athena/Document/coastsatv2/CoastSat-master/CoastSat-master/example.py", line 108, in settings['reference_shoreline'] = SDS_preprocess.get_reference_sl(metadata, settings) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/athena/Document/coastsatv2/CoastSat-master/CoastSat-master/coastsat/SDS_preprocess.py", line 942, in get_reference_sl phi = np.pi/2 - np.math.atan2(deltax, deltay) ^^^^^^^ File "/home/athena/miniconda3/envs/coastsat/lib/python3.12/site-packages/numpy/init.py", line 410, in getattr raise AttributeError("module {!r} has no attribute " AttributeError: module 'numpy' has no attribute 'math'. Did you mean: 'emath'? Type of deltax <class 'numpy.float64'> Type of deltay <class 'numpy.float64'> phi = np.pi/2 - np.arctan2(deltax, deltay) solve the numpy error in SDS_preprocess reference shoreline

kvos commented 4 months ago

ok so np.arctan2() is the solution? if you want to PR this one too that would be great, otherwise I'll make the update. Thanks again for looking into this!

thekester commented 4 months ago

Yes, in my opinion, using np.arctan2() is the appropriate solution because deltax and deltay are of type np.float64. This ensures better compatibility and avoids potential issues that could arise from using the standard library's math.atan2().

I've created a PR to address this issue: https://github.com/kvos/CoastSat/pull/520 , thanks again for that amazing project !