Skip to content

Commit

Permalink
fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
AngryMaciek committed Jul 31, 2023
1 parent 1b51119 commit 1817cc0
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 160 deletions.
27 changes: 3 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,10 @@ uninstall:
@python -m pip uninstall moranpycess --yes

format:
@black \
moranpycess/__init__.py \
moranpycess/Individual.py \
moranpycess/MoranProcess.py \
moranpycess/MoranProcess2D.py \
moranpycess/MoranProcess3D.py \
moranpycess/CustomExceptions.py \
tests/unit/context.py \
tests/unit/Individual.py \
tests/unit/MoranProcess.py \
tests/unit/MoranProcess2D.py \
tests/unit/MoranProcess3D.py \
@black moranpycess/ tests/unit/

lint:
@flake8 --max-line-length=88 --ignore F401,E402 moranpycess/__init__.py
@flake8 --max-line-length=88 moranpycess/Individual.py
@flake8 --max-line-length=101 --ignore F401,E231,W503,E741 moranpycess/MoranProcess.py
@flake8 --max-line-length=101 --ignore F401,E231,W503,E741 moranpycess/MoranProcess2D.py
@flake8 --max-line-length=101 --ignore F401,E231,W503,E741 moranpycess/MoranProcess3D.py
@flake8 --max-line-length=88 moranpycess/CustomExceptions.py
@flake8 --max-line-length=88 --ignore F401,E402 tests/unit/context.py
@flake8 --max-line-length=88 tests/unit/Individual.py
@flake8 --max-line-length=88 tests/unit/MoranProcess.py
@flake8 --max-line-length=88 --ignore E231 tests/unit/MoranProcess2D.py
@flake8 --max-line-length=88 --ignore E231 tests/unit/MoranProcess3D.py
@flake8 moranpycess/ tests/unit/

build:
@conda build . -c conda-forge
Expand All @@ -77,4 +56,4 @@ clean:
@find . -type f -name '*.DS_Store' -delete
@rm -rf build moranpycess.egg-info
@rm -rf .coverage .pytest_cache
@rm -rf moranpycess/__pycache__
@rm -rf moranpycess/__pycache__ tests/unit/__pycache__
101 changes: 62 additions & 39 deletions moranpycess/MoranProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ def __init__(
e.args += ("Invalid Transition Matrix",)
raise
# check if the values are correct
message = "Transition probabilities need to add up to 1.0."
for v in np.sum(TransitionMatrix, axis=1):
if v != 1.0:
raise IncorrectValueError(
parameter="Transition Matrix",
message="Transition probabilities need to add up to 1.0.",
message=message,
)
self.TransitionMatrix = copy.deepcopy(TransitionMatrix)

Expand Down Expand Up @@ -403,24 +404,32 @@ def simulate(self, generations):

# prepare a dataframe to store the logs
colnames = (
[l + "__size" for l in self.init_label_list]
+ [l + "__AvgBirthPayoff" for l in self.init_label_list]
+ [l + "__AvgDeathPayoff" for l in self.init_label_list]
+ [l + "__BirthFitness" for l in self.init_label_list]
+ [l + "__DeathFitness" for l in self.init_label_list]
[label + "__size" for label in self.init_label_list]
+ [label + "__AvgBirthPayoff" for label in self.init_label_list]
+ [label + "__AvgDeathPayoff" for label in self.init_label_list]
+ [label + "__BirthFitness" for label in self.init_label_list]
+ [label + "__DeathFitness" for label in self.init_label_list]
+ ["Entropy"]
)
log_df = pd.DataFrame(index=range(generations + 1), columns=colnames)
log_df.index.name = "generation"

# update the dataframe with features of the initial population
for l in range(len(self.init_label_list)):
label = self.init_label_list[l]
log_df.at[0, label + "__size"] = self.init_size_list[l]
log_df.at[0, label + "__AvgBirthPayoff"] = self.AvgBirthPayoffDict[label]
log_df.at[0, label + "__AvgDeathPayoff"] = self.AvgDeathPayoffDict[label]
log_df.at[0, label + "__BirthFitness"] = self.BirthFitnessDict[label]
log_df.at[0, label + "__DeathFitness"] = self.DeathFitnessDict[label]
for index in range(len(self.init_label_list)):
label = self.init_label_list[index]
log_df.at[0, label + "__size"] = self.init_size_list[index]
log_df.at[0, label + "__AvgBirthPayoff"] = self.AvgBirthPayoffDict[
label
]
log_df.at[0, label + "__AvgDeathPayoff"] = self.AvgDeathPayoffDict[
label
]
log_df.at[0, label + "__BirthFitness"] = self.BirthFitnessDict[
label
]
log_df.at[0, label + "__DeathFitness"] = self.DeathFitnessDict[
label
]
log_df.at[0, "Entropy"] = self.Entropy

for g in range(generations):
Expand All @@ -435,8 +444,12 @@ def simulate(self, generations):
# remove the selected individual from the population
self.population.remove(selectedDeath)
# update the list with population info
self.curr_size_list[self.init_label_list.index(selectedBirth.label)] += 1
self.curr_size_list[self.init_label_list.index(selectedDeath.label)] -= 1
self.curr_size_list[
self.init_label_list.index(selectedBirth.label)
] += 1
self.curr_size_list[
self.init_label_list.index(selectedDeath.label)
] -= 1

# perform transitions (if TransitionMatrix was specified)
if self.TransitionMatrix is not None:
Expand All @@ -450,11 +463,15 @@ def simulate(self, generations):
old_label = ind.label
ind.label = new_label
# update the list with population info
self.curr_size_list[self.init_label_list.index(new_label)] += 1
self.curr_size_list[self.init_label_list.index(old_label)] -= 1
self.curr_size_list[
self.init_label_list.index(new_label)
] += 1
self.curr_size_list[
self.init_label_list.index(old_label)
] -= 1

# after each birth-death cycle:
# re-evaluate the payoffs and fitnesses of all Individuals in the population
# re-evaluate the payoffs and fitnesses of all ind in the pop
self._UpdateAvgBirthPayoffForAll()
self._UpdateAvgDeathPayoffForAll()
self._UpdateBirthFitnessForAll()
Expand All @@ -463,21 +480,21 @@ def simulate(self, generations):
self._UpdateEntropy()

# update the log dataframe
for l in range(len(self.init_label_list)):
label = self.init_label_list[l]
log_df.at[g + 1, label + "__size"] = self.curr_size_list[l]
log_df.at[g + 1, label + "__AvgBirthPayoff"] = self.AvgBirthPayoffDict[
label
]
log_df.at[g + 1, label + "__AvgDeathPayoff"] = self.AvgDeathPayoffDict[
label
]
log_df.at[g + 1, label + "__BirthFitness"] = self.BirthFitnessDict[
label
]
log_df.at[g + 1, label + "__DeathFitness"] = self.DeathFitnessDict[
label
]
for index in range(len(self.init_label_list)):
label = self.init_label_list[index]
log_df.at[g + 1, label + "__size"] = self.curr_size_list[index]
log_df.at[
g + 1, label + "__AvgBirthPayoff"
] = self.AvgBirthPayoffDict[label]
log_df.at[
g + 1, label + "__AvgDeathPayoff"
] = self.AvgDeathPayoffDict[label]
log_df.at[
g + 1, label + "__BirthFitness"
] = self.BirthFitnessDict[label]
log_df.at[
g + 1, label + "__DeathFitness"
] = self.DeathFitnessDict[label]
log_df.at[g + 1, "Entropy"] = self.Entropy

return log_df
Expand All @@ -504,7 +521,7 @@ def PlotSize(self, df, path):
for axis in ["top", "bottom", "left", "right"]:
ax.spines[axis].set_linewidth(1)
cmap = plt.get_cmap("coolwarm")
columns = [l + "__size" for l in self.init_label_list]
columns = [label + "__size" for label in self.init_label_list]
df_copy = df[columns].copy()
df_copy.columns = self.init_label_list
df_copy.plot(linewidth=1.5, ax=ax, cmap=cmap)
Expand All @@ -530,7 +547,9 @@ def PlotAvgBirthPayoff(self, df, path):
for axis in ["top", "bottom", "left", "right"]:
ax.spines[axis].set_linewidth(1)
cmap = plt.get_cmap("coolwarm")
columns = [l + "__AvgBirthPayoff" for l in self.init_label_list]
columns = [
label + "__AvgBirthPayoff" for label in self.init_label_list
]
df_copy = df[columns].copy()
df_copy.columns = self.init_label_list
df_copy.plot(linewidth=1.5, ax=ax, cmap=cmap)
Expand All @@ -554,7 +573,9 @@ def PlotAvgDeathPayoff(self, df, path):
for axis in ["top", "bottom", "left", "right"]:
ax.spines[axis].set_linewidth(1)
cmap = plt.get_cmap("coolwarm")
columns = [l + "__AvgDeathPayoff" for l in self.init_label_list]
columns = [
label + "__AvgDeathPayoff" for label in self.init_label_list
]
df_copy = df[columns].copy()
df_copy.columns = self.init_label_list
df_copy.plot(linewidth=1.5, ax=ax, cmap=cmap)
Expand All @@ -578,7 +599,7 @@ def PlotBirthFitness(self, df, path):
for axis in ["top", "bottom", "left", "right"]:
ax.spines[axis].set_linewidth(1)
cmap = plt.get_cmap("coolwarm")
columns = [l + "__BirthFitness" for l in self.init_label_list]
columns = [label + "__BirthFitness" for label in self.init_label_list]
df_copy = df[columns].copy()
df_copy.columns = self.init_label_list
df_copy.plot(linewidth=1.5, ax=ax, cmap=cmap)
Expand All @@ -602,7 +623,7 @@ def PlotDeathFitness(self, df, path):
for axis in ["top", "bottom", "left", "right"]:
ax.spines[axis].set_linewidth(1)
cmap = plt.get_cmap("coolwarm")
columns = [l + "__DeathFitness" for l in self.init_label_list]
columns = [label + "__DeathFitness" for label in self.init_label_list]
df_copy = df[columns].copy()
df_copy.columns = self.init_label_list
df_copy.plot(linewidth=1.5, ax=ax, cmap=cmap)
Expand All @@ -625,7 +646,9 @@ def PlotEntropy(self, df, path):
ax.tick_params(width=1)
for axis in ["top", "bottom", "left", "right"]:
ax.spines[axis].set_linewidth(1)
df["Entropy"].plot(color="black", linewidth=1.5, ax=ax, label="Entropy")
df["Entropy"].plot(
color="black", linewidth=1.5, ax=ax, label="Entropy"
)
plt.xlabel("Generation", size=14)
plt.ylabel("", size=14)
ax.tick_params(axis="both", which="major", labelsize=12)
Expand Down
Loading

0 comments on commit 1817cc0

Please sign in to comment.