coryjog / anemoi

Wind analysis package
MIT License
20 stars 2 forks source link

KeyError: 'Level sensor must be same as name (None)' #18

Open hecnacor95 opened 5 years ago

hecnacor95 commented 5 years ago

Greetings, I'm just following the tutorial (https://coryjog.github.io/anemoi/docs_tutorial.html#), but can't create a MetMast. Here is the code: `mast_data = pd.DataFrame(index=pd.date_range('2017-01-01 00:00', '2017-01-01 01:00', freq='10T'), data = np.random.rand(7,4), columns=['SPD_58_N', 'SPD_48_N', 'SPD_38_N', 'DIR_56'])

mast = an.MetMast(data=mast_data, name=1001, lat=45.0, lon=-90.0, elev=100.0, height=60, primary_ano = 'SPD_58_N', primary_vane = 'DIR_56') print(mast)`

It just print the error:

`'--------------------------------------------------------------------------- KeyError Traceback (most recent call last)

in () 10 height=60, 11 primary_ano = 'SPD_58_N', ---> 12 primary_vane = 'DIR_56') 13 print(mast) ~/miniconda3/lib/python3.6/site-packages/anemoi/mast.py in __init__(self, data, name, lat, lon, elev, height, primary_ano, primary_vane, shear_sensors) 71 data.columns = an.utils.mast_data.remove_sensor_levels_from_mast_data_columns(data.columns) 72 ---> 73 data.columns = an.utils.mast_data.add_sensor_levels_to_from_mast_data_columns(data.columns) 74 data = data.sort_index(axis=1) 75 ~/miniconda3/lib/python3.6/site-packages/anemoi/utils/mast_data.py in add_sensor_levels_to_from_mast_data_columns(mast_data_columns) 47 mast_data_columns = remove_sensor_levels_from_mast_data_columns(mast_data_columns) 48 ---> 49 sensors = pd.DataFrame(mast_data_columns.get_level_values('sensor')) 50 details = pd.Series(sensors.sensor).str.split('_', expand=True) 51 details.columns = ['type', 'height', 'orient', 'signal'] ~/miniconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in _get_level_values(self, level) 3169 """ 3170 -> 3171 self._validate_index_level(level) 3172 return self 3173 ~/miniconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in _validate_index_level(self, level) 1956 elif level != self.name: 1957 raise KeyError('Level %s must be same as name (%s)' % -> 1958 (level, self.name)) 1959 1960 def _get_level_number(self, level): KeyError: 'Level sensor must be same as name (None)'`
coryjog commented 5 years ago

Thanks for the feedback!

We'll work to update this but in the meantime, you can fix this with:

mast_data = pd.DataFrame(index=pd.date_range('2017-01-01 00:00', '2017-01-01 01:00', freq='10T'),
data = np.random.rand(7,4),
columns=['SPD_58_N', 'SPD_48_N', 'SPD_38_N', 'DIR_56'])
data.columns.name = 'sensor' # <-this is new

mast = an.MetMast(data=mast_data,
name=1001,
lat=45.0,
lon=-90.0,
elev=100.0,
height=60,
primary_ano = 'SPD_58_N',
primary_vane = 'DIR_56')
print(mast)
hecnacor95 commented 5 years ago

Hello coryjog, thank you for your attention. Now, I'm getting a new error:

In [29]: %paste
mast_data = pd.DataFrame(index=pd.date_range('2017-01-01 00:00', '2017-01-01 01:00', freq='10T'),
                        data = np.random.rand(7,4),
                        columns=['SPD_58_N', 'SPD_48_N', 'SPD_38_N', 'DIR_56'])
mast_data.columns.name = 'sensor' # <-this is new
mast = an.MetMast(data=mast_data,
                  name=1001,
                  lat=45.0,
                  lon=-90.0,
                  elev=100.0,
                  height=60,
                  primary_ano = 'SPD_58_N',
                  primary_vane = 'DIR_56')
print(mast)

## -- End pasted text --
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-29-2230eb3a6df4> in <module>()
     10                   height=60,
     11                   primary_ano = 'SPD_58_N',
---> 12                   primary_vane = 'DIR_56')
     13 print(mast)

~/miniconda3/lib/python3.6/site-packages/anemoi/mast.py in __init__(self, data, name, lat, lon, elev, height, primary_ano, primary_vane, shear_sensors)
     71                 data.columns = an.utils.mast_data.remove_sensor_levels_from_mast_data_columns(data.columns)
     72
---> 73             data.columns = an.utils.mast_data.add_sensor_levels_to_from_mast_data_columns(data.columns)
     74             data = data.sort_index(axis=1)
     75

~/miniconda3/lib/python3.6/site-packages/anemoi/utils/mast_data.py in add_sensor_levels_to_from_mast_data_columns(mast_data_columns)
     49     sensors = pd.DataFrame(mast_data_columns.get_level_values('sensor'))
     50     details = pd.Series(sensors.sensor).str.split('_', expand=True)
---> 51     details.columns = ['type', 'height', 'orient', 'signal']
     52     details = pd.concat([details,sensors], axis=1)
     53     null_signal = details.signal[details.signal.isnull()].index

~/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
   4387         try:
   4388             object.__getattribute__(self, name)
-> 4389             return object.__setattr__(self, name, value)
   4390         except AttributeError:
   4391             pass

pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set__()

~/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
    644
    645     def _set_axis(self, axis, labels):
--> 646         self._data.set_axis(axis, labels)
    647         self._clear_item_cache()
    648

~/miniconda3/lib/python3.6/site-packages/pandas/core/internals.py in set_axis(self, axis, new_labels)
   3321             raise ValueError(
   3322                 'Length mismatch: Expected axis has {old} elements, new '
-> 3323                 'values have {new} elements'.format(old=old_len, new=new_len))
   3324
   3325         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 3 elements, new values have 4 elements
jadobilo commented 5 years ago

I had the same error. I added the data.columns.name = 'sensor' suggested. Then got that same other error. Despite the indications in the tutorial, which said that the _avg was not mandatory for speed sensors, I added it for all speed sensors and it worked. I don't know if this was the original intent, or if a code modification will be required. Please keep us updated :)

mast_data = pd.DataFrame(index=pd.date_range('2017-01-01 00:00', '2017-01-01 01:00', freq='10T'), data = np.random.rand(7,4), columns=['SPD_58_N_AVG', 'SPD_48_N_AVG', 'SPD_38_N_AVG', 'DIR_56'])

mast_data.columns.name = 'sensor'

mast = an.MetMast(data=mast_data, name=1001, lat=45.0, lon=-90.0, elev=100.0, height=60, primary_ano = 'SPD_58_N', primary_vane = 'DIR_56')

print(mast)