Skip to content

Commit

Permalink
Merge branch 'lint' into documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
danyoungday committed Apr 2, 2024
2 parents eba2ec7 + b561ae7 commit 3f11496
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/eluc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

8 changes: 6 additions & 2 deletions use_cases/eluc/.pylintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[MASTER]
ignore=demo
ignore=demo, prescriptors/esp

recursive=y

fail-under=9.0

jobs=0

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
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
2 changes: 1 addition & 1 deletion use_cases/eluc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion use_cases/eluc/data/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion use_cases/eluc/prescriptors/nsga2/nsga2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 8 additions & 8 deletions use_cases/eluc/prescriptors/nsga2/torch_prescriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -226,23 +226,23 @@ 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:
"""
Gets the average eluc and change for a population of candidates.
"""
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:
"""
Expand Down
4 changes: 2 additions & 2 deletions use_cases/eluc/tests/test_nsga2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,7 +52,7 @@ def get_fields(df):
"valued": "CONTINUOUS"
}

return fields
return fields

class TestTorchPrescriptor(unittest.TestCase):
"""
Expand Down

0 comments on commit 3f11496

Please sign in to comment.