Skip to content

Commit

Permalink
Fixed some linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-tfn committed Dec 13, 2023
1 parent 10feeeb commit 35c3ec6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/caf/distribute/cost_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

# Third Party
import numpy as np
from scipy.stats import lognorm


# Local Imports
Expand Down
17 changes: 7 additions & 10 deletions src/caf/distribute/gravity_model/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
# Built-Ins
import abc
import dataclasses
import functools
import logging
import os
import warnings
from typing import Any, Optional, Union
from typing import Any, Optional

# Third Party
import numpy as np
import pandas as pd
from caf.toolkit import cost_utils, io, timing
from scipy import optimize
from matplotlib import pyplot as plt

from caf.toolkit import cost_utils, io, timing
from caf.distribute import cost_functions
# Local Imports
# pylint: disable=import-error,wrong-import-position
from caf.distribute import cost_functions


# pylint: enable=import-error,wrong-import-position

Expand Down Expand Up @@ -96,6 +95,9 @@ class GravityModelCalibrateResults(GravityModelResults):
cost_params: dict[str, Any]

def plot_distributions(self):
"""
Plot a comparison of the achieved and target distributions.
"""
fig, ax = plt.subplots(figsize=(10, 6))
df_1 = self.cost_distribution.df
df_1["normalised"] = (

Check warning on line 103 in src/caf/distribute/gravity_model/core.py

View check run for this annotation

Codecov / codecov/patch

src/caf/distribute/gravity_model/core.py#L101-L103

Added lines #L101 - L103 were not covered by tests
Expand Down Expand Up @@ -464,11 +466,6 @@ def _calculate_perceived_factors(
def _apply_perceived_factors(self, cost_matrix: np.ndarray) -> np.ndarray:
return cost_matrix * self._perceived_factors

def _calculate_initial_parameters(self) -> dict[str, Any]:
...
# TODO(MB) Make an abstract method for calculating initial parameters
# This already exists in SingleArea class

def _guess_init_params(
self,
cost_args: list[float],
Expand Down
11 changes: 5 additions & 6 deletions src/caf/distribute/gravity_model/multi_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import os
from typing import Optional
from pathlib import Path
import functools

# Third Party
import numpy as np
import pandas as pd
from scipy import optimize
from caf.distribute.gravity_model.core import GravityModelCalibrateResults
import functools

# Local Imports
from caf.toolkit import cost_utils, timing, BaseConfig
Expand Down Expand Up @@ -197,6 +197,7 @@ def __init__(
self.furness_jac = params.furness_jac

def process_tlds(self):
"""Get distributions in the right format for a multi-area gravity model"""
dists = []
for cat in self.tlds.index.unique():
tld = self.tlds.loc[cat]
Expand All @@ -217,8 +218,8 @@ def process_tlds(self):

return dists

def _calculate_perceived_factors(self) -> None:
# TODO this
def _calculate_perceived_factors(self, target_cost_distribution: cost_utils.CostDistribution,
achieved_band_shares: np.ndarray) -> None:
raise NotImplementedError("WIP")

Check warning on line 223 in src/caf/distribute/gravity_model/multi_area.py

View check run for this annotation

Codecov / codecov/patch

src/caf/distribute/gravity_model/multi_area.py#L223

Added line #L223 was not covered by tests

@property
Expand Down Expand Up @@ -450,7 +451,6 @@ def _jacobian_function(
jac_width = len(cost_distributions) * params_len
jacobian = np.zeros((jac_length, jac_width))
# Build seed matrix
# TODO pull this out into a function/method
base_mat = self._create_seed_matrix(cost_distributions, init_params, params_len)
# Calculate net effect of furnessing (saves a lot of time on furnessing here)
furness_factor = np.divide(
Expand Down Expand Up @@ -509,7 +509,6 @@ def _gravity_function(
del diff_step

base_mat = self._create_seed_matrix(cost_distributions, init_params, params_len)
# TODO add furness tolerance as parameter rather than hard coding
matrix, iters, rmse = furness.doubly_constrained_furness(
seed_vals=base_mat,
row_targets=self.row_targets,
Expand All @@ -535,7 +534,7 @@ def _gravity_function(

for i, dist in enumerate(cost_distributions):
j = 0
for name, val in dist.function_params.items():
for name in dist.function_params.keys():
log_costs[f"{name}_{i}"] = init_params[params_len * i + j]
j += 1
log_costs[f"convergence_{i}"] = convergences[dist.name]
Expand Down
9 changes: 5 additions & 4 deletions src/caf/distribute/gravity_model/single_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

# Third Party
import numpy as np
from caf.toolkit import cost_utils, timing, toolbox
from scipy import optimize

# Local Imports
# pylint: disable=import-error,wrong-import-position
from caf.toolkit import cost_utils, timing, toolbox
from caf.distribute import cost_functions, furness
from caf.distribute.gravity_model import (
core,
GravityModelCalibrateResults,
GravityModelRunResults,
)

# Local Imports
# pylint: disable=import-error,wrong-import-position


# pylint: enable=import-error,wrong-import-position

# # # CONSTANTS # # #
Expand Down
1 change: 0 additions & 1 deletion src/caf/distribute/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def infill_cost_matrix(
-------
np.ndarray: The input matrix with values infilled.
"""
# TODO add multiple factors by area-type
min_row = np.min(np.ma.masked_where(cost_matrix <= 0, cost_matrix), axis=1) * diag_factor
np.fill_diagonal(cost_matrix, min_row)
cost_matrix[cost_matrix > 1e10] = zeros_infill
Expand Down

0 comments on commit 35c3ec6

Please sign in to comment.