Skip to content

Move base plots from LadybugTools_Toolkit #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 120 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,123 @@ Alligator/Alligator_REMOTE_9848.csproj

# User defined files #
######################
build.ps1
build.ps1

# Testing files
.dev/
prototypes/

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
*.ipynb
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# unignored files
!example.sql
12 changes: 11 additions & 1 deletion Python_Engine/Python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@

virtualenv
jupyterlab
black
pylint
pytest
pytest-cov
pytest-order
case-converter
numpy==1.26.4
pandas==2.2.1
matplotlib==3.8.3
7 changes: 7 additions & 0 deletions Python_Engine/Python/run_tests.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SET PYTHON_EXE=C:\ProgramData\BHoM\Extensions\PythonEnvironments\Python_Toolkit\v3_10\python.exe
SET TEST_DIR=C:\ProgramData\BHoM\Extensions\PythonCode\Python_Toolkit\tests

@REM C:\ProgramData\BHoM\Extensions\PythonEnvironments\Python_Toolkit\v3_10\python.exe -m pytest --cov-report term --cov-report html:cov_html --cov python_toolkit -v C:\ProgramData\BHoM\Extensions\PythonCode\Python_Toolkit\tests

"%PYTHON_EXE%" -m pytest --cov-report term --cov-report html:cov_html --cov python_toolkit -v "%TEST_DIR%"
cmd /k
22 changes: 22 additions & 0 deletions Python_Engine/Python/src/python_toolkit/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from .timeseries import (
validate_timeseries,
timeseries_summary_monthly
)
from .cardinality import (
cardinality,
angle_from_cardinal,
angle_from_north,
angle_to_vector
)
from .decay_rate import (
DecayMethod,
proximity_decay,
decay_rate_smoother
)
from .helpers import (
sanitise_string,
convert_keys_to_snake_case,
remove_leap_days,
timedelta_tostring,
safe_filename
)
179 changes: 179 additions & 0 deletions Python_Engine/Python/src/python_toolkit/helpers/cardinality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import numpy as np

from ..bhom.analytics import bhom_analytics

@bhom_analytics()
def cardinality(direction_angle: float, directions: int = 16):
"""Returns the cardinal orientation of a given angle, where that angle is related to north at
0 degrees.
Args:
direction_angle (float):
The angle to north in degrees (+Ve is interpreted as clockwise from north at 0.0
degrees).
directions (int):
The number of cardinal directions into which angles shall be binned (This value should
be one of 4, 8, 16 or 32, and is centred about "north").
Returns:
int:
The cardinal direction the angle represents.
"""

if direction_angle > 360 or direction_angle < 0:
raise ValueError(
"The angle entered is beyond the normally expected range for an orientation in degrees."
)

cardinal_directions = {
4: ["N", "E", "S", "W"],
8: ["N", "NE", "E", "SE", "S", "SW", "W", "NW"],
16: [
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW",
],
32: [
"N",
"NbE",
"NNE",
"NEbN",
"NE",
"NEbE",
"ENE",
"EbN",
"E",
"EbS",
"ESE",
"SEbE",
"SE",
"SEbS",
"SSE",
"SbE",
"S",
"SbW",
"SSW",
"SWbS",
"SW",
"SWbW",
"WSW",
"WbS",
"W",
"WbN",
"WNW",
"NWbW",
"NW",
"NWbN",
"NNW",
"NbW",
],
}

if directions not in cardinal_directions:
raise ValueError(
f'The input "directions" must be one of {list(cardinal_directions.keys())}.'
)

val = int((direction_angle / (360 / directions)) + 0.5)

arr = cardinal_directions[directions]

return arr[(val % directions)]

@bhom_analytics()
def angle_from_cardinal(cardinal_direction: str) -> float:
"""
For a given cardinal direction, return the corresponding angle in degrees.

Args:
cardinal_direction (str):
The cardinal direction.
Returns:
float:
The angle associated with the cardinal direction.
"""
cardinal_directions = [
"N",
"NbE",
"NNE",
"NEbN",
"NE",
"NEbE",
"ENE",
"EbN",
"E",
"EbS",
"ESE",
"SEbE",
"SE",
"SEbS",
"SSE",
"SbE",
"S",
"SbW",
"SSW",
"SWbS",
"SW",
"SWbW",
"WSW",
"WbS",
"W",
"WbN",
"WNW",
"NWbW",
"NW",
"NWbN",
"NNW",
"NbW",
]
if cardinal_direction not in cardinal_directions:
raise ValueError(f"{cardinal_direction} is not a known cardinal_direction.")
angles = np.arange(0, 360, 11.25)

lookup = dict(zip(cardinal_directions, angles))

return lookup[cardinal_direction]

def angle_from_north(vector: list[float]) -> float:
"""For an X, Y vector, determine the clockwise angle to north at [0, 1].

Args:
vector (list[float]):
A vector of length 2.

Returns:
float:
The angle between vector and north in degrees clockwise from [0, 1].
"""
north = [0, 1]
angle1 = np.arctan2(*north[::-1])
angle2 = np.arctan2(*vector[::-1])
return np.rad2deg((angle1 - angle2) % (2 * np.pi))

def angle_to_vector(clockwise_angle_from_north: float) -> list[float]:
"""Return the X, Y vector from of an angle from north at 0-degrees.

Args:
clockwise_angle_from_north (float):
The angle from north in degrees clockwise from [0, 360], though
any number can be input here for angles greater than a full circle.

Returns:
list[float]:
A vector of length 2.
"""

clockwise_angle_from_north = np.radians(clockwise_angle_from_north)

return np.sin(clockwise_angle_from_north), np.cos(clockwise_angle_from_north)
Loading