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 22 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/


settings.json
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved
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
78 changes: 72 additions & 6 deletions src/caf/distribute/gravity_model/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,62 @@ class GravityModelCalibrateResults(GravityModelResults):
cost_function: cost_functions.CostFunction
cost_params: dict[str, Any]

def plot_distributions(self) -> figure.Figure:
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.
"""

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

if (
(
set(self.cost_distribution.max_vals)
!= set(self.target_cost_distribution.max_vals)
)
or (
set(self.cost_distribution.min_vals)
!= set(self.target_cost_distribution.min_vals)
)
or (
set(self.cost_distribution.avg_vals)
!= set(self.target_cost_distribution.avg_vals)
)
):
raise ValueError(
"To plot distributions, the target and achieved distributions must have the same binning."
)
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved

max_bin_edge = self.cost_distribution.max_vals

min_bin_edge = self.cost_distribution.min_vals

bin_centres = self.cost_distribution.avg_vals
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved

if truncate_last_bin:
max_bin = max_bin_edge.max()
max_bin_edge[max_bin_edge.argmax()] = min_bin_edge[max_bin_edge.argmax()] * 1.2

bin_centres[max_bin_edge.argmax()] = (
max_bin_edge[max_bin_edge.argmax()] + min_bin_edge[max_bin_edge.argmax()]
) / 2

fig.text(0.8, 0.025, f"final bin edge cut from {max_bin}", ha="center")
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved

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,
Expand All @@ -125,6 +160,22 @@ def plot_distributions(self) -> figure.Figure:

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.
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
pd.DataFrame
a summary of the calibration
"""
output_params = self.cost_params.copy()
output_params["convergence"] = self.cost_convergence
return pd.Series(output_params)


@dataclasses.dataclass
class GravityModelRunResults(GravityModelResults):
Expand Down Expand Up @@ -162,6 +213,21 @@ 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.
Parameters
Kieran-Fishwick-TfN marked this conversation as resolved.
Show resolved Hide resolved

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