Skip to content

Commit

Permalink
Merge pull request #129 from NDF-Poli-USP/issue_0093-add-lint-at-the-…
Browse files Browse the repository at this point in the history
…actions

Issue 0093 add lint at the actions
  • Loading branch information
Olender authored Oct 21, 2024
2 parents 74bafaa + 9702d81 commit a701161
Show file tree
Hide file tree
Showing 47 changed files with 380 additions and 601 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run lint

on:
# Push to master or PR
push:
branches:
- master
pull_request:

jobs:
linter:
name: "Run linter"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Setup flake8 annotations
uses: rbialon/flake8-annotations@v1
- name: Install linter
run: |
pip install flake8 pylint
- name: Lint codebase
run: |
make lint GITHUB_ACTIONS_FORMATTING=1
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ clean:
@rm -rf dist/

format:
isort -rc spyro/ test/*.py
black spyro/ test/*.py

black:
black .
autopep8 --in-place --global-config setup.cfg --recursive .

lint:
isort --check . spyro/ setup.py test/*.py
black --check spyro/ setup.py test/*.py
flake8 setup.py spyro/ test/*.py
6 changes: 3 additions & 3 deletions demos/with_automatic_differentiation/run_forward_ad.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import firedrake as fire
import spyro
from demos.with_automatic_differentiation.utils import \
model_settings, make_c_camembert
model_settings, make_c_camembert
import os
os.environ["OMP_NUM_THREADS"] = "1"

Expand All @@ -16,7 +16,7 @@
element = fire.FiniteElement(
model["opts"]["method"], mesh.ufl_cell(), degree=model["opts"]["degree"],
variant=model["opts"]["quadrature"]
)
)
V = fire.FunctionSpace(mesh, element)


Expand All @@ -43,4 +43,4 @@
sol = forward_solver.solution
fire.VTKFile(
"solution_" + str(source_number) + ".pvd", comm=my_ensemble.comm
).write(sol)
).write(sol)
6 changes: 3 additions & 3 deletions demos/with_automatic_differentiation/run_fwi_ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def forward(
sol = forward_solver.solution
fire.VTKFile(
"solution_" + str(source_number) + ".pvd", comm=my_ensemble.comm
).write(sol)
).write(sol)

return receiver_data, J

Expand All @@ -77,7 +77,7 @@ def forward(
element = fire.FiniteElement(
model["opts"]["method"], mesh.ufl_cell(), degree=model["opts"]["degree"],
variant=model["opts"]["quadrature"]
)
)
V = fire.FunctionSpace(mesh, element)


Expand All @@ -97,7 +97,7 @@ def forward(
guess_rec, J = forward(
c_guess, compute_functional=True, true_data_receivers=true_rec,
annotate=True
)
)

# :class:`~.EnsembleReducedFunctional` is employed to recompute in
# parallel the functional and its gradient associated with the multiple sources
Expand Down
3 changes: 2 additions & 1 deletion demos/with_automatic_differentiation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import firedrake as fire
import spyro


def model_settings():
"""Model settings for forward and Full Waveform Inversion (FWI)
simulations.
Expand Down Expand Up @@ -29,7 +30,7 @@ def model_settings():
# None - no shots parallelism.
"type": "shots_parallelism",
"num_spacial_cores": 1, # Number of cores to use in the spatial
# parallelism.
# parallelism.
}

# Define the domain size without the ABL.
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ ignore =
E501,F403,F405,E226,E402,E721,E731,E741,W503,F999,
N801,N802,N803,N806,N807,N811,N813,N814,N815,N816
exclude = .git,__pycache__

[coverage:run]
omit=*/site-packages/*,*/test/*,*/.eggs/*,/home/alexandre/firedrake/*

[pep8]
ignore = E501,E226,E731,E741,W503
exclude = .git,__pycache__
2 changes: 1 addition & 1 deletion spyro/domains/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ def FE_method(mesh, method, degree, dim=1):

if dim > 1:
element = VectorElement(element, dim=dim)

function_space = FunctionSpace(mesh, element)
return function_space
24 changes: 12 additions & 12 deletions spyro/examples/camembert_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@
import numpy as np
import spyro

L = 500 # [m]
L = 500 # [m]

rho = 7850 # [kg/m3]
lambda_in = 6.86e9 # [Pa]
lambda_out = 9.88e9 # [Pa]
lambda_out = 9.88e9 # [Pa]
mu_in = 3.86e9 # [Pa]
mu_out = 5.86e9 # [Pa]

smag = 1e6
freq = 2 # Central frequency of Ricker wavelet [Hz]
hf = 90 # [m]
hs = 100 # [m]
freq = 2 # Central frequency of Ricker wavelet [Hz]
hf = 90 # [m]
hs = 100 # [m]
source_locations = spyro.create_transect((-hf, 0.2*L), (-hf, 0.8*L), 3)
receiver_locations = spyro.create_transect((-hs, 0), (-hs, L), 40)
source_locations = [[-hf, 0.5*L]]

time_step = 2e-4 # [s]
final_time = 1.5 # [s]
time_step = 2e-4 # [s]
final_time = 1.5 # [s]
out_freq = int(0.01/time_step)

n = 20
mesh = fire.RectangleMesh(n, n, 0, L, originX=-L, diagonal='crossed')
z, x = fire.SpatialCoordinate(mesh)

zc = 250 # [m]
xc = 250 # [m]
ri = 50 # [m]
zc = 250 # [m]
xc = 250 # [m]
ri = 50 # [m]
camembert = lambda v_inside, v_outside: fire.conditional(
(z - zc) ** 2 + (x - xc) ** 2 < ri**2, v_inside, v_outside)

Expand Down Expand Up @@ -60,8 +60,8 @@
"delay": 0,
"delay_type": "time",
"amplitude": np.array([0, smag]),
#"amplitude": smag * np.eye(2),
#"amplitude": smag * np.array([[0, 1], [-1, 0]]),
# "amplitude": smag * np.eye(2),
# "amplitude": smag * np.array([[0, 1], [-1, 0]]),
"receiver_locations": receiver_locations,
}

Expand Down
1 change: 1 addition & 0 deletions spyro/examples/cut_marmousi.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Cut_marmousi_acoustic(Example_model_acoustic):
Dictionary with the parameters of the model that are different from
the default model. The default is None.
"""

def __init__(
self,
dictionary=None,
Expand Down
8 changes: 4 additions & 4 deletions spyro/examples/elastic_cube_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
N = 5 # Number of elements in each direction
h = L/N # Element size [m]

c_p = 5000 # P-wave velocity [m/s]
c_s = 2500 # S-wave velocity [m/s]
rho = 1000 # Density [kg/m3]
c_p = 5000 # P-wave velocity [m/s]
c_s = 2500 # S-wave velocity [m/s]
rho = 1000 # Density [kg/m3]

smag = 1e9 # Source magnitude
smag = 1e9 # Source magnitude
freq = 1 # Source frequency [Hz]

final_time = 2
Expand Down
1 change: 1 addition & 0 deletions spyro/examples/marmousi.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Marmousi_acoustic(Example_model_acoustic):
Dictionary with the parameters of the model that are different from
the default model. The default is None.
"""

def __init__(
self,
dictionary=None,
Expand Down
1 change: 1 addition & 0 deletions spyro/examples/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Rectangle_acoustic(Example_model_acoustic):
If True, the mesh will be periodic in all directions. The default is
False.
"""

def __init__(
self,
dictionary=None,
Expand Down
1 change: 0 additions & 1 deletion spyro/io/dictionaryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ def get_mesh_type(self):

def _derive_mesh_type(self):
dictionary = self.mesh_dictionary
user_mesh_in_dictionary = False
if "user_mesh" not in dictionary:
dictionary["user_mesh"] = None

Expand Down
18 changes: 10 additions & 8 deletions spyro/io/field_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

from .basicio import parallel_print


class Field:
def __init__(self, name, file, callback):
self.name = name
self.file = file
self.callback = callback

def write(self, t):
self.file.write(self.callback(), time=t, name=self.name)


class FieldLogger:
def __init__(self, comm, vis_dict):
self.comm = comm
Expand All @@ -21,14 +23,14 @@ def __init__(self, comm, vis_dict):
self.__source_id = None
self.__enabled_fields = []
self.__wave_data = []

def add_field(self, key, name, callback):
self.__wave_data.append((key, name, callback))

def start_logging(self, source_id):
if self.__source_id != None:
if self.__source_id is not None:
warnings.warn("Started a new record without stopping the previous one")

self.__source_id = source_id
self.__enabled_fields = []
for key, name, callback in self.__wave_data:
Expand All @@ -42,10 +44,10 @@ def start_logging(self, source_id):

file = VTKFile(filename, comm=self.comm.comm)
self.__enabled_fields.append(Field(name, file, callback))

def stop_logging(self):
self.__source_id = None

def log(self, t):
for field in self.__enabled_fields:
field.write(t)
field.write(t)
8 changes: 4 additions & 4 deletions spyro/io/model_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,9 @@ def _sanitize_optimization_and_velocity(self):
if "velocity_conditional" not in dictionary["synthetic_data"]:
self.velocity_model_type = None
warnings.warn(
"No velocity model set initially. If using " \
"user defined conditional or expression, please " \
"input it in the Wave object."
"No velocity model set initially. If using "
"user defined conditional or expression, please "
"input it in the Wave object."
)

if "velocity_conditional" in dictionary["synthetic_data"]:
Expand All @@ -599,7 +599,7 @@ def _sanitize_optimization_and_velocity_for_fwi(self):
else:
default_optimization_parameters = {
"General": {"Secant": {"Type": "Limited-Memory BFGS",
"Maximum Storage": 10}},
"Maximum Storage": 10}},
"Step": {
"Type": "Augmented Lagrangian",
"Augmented Lagrangian": {
Expand Down
6 changes: 3 additions & 3 deletions spyro/meshing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .meshing_functions import RectangleMesh
from .meshing_functions import PeriodicRectangleMesh, BoxMesh
from .meshing_functions import AutomaticMesh
from .meshing_functions import RectangleMesh # noqa: F401
from .meshing_functions import PeriodicRectangleMesh, BoxMesh # noqa: F401
from .meshing_functions import AutomaticMesh # noqa: F401

all = [
"RectangleMesh",
Expand Down
Loading

0 comments on commit a701161

Please sign in to comment.