Open-Source-Spatial-Clean-Cooking-Tool / OnStove

This repository contain the general code for the Open Source Spatial Clean Cooking Tool OnStove
MIT License
7 stars 9 forks source link

Error in dimensions when running def carb() #255

Closed babakkhavari closed 2 years ago

babakkhavari commented 2 years ago

When running OnStove the code crashes for base line carb calculation. Seems to be related to the LPG transportation emissions.

[Nepal] Calculating clean cooking access
[Nepal] Calculating base fuel properties

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [37], in <cell line: 3>()
      1 # names = ['Electricity', 'Collected_Traditional_Biomass', 'Collected_Improved_Biomass', 'Charcoal', 'Charcoal ICS', 'LPG', 'Biogas']
      2 names = ['Electricity', 'LPG', 'Biogas', 'Collected_Traditional_Biomass', 'Collected_Improved_Biomass']
----> 3 nepal.run(technologies=names)

File ~\OneDrive - KTH\OnSSTOVE\onsstove\onsstove.py:823, in OnSSTOVE.run(self, technologies, restriction)
    820 if self.base_fuel is None:
    821     print(f'[{self.specs["Country_name"]}] Calculating base fuel properties')
--> 823     self.set_base_fuel(self.techs.values())
    824 if technologies == 'all':
    825     techs = [tech for tech in self.techs.values()]

File ~\OneDrive - KTH\OnSSTOVE\onsstove\onsstove.py:503, in OnSSTOVE.set_base_fuel(self, techs)
    500 current_share = (self.gdf['IsUrban'] > 20) * tech.current_share_urban
    501 current_share[self.gdf['IsUrban'] < 20] = tech.current_share_rural
--> 503 tech.carb(self)
    504 tech.total_time(self)
    505 tech.required_energy(self)

File ~\OneDrive - KTH\OnSSTOVE\onsstove\technology.py:645, in LPG.carb(self, model)
    643 def carb(self, model):
    644     super().carb(model)
--> 645     self.carbon += self.transport_emissions(model)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\generic.py:11375, in NDFrame.__iadd__(self, other)
  11373 def __iadd__(self, other):
  11374     # error: Unsupported left operand type for + ("Type[NDFrame]")
> 11375     return self._inplace_method(other, type(self).__add__)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\generic.py:11352, in NDFrame._inplace_method(self, other, op)
  11347 @final
  11348 def _inplace_method(self, other, op):
  11349     """
  11350     Wrap arithmetic method to operate inplace.
  11351     """
> 11352     result = op(self, other)
  11354     if (
  11355         self.ndim == 1
  11356         and result._indexed_same(self)
  11357         and is_dtype_equal(result.dtype, self.dtype)
  11358     ):
  11359         # GH#36498 this inplace op can _actually_ be inplace.
  11360         self._values[:] = result._values

File ~\.conda\envs\cca\lib\site-packages\pandas\core\ops\common.py:70, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     66             return NotImplemented
     68 other = item_from_zerodim(other)
---> 70 return method(self, other)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\arraylike.py:100, in OpsMixin.__add__(self, other)
     98 @unpack_zerodim_and_defer("__add__")
     99 def __add__(self, other):
--> 100     return self._arith_method(other, operator.add)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\series.py:5639, in Series._arith_method(self, other, op)
   5637 def _arith_method(self, other, op):
   5638     self, other = ops.align_method_SERIES(self, other)
-> 5639     return base.IndexOpsMixin._arith_method(self, other, op)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\base.py:1295, in IndexOpsMixin._arith_method(self, other, op)
   1292 rvalues = ensure_wrapped_if_datetimelike(rvalues)
   1294 with np.errstate(all="ignore"):
-> 1295     result = ops.arithmetic_op(lvalues, rvalues, op)
   1297 return self._construct_result(result, name=res_name)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\ops\array_ops.py:222, in arithmetic_op(left, right, op)
    217 else:
    218     # TODO we should handle EAs consistently and move this check before the if/else
    219     # (https://github.com/pandas-dev/pandas/issues/41165)
    220     _bool_arith_check(op, left, right)
--> 222     res_values = _na_arithmetic_op(left, right, op)
    224 return res_values

File ~\.conda\envs\cca\lib\site-packages\pandas\core\ops\array_ops.py:163, in _na_arithmetic_op(left, right, op, is_cmp)
    160     func = partial(expressions.evaluate, op)
    162 try:
--> 163     result = func(left, right)
    164 except TypeError:
    165     if not is_cmp and (is_object_dtype(left.dtype) or is_object_dtype(right)):
    166         # For object dtype, fallback to a masked operation (only operating
    167         #  on the non-missing values)
    168         # Don't do this for comparisons, as that will handle complex numbers
    169         #  incorrectly, see GH#32047

File ~\.conda\envs\cca\lib\site-packages\pandas\core\computation\expressions.py:239, in evaluate(op, a, b, use_numexpr)
    236 if op_str is not None:
    237     if use_numexpr:
    238         # error: "None" not callable
--> 239         return _evaluate(op, op_str, a, b)  # type: ignore[misc]
    240 return _evaluate_standard(op, op_str, a, b)

File ~\.conda\envs\cca\lib\site-packages\pandas\core\computation\expressions.py:69, in _evaluate_standard(op, op_str, a, b)
     67 if _TEST_MODE:
     68     _store_test_result(False)
---> 69 return op(a, b)

ValueError: operands could not be broadcast together with shapes (96673,) (519,906) 
babakkhavari commented 2 years ago

After some further testing I am thinking that it might be related to the traveltime map. Unless this is relative and the case of Nepal is really heavy on travel time.

image

babakkhavari commented 2 years ago
model.techs['LPG'].travel_time = model.raster_to_dataframe(travel_time.layer,
                                                           nodata=travel_time.meta['nodata'],
                                                           fill_nodata='interpolate', method='read') * 2 / 60

added in transport_emissions(model) and return a series?