sandialabs / chama

Python package for sensor placement optimization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Which type should be expected in _df_columns_required ?

aGGeRReS opened this issue · comments

Hello.
I am trying to run of the code samples provided in WNTR projects repository. https://github.com/USEPA/WNTR/blob/main/examples/sensor_placement.py

At a certain moment I get the following error.

Traceback (most recent call last):
  File "../main.py", line 68, in <module>
    results[n] = impactform.solve(min_det_time, sensor_characteristics,
  File "..\optimize.py", line 107, in solve
    self.create_pyomo_model(impact=impact, sensor=sensor, scenario=scenario,
  File "..\optimize.py", line 140, in create_pyomo_model
    cu._df_columns_required('impact', impact,
  File "..\chama\utils.py", line 23, in _df_columns_required
    raise TypeError('Expected column "{0}" of type {1} in DataFrame '
TypeError: Expected column "Impact" of type [<class 'numpy.float64'>, <class 'numpy.int64'>] in DataFrame "impact".

I looked at your examples (e.g. https://chama.readthedocs.io/en/stable/optimization.html) and source code. In the examples it is shown that Impact column is of (most likely) float64 type. However, in the source code it is written the following:

cu._df_columns_required('impact', impact,
                                {'Scenario': np.object,
                                 'Sensor': np.object,
                                 impact_col_name: [np.float64, np.int64]})

So the example from tutorial and a source code don't match.
What is the best way to fix it? Should I simply change to e.g. impact_col_name: np.float64? Or is there another workaround?
Thank you.

Chama requires the impact values are np.float64 or np.int64. It looks like the values stored in min_det_time['Impact'] are np.int32 instead of np.float64 or np.int64. I'm not sure why that changed. A quick workaround is to add the following at line 55 in sensor_placement.py

min_det_time['Impact'] = min_det_time['Impact'].astype('int64')

Thank you, @kaklise .
My original "solution" worked for me, but I also tried this one:

min_det_time=min_det_time.astype({'Impact':'float64'})

Which also worked.

Your solution works fine as well. But I'll stick to converting to float64 in order to preserve precision. Sorry for late answer. I'm not sure whether I should close this issue: maybe this needs extra look from maintainers of the package.

Thanks. You can leave the ticket open. The assert statements in Chama need to be changed to accept floats and ints.

This issue has been resolved with #30