Skip to content

Commit

Permalink
hffms tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgboss committed Feb 23, 2024
1 parent fafc2e0 commit bbde64c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions api/app/fire_behaviour/cffdrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import math
from typing import Optional
import rpy2
import rpy2.robjects as robjs
from rpy2.robjects import pandas2ri
from rpy2.rinterface import NULL
Expand All @@ -11,6 +12,7 @@
from app.utils.singleton import Singleton
from app.schemas.fba_calc import FuelTypeEnum


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -806,19 +808,21 @@ def hourly_fine_fuel_moisture_code(weatherstream: pd.DataFrame, ffmc_old: float,

# We have to change field names to exactly what the CFFDRS lib expects.
# This may need to be adjusted depending on the future data input model, which is currently unknown
column_name_map = {'temperature':'temp', 'relative_humidity': 'rh', 'wind_speed': 'ws', 'precipitation': 'prec'}
column_name_map = {'temperature':'temp', 'relative_humidity': 'rh', 'wind_speed': 'ws', 'precipitation': 'prec', 'datetime': 'hr'}
weatherstream = weatherstream.rename(columns=column_name_map)

r_weatherstream = pandas_to_r_converter(weatherstream)

result = CFFDRS.instance().cffdrs.hffmc(weatherstream=r_weatherstream,
try:
result = CFFDRS.instance().cffdrs.hffmc(r_weatherstream,
ffmc_old=ffmc_old, time_step=time_step, calc_step=calc_step,
batch=batch, hourlyFWI=hourly_fwi)

if isinstance(result, robjs.vectors.FloatVector):
weatherstream['hffmc'] = list(result)
return weatherstream
raise CFFDRSException("Failed to calculate hffmc")
if isinstance(result, robjs.vectors.FloatVector):
weatherstream['hffmc'] = list(result)
return weatherstream
except rpy2.rinterface_lib.embedded.RRuntimeError as e:
logger.error(f"An error occurred when calculating hourly ffmc: {e}")
raise CFFDRSException("Failed to calculate hffmc")


def get_ffmc_for_target_hfi(
Expand Down
4 changes: 2 additions & 2 deletions api/app/tests/fire_behavior/test_cffdrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'datetime': hourly_datetimes,
'temp': np.random.default_rng(111).uniform(20.0, 30.0, size=len(hourly_datetimes)),
'rh': np.random.default_rng(111).uniform(40.0, 100.0, size=len(hourly_datetimes)),
'precip': np.random.default_rng(111).uniform(0.0, 1.0, size=len(hourly_datetimes)),
'precipitation': np.random.default_rng(111).uniform(0.0, 1.0, size=len(hourly_datetimes)),
'ws': np.random.default_rng(111).uniform(0.0, 30.0, size=len(hourly_datetimes)),
}

Expand All @@ -39,7 +39,7 @@ def test_hourly_ffmc_calculates_values():

def test_hourly_ffmc_no_temperature():
ffmc_old = 80.0
df_hourly = pd.DataFrame({'celsius': [12, 1], 'precip': [0, 1], 'ws': [14, 12], 'rh':[50, 50]})
df_hourly = pd.DataFrame({'datetime': [hourly_datetimes[0], hourly_datetimes[1]], 'celsius': [12, 1], 'precipitation': [0, 1], 'ws': [14, 12], 'rh':[50, 50]})

with pytest.raises(CFFDRSException):
hourly_fine_fuel_moisture_code(df_hourly, ffmc_old)
Expand Down

0 comments on commit bbde64c

Please sign in to comment.