Skip to content

Commit cc8e3cb

Browse files
Align Python code style via pre-commit (#461)
* Apply `isort`, `pyupgrade` and `autopep8` via the pre-commit
1 parent 0acd83b commit cc8e3cb

File tree

61 files changed

+643
-438
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+643
-438
lines changed

.github/pull_request_template.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Always to be checked:
88
* [ ] There is at least one issue associated with the pull request.
99
* [ ] The branch follows the naming conventions as defined in the [git workflow](git-workflow).
1010
* [ ] New code adheres with the [coding guidelines](coding-guidelines)
11+
* [ ] Make sure that the [pre-commit linting/style checks pass](https://github.com/DLR-SC/memilio/wiki).
1112

1213
If functions were changed or functionality was added:
1314
* [ ] Tests for new functionality has been added

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ on:
1313
workflow_dispatch:
1414

1515
jobs:
16+
pre-commit:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
- uses: actions/setup-python@v2
21+
- uses: pre-commit/[email protected]
22+
1623
build-cpp-gcc_clang:
1724
if: github.event.pull_request.draft == false
1825
strategy:

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exclude: ^(docs|cpp)/
2+
repos:
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v4.3.0
5+
hooks:
6+
- id: check-merge-conflict
7+
- id: check-yaml
8+
- repo: https://github.com/PyCQA/isort
9+
rev: 5.10.1
10+
hooks:
11+
- id: isort
12+
name: isort
13+
- repo: https://github.com/asottile/pyupgrade
14+
rev: v3.2.2
15+
hooks:
16+
- id: pyupgrade
17+
args: [--py37-plus]
18+
- repo: https://github.com/pre-commit/mirrors-autopep8
19+
rev: v2.0.0
20+
hooks:
21+
- id: autopep8

pycode/examples/epidata/PlotCaseData.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
WARNING: This file is currently not tested and maintained.
2424
"""
2525

26-
import pandas as pd
26+
import datetime
2727
import os
28+
2829
import matplotlib.pyplot as plt
29-
import datetime
30+
import pandas as pd
3031

31-
import memilio.epidata.getDIVIData as getDIVIData
32-
import memilio.epidata.getCaseData as getCaseData
3332
import memilio.epidata.defaultDict as dd
33+
import memilio.epidata.getCaseData as getCaseData
34+
import memilio.epidata.getDIVIData as getDIVIData
3435

3536
yesterday = pd.Timestamp(datetime.date.today()) - pd.DateOffset(days=1)
3637

@@ -194,7 +195,8 @@ def plot_cases_age(
194195
df.loc[mask & (df["Age_RKI"] == value)]['Date'],
195196
df.loc[mask & (df["Age_RKI"] == value)]["Confirmed"])
196197
plt.legend(ages)
197-
plt.title('7-days moving average of age-resolved cumulative confirmed infections')
198+
plt.title(
199+
'7-days moving average of age-resolved cumulative confirmed infections')
198200
plt.xlabel('Date')
199201
plt.xticks(rotation=25)
200202
plt.ylabel('Confirmed cases')
@@ -267,10 +269,12 @@ def plot_cases_county(
267269
data_folder, "cases_all_county_ma7.json"))
268270
mask = (df['Date'] >= daystart) & (
269271
df['Date'] <= daystart + pd.DateOffset(days=simulationperiod)) & (df["County"] == county)
270-
fig_name = 'cases_confirmed_infections_county_' + county.replace(" ", "_") + '_ma7'
272+
fig_name = 'cases_confirmed_infections_county_' + \
273+
county.replace(" ", "_") + '_ma7'
271274
plt.figure(fig_name)
272275
plt.plot(df.loc[mask]["Date"], df.loc[mask]["Confirmed"])
273-
plt.title('7-days moving average of cumulative confirmed infections in ' + county)
276+
plt.title(
277+
f'7-days moving average of cumulative confirmed infections in ' + county)
274278
plt.xlabel('Date')
275279
plt.xticks(rotation=25)
276280
plt.ylabel('Confirmed cases')
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#############################################################################
22
# Copyright (C) 2020-2021 German Aerospace Center (DLR-SC)
33
#
4-
# Authors:
4+
# Authors:
55
#
66
# Contact: Martin J. Kuehn <[email protected]>
77
#
@@ -20,11 +20,12 @@
2020
"""@PlotGermanData.py
2121
WARNING: This file is currently not tested and maintained.
2222
"""
23-
import pandas as pd
24-
import numpy as np
25-
import matplotlib.pyplot as plt
2623
from datetime import datetime
2724

25+
import matplotlib.pyplot as plt
26+
import numpy as np
27+
import pandas as pd
28+
2829
df = pd.read_json('cases_all_age.json')
2930

3031
for i in range(len(df)):
@@ -34,46 +35,50 @@
3435
ages = df.Age_RKI.unique()
3536
num_groups = len(ages)
3637
time = range(126)
37-
dates = pd.date_range(start=datetime(2020,1,28), end=datetime(2020,6,2))
38+
dates = pd.date_range(start=datetime(2020, 1, 28), end=datetime(2020, 6, 2))
3839
group_data = np.zeros((len(time), num_groups, 3))
3940

4041
for age, i in zip(ages, range(num_groups)):
4142
for date, j in zip(dates, range(len(time))):
4243
if date.date() in [x.date() for x in df.Date[df.Age_RKI == age]]:
43-
group_data[j, i, 0] = df.Confirmed[(df.Age_RKI == age) & (df.Date == date)].values
44-
group_data[j, i, 1] = df.Deaths[(df.Age_RKI == age) & (df.Date == date)].values
45-
group_data[j, i, 2] = df.Recovered[(df.Age_RKI == age) & (df.Date == date)].values
44+
group_data[j, i, 0] = df.Confirmed[(
45+
df.Age_RKI == age) & (df.Date == date)].values
46+
group_data[j, i, 1] = df.Deaths[(
47+
df.Age_RKI == age) & (df.Date == date)].values
48+
group_data[j, i, 2] = df.Recovered[(
49+
df.Age_RKI == age) & (df.Date == date)].values
4650

47-
datelist = np.array(pd.date_range(datetime(2020,1, 28), periods=len(time), freq='D').strftime('%m-%d').tolist())
51+
datelist = np.array(pd.date_range(datetime(2020, 1, 28),
52+
periods=len(time), freq='D').strftime('%m-%d').tolist())
4853
tick_range = np.arange(int(len(time)/10)+1)*10
4954
compartiments = ['Confirmed', 'Dead', 'Recovered']
50-
fig, ax = plt.subplots(3,1, figsize=(8,10))
55+
fig, ax = plt.subplots(3, 1, figsize=(8, 10))
5156
for comp, j in zip(compartiments, range(len(compartiments))):
5257
for age, i in zip(ages, range(len(ages))):
5358
ax[j].plot(time, group_data[:, i, j], label=age)
5459
ax[j].legend()
5560
ax[j].set_title(comp)
5661
ax[j].set_xticks([])
5762
ax[2].set_xticks(tick_range)
58-
ax[2].set_xticklabels(datelist[tick_range],rotation=45)
63+
ax[2].set_xticklabels(datelist[tick_range], rotation=45)
5964
fig.tight_layout()
6065
fig.savefig('Cases_Groups.pdf')
6166

6267

6368
all_data = np.sum(group_data, axis=1)
6469
new_inf = []
65-
for i in range(1,len(all_data[:,0])):
66-
new_inf.append(all_data[i,0] - all_data[i-1,0])
70+
for i in range(1, len(all_data[:, 0])):
71+
new_inf.append(all_data[i, 0] - all_data[i-1, 0])
6772

68-
fig, ax = plt.subplots(4,1, figsize=(8,14))
73+
fig, ax = plt.subplots(4, 1, figsize=(8, 14))
6974
for comp, j in zip(compartiments, range(len(compartiments))):
70-
ax[j].plot(time, np.sum(group_data[:,:,j], axis=1))
75+
ax[j].plot(time, np.sum(group_data[:, :, j], axis=1))
7176
ax[j].set_title(comp)
7277
ax[j].set_xticks([])
7378
ax[3].plot(time[:-1], new_inf)
7479
ax[3].set_title('New Daily Infections')
7580
ax[3].set_xticks(tick_range)
76-
ax[3].set_xticklabels(datelist[tick_range],rotation=45)
81+
ax[3].set_xticklabels(datelist[tick_range], rotation=45)
7782
fig.tight_layout()
7883
fig.savefig('Cases_All.pdf')
7984
plt.show()

pycode/examples/simulation/migration.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919
#############################################################################
20+
import argparse
21+
22+
import numpy as np
23+
2024
import memilio.simulation as mio
2125
import memilio.simulation.secir as secir
22-
import numpy as np
23-
import argparse
2426

2527

2628
def parameter_study():

pycode/examples/simulation/migration_parameter_study.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919
#############################################################################
20+
import argparse
21+
22+
import numpy as np
23+
2024
import memilio.simulation as mio
2125
import memilio.simulation.secir as secir
22-
import numpy as np
23-
import argparse
2426

2527

2628
def parameter_study():
@@ -92,21 +94,21 @@ def parameter_study():
9294

9395
# process the result of one run
9496
def handle_result(graph):
95-
print('run {}'.format(handle_result.c))
97+
print(f'run {handle_result.c}')
9698
handle_result.c = handle_result.c + 1
9799
for node_idx in range(graph.num_nodes):
98100
node = graph.get_node(node_idx)
99101
result = node.property.result
100102
model = node.property.model
101-
print(" node {}".format(node_idx))
103+
print(f" node {node_idx}")
102104
print(
103105
" initial InfectedNoSymptoms count {}.".format(
104106
model.populations
105107
[secir.AgeGroup(0),
106108
secir.InfectionState.InfectedNoSymptoms].value))
107-
print(" compartments at t = {}:".format(result.get_time(0)))
109+
print(f" compartments at t = {result.get_time(0)}:")
108110
print(" ", result.get_value(0))
109-
print(" compartments at t = {}:".format(result.get_last_time()))
111+
print(f" compartments at t = {result.get_last_time()}:")
110112
print(" ", result.get_last_value())
111113
handle_result.c = 0
112114

pycode/examples/simulation/oseir_simple.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919
#############################################################################
20+
import argparse
21+
22+
import numpy as np
23+
2024
from memilio.simulation import Damping
21-
from memilio.simulation.oseir import Model, simulate, Index_InfectionState, interpolate_simulation_result
25+
from memilio.simulation.oseir import Index_InfectionState
2226
from memilio.simulation.oseir import InfectionState as State
23-
import numpy as np
24-
import argparse
27+
from memilio.simulation.oseir import (Model, interpolate_simulation_result,
28+
simulate)
2529

2630

2731
def run_oseir_simulation():

pycode/examples/simulation/parameter_studies.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
# limitations under the License.
1919
#############################################################################
2020

21+
import argparse
22+
2123
import numpy as np
24+
2225
import memilio.simulation as mio
2326
import memilio.simulation.secir as secir
24-
import argparse
2527

2628

2729
def parameter_study():

pycode/examples/simulation/secir_groups.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919
#############################################################################
20-
from memilio.simulation import UncertainContactMatrix, ContactMatrix, Damping
21-
from memilio.simulation.secir import Model, simulate, AgeGroup, Index_InfectionState, Simulation, interpolate_simulation_result
22-
from memilio.simulation.secir import InfectionState as State
20+
import argparse
21+
import os
22+
from datetime import date, datetime
23+
24+
import matplotlib.pyplot as plt
2325
import numpy as np
2426
import pandas as pd
25-
import matplotlib.pyplot as plt
26-
from datetime import datetime, date
27-
import os
28-
import argparse
27+
28+
from memilio.simulation import ContactMatrix, Damping, UncertainContactMatrix
29+
from memilio.simulation.secir import AgeGroup, Index_InfectionState
30+
from memilio.simulation.secir import InfectionState as State
31+
from memilio.simulation.secir import (Model, Simulation,
32+
interpolate_simulation_result, simulate)
2933

3034

3135
def run_secir_groups_simulation(show_plot=True):

0 commit comments

Comments
 (0)