Skip to content

Commit

Permalink
BHoM managed Python virtualenvs (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Greenroyd authored Sep 9, 2022
2 parents f082aa5 + e7e0111 commit 039d248
Show file tree
Hide file tree
Showing 265 changed files with 35,683 additions and 7,651 deletions.
21 changes: 21 additions & 0 deletions .ci/unit_tests/python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pathlib import Path

import pandas as pd
from ladybug.epw import EPW
from ladybugtools_toolkit.external_comfort.materials.materials import Materials

EPW_OBJ = EPW(Path(__file__).parent / "files" / "GBR_London.Gatwick.037760_IWEC.epw")
EPW_DF = pd.read_csv(
Path(__file__).parent / "files" / "GBR_London.Gatwick.037760_IWEC.csv",
parse_dates=True,
index_col=0,
header=0,
)
RES_FILE = Path(__file__).parent / "files" / "example.res"
SQL_FILE = Path(__file__).parent / "files" / "example.sql"
ILL_FILE = Path(__file__).parent / "files" / "example.ill"
PTS_FILE = Path(__file__).parent / "files" / "example.pts"

GROUND_MATERIAL = Materials.ASPHALT_PAVEMENT.value
SHADE_MATERIAL = Materials.FABRIC.value
IDENTIFIER = "pytest"
90 changes: 90 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_external_comfort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import matplotlib.pyplot as plt
from ladybugtools_toolkit.external_comfort.external_comfort import ExternalComfort
from ladybugtools_toolkit.external_comfort.simulate.simulation_result import (
SimulationResult,
)
from ladybugtools_toolkit.external_comfort.typology.typologies import Typologies
from matplotlib import pyplot as plt

from .. import EPW_OBJ, GROUND_MATERIAL, IDENTIFIER, SHADE_MATERIAL

SIM_RESULT = SimulationResult(EPW_OBJ, GROUND_MATERIAL, SHADE_MATERIAL, IDENTIFIER)
EC_RESULT = ExternalComfort(SIM_RESULT, Typologies.OPENFIELD.value)


def test_to_dict():
keys = [
"universal_thermal_climate_index",
"dry_bulb_temperature",
"relative_humidity",
"mean_radiant_temperature",
"wind_speed",
"name",
"shelters",
"evaporative_cooling_effectiveness",
"epw",
"ground_material",
"shade_material",
"model",
"shaded_above_temperature",
"shaded_below_temperature",
"shaded_diffuse_radiation",
"shaded_direct_radiation",
"shaded_longwave_mean_radiant_temperature",
"shaded_mean_radiant_temperature",
"shaded_total_radiation",
"unshaded_above_temperature",
"unshaded_below_temperature",
"unshaded_diffuse_radiation",
"unshaded_direct_radiation",
"unshaded_longwave_mean_radiant_temperature",
"unshaded_mean_radiant_temperature",
"unshaded_total_radiation",
]
d = EC_RESULT.to_dict()
for key in keys:
assert key in d.keys()


def test_to_dataframe():
assert EC_RESULT.to_dataframe().shape == (8760, 5)


def test_plot_utci_day_comfort_metrics():
assert isinstance(EC_RESULT.plot_utci_day_comfort_metrics(), plt.Figure)
plt.close("all")


def test_plot_utci_heatmap():
assert isinstance(EC_RESULT.plot_utci_heatmap(), plt.Figure)
plt.close("all")


def test_plot_utci_heatmap_histogram():
assert isinstance(EC_RESULT.plot_utci_heatmap_histogram(), plt.Figure)
plt.close("all")


def test_plot_utci_distance_to_comfortable():
assert isinstance(EC_RESULT.plot_utci_distance_to_comfortable(), plt.Figure)
plt.close("all")


def test_plot_dbt_heatmap():
assert isinstance(EC_RESULT.plot_dbt_heatmap(), plt.Figure)
plt.close("all")


def test_plot_rh_heatmap():
assert isinstance(EC_RESULT.plot_rh_heatmap(), plt.Figure)
plt.close("all")


def test_plot_ws_heatmap():
assert isinstance(EC_RESULT.plot_ws_heatmap(), plt.Figure)
plt.close("all")


def test_plot_mrt_heatmap():
assert isinstance(EC_RESULT.plot_mrt_heatmap(), plt.Figure)
plt.close("all")
16 changes: 16 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_ground_temperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from ladybugtools_toolkit.external_comfort.ground_temperature.ground_temperature_at_depth import (
ground_temperature_at_depth,
)
from ladybugtools_toolkit.external_comfort.ground_temperature.hourly_ground_temperature import (
hourly_ground_temperature,
)

from .. import EPW_OBJ


def test_ground_temperature_at_depth():
assert ground_temperature_at_depth(EPW_OBJ, 0.5).average == 10.108297296260833


def test_hourly_ground_temperature():
hourly_ground_temperature(EPW_OBJ)
43 changes: 43 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
from ladybugtools_toolkit.external_comfort.materials.materials import Materials
from ladybugtools_toolkit.external_comfort.model.create_ground_zone import (
create_ground_zone,
)
from ladybugtools_toolkit.external_comfort.model.create_model import create_model
from ladybugtools_toolkit.external_comfort.model.create_shade_valence import (
create_shade_valence,
)
from ladybugtools_toolkit.external_comfort.model.create_shade_zone import (
create_shade_zone,
)

from .. import IDENTIFIER


def test_create_ground_zone():
assert create_ground_zone(Materials.ASPHALT_PAVEMENT.value).volume == 10 * 10 * 1


def test_create_ground_zone_material():
with pytest.raises(AssertionError):
create_ground_zone("not_a_material")


def test_create_shade_valence():
assert create_shade_valence()[0].area == 10 * 3


def test_create_shade_zone():
assert create_shade_zone(Materials.FABRIC.value).volume == 10 * 10 * 0.2


def test_create_shade_zone_material():
with pytest.raises(AssertionError):
create_shade_zone("not_a_material")


def test_create_model():
model = create_model(
Materials.ASPHALT_PAVEMENT.value, Materials.FABRIC.value, identifier=IDENTIFIER
)
assert model.identifier == IDENTIFIER
30 changes: 30 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_moisture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from ladybugtools_toolkit.external_comfort.moisture.evaporative_cooling_effect import (
evaporative_cooling_effect,
)
from ladybugtools_toolkit.external_comfort.moisture.evaporative_cooling_effect_collection import (
evaporative_cooling_effect_collection,
)

from .. import EPW_OBJ


def test_evaporative_cooling_effect():
dbt, rh = evaporative_cooling_effect(20, 50, 0.5)
assert (dbt == pytest.approx(16.9, rel=0.1)) and (rh == pytest.approx(75, rel=0.1))


def test_evaporative_cooling_effect_collection():
dbt, rh = evaporative_cooling_effect_collection(
EPW_OBJ, evaporative_cooling_effectiveness=0.3
)
assert (pytest.approx(dbt.average, rel=0.1) == 9.638794225575671) and (
pytest.approx(rh.average, rel=0.1) == 85.50440639269416
)


def test_evaporative_cooling_effect_collection_bad():
with pytest.raises(ValueError):
evaporative_cooling_effect_collection(
EPW_OBJ, evaporative_cooling_effectiveness=1.2
)
42 changes: 42 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_shelter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from ladybugtools_toolkit.external_comfort.shelter.shelter import Shelter
from ladybugtools_toolkit.ladybug_extension.epw.sun_position_list import (
sun_position_list,
)

from .. import EPW_OBJ

SUNS = sun_position_list(EPW_OBJ)


def test_sun_blocked_south():
south_shelter = Shelter(porosity=0, altitude_range=(0, 90), azimuth_range=(90, 270))
south_suns = [
i for i in SUNS if (i.azimuth > 90) and (i.azimuth < 270) and (i.altitude > 0)
]
assert sum(south_shelter.sun_blocked(south_suns)) == len(south_suns)


def test_sun_blocked_north():
north_shelter = Shelter(porosity=0, altitude_range=(0, 90), azimuth_range=(270, 90))
north_suns = [
i for i in SUNS if ((i.azimuth < 90) or (i.azimuth > 270)) and (i.altitude > 0)
]
assert sum(north_shelter.sun_blocked(north_suns)) == len(north_suns)


def test_sky_blocked_opaque():
shelter = Shelter(porosity=0, altitude_range=(0, 90), azimuth_range=(0, 360))
assert shelter.sky_blocked() == 1


def test_sky_blocked_porous():
shelter = Shelter(porosity=0.5, altitude_range=(0, 90), azimuth_range=(0, 360))
assert shelter.sky_blocked() == 0.5


def test_effective_wind_speed():
shelter = Shelter(porosity=0.5, altitude_range=(0, 90), azimuth_range=(0, 360))
assert shelter.effective_wind_speed(EPW_OBJ).average == pytest.approx(
0.40605379566207317
)
42 changes: 42 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_simulate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from ladybugtools_toolkit.external_comfort.materials.materials import Materials
from ladybugtools_toolkit.external_comfort.model.create_model import create_model
from ladybugtools_toolkit.external_comfort.simulate.longwave_mean_radiant_temperature import (
longwave_mean_radiant_temperature,
)
from ladybugtools_toolkit.external_comfort.simulate.solar_radiation import (
solar_radiation,
)
from ladybugtools_toolkit.external_comfort.simulate.surface_temperature import (
surface_temperature,
)

from .. import EPW_OBJ, GROUND_MATERIAL, IDENTIFIER, SHADE_MATERIAL

model = create_model(
ground_material=GROUND_MATERIAL,
shade_material=SHADE_MATERIAL,
identifier=IDENTIFIER,
)


def test_solar_radiation():
result = solar_radiation(model, EPW_OBJ)
assert result["unshaded_total_radiation"].average == pytest.approx(203, rel=1.5)


def test_surface_temperature():
result = surface_temperature(model, EPW_OBJ)
assert (
result["unshaded_above_temperature"].average == EPW_OBJ.sky_temperature.average
)


def test_longwave_mean_radiant_temperature():
assert (
longwave_mean_radiant_temperature(
[EPW_OBJ.dry_bulb_temperature * 0.5, EPW_OBJ.dry_bulb_temperature * 2],
view_factors=[0.25, 0.75],
).average
== 16.924573102344482
)
34 changes: 34 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_thermal_comfort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from ladybug.analysisperiod import AnalysisPeriod
from ladybug_comfort.collection.utci import UTCI
from ladybugtools_toolkit.external_comfort.thermal_comfort.utci.describe_utci_collection import (
describe_utci_collection,
)
from ladybugtools_toolkit.external_comfort.thermal_comfort.utci.utci import utci

from .. import EPW_OBJ

LB_UTCI_COLLECTION = UTCI(
EPW_OBJ.dry_bulb_temperature,
EPW_OBJ.relative_humidity,
EPW_OBJ.dry_bulb_temperature,
EPW_OBJ.wind_speed,
).universal_thermal_climate_index


def test_utci():
assert utci(
EPW_OBJ.dry_bulb_temperature.values,
EPW_OBJ.relative_humidity.values,
EPW_OBJ.dry_bulb_temperature.values,
EPW_OBJ.wind_speed.values,
).mean() == pytest.approx(LB_UTCI_COLLECTION.average, rel=2)


def test_describe_utci_collection():
assert (
describe_utci_collection(
LB_UTCI_COLLECTION, AnalysisPeriod(st_month=6, end_month=3)
)
== 'For Jun 01 to Mar 31 between 00:00 and 23:00, every hour, "No thermal stress" is expected for 2633 out of a possible 7296 hours (36.1%). "Cold stress" is expected for 4655 hours (63.8%). "Heat stress" is expected for 8 hours (0.1%).'
)
49 changes: 49 additions & 0 deletions .ci/unit_tests/python/external_comfort/test_typology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy as np
import pytest
from ladybugtools_toolkit.external_comfort.shelter.shelter import Shelter
from ladybugtools_toolkit.external_comfort.simulate.simulation_result import (
SimulationResult,
)
from ladybugtools_toolkit.external_comfort.typology.typology import Typology

from .. import EPW_OBJ, GROUND_MATERIAL, IDENTIFIER, SHADE_MATERIAL

TYPOLOGY = Typology(
name=IDENTIFIER,
shelters=[Shelter(porosity=0.5, altitude_range=(45, 90), azimuth_range=(90, 270))],
evaporative_cooling_effectiveness=0.1,
)


def test_sky_exposure():
assert TYPOLOGY.sky_exposure() == pytest.approx(0.9267766952966369, rel=0.1)


def test_sun_exposure():
x = np.array(TYPOLOGY.sun_exposure(EPW_OBJ))
assert x[~np.isnan(x)].sum() == 4093


def test_dry_bulb_temperature():
assert TYPOLOGY.dry_bulb_temperature(EPW_OBJ).average == pytest.approx(
10.036858349164525, rel=0.1
)


def test_relative_humidity():
assert TYPOLOGY.relative_humidity(EPW_OBJ).average == pytest.approx(
81.36280821917846, rel=0.1
)


def test_wind_speed():
assert TYPOLOGY.wind_speed(EPW_OBJ).average == pytest.approx(
2.026758704337967, rel=0.1
)


def test_mean_radiant_temperature():
sim_result = SimulationResult(EPW_OBJ, GROUND_MATERIAL, SHADE_MATERIAL, IDENTIFIER)
assert TYPOLOGY.mean_radiant_temperature(sim_result).average == pytest.approx(
15.64262493993061, rel=0.5
)
Loading

0 comments on commit 039d248

Please sign in to comment.