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

flexmeasures delete prognoses fails with TypeError #1092

Open
Ragnar-the-mighty opened this issue Jun 11, 2024 · 6 comments · May be fixed by #1095
Open

flexmeasures delete prognoses fails with TypeError #1092

Ragnar-the-mighty opened this issue Jun 11, 2024 · 6 comments · May be fixed by #1095
Labels
bug Something isn't working Data

Comments

@Ragnar-the-mighty
Copy link

I am on v0.21
When I call
flexmeasures delete prognoses --sensor 267

I get this error
File "/opt/anaconda3/envs/flexmeasures-venv/lib/python3.10/site-packages/flexmeasures/cli/data_delete.py", line 224, in delete_prognoses
depopulate_prognoses(db, sensor_id)
File "/opt/anaconda3/envs/flexmeasures-venv/lib/python3.10/site-packages/flexmeasures/data/transactional.py", line 38, in wrap
db_function(*args, **kwargs)
File "/opt/anaconda3/envs/flexmeasures-venv/lib/python3.10/site-packages/flexmeasures/data/scripts/data_gen.py", line 351, in depopulate_prognoses
click.echo("Deleted %d forecasts (ex-ante beliefs)" % num_forecasts_deleted)
TypeError: %d format: a real number is required, not CursorResult

@nhoening
Copy link
Contributor

Indeed, above that line we have num_forecasts_deleted = db.session.execute(query). That used to work, but I suppose that after we moved to SQLAlchemy 2, it no longer does (actually there are a few places in data_gen.py which have this same problem).

This part of the SQLAlchemy documentation suggests to use .rowcount, so this should be better:

deletion_result = db.session.execute(query)
num_forecasts_deleted = deletion_result.rowcount

Are you able to try out if this fixes your problem?

@nhoening nhoening added bug Something isn't working Data labels Jun 11, 2024
@Ragnar-the-mighty
Copy link
Author

The code change removed the error but the result I got was
Deleted 0 forecasts (ex-ante beliefs)

I do have forecast so this is not the behavior I expected.

@nhoening
Copy link
Contributor

Maybe the previous run had already deleted them?

@Ragnar-the-mighty
Copy link
Author

No I am simulating back in time so my horizon is negative. I think that is why nothing got deleted.
I am still learning about the time capabilities of sensors.

@nhoening
Copy link
Contributor

Ah, yes that is different, then they count as measurements.

When you create beliefs, you can set the horizon which fits the assumption of the simulation. We are using FlexMeasures to simulate internally, as well.

For deleting whatever beliefs of a sensor (or even per asset), try flexmeasures delete beliefs --help

However, that code might also exhibit the same problem you point out in this issue (namely in flexmeasures/cli/data_delete.py), e.g. here:

num_beliefs_up_for_deletion = db.session.scalar(select(func.count()).select_from(q))

Do you want to add the fix you tested into a PR? That would be very welcome (and make you a FlexMeasures contributor :)) - but I can also address it myself. Thanks for pointing it out in any case!

@Ragnar-the-mighty
Copy link
Author

Let me test a little more then I try to address the PR.
I make a comment if I need some help fixing the PR.

Ragnar-the-mighty added a commit to Ragnar-the-mighty/flexmeasures that referenced this issue Jun 12, 2024
After move to SQLAlchemy 2  the code
num_forecasts_deleted = db.session.execute(query)
Does not work as it use to.
Changed code to use .rowcount

There are more places that has same issues but I do not have test cases so I only fixed this one.
@Ragnar-the-mighty Ragnar-the-mighty linked a pull request Jun 12, 2024 that will close this issue
2 tasks
nhoening added a commit to Ragnar-the-mighty/flexmeasures that referenced this issue Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Data
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants