Skip to content
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

Type hint fix #46

Merged
merged 51 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
096c210
docs|feat [gravity model]: changed type hints in multitld calibration…
Kieran-Fishwick-TfN Sep 9, 2024
559c925
fixes: changed summary to a property and added an abrast base property
Kieran-Fishwick-TfN Sep 9, 2024
61ac7cf
api changes: IN PROGRESS changed mult area gm calibrator init vars - …
Kieran-Fishwick-TfN Sep 9, 2024
50cc9b1
api changes
Kieran-Fishwick-TfN Sep 10, 2024
a8be59d
bug fix wityh undefined class attr
Kieran-Fishwick-TfN Sep 10, 2024
519ce1f
Tidied up and added from pandas function
Kieran-Fishwick-TfN Sep 11, 2024
05fba60
fixes: run method fixes
Kieran-Fishwick-TfN Sep 12, 2024
b52b91f
ran black
Kieran-Fishwick-TfN Sep 13, 2024
c671989
comment
Kieran-Fishwick-TfN Sep 24, 2024
e0c7fd6
added some validation
Kieran-Fishwick-TfN Sep 24, 2024
aad8088
added extra check
Kieran-Fishwick-TfN Sep 24, 2024
10a3070
minor changes to validation, changed tanner param limits and start va…
Kieran-Fishwick-TfN Sep 25, 2024
6a7df03
ran black
Kieran-Fishwick-TfN Sep 25, 2024
2a5163e
added a wrapper around the multicost_distribution class
Kieran-Fishwick-TfN Sep 30, 2024
f065f56
ran black
Kieran-Fishwick-TfN Sep 30, 2024
6580ea6
changed multicostdistribution to allow different values for cost func…
Kieran-Fishwick-TfN Oct 2, 2024
a5b3043
ran black, type hint update
Kieran-Fishwick-TfN Oct 2, 2024
55f0b5a
doctring minor update
Kieran-Fishwick-TfN Oct 2, 2024
05f707c
bug fix with mult cost distribution from pandas constructor
Kieran-Fishwick-TfN Oct 2, 2024
5da8468
Merge branch 'type-hint-fix' of https://github.com/Transport-for-the-…
Kieran-Fishwick-TfN Oct 2, 2024
ef397bc
added option to truncate final bin in plot _distributions
Kieran-Fishwick-TfN Oct 3, 2024
5da00e9
ran black
Kieran-Fishwick-TfN Oct 3, 2024
d3d9171
addressed comments in core
Kieran-Fishwick-TfN Oct 25, 2024
9e4e846
Update .gitignore
Kieran-Fishwick-TfN Oct 25, 2024
bea78b2
Update src/caf/distribute/gravity_model/multi_area.py
Kieran-Fishwick-TfN Oct 25, 2024
02ed30e
Update src/caf/distribute/gravity_model/multi_area.py
Kieran-Fishwick-TfN Oct 25, 2024
247531d
Update src/caf/distribute/gravity_model/multi_area.py
Kieran-Fishwick-TfN Oct 25, 2024
1080523
working through comments
Kieran-Fishwick-TfN Oct 25, 2024
54984f4
addressed most comments, updated tests to the new API
Kieran-Fishwick-TfN Oct 29, 2024
d943bf0
created GMCalibParams as an input to calibrate and reformatted
Kieran-Fishwick-TfN Oct 29, 2024
bcc97c5
bug fixes
Kieran-Fishwick-TfN Oct 29, 2024
4f2e672
docs for new calib params class and a lil tidy up
Kieran-Fishwick-TfN Oct 29, 2024
7648781
updated tests to new API
Kieran-Fishwick-TfN Oct 29, 2024
cd9c3e9
ran black. some linting fixes
Kieran-Fishwick-TfN Oct 29, 2024
ebf4001
ran black (again)
Kieran-Fishwick-TfN Oct 29, 2024
95a0ac0
set python to v3.10 attempt to fix tests on github
Kieran-Fishwick-TfN Oct 29, 2024
6911e52
undo the 3.10 requirements. stupid fix added the annotations import
Kieran-Fishwick-TfN Oct 29, 2024
7506f58
added more annotation imports
Kieran-Fishwick-TfN Oct 29, 2024
bca6aa0
ran isort
Kieran-Fishwick-TfN Oct 29, 2024
2a2d794
linting error fixes
Kieran-Fishwick-TfN Oct 29, 2024
7739a00
updated pylint version pylint
Kieran-Fishwick-TfN Oct 29, 2024
d2c100e
MORE LINTING FIXES!!!!!!
Kieran-Fishwick-TfN Oct 29, 2024
fd84360
MOOOOOOORE PYLINT FIXES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Kieran-Fishwick-TfN Oct 29, 2024
6e2dc71
pylint fixes (please work) and warnings bug fix
Kieran-Fishwick-TfN Oct 29, 2024
f847b31
ran black
Kieran-Fishwick-TfN Oct 29, 2024
5fa2e37
mypy fixes
Kieran-Fishwick-TfN Oct 30, 2024
269942c
ran black PyDocStyle fixes
Kieran-Fishwick-TfN Oct 30, 2024
9b64059
fixing Imperative moods -_-
Kieran-Fishwick-TfN Oct 30, 2024
6f43a74
I dont know what an imperative mood is but I dont like them
Kieran-Fishwick-TfN Oct 30, 2024
a6bd79d
ran black
Kieran-Fishwick-TfN Oct 30, 2024
d7c1d36
removed redundant TODO
Kieran-Fishwick-TfN Oct 31, 2024
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,6 @@ dmypy.json

# Pyre type checker
.pyre/

# VS code settings
.vscode/
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ lint = [
"mypy>=1.0.0",
"mypy_extensions>=1.0.0",
"pydocstyle[toml]>=6.1.1",
"pylint>=2.14.5",
"pylint>=3.2",
]

test = [
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ matplotlib>=3.8.2
# Ipf requirements
sparse>=0.13.0
numba>=0.60.0

2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ isort>=5.12.0
mypy>=1.0.0
mypy_extensions>=1.0.0
pydocstyle[toml]>=6.1.1
pylint>=2.14.5
pylint==3.2

# Testing
pytest>=7.4.0
Expand Down
4 changes: 2 additions & 2 deletions src/caf/distribute/cost_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def get_cost_function(self) -> CostFunction:
if self == BuiltInCostFunction.TANNER:
return CostFunction(
name=self.name,
params={"alpha": (-5, 5), "beta": (-5, 5)},
default_params={"alpha": 1, "beta": 1},
params={"alpha": (-1, 1), "beta": (-1, 1)},
default_params={"alpha": 0.1, "beta": -0.1},
function=tanner,
)

Expand Down
5 changes: 4 additions & 1 deletion src/caf/distribute/gravity_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@

# Models
from caf.distribute.gravity_model.single_area import SingleAreaGravityModelCalibrator
from caf.distribute.gravity_model.multi_area import MultiAreaGravityModelCalibrator
from caf.distribute.gravity_model.multi_area import (
MultiAreaGravityModelCalibrator,
GMCalibParams,
)
83 changes: 74 additions & 9 deletions src/caf/distribute/gravity_model/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
"""Core abstract functionality for gravity model classes to build on."""
from __future__ import annotations

# Built-Ins
import abc
import dataclasses
Expand Down Expand Up @@ -90,41 +92,91 @@ class GravityModelCalibrateResults(GravityModelResults):
# Targets
target_cost_distribution: cost_utils.CostDistribution
cost_function: cost_functions.CostFunction
cost_params: dict[str, Any]
cost_params: dict[str | int, Any]

def plot_distributions(self) -> figure.Figure:
"""
Plot a comparison of the achieved and target distributions.
def plot_distributions(self, truncate_last_bin: bool = False) -> figure.Figure:
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved
"""Plot a comparison of the achieved and target distributions.

This method returns a matplotlib figure which can be saved or plotted
as the user decides.

Parameters
----------
truncate_last_bin : bool, optional
whether to truncate the graph to 1.2x the lower bin edge, by default False

Returns
-------
figure.Figure
the plotted distributions

Raises
------
ValueError
when the target and achieved distributions have different binning
"""

fig, ax = plt.subplots(figsize=(10, 6))

errors = []
for attr in ("max_vals", "min_vals", "avg_vals"):
if set(getattr(self.cost_distribution, attr)) != set(
getattr(self.target_cost_distribution, attr)
):
errors.append(attr)

if len(errors) > 0:
raise ValueError(
"To plot distributions, the target and achieved distributions"
" must have the same binning. The distributions have different "
+ " and ".join(errors)
)

max_bin_edge = self.cost_distribution.max_vals
min_bin_edge = self.cost_distribution.min_vals
bin_centres = self.cost_distribution.avg_vals

ax.bar(
self.cost_distribution.avg_vals,
bin_centres,
self.cost_distribution.band_share_vals,
width=self.cost_distribution.max_vals - self.cost_distribution.min_vals,
width=max_bin_edge - min_bin_edge,
label="Achieved Distribution",
color="blue",
alpha=0.7,
)
ax.bar(
self.cost_distribution.avg_vals,
bin_centres,
self.target_cost_distribution.band_share_vals,
width=self.target_cost_distribution.max_vals
- self.target_cost_distribution.min_vals,
width=max_bin_edge - min_bin_edge,
label="Target Distribution",
color="orange",
alpha=0.7,
)

if truncate_last_bin:
top_min_bin = min_bin_edge.max()
ax.set_xlim(0, top_min_bin[-1] * 1.2)
fig.text(0.8, 0.025, f"final bin edge cut from {max_bin_edge.max()}", ha="center")

ax.set_xlabel("Cost")
ax.set_ylabel("Trips")
ax.set_title("Distribution Comparison")
ax.legend()

return fig

@property
def summary(self) -> pd.Series:
"""Summary of the GM calibration parameters as a series.

Outputs the gravity model achieved parameters and the convergence.

Returns
-------
pd.DataFrame
a summary of the calibration
"""


@dataclasses.dataclass
class GravityModelRunResults(GravityModelResults):
Expand Down Expand Up @@ -162,6 +214,19 @@ class GravityModelRunResults(GravityModelResults):
cost_function: Optional[cost_functions.CostFunction] = None
cost_params: Optional[dict[str, Any]] = None

@property
def summary(self) -> pd.Series:
"""Summary of the GM run parameters as a series.

Outputs the gravity model parameters used to generate the distribution.

Returns
-------
pd.DataFrame
a summary of the run
"""
return pd.Series(self.cost_params)


class GravityModelBase(abc.ABC):
"""Base Class for gravity models.
Expand Down
Loading
Loading