diff --git a/.github/workflows/eluc.yml b/.github/workflows/eluc.yml index d5a9ec1..1e6241a 100644 --- a/.github/workflows/eluc.yml +++ b/.github/workflows/eluc.yml @@ -30,7 +30,7 @@ jobs: pip install pylint if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Lint with PyLint - run: pylint --ignore="demo" --recursive=y --fail-under=9 ./* + run: pylint ./* - name: Run unit tests run: python -m unittest diff --git a/use_cases/eluc/.pylintrc b/use_cases/eluc/.pylintrc index 036fe36..b65f941 100644 --- a/use_cases/eluc/.pylintrc +++ b/use_cases/eluc/.pylintrc @@ -1,5 +1,9 @@ [MASTER] -ignore=demo +ignore=demo, prescriptors/esp + +recursive=y + +fail-under=9.0 jobs=0 @@ -7,4 +11,4 @@ max-line-length=120 suggestion-mode=yes -good-names=X_train, X_val, X_test, y_train, y_val, y_test, X, Y, y, X_test_scaled \ No newline at end of file +good-names=X_train, X_val, X_test, y_train, y_val, y_test, X, Y, y, X_test_scaled, df, da, ds, i, n \ No newline at end of file diff --git a/use_cases/eluc/README.md b/use_cases/eluc/README.md index e1499c6..a3fdf4b 100644 --- a/use_cases/eluc/README.md +++ b/use_cases/eluc/README.md @@ -23,7 +23,7 @@ BLUE simulations with committed emissions could be used to estimate the long-ter "Committed emissions" means all the emissions that are caused by a land-use change event are attributed to the year of the event. BLUE (bookkeeping of land use emissions) is a bookkeeping model that attributes carbon fluxes to land use activities. -See [BLUE: Bookkeeping of land use emissions](https://doi.org/10.1002/2014GB004997) for more details. +See [BLUE: Bookkeeping of Land Use Emissions](https://doi.org/10.1002/2014GB004997) for more details. ### LUC diff --git a/use_cases/eluc/data/conversion.py b/use_cases/eluc/data/conversion.py index f71e099..efe1e72 100644 --- a/use_cases/eluc/data/conversion.py +++ b/use_cases/eluc/data/conversion.py @@ -58,6 +58,8 @@ def construct_countries_df(): for i in range(len(countries_df)): old_abbrev = countries_df.iloc[i]["abbrevs"] if old_abbrev in MANUAL_MAP and MANUAL_MAP[old_abbrev] in codes_df["Numeric code"].unique(): - countries_df.iloc[i]["abbrevs"] = codes_df[codes_df["Numeric code"] == MANUAL_MAP[old_abbrev]]["Alpha-2 code"].iloc[0] + numeric_code = codes_df["Numeric code"] + old_abbrev = MANUAL_MAP[old_abbrev] + countries_df.iloc[i]["abbrevs"] = codes_df[numeric_code == old_abbrev]["Alpha-2 code"].iloc[0] return countries_df \ No newline at end of file diff --git a/use_cases/eluc/prescriptors/nsga2/nsga2_utils.py b/use_cases/eluc/prescriptors/nsga2/nsga2_utils.py index 7f39cc8..72f92c6 100644 --- a/use_cases/eluc/prescriptors/nsga2/nsga2_utils.py +++ b/use_cases/eluc/prescriptors/nsga2/nsga2_utils.py @@ -64,7 +64,7 @@ def calculate_crowding_distance(front): n_objectives = len(front[0].metrics) distances = [0 for _ in range(len(front))] for m in range(n_objectives): - front.sort(key=lambda c: c.metrics[m]) + front.sort(key=lambda cand: cand.metrics[m]) obj_min = front[0].metrics[m] obj_max = front[-1].metrics[m] distances[0] = float('inf') diff --git a/use_cases/eluc/prescriptors/nsga2/torch_prescriptor.py b/use_cases/eluc/prescriptors/nsga2/torch_prescriptor.py index 5b06b62..cf0ed81 100644 --- a/use_cases/eluc/prescriptors/nsga2/torch_prescriptor.py +++ b/use_cases/eluc/prescriptors/nsga2/torch_prescriptor.py @@ -32,7 +32,7 @@ def __init__(self, batch_size: int, candidate_params: dict, seed_dir=None): - + self.candidate_params = candidate_params self.pop_size = pop_size self.n_generations = n_generations @@ -149,7 +149,7 @@ def _select_parents(self, candidates: list[Candidate], n_parents: int) -> list[C for candidate, distance in zip(front, crowding_distance): candidate.distance = distance if len(parents) + len(front) > n_parents: # If adding this front exceeds num_parents - front = sorted(front, key=lambda c: c.distance, reverse=True) + front = sorted(front, key=lambda cand: cand.distance, reverse=True) parents += front[:n_parents - len(parents)] break parents += front @@ -169,7 +169,7 @@ def _make_new_pop(self, parents: list[Candidate], pop_size: int, gen:int) -> lis Makes new population by creating children from parents. We use tournament selection to select parents for crossover. """ - sorted_parents = sorted(parents, key=lambda c: (c.rank, -c.distance)) + sorted_parents = sorted(parents, key=lambda cand: (cand.rank, -cand.distance)) children = [] for i in range(pop_size): parent1, parent2 = self._tournament_selection(sorted_parents) @@ -226,15 +226,15 @@ def _record_gen_results(self, gen: int, candidates: list[Candidate], save_path: Save the pareto front to disk. """ # Save statistics of candidates - gen_results = [c.record_state() for c in candidates] + gen_results = [cand.record_state() for cand in candidates] gen_results_df = pd.DataFrame(gen_results) gen_results_df.to_csv(save_path / f"{gen}.csv", index=False) # Save rank 1 candidate state dicts (save_path / f"{gen}").mkdir(parents=True, exist_ok=True) - pareto_candidates = [c for c in candidates if c.rank == 1] - for c in pareto_candidates: - torch.save(c.state_dict(), save_path / f"{gen}" / f"{c.gen}_{c.cand_id}.pt") + pareto_candidates = [cand for cand in candidates if cand.rank == 1] + for cand in pareto_candidates: + torch.save(cand.state_dict(), save_path / f"{gen}" / f"{cand.gen}_{cand.cand_id}.pt") def _record_candidate_avgs(self, gen: int, candidates: list[Candidate]) -> dict: """ @@ -242,7 +242,7 @@ def _record_candidate_avgs(self, gen: int, candidates: list[Candidate]) -> dict: """ avg_eluc = np.mean([c.metrics[0] for c in candidates]) avg_change = np.mean([c.metrics[1] for c in candidates]) - return {"gen": gen, "eluc": avg_eluc, "change": avg_change} + return {"gen": gen, "eluc": avg_eluc, "change": avg_change} def prescribe_land_use(self, context_df: pd.DataFrame, **kwargs) -> pd.DataFrame: """ diff --git a/use_cases/eluc/tests/test_nsga2.py b/use_cases/eluc/tests/test_nsga2.py index 4e965ff..6dcfb20 100644 --- a/use_cases/eluc/tests/test_nsga2.py +++ b/use_cases/eluc/tests/test_nsga2.py @@ -21,7 +21,7 @@ def get_fields(df): Dummy fields method to create the encoder for dummy data. """ fields_df = df[constants.CAO_MAPPING["context"] + constants.CAO_MAPPING["actions"] + ["ELUC"]].astype("float64") - fields = dict() + fields = {} for col in constants.CAO_MAPPING["context"] + constants.CAO_MAPPING["actions"] + ["ELUC"]: # Set range of land and diff land uses manually to their true ranges because they # do not need to be scaled @@ -52,7 +52,7 @@ def get_fields(df): "valued": "CONTINUOUS" } - return fields + return fields class TestTorchPrescriptor(unittest.TestCase): """