FlexMeasures / flexmeasures

The intelligent & developer-friendly EMS to support real-time energy flexibility apps, rapidly and scalable.
https://flexmeasures.io
Apache License 2.0
133 stars 34 forks source link

flexmeasures delete prognoses fails with TypeError #1092

Open Ragnar-the-mighty opened 3 weeks ago

Ragnar-the-mighty commented 3 weeks ago

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 commented 3 weeks ago

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?

Ragnar-the-mighty commented 3 weeks ago

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 commented 3 weeks ago

Maybe the previous run had already deleted them?

Ragnar-the-mighty commented 3 weeks ago

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 commented 3 weeks ago

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 commented 3 weeks ago

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.