From 346a6fe263954f3e7de3babadd341f8e5ffcca4a Mon Sep 17 00:00:00 2001 From: sebhmg Date: Fri, 18 Oct 2024 21:46:09 -0400 Subject: [PATCH] [DEVOPS-515] fix black in precommit and run it --- .pre-commit-config.yaml | 8 +-- simpeg/data.py | 2 +- .../frequency_domain/simulation.py | 2 +- .../natural_source/simulation_1d.py | 2 +- .../static/induced_polarization/simulation.py | 2 +- .../static/self_potential/simulation.py | 2 +- .../simulation.py | 2 +- .../spectral_induced_polarization/survey.py | 2 +- simpeg/flow/richards/empirical.py | 12 ++-- simpeg/flow/richards/simulation.py | 2 +- simpeg/inversion.py | 2 +- simpeg/regularization/__init__.py | 4 +- simpeg/utils/plot_utils.py | 4 +- tests/base/test_Props.py | 2 +- tests/pf/test_forward_PFproblem.py | 2 +- tutorials/08-tdem/plot_inv_3_tem_3d.py | 69 +++++++++++-------- 16 files changed, 65 insertions(+), 54 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a8bdc44d29..9e0f4bc35f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,14 +1,14 @@ +default_language_version: + python: python3 repos: - - repo: https://github.com/psf/black - rev: 24.10.0 + - repo: https://github.com/psf/black-pre-commit-mirror + rev: 24.3.0 hooks: - id: black - language_version: python3 - repo: https://github.com/pycqa/flake8 rev: 7.1.1 hooks: - id: flake8 - language_version: python3 additional_dependencies: - flake8-bugbear==23.12.2 - flake8-builtins==2.2.0 diff --git a/simpeg/data.py b/simpeg/data.py index 1d53972caa..00be1504a7 100644 --- a/simpeg/data.py +++ b/simpeg/data.py @@ -58,7 +58,7 @@ def __init__( relative_error=None, noise_floor=None, standard_deviation=None, - **kwargs + **kwargs, ): super().__init__(**kwargs) self.survey = survey diff --git a/simpeg/electromagnetics/frequency_domain/simulation.py b/simpeg/electromagnetics/frequency_domain/simulation.py index fc99133600..33f3f424b3 100644 --- a/simpeg/electromagnetics/frequency_domain/simulation.py +++ b/simpeg/electromagnetics/frequency_domain/simulation.py @@ -73,7 +73,7 @@ def __init__( forward_only=False, permittivity=None, storeJ=False, - **kwargs + **kwargs, ): super().__init__(mesh=mesh, survey=survey, **kwargs) self.forward_only = forward_only diff --git a/simpeg/electromagnetics/natural_source/simulation_1d.py b/simpeg/electromagnetics/natural_source/simulation_1d.py index 46de6f436e..c96656530f 100644 --- a/simpeg/electromagnetics/natural_source/simulation_1d.py +++ b/simpeg/electromagnetics/natural_source/simulation_1d.py @@ -54,7 +54,7 @@ def __init__( thicknesses=None, thicknessesMap=None, fix_Jmatrix=False, - **kwargs + **kwargs, ): super().__init__(mesh=None, survey=survey, **kwargs) self.fix_Jmatrix = fix_Jmatrix diff --git a/simpeg/electromagnetics/static/induced_polarization/simulation.py b/simpeg/electromagnetics/static/induced_polarization/simulation.py index d4dfa53e3f..b5a6ea89d4 100644 --- a/simpeg/electromagnetics/static/induced_polarization/simulation.py +++ b/simpeg/electromagnetics/static/induced_polarization/simulation.py @@ -70,7 +70,7 @@ def __init__( etaMap=None, Ainv=None, # A DC's Ainv _f=None, # A pre-computed DC field - **kwargs + **kwargs, ): super().__init__(mesh=mesh, survey=survey, **kwargs) self.sigma = sigma diff --git a/simpeg/electromagnetics/static/self_potential/simulation.py b/simpeg/electromagnetics/static/self_potential/simulation.py index 402fcbb791..4a57592349 100644 --- a/simpeg/electromagnetics/static/self_potential/simulation.py +++ b/simpeg/electromagnetics/static/self_potential/simulation.py @@ -64,7 +64,7 @@ def __init__( rho=rho, sigmaMap=None, rhoMap=None, - **kwargs + **kwargs, ) self.q = q self.qMap = qMap diff --git a/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py b/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py index 87e5638875..04263d3197 100644 --- a/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py +++ b/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py @@ -45,7 +45,7 @@ def __init__( storeJ=False, actinds=None, storeInnerProduct=True, - **kwargs + **kwargs, ): super().__init__(mesh=mesh, survey=survey, **kwargs) self.tau = tau diff --git a/simpeg/electromagnetics/static/spectral_induced_polarization/survey.py b/simpeg/electromagnetics/static/spectral_induced_polarization/survey.py index 05f5a0d460..7039d4fb05 100644 --- a/simpeg/electromagnetics/static/spectral_induced_polarization/survey.py +++ b/simpeg/electromagnetics/static/spectral_induced_polarization/survey.py @@ -26,7 +26,7 @@ def __init__( source_list=None, survey_geometry="surface", survey_type="dipole-dipole", - **kwargs + **kwargs, ): if source_list is None: raise AttributeError("Survey cannot be instantiated without sources") diff --git a/simpeg/flow/richards/empirical.py b/simpeg/flow/richards/empirical.py index d726aaad91..ae74f05b96 100644 --- a/simpeg/flow/richards/empirical.py +++ b/simpeg/flow/richards/empirical.py @@ -100,7 +100,7 @@ def __init__( alphaMap=None, beta=3.96, betaMap=None, - **kwargs + **kwargs, ): super().__init__(mesh=mesh, **kwargs) self.theta_r = theta_r @@ -224,7 +224,7 @@ def __init__( AMap=None, gamma=4.74, gammaMap=None, - **kwargs + **kwargs, ): super().__init__(mesh=mesh, **kwargs) self.Ks = Ks @@ -292,7 +292,7 @@ def haverkamp(mesh, **kwargs): Haverkamp_theta, ["Ks", "A", "gamma"], ["alpha", "beta", "theta_r", "theta_s"], - **kwargs + **kwargs, ) @@ -344,7 +344,7 @@ def __init__( nMap=None, alpha=0.036, alphaMap=None, - **kwargs + **kwargs, ): super().__init__(mesh=mesh, **kwargs) self.theta_r = theta_r @@ -499,7 +499,7 @@ def __init__( nMap=None, alpha=0.036, alphaMap=None, - **kwargs + **kwargs, ): super().__init__(mesh=mesh, **kwargs) self.Ks = Ks @@ -807,7 +807,7 @@ def van_genuchten(mesh, **kwargs): Vangenuchten_theta, ["alpha", "n", "Ks", "I"], ["alpha", "n", "theta_r", "theta_s"], - **kwargs + **kwargs, ) diff --git a/simpeg/flow/richards/simulation.py b/simpeg/flow/richards/simulation.py index b897230230..c6dae5c408 100644 --- a/simpeg/flow/richards/simulation.py +++ b/simpeg/flow/richards/simulation.py @@ -33,7 +33,7 @@ def __init__( do_newton=False, root_finder_max_iter=30, root_finder_tol=1e-4, - **kwargs + **kwargs, ): debug = kwargs.pop("debug", None) if debug is not None: diff --git a/simpeg/inversion.py b/simpeg/inversion.py index 9826f8cca9..a3e51cf541 100644 --- a/simpeg/inversion.py +++ b/simpeg/inversion.py @@ -15,7 +15,7 @@ def __init__( counter=None, debug=False, name="BaseInversion", - **kwargs + **kwargs, ): if directiveList is None: directiveList = [] diff --git a/simpeg/regularization/__init__.py b/simpeg/regularization/__init__.py index dbc30a5984..e28f24ca0a 100644 --- a/simpeg/regularization/__init__.py +++ b/simpeg/regularization/__init__.py @@ -200,7 +200,7 @@ def __init__(self, mesh=None, alpha_x=1.0, alpha_y=1.0, alpha_z=1.0, **kwargs): length_scale_x=alpha_x, length_scale_y=alpha_y, length_scale_z=alpha_z, - **kwargs + **kwargs, ) @@ -217,7 +217,7 @@ def __init__( alpha_x=alpha_x, alpha_y=alpha_y, alpha_z=alpha_z, - **kwargs + **kwargs, ) diff --git a/simpeg/utils/plot_utils.py b/simpeg/utils/plot_utils.py index fc50246fd8..1c96afd170 100644 --- a/simpeg/utils/plot_utils.py +++ b/simpeg/utils/plot_utils.py @@ -255,7 +255,7 @@ def hillshade(array, azimuth, angle_altitude): Y, hillshade(DATA, shade_azimuth, shade_angle_altitude), shade_ncontour, - **shadeOpts + **shadeOpts, ) if dataloc: @@ -279,7 +279,7 @@ def plot_1d_layer_model( plot_elevation=False, show_layers=False, vlim=None, - **kwargs + **kwargs, ): """ Plot the vertical profile for a 1D layered Earth model. diff --git a/tests/base/test_Props.py b/tests/base/test_Props.py index bb7de0602d..53c2948788 100644 --- a/tests/base/test_Props.py +++ b/tests/base/test_Props.py @@ -106,7 +106,7 @@ def __init__( AMap=None, gamma=4.74, gammaMap=None, - **kwargs + **kwargs, ): super().__init__(**kwargs) self.Ks = Ks diff --git a/tests/pf/test_forward_PFproblem.py b/tests/pf/test_forward_PFproblem.py index 660af07d3f..0777e45c56 100644 --- a/tests/pf/test_forward_PFproblem.py +++ b/tests/pf/test_forward_PFproblem.py @@ -69,7 +69,7 @@ def test_ana_forward(self): *self.sphere_center, self.chiblk, self.b0, - "secondary" + "secondary", ) n_obs, n_comp = self.rxLoc.shape[0], len(self.survey.components) diff --git a/tutorials/08-tdem/plot_inv_3_tem_3d.py b/tutorials/08-tdem/plot_inv_3_tem_3d.py index 0b1d9cf2a7..838a5a9889 100644 --- a/tutorials/08-tdem/plot_inv_3_tem_3d.py +++ b/tutorials/08-tdem/plot_inv_3_tem_3d.py @@ -32,7 +32,16 @@ from discretize.utils import mkvc, refine_tree_xyz from SimPEG import dask from SimPEG.utils import plot2Ddata, surface2ind_topo, model_builder -from SimPEG import data, data_misfit, directives, maps, optimization, regularization, inverse_problem, inversion +from SimPEG import ( + data, + data_misfit, + directives, + maps, + optimization, + regularization, + inverse_problem, + inversion, +) import SimPEG.electromagnetics.time_domain as tdem from geoh5py.workspace import Workspace from geoh5py.objects import Curve @@ -42,6 +51,7 @@ import matplotlib.pyplot as plt import os from time import time + try: from pymatsolver import Pardiso as Solver except ImportError: @@ -237,7 +247,7 @@ # Conductivity in S/m air_conductivity = np.log(1e-8) background_conductivity = np.log(1e-3) -block_conductivity = np.log(1e+0) +block_conductivity = np.log(1e0) # Active cells are cells below the surface. ind_active = surface2ind_topo(mesh, topo_xyz) @@ -255,19 +265,15 @@ # & (mesh.gridCC[ind_active, 2] < -150.0) # ) -face = np.r_[ +face = np.r_[ np.c_[-50, -100, -50], np.c_[-50, 100, -50], np.c_[50, 100, -50], - np.c_[50, -100, -50] + np.c_[50, -100, -50], ] ind_block = model_builder.get_indices_polygon( - mesh, - np.r_[ - face + np.c_[100, 0, 0], - face + np.c_[-150, 0, -225] - ] + mesh, np.r_[face + np.c_[100, 0, 0], face + np.c_[-150, 0, -225]] ) model[ind_block[ind_active]] = block_conductivity @@ -284,7 +290,7 @@ ax1.set_title("Conductivity Model at Y = 0 m") ax1.set_xlim([-500, 500]) ax1.set_ylim([-500, 0]) -ax1.set_aspect('equal') +ax1.set_aspect("equal") plt.show() ###################################################### # Define the Time-Stepping @@ -320,19 +326,21 @@ # --------------------- # + def reshape(values): data = np.zeros((len(time_channels), 1, nrx)) data[value_sortings[:, 0], value_sortings[:, 1], value_sortings[:, 2]] = values return data + # Predict data for a given model dpred = simulation.dpred(model) floors = ( - np.ones_like(reshape(np.abs(dpred))) * np.median(reshape(np.abs(dpred)), axis=2).flatten()[:, None, None]*0.75 + np.ones_like(reshape(np.abs(dpred))) + * np.median(reshape(np.abs(dpred)), axis=2).flatten()[:, None, None] + * 0.75 ) + 1e-16 -noise = np.random.randn(dpred.shape[0]) * ( #1e-15) - np.abs(dpred) * 0.02 -) +noise = np.random.randn(dpred.shape[0]) * (np.abs(dpred) * 0.02) # 1e-15) data_object = data.Data( survey, @@ -349,13 +357,16 @@ def reshape(values): # noise_floor=np.abs(dpred) * 0.1 + 1e-13 ) -dmis = data_misfit.L2DataMisfit(simulation=simulation, data=data_object, model_map=maps.IdentityMap(nP=nC)) +dmis = data_misfit.L2DataMisfit( + simulation=simulation, data=data_object, model_map=maps.IdentityMap(nP=nC) +) reg = regularization.Sparse( - mesh, alpha_s=0., + mesh, + alpha_s=0.0, active_cells=ind_active, mapping=maps.IdentityMap(nP=nC), - gradient_type="total" + gradient_type="total", ) m0 = np.log(2e-3) * np.ones(nC) reg.mref = np.log(1e-3) * np.ones(nC) @@ -368,12 +379,14 @@ def reshape(values): inv_prob = inverse_problem.BaseInvProblem(dmis, reg, opt) directive_list = [ directives.UpdateSensitivityWeights(threshold=5), - directives.SaveIterationsGeoH5(octree, transforms=[plotting_map], sorting=mesh._ubc_order), + directives.SaveIterationsGeoH5( + octree, transforms=[plotting_map], sorting=mesh._ubc_order + ), directives.SaveIterationsGeoH5( tem_survey, attribute_type="predicted", channels=[f"{val:.2e}" for val in time_channels], - association="VERTEX" + association="VERTEX", ), directives.Update_IRLS( max_irls_iterations=2, @@ -384,12 +397,10 @@ def reshape(values): chifact_target=1.0, ), directives.UpdatePreconditioner(), - directives.BetaEstimate_ByEig(beta0_ratio=5e+2, method="old") + directives.BetaEstimate_ByEig(beta0_ratio=5e2, method="old"), ] -inv = inversion.BaseInversion( - inv_prob, directiveList=directive_list -) +inv = inversion.BaseInversion(inv_prob, directiveList=directive_list) # Run the inversion mrec = inv.run(m0) @@ -400,7 +411,7 @@ def reshape(values): fig = plt.figure(figsize=(10, 8)) -ax1 = plt.subplot(2,1,2) +ax1 = plt.subplot(2, 1, 2) im = mesh.plotSlice( recovered_conductivity_model_log10, ax=ax1, @@ -415,11 +426,11 @@ def reshape(values): ax1.set_ylabel("z (m)") ax1.set_xlim([-500, 500]) ax1.set_ylim([-500, 0]) -ax1.set_aspect('equal') +ax1.set_aspect("equal") plt.show() if hasattr(inv_prob, "l2model"): - ax2 = plt.subplot(2,1,1) + ax2 = plt.subplot(2, 1, 1) im = mesh.plotSlice( np.log10(plotting_map * inv_prob.l2model), normal="Y", @@ -434,7 +445,7 @@ def reshape(values): plt.colorbar(im[0]) ax2.set_xlim([-500, 500]) ax2.set_ylim([-500, 0]) - ax2.set_aspect('equal') + ax2.set_aspect("equal") plt.show() ####################################################### # Optional: Export Data @@ -444,7 +455,7 @@ def reshape(values): # plt.figure() -axs = plt.subplot(2,1,2) +axs = plt.subplot(2, 1, 2) plt.plot(reshape(dpred).squeeze().T, "k") plt.plot(reshape(data_object.dobs).squeeze().T, "b") plt.plot(reshape(np.r_[inv_prob.dpred]).squeeze().T, "r") @@ -455,7 +466,7 @@ def reshape(values): axs.set_ylim([-1e-10, 0]) if hasattr(inv_prob, "l2model"): - axs = plt.subplot(2,1,1) + axs = plt.subplot(2, 1, 1) plt.plot(reshape(dpred).squeeze().T, "k") plt.plot(reshape(data_object.dobs).squeeze().T, "b") plt.plot(reshape(simulation.dpred(inv_prob.l2model)).squeeze().T, "r")