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

Key Error When Running Code with FES2022 #569

Open
Russ-Nasrallah opened this issue Jan 22, 2025 · 10 comments
Open

Key Error When Running Code with FES2022 #569

Russ-Nasrallah opened this issue Jan 22, 2025 · 10 comments

Comments

@Russ-Nasrallah
Copy link

Hello Again (sorry to be asking so many questions) ,
I installed FES2022 and I think I have corrected the python script in all the places required to point towards the NetCDF files, however when I try and run the model with tide prediction and beach slope, I am getting the following error:

`Traceback (most recent call last):

File ~\AppData\Local\miniforge3\envs\coastsat\Lib\site-packages\pandas\core\indexes\base.py:3805 in get_loc
return self._engine.get_loc(casted_key)

File index.pyx:167 in pandas._libs.index.IndexEngine.get_loc

File index.pyx:196 in pandas._libs.index.IndexEngine.get_loc

File pandas\_libs\hashtable_class_helper.pxi:7081 in pandas._libs.hashtable.PyObjectHashTable.get_item

File pandas\_libs\hashtable_class_helper.pxi:7089 in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: '1'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File ~\AppData\Local\miniforge3\envs\coastsat\Lib\site-packages\spyder_kernels\customize\utils.py:209 in exec_encapsulate_locals
exec_fun(compile(code_ast, filename, "exec"), globals)

File c:\coastsat-master\cape_disappointment_coastsat_small.py:370
cross_distance[key] = np.array(df[key])

File ~\AppData\Local\miniforge3\envs\coastsat\Lib\site-packages\pandas\core\frame.py:4102 in getitem
indexer = self.columns.get_loc(key)

File ~\AppData\Local\miniforge3\envs\coastsat\Lib\site-packages\pandas\core\indexes\base.py:3812 in get_loc
raise KeyError(key) from err

KeyError: '1'`

This is followed by the code producing some plots, but the tidal plot has no output and is completely blank.

thanks for any guidance, I couldnt find any similar issues to help me resolve this.

I've attached a copy of the code I've modified below if it helps at all.
CoastSat_Cape_D.txt

@Russ-Nasrallah
Copy link
Author

Wanted to add some additional information,
I believe I was having one error with the following section:

Image

I modified it to be:

Image

And no longer had issues with the ocean_short variable in the S_slope.compute_tide module being nan

While this allows me to pull a tidal time-series from FES2022 now, I still am getting a key error on that section.

@phillipjws
Copy link

You may want to try moving the point a couple kms offshore, due to the nature of the model, if it doesn't have data for the requested coordinates, it interpolates selecting points near the coordinates you provided. If any of these coordinates are on land, it wont give you a usable tide level.

@Russ-Nasrallah
Copy link
Author

You may want to try moving the point a couple kms offshore, due to the nature of the model, if it doesn't have data for the requested coordinates, it interpolates selecting points near the coordinates you provided. If any of these coordinates are on land, it wont give you a usable tide level.

Thanks! I did try this, but it was the error I mentioned above that was creating issues. Still having trouble with the key error though.

@phillipjws
Copy link

What are the keys in both of the dictionaries if you print both of them? Could help to diagnose the issue, like before entering the loop where the error occurs add a

print(f'cross distance keys: {cross_distance.keys()}') 
print(f'df keys: {df.keys()}')
print(f'transect keys: {transects.keys()}')

The exception shows that it can't find a matching key in df that exists in transects, I don't know too much about np.array but this could be making them all into int instead of string or something similar that could be the cause.

@Russ-Nasrallah
Copy link
Author

What are the keys in both of the dictionaries if you print both of them? Could help to diagnose the issue, like before entering the loop where the error occurs add a

print(f'cross distance keys: {cross_distance.keys()}') 
print(f'df keys: {df.keys()}')
print(f'transect keys: {transects.keys()}')

The exception shows that it can't find a matching key in df that exists in transects, I don't know too much about np.array but this could be making them all into int instead of string or something similar that could be the cause.

I was just exploring this as well, Phillip.
Heres the answer to what you were asking about:

Image

I have not changed anything in the code that I think would modify the key values to not work... Im wondering what is going on.

@Russ-Nasrallah
Copy link
Author

Do the Keys need to be listed as "Transect 1", "Transect 2" etc...?

@phillipjws
Copy link

Oh! I remember running into this a while back, its due to the naming of the transects. You'll see in df they're all Transect n but in the other dicts the keys are just the numbers. I have your file open and you'll notice in line 315 (of the version you uploaded) it adds Transect to the name of the key in out_dict. If you remove the string concatenation it should work.

@Russ-Nasrallah
Copy link
Author

@phillipjws Let me try this out and run through the code again completely. I did try some piecewise adjustments and got it passed that df[key] issue but it choked on another couple problem:

Image

@kvos
Copy link
Owner

kvos commented Jan 27, 2025

yes well spotted @phillipjws , it's an issue with the keys of the dictionnary (name of the transects). Make sure the name of the transects matches with cross_distance, otherwise it will break

@Russ-Nasrallah
Copy link
Author

@kvos any thoughts on these boolean errors that occur after I address the key error?

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

No branches or pull requests

3 participants