From c0274e36435c53f3cd889f80510011afc7711c6f Mon Sep 17 00:00:00 2001 From: Rocco Meli Date: Sun, 22 Dec 2024 23:01:21 +0100 Subject: [PATCH] fmt misc --- benchmarks/benchmarks/GRO.py | 3 +- benchmarks/benchmarks/ag_methods.py | 128 ++++++++-------- benchmarks/benchmarks/analysis/distances.py | 34 ++--- benchmarks/benchmarks/analysis/leaflet.py | 35 +++-- benchmarks/benchmarks/analysis/psa.py | 156 ++++++++------------ benchmarks/benchmarks/analysis/rdf.py | 21 +-- benchmarks/benchmarks/analysis/rms.py | 61 +++----- benchmarks/benchmarks/import.py | 4 +- benchmarks/benchmarks/selections.py | 78 +++++----- benchmarks/benchmarks/topology.py | 26 ++-- benchmarks/benchmarks/traj_reader.py | 22 +-- maintainer/adapt_sitemap.py | 75 +++++++--- maintainer/norm_version.py | 2 +- maintainer/update_json_stubs_sitemap.py | 110 +++++++------- 14 files changed, 362 insertions(+), 393 deletions(-) diff --git a/benchmarks/benchmarks/GRO.py b/benchmarks/benchmarks/GRO.py index ad34b41915e..ba47f0943f8 100644 --- a/benchmarks/benchmarks/GRO.py +++ b/benchmarks/benchmarks/GRO.py @@ -1,8 +1,9 @@ +import MDAnalysis as mda import numpy as np from MDAnalysis.coordinates.GRO import GROReader from MDAnalysis.topology.GROParser import GROParser from MDAnalysisTests.datafiles import GRO -import MDAnalysis as mda + class GROReadBench(object): def time_read_GRO_coordinates(self): diff --git a/benchmarks/benchmarks/ag_methods.py b/benchmarks/benchmarks/ag_methods.py index 52616ba4511..e2c5cb8e434 100644 --- a/benchmarks/benchmarks/ag_methods.py +++ b/benchmarks/benchmarks/ag_methods.py @@ -2,35 +2,31 @@ import numpy as np try: - from MDAnalysisTests.datafiles import (GRO, TPR, XTC, - PSF, DCD, - TRZ_psf, TRZ) from MDAnalysis.exceptions import NoDataError + from MDAnalysisTests.datafiles import DCD, GRO, PSF, TPR, TRZ, XTC, TRZ_psf except: pass + class AtomGroupMethodsBench(object): """Benchmarks for the various MDAnalysis atomgroup methods. """ + # NOTE: the write() method has been # excluded as file writing is considered # a separate benchmarking category params = (10, 100, 1000, 10000) - param_names = ['num_atoms'] + param_names = ["num_atoms"] def setup(self, num_atoms): self.u = MDAnalysis.Universe(GRO) self.ag = self.u.atoms[:num_atoms] self.weights = np.ones(num_atoms) - self.vdwradii = {'H':1.0, - 'C':1.0, - 'N':1.0, - 'O':1.0, - 'DUMMY':1.0} - self.rot_matrix = np.ones((3,3)) - self.trans = np.ones((4,4)) + self.vdwradii = {"H": 1.0, "C": 1.0, "N": 1.0, "O": 1.0, "DUMMY": 1.0} + self.rot_matrix = np.ones((3, 3)) + self.trans = np.ones((4, 4)) def time_bbox_pbc(self, num_atoms): """Benchmark bounding box calculation @@ -60,15 +56,13 @@ def time_center_pbc(self, num_atoms): """Benchmark center calculation with pbc active. """ - self.ag.center(weights=self.weights, - pbc=True) + self.ag.center(weights=self.weights, pbc=True) def time_center_no_pbc(self, num_atoms): """Benchmark center calculation with pbc inactive. """ - self.ag.center(weights=self.weights, - pbc=False) + self.ag.center(weights=self.weights, pbc=False) def time_centroid_pbc(self, num_atoms): """Benchmark centroid calculation with @@ -83,8 +77,7 @@ def time_centroid_no_pbc(self, num_atoms): self.ag.centroid(pbc=False) def time_concatenate(self, num_atoms): - """Benchmark atomgroup concatenation. - """ + """Benchmark atomgroup concatenation.""" self.ag.concatenate(self.ag) def time_difference(self, num_atoms): @@ -97,7 +90,7 @@ def time_groupby(self, num_atoms): """Benchmark atomgroup groupby operation. """ - self.ag.groupby('resnames') + self.ag.groupby("resnames") def time_guess_bonds(self, num_atoms): """Benchmark atomgroup bond guessing @@ -106,18 +99,15 @@ def time_guess_bonds(self, num_atoms): self.ag.guess_bonds(self.vdwradii) def time_intersection(self, num_atoms): - """Benchmark ag intersection. - """ + """Benchmark ag intersection.""" self.ag.intersection(self.ag) def time_is_strict_subset(self, num_atoms): - """Benchmark ag strict subset operation. - """ + """Benchmark ag strict subset operation.""" self.ag.is_strict_subset(self.ag) def time_is_strict_superset(self, num_atoms): - """Benchmark ag strict superset operation. - """ + """Benchmark ag strict superset operation.""" self.ag.is_strict_superset(self.ag) def time_isdisjoint(self, num_atoms): @@ -155,19 +145,17 @@ def time_rotateby(self, num_atoms): """Benchmark rotation by an angle of the ag coordinates. """ - self.ag.rotateby(angle=45, - axis=[1,0,0]) + self.ag.rotateby(angle=45, axis=[1, 0, 0]) def time_split(self, num_atoms): """Benchmark ag splitting into multiple ags based on a simple criterion. """ - self.ag.split('residue') + self.ag.split("residue") def time_subtract(self, num_atoms): - """Benchmark ag subtraction. - """ + """Benchmark ag subtraction.""" self.ag.subtract(self.ag) def time_symmetric_difference(self, num_atoms): @@ -187,7 +175,7 @@ def time_translate(self, num_atoms): translation vector to the ag coordinates. """ - self.ag.translate([0,0.5,1]) + self.ag.translate([0, 0.5, 1]) def time_union(self, num_atoms): """Benchmark union operation @@ -202,14 +190,13 @@ def time_wrap(self, num_atoms): self.ag.wrap() - class AtomGroupAttrsBench(object): """Benchmarks for the various MDAnalysis atomgroup attributes. """ params = (10, 100, 1000, 10000) - param_names = ['num_atoms'] + param_names = ["num_atoms"] def setup(self, num_atoms): self.u = MDAnalysis.Universe(GRO) @@ -235,7 +222,7 @@ def time_dihedral(self, num_atoms): """ self.ag[:4].dihedral - #TODO: use universe / ag that + # TODO: use universe / ag that # is suitable for force calc def time_forces(self, num_atoms): """Benchmark atomgroup force @@ -246,7 +233,7 @@ def time_forces(self, num_atoms): except NoDataError: pass - #TODO: use universe / ag that + # TODO: use universe / ag that # is suitable for velocity extraction def time_velocity(self, num_atoms): """Benchmark atomgroup velocity @@ -265,8 +252,7 @@ def time_improper(self, num_atoms): self.ag[:4].improper def time_indices(self, num_atoms): - """Benchmark atom index calculation. - """ + """Benchmark atom index calculation.""" self.ag.ix def time_atomcount(self, num_atoms): @@ -326,15 +312,17 @@ def time_bond(self, num_atoms): class CompoundSplitting(object): """Test how fast can we split compounds into masks and apply them - + The benchmark used in Issue #3000. Parameterizes multiple compound number and size combinations. """ - - params = [(100, 10000, 1000000), # n_atoms - (1, 10, 100), # n_compounds - (True, False), # homogeneous - (True, False)] # contiguous + + params = [ + (100, 10000, 1000000), # n_atoms + (1, 10, 100), # n_compounds + (True, False), # homogeneous + (True, False), + ] # contiguous def setup(self, n_atoms, n_compounds, homogeneous, contiguous): rg = np.random.Generator(np.random.MT19937(3000)) @@ -345,7 +333,7 @@ def setup(self, n_atoms, n_compounds, homogeneous, contiguous): if n_compounds == 1 and not (homogeneous and contiguous): raise NotImplementedError - + if n_compounds == n_atoms: if not (homogeneous and contiguous): raise NotImplementedError @@ -354,51 +342,56 @@ def setup(self, n_atoms, n_compounds, homogeneous, contiguous): ats_per_compound, remainder = divmod(n_atoms, n_compounds) if remainder: raise NotImplementedError - compound_indices = np.tile(np.arange(n_compounds), - (ats_per_compound, 1)).T.ravel() + compound_indices = np.tile( + np.arange(n_compounds), (ats_per_compound, 1) + ).T.ravel() else: - compound_indices = np.sort(np.floor(rg.random(n_atoms) - * n_compounds).astype(np.int)) - + compound_indices = np.sort( + np.floor(rg.random(n_atoms) * n_compounds).astype(np.int) + ) + unique_indices = np.unique(compound_indices) if len(unique_indices) != n_compounds: raise RuntimeError - + if not contiguous: rg.shuffle(compound_indices) - - self.u = MDAnalysis.Universe.empty(n_atoms, - n_residues=n_compounds, - n_segments=1, - atom_resindex=compound_indices, - trajectory=True) - self.u.atoms.positions = rg.random((n_atoms, 3), - dtype=np.float32) * 100 + + self.u = MDAnalysis.Universe.empty( + n_atoms, + n_residues=n_compounds, + n_segments=1, + atom_resindex=compound_indices, + trajectory=True, + ) + self.u.atoms.positions = rg.random((n_atoms, 3), dtype=np.float32) * 100 self.u.dimensions = [50, 50, 50, 90, 90, 90] def time_center_compounds(self, *args): - self.u.atoms.center(None, compound='residues') + self.u.atoms.center(None, compound="residues") class FragmentFinding(object): """Test how quickly we find fragments (distinct molecules from bonds)""" + # if we try to parametrize over topology & # trajectory formats asv will use all # possible combinations, so instead handle # this in setup() - params = ('large_fragment_small_solvents', - 'large_fragment', - 'polymer_chains', # 20ish polymer chains - ) - param_names = ['universe_type'] + params = ( + "large_fragment_small_solvents", + "large_fragment", + "polymer_chains", # 20ish polymer chains + ) + param_names = ["universe_type"] def setup(self, universe_type): - if universe_type == 'large_fragment_small_solvents': - univ = (TPR, XTC) - elif universe_type == 'large_fragment': + if universe_type == "large_fragment_small_solvents": + univ = (TPR, XTC) + elif universe_type == "large_fragment": univ = (PSF, DCD) else: - univ = (TRZ_psf, TRZ) + univ = (TRZ_psf, TRZ) self.u = MDAnalysis.Universe(*univ) def time_find_fragments(self, universe_type): @@ -407,6 +400,7 @@ def time_find_fragments(self, universe_type): class FragmentCaching(FragmentFinding): """Test how quickly we find cached fragments""" + def setup(self, universe_type): super(FragmentCaching, self).setup(universe_type) frags = self.u.atoms.fragments # Priming the cache diff --git a/benchmarks/benchmarks/analysis/distances.py b/benchmarks/benchmarks/analysis/distances.py index 71af1e2c2b7..bbe60955734 100644 --- a/benchmarks/benchmarks/analysis/distances.py +++ b/benchmarks/benchmarks/analysis/distances.py @@ -24,32 +24,20 @@ class DistancesBench(object): def setup(self, num_atoms): np.random.seed(17809) - self.coords_1 = np.random.random_sample((num_atoms, 3)).astype( - np.float32 - ) + self.coords_1 = np.random.random_sample((num_atoms, 3)).astype(np.float32) np.random.seed(9008716) - self.coords_2 = np.random.random_sample((num_atoms, 3)).astype( - np.float32 - ) + self.coords_2 = np.random.random_sample((num_atoms, 3)).astype(np.float32) np.random.seed(15809) - self.coords_3 = np.random.random_sample((num_atoms, 3)).astype( - np.float32 - ) + self.coords_3 = np.random.random_sample((num_atoms, 3)).astype(np.float32) np.random.seed(871600) - self.coords_4 = np.random.random_sample((num_atoms, 3)).astype( - np.float32 - ) + self.coords_4 = np.random.random_sample((num_atoms, 3)).astype(np.float32) - self.allocated_array_2D = np.empty( - (num_atoms, num_atoms), dtype=np.float64 - ) + self.allocated_array_2D = np.empty((num_atoms, num_atoms), dtype=np.float64) self.array_shape_1D = int(num_atoms * (num_atoms - 1) / 2.0) - self.allocated_array_1D = np.empty( - self.array_shape_1D, dtype=np.float64 - ) + self.allocated_array_1D = np.empty(self.array_shape_1D, dtype=np.float64) self.u = mda.Universe(GRO) self.ag1 = self.u.atoms[:num_atoms] - self.ag2 = self.u.atoms[num_atoms: 2 * num_atoms] + self.ag2 = self.u.atoms[num_atoms : 2 * num_atoms] self.ag3 = self.u.atoms[-num_atoms:] def time_distance_array(self, num_atoms): @@ -139,9 +127,7 @@ def time_between(self, num_atoms): of atomgroup that is within a specific distance of two other atomgroups. """ - distances.between( - group=self.ag3, A=self.ag1, B=self.ag2, distance=15.0 - ) + distances.between(group=self.ag3, A=self.ag1, B=self.ag2, distance=15.0) def time_calc_bonds(self, num_atoms): """Benchmark calculation of bonds between @@ -153,9 +139,7 @@ def time_calc_angles(self, num_atoms): """Benchmark calculation of angles between atoms in three atomgroups. """ - mda.lib.distances.calc_angles( - self.coords_1, self.coords_2, self.coords_3 - ) + mda.lib.distances.calc_angles(self.coords_1, self.coords_2, self.coords_3) def time_calc_dihedrals(self, num_atoms): """Benchmark calculation of dihedrals between diff --git a/benchmarks/benchmarks/analysis/leaflet.py b/benchmarks/benchmarks/analysis/leaflet.py index 791ca7c8e68..5939d46efbd 100644 --- a/benchmarks/benchmarks/analysis/leaflet.py +++ b/benchmarks/benchmarks/analysis/leaflet.py @@ -12,29 +12,30 @@ except: pass + class LeafletBench(object): """Benchmarks for MDAnalysis.analysis.leaflet. LeafletFinder """ - params = ([7.0, 15.0, 23.0], - [None, True, False], - [True, False]) - param_names = ['cutoff', 'sparse', 'pbc'] + params = ([7.0, 15.0, 23.0], [None, True, False], [True, False]) + param_names = ["cutoff", "sparse", "pbc"] def setup(self, cutoff, sparse, pbc): self.u = MDAnalysis.Universe(Martini_membrane_gro) - self.headgroup_sel = 'name PO4' + self.headgroup_sel = "name PO4" def time_leafletfinder(self, cutoff, sparse, pbc): """Benchmark LeafletFinder for test lipid membrane system. """ - leaflet.LeafletFinder(universe=self.u, - select=self.headgroup_sel, - cutoff=cutoff, - pbc=pbc, - sparse=sparse) + leaflet.LeafletFinder( + universe=self.u, + select=self.headgroup_sel, + cutoff=cutoff, + pbc=pbc, + sparse=sparse, + ) class LeafletOptimizeBench(object): @@ -42,20 +43,18 @@ class LeafletOptimizeBench(object): optimize_cutoff """ - params = ([None, True, False], - [True, False]) - param_names = ['sparse', 'pbc'] + params = ([None, True, False], [True, False]) + param_names = ["sparse", "pbc"] def setup(self, sparse, pbc): self.u = MDAnalysis.Universe(Martini_membrane_gro) - self.headgroup_sel = 'name PO4' + self.headgroup_sel = "name PO4" def time_optimize_cutoff(self, sparse, pbc): """Benchmark optimize_cutoff for test lipid membrane system using default network distance range. """ - leaflet.optimize_cutoff(universe=self.u, - select=self.headgroup_sel, - pbc=pbc, - sparse=sparse) + leaflet.optimize_cutoff( + universe=self.u, select=self.headgroup_sel, pbc=pbc, sparse=sparse + ) diff --git a/benchmarks/benchmarks/analysis/psa.py b/benchmarks/benchmarks/analysis/psa.py index bc7b73ac835..14f0b27907d 100644 --- a/benchmarks/benchmarks/analysis/psa.py +++ b/benchmarks/benchmarks/analysis/psa.py @@ -6,97 +6,85 @@ except: pass + class PSA_sqnormBench(object): - """Benchmarks for MDAnalysis.analysis.psa. - sqnorm - """ + """Benchmarks for MDAnalysis.analysis.psa. + sqnorm + """ - params = ([2,3,4], - [100,1000,10000], - [None, 0, 1, -1]) + params = ([2, 3, 4], [100, 1000, 10000], [None, 0, 1, -1]) - # num_cols is equivalent to dimensions - # num_rows is equivalent to i.e., num atoms - param_names = ['num_cols', - 'num_rows', - 'axis'] + # num_cols is equivalent to dimensions + # num_rows is equivalent to i.e., num atoms + param_names = ["num_cols", "num_rows", "axis"] - def setup(self, num_cols, num_rows, axis): - np.random.seed(170089) - self.v = np.random.rand(num_rows, num_cols) + def setup(self, num_cols, num_rows, axis): + np.random.seed(170089) + self.v = np.random.rand(num_rows, num_cols) + + def time_sqnorm(self, num_cols, num_rows, axis): + """Benchmark sqnorm in psa module""" + psa.sqnorm(v=self.v, axis=axis) - def time_sqnorm(self, num_cols, num_rows, axis): - """Benchmark sqnorm in psa module - """ - psa.sqnorm(v=self.v, axis=axis) class PSA_get_msd_matrixBench(object): - """Benchmarks for MDAnalysis.analysis.psa. - get_msd_matrix - """ + """Benchmarks for MDAnalysis.analysis.psa. + get_msd_matrix + """ + + params = ([10, 100, 1000], [5, 25, 50]) + + # since the function is defined to work with + # 3N dimension data sets, we will restrict + # benchmarks to that dimensionality + param_names = ["time_steps", "n_atoms"] + + def setup(self, time_steps, n_atoms): + np.random.seed(170089) + self.P = np.random.rand(time_steps, n_atoms, 3) + np.random.seed(971132) + self.Q = np.random.rand(time_steps, n_atoms, 3) + + def time_get_msd_matrix(self, time_steps, n_atoms): + """Benchmark for get_msd_matrix in psa module""" + # only default argument for axis is benchmarked + psa.get_msd_matrix(P=self.P, Q=self.Q, axis=None) - params = ([10,100,1000], - [5,25,50]) - - # since the function is defined to work with - # 3N dimension data sets, we will restrict - # benchmarks to that dimensionality - param_names = ['time_steps', - 'n_atoms'] - - def setup(self, time_steps, n_atoms): - np.random.seed(170089) - self.P = np.random.rand(time_steps, - n_atoms, - 3) - np.random.seed(971132) - self.Q = np.random.rand(time_steps, - n_atoms, - 3) - - def time_get_msd_matrix(self, time_steps, n_atoms): - """Benchmark for get_msd_matrix in psa module - """ - # only default argument for axis is benchmarked - psa.get_msd_matrix(P=self.P, - Q=self.Q, - axis=None) class PSA_get_coord_axesBench(object): """Benchmarks for MDAnalysis.analysis.psa. get_coord_axes """ - params = ([10,100,1000], - [5, 25, 50]) + params = ([10, 100, 1000], [5, 25, 50]) - param_names = ['time_steps', - 'n_atoms'] + param_names = ["time_steps", "n_atoms"] def setup(self, time_steps, n_atoms): np.random.seed(170089) # only using condensed path input # data structure for now - self.path = np.random.rand(time_steps, - n_atoms * 3) + self.path = np.random.rand(time_steps, n_atoms * 3) def time_get_coord_axes(self, time_steps, n_atoms): - """Benchmark get_coord_axes in psa module - """ + """Benchmark get_coord_axes in psa module""" psa.get_coord_axes(path=self.path) + class PSA_get_path_metric_funcBench(object): """Benchmark for MDAnalysis.analysis.psa. get_path_metric_func """ - params = (['hausdorff', - 'weighted_average_hausdorff', - 'average_hausdorff', - 'hausdorff_neighbors', - 'discrete_frechet']) + params = [ + "hausdorff", + "weighted_average_hausdorff", + "average_hausdorff", + "hausdorff_neighbors", + "discrete_frechet", + ] - param_names = ['path_metric'] + param_names = ["path_metric"] def time_get_path_metric_func(self, path_metric): """Benchmark for get_path_metric_func in psa @@ -104,54 +92,38 @@ def time_get_path_metric_func(self, path_metric): """ psa.get_path_metric_func(name=path_metric) + class PSA_metricBench(object): """Benchmarks for the various path metric calculations in the psa module. """ - params = ([10,100,200], - [5,25,50]) + params = ([10, 100, 200], [5, 25, 50]) - param_names = ['time_steps', - 'n_atoms'] + param_names = ["time_steps", "n_atoms"] def setup(self, time_steps, n_atoms): np.random.seed(170089) - self.P = np.random.rand(time_steps, - n_atoms, - 3) + self.P = np.random.rand(time_steps, n_atoms, 3) np.random.seed(971132) - self.Q = np.random.rand(time_steps, - n_atoms, - 3) + self.Q = np.random.rand(time_steps, n_atoms, 3) def time_hausdorff(self, time_steps, n_atoms): - """Benchmark for hausdorff() in psa module. - """ - psa.hausdorff(P=self.P, - Q=self.Q) + """Benchmark for hausdorff() in psa module.""" + psa.hausdorff(P=self.P, Q=self.Q) def time_hausdorff_wavg(self, time_steps, n_atoms): - """Benchmark for hausdorff_wavg() in psa module. - """ - psa.hausdorff_wavg(P=self.P, - Q=self.Q) + """Benchmark for hausdorff_wavg() in psa module.""" + psa.hausdorff_wavg(P=self.P, Q=self.Q) def time_hausdorff_avg(self, time_steps, n_atoms): - """Benchmark for hausdorff_avg() in psa module. - """ - psa.hausdorff_avg(P=self.P, - Q=self.Q) - + """Benchmark for hausdorff_avg() in psa module.""" + psa.hausdorff_avg(P=self.P, Q=self.Q) def time_hausdorff_neighbors(self, time_steps, n_atoms): - """Benchmark for hausdorff_neighbors() in psa module. - """ - psa.hausdorff_neighbors(P=self.P, - Q=self.Q) + """Benchmark for hausdorff_neighbors() in psa module.""" + psa.hausdorff_neighbors(P=self.P, Q=self.Q) def time_discrete_frechet(self, time_steps, n_atoms): - """Benchmark for discrete_frechet() in psa module. - """ - psa.discrete_frechet(P=self.P, - Q=self.Q) + """Benchmark for discrete_frechet() in psa module.""" + psa.discrete_frechet(P=self.P, Q=self.Q) diff --git a/benchmarks/benchmarks/analysis/rdf.py b/benchmarks/benchmarks/analysis/rdf.py index 507b112df89..7a1a4fa41f9 100644 --- a/benchmarks/benchmarks/analysis/rdf.py +++ b/benchmarks/benchmarks/analysis/rdf.py @@ -10,21 +10,17 @@ except: pass + class SimpleRdfBench(object): - """Benchmarks for MDAnalysis.analysis.rdf - """ + """Benchmarks for MDAnalysis.analysis.rdf""" - params = ([20,75,200], - [[0,5], [0,15], [0,20]], - [1, 100, 1000, 10000]) + params = ([20, 75, 200], [[0, 5], [0, 15], [0, 20]], [1, 100, 1000, 10000]) - param_names = ['nbins', - 'range_val', - 'natoms'] + param_names = ["nbins", "range_val", "natoms"] def setup(self, nbins, range_val, natoms): - - self.sel_str = 'name OW' + + self.sel_str = "name OW" self.u = MDAnalysis.Universe(TPR, XTC) @@ -36,10 +32,7 @@ def setup(self, nbins, range_val, natoms): # do not include initialization of the # InterRDF object in the benchmark itself - self.rdf = InterRDF(g1=self.sel, - g2=self.sel, - nbins=nbins, - range=range_val) + self.rdf = InterRDF(g1=self.sel, g2=self.sel, nbins=nbins, range=range_val) def time_interrdf(self, nbins, range_val, natoms): """Benchmark a full trajectory parse diff --git a/benchmarks/benchmarks/analysis/rms.py b/benchmarks/benchmarks/analysis/rms.py index bd1040cee04..dc99c337c22 100644 --- a/benchmarks/benchmarks/analysis/rms.py +++ b/benchmarks/benchmarks/analysis/rms.py @@ -1,7 +1,7 @@ import MDAnalysis try: - from MDAnalysisTests.datafiles import PSF, DCD + from MDAnalysisTests.datafiles import DCD, PSF except: pass @@ -10,18 +10,12 @@ except: pass + class SimpleRmsBench(object): - """Benchmarks for MDAnalysis.analysis.rms.rmsd - """ - - params = ([100, 500, 2000], - [None, [1.0, 0.5]], - [False, True], - [False, True]) - param_names = ['num_atoms', - 'weights', - 'center', - 'superposition'] + """Benchmarks for MDAnalysis.analysis.rms.rmsd""" + + params = ([100, 500, 2000], [None, [1.0, 0.5]], [False, True], [False, True]) + param_names = ["num_atoms", "weights", "center", "superposition"] def setup(self, num_atoms, weights, center, superposition): # mimic rmsd docstring example code @@ -43,33 +37,32 @@ def time_rmsd(self, num_atoms, weights, center, superposition): its docstring example code along with several possible permutations of parameters. """ - rms.rmsd(a=self.A, - b=self.B, - weights=weights, - center=center, - superposition=superposition) + rms.rmsd( + a=self.A, + b=self.B, + weights=weights, + center=center, + superposition=superposition, + ) + class RmsdTrajBench(object): - """Benchmarks for MDAnalysis.analysis.rms.RMSD - """ + """Benchmarks for MDAnalysis.analysis.rms.RMSD""" # TODO: RMSD has many parameters / options, # some of which are apparently still considered # experimental -- we'll eventually want to # benchmark more of these - params = (['all', 'backbone'], - [None, 'mass']) + params = (["all", "backbone"], [None, "mass"]) - param_names = ['select', - 'weights'] + param_names = ["select", "weights"] def setup(self, select, weights): self.u = MDAnalysis.Universe(PSF, DCD) - self.RMSD_inst = rms.RMSD(atomgroup=self.u, - reference=None, - select=select, - weights=weights) + self.RMSD_inst = rms.RMSD( + atomgroup=self.u, reference=None, select=select, weights=weights + ) def time_RMSD(self, select, weights): """Benchmark RMSD.run() method, which parses @@ -79,22 +72,16 @@ def time_RMSD(self, select, weights): class RmsfTrajBench(object): - """Benchmarks for MDAnalysis.analysis.rms.RMSF - """ + """Benchmarks for MDAnalysis.analysis.rms.RMSF""" - params = ([100,500,2000], - [None, 3], - [None,'mass']) + params = ([100, 500, 2000], [None, 3], [None, "mass"]) - param_names = ['n_atoms', - 'step', - 'weights'] + param_names = ["n_atoms", "step", "weights"] def setup(self, n_atoms, step, weights): self.u = MDAnalysis.Universe(PSF, DCD) self.ag = self.u.atoms[:n_atoms] - self.RMSF_inst = rms.RMSF(atomgroup=self.ag, - weights=weights) + self.RMSF_inst = rms.RMSF(atomgroup=self.ag, weights=weights) def time_RMSF(self, n_atoms, step, weights): """Benchmark RMSF.run() method, which parses diff --git a/benchmarks/benchmarks/import.py b/benchmarks/benchmarks/import.py index d1ca3cb43bd..387010e21dc 100644 --- a/benchmarks/benchmarks/import.py +++ b/benchmarks/benchmarks/import.py @@ -1,6 +1,6 @@ class ImportBench(object): def time_import(self): - """Benchmark time needed to import MDAnalysis - """ + """Benchmark time needed to import MDAnalysis""" import MDAnalysis as mda + pass diff --git a/benchmarks/benchmarks/selections.py b/benchmarks/benchmarks/selections.py index e1524ad378d..18d204f6bf6 100644 --- a/benchmarks/benchmarks/selections.py +++ b/benchmarks/benchmarks/selections.py @@ -5,21 +5,25 @@ except: pass + class SimpleSelectionBench(object): """Benchmarks for the various MDAnalysis simple selection strings. """ - params = ('protein', - 'backbone', - 'nucleic', - 'nucleicbackbone', - 'resid 1:10', - 'resnum 1:10', - 'resname LYS', - 'name CA', - 'bynum 0:10') - param_names = ['selection_string'] + params = ( + "protein", + "backbone", + "nucleic", + "nucleicbackbone", + "resid 1:10", + "resnum 1:10", + "resname LYS", + "name CA", + "bynum 0:10", + ) + + param_names = ["selection_string"] def setup(self, selection_string): self.u = MDAnalysis.Universe(GRO) @@ -28,11 +32,12 @@ def time_simple_selections(self, selection_string): """Benchmark simple selections on the protein-based standard test GRO file. """ - if hasattr(MDAnalysis.Universe, 'select_atoms'): + if hasattr(MDAnalysis.Universe, "select_atoms"): self.u.select_atoms(selection_string) else: self.u.selectAtoms(selection_string) + class GeoSelectionBench(object): """Benchmarks for the various MDAnalysis geometric selection strings. @@ -41,40 +46,37 @@ class GeoSelectionBench(object): # all selection strings verified # to produce non-zero atom groups # with GRO test file - params = (['around 5.0 resid 1', - 'sphlayer 2.4 6.0 (protein)', - 'sphzone 6.0 (protein)', - 'cylayer 5 10 10 -8 protein', - 'cyzone 15 4 -8 protein', - 'point 5.0 5.0 5.0 3.5', - 'prop z >= 5.0', - 'prop abs z <= 5.0'], - [True, False], # updating flags - [[False, True], [True, False]]) # periodic flags - + params = ( + [ + "around 5.0 resid 1", + "sphlayer 2.4 6.0 (protein)", + "sphzone 6.0 (protein)", + "cylayer 5 10 10 -8 protein", + "cyzone 15 4 -8 protein", + "point 5.0 5.0 5.0 3.5", + "prop z >= 5.0", + "prop abs z <= 5.0", + ], + [True, False], # updating flags + [[False, True], [True, False]], + ) # periodic flags # benchmarks should include static & # dynamic selections & periodic # vs non-periodic - param_names = ['selection_string', - 'dynamic_selection', - 'periodic_selection'] - + param_names = ["selection_string", "dynamic_selection", "periodic_selection"] - def setup(self, - selection_string, - dynamic_selection, - periodic_selection): + def setup(self, selection_string, dynamic_selection, periodic_selection): self.u = MDAnalysis.Universe(GRO) - def time_geometric_selections(self, - selection_string, - dynamic_selection, - periodic_selection): + def time_geometric_selections( + self, selection_string, dynamic_selection, periodic_selection + ): # TODO: Do we need a kwarg similar to old `use_KDTree_routines` # flag? We used to benchmark that. - self.u.select_atoms(selection_string, - updating=dynamic_selection, - periodic=periodic_selection[0], - ) + self.u.select_atoms( + selection_string, + updating=dynamic_selection, + periodic=periodic_selection[0], + ) diff --git a/benchmarks/benchmarks/topology.py b/benchmarks/benchmarks/topology.py index 45c3fcf95bf..661eeab9585 100644 --- a/benchmarks/benchmarks/topology.py +++ b/benchmarks/benchmarks/topology.py @@ -3,32 +3,30 @@ from MDAnalysis.guesser import DefaultGuesser try: - from MDAnalysisTests.datafiles import GRO from MDAnalysis.exceptions import NoDataError + from MDAnalysisTests.datafiles import GRO except: pass + class TopologyGuessBench(object): """Benchmarks for individual topology functions """ + params = (10, 100, 1000, 10000) - param_names = ['num_atoms'] - + param_names = ["num_atoms"] + def setup(self, num_atoms): self.u = MDAnalysis.Universe(GRO) self.ag = self.u.atoms[:num_atoms] - self.vdwradii = {'H':1.0, - 'C':1.0, - 'N':1.0, - 'O':1.0, - 'DUMMY':1.0} + self.vdwradii = {"H": 1.0, "C": 1.0, "N": 1.0, "O": 1.0, "DUMMY": 1.0} def time_guessbonds(self, num_atoms): """Benchmark for guessing bonds""" - DefaultGuesser(None).guess_bonds(self.ag, self.ag.positions, - box=self.ag.dimensions, - vdwradii=self.vdwradii) + DefaultGuesser(None).guess_bonds( + self.ag, self.ag.positions, box=self.ag.dimensions, vdwradii=self.vdwradii + ) class BondsBench(object): @@ -37,11 +35,11 @@ class BondsBench(object): """ params = (1000, 10000, 100000, 1000000) - param_names = ['num_bonds'] + param_names = ["num_bonds"] def setup(self, num_bonds): - self.u = MDAnalysis.Universe.empty(2*num_bonds) - bonds = np.arange(2*num_bonds).reshape(num_bonds, 2) + self.u = MDAnalysis.Universe.empty(2 * num_bonds) + bonds = np.arange(2 * num_bonds).reshape(num_bonds, 2) self.u.add_bonds(bonds) def time_bonds(self, num_bonds): diff --git a/benchmarks/benchmarks/traj_reader.py b/benchmarks/benchmarks/traj_reader.py index b2fce682c60..f30e4156f2b 100644 --- a/benchmarks/benchmarks/traj_reader.py +++ b/benchmarks/benchmarks/traj_reader.py @@ -1,4 +1,3 @@ - try: from MDAnalysis.coordinates.DCD import DCDReader from MDAnalysisTests.datafiles import DCD @@ -23,15 +22,19 @@ except ImportError: pass -traj_dict = {'XTC': [XTC, XTCReader], - 'TRR': [TRR, TRRReader], - 'DCD': [DCD, DCDReader], - 'NCDF': [NCDF, NCDFReader]} +traj_dict = { + "XTC": [XTC, XTCReader], + "TRR": [TRR, TRRReader], + "DCD": [DCD, DCDReader], + "NCDF": [NCDF, NCDFReader], +} + class TrajReaderCreation(object): """Benchmarks for trajectory file format reading.""" - params = (['XTC', 'TRR', 'DCD', 'NCDF']) - param_names = ['traj_format'] + + params = ["XTC", "TRR", "DCD", "NCDF"] + param_names = ["traj_format"] def setup(self, traj_format): self.traj_dict = traj_dict @@ -46,8 +49,9 @@ def time_reads(self, traj_format): class TrajReaderIteration(object): """Benchmarks for trajectory file format striding.""" - params = (['XTC', 'TRR', 'DCD', 'NCDF']) - param_names = ['traj_format'] + + params = ["XTC", "TRR", "DCD", "NCDF"] + param_names = ["traj_format"] def setup(self, traj_format): self.traj_dict = traj_dict diff --git a/maintainer/adapt_sitemap.py b/maintainer/adapt_sitemap.py index a731bf20872..16c45a96704 100755 --- a/maintainer/adapt_sitemap.py +++ b/maintainer/adapt_sitemap.py @@ -3,8 +3,8 @@ # # Adjust path in sitemap.xml -from xml.etree import ElementTree import argparse +from xml.etree import ElementTree # defaults for MDAnalysis, see https://github.com/MDAnalysis/mdanalysis/pull/1890 # and https://github.com/MDAnalysis/MDAnalysis.github.io/issues/78 @@ -15,39 +15,60 @@ # change if sitemaps.org updates their schema NAMESPACE = {"sitemaps": "http://www.sitemaps.org/schemas/sitemap/0.9"} + def replace_loc(tree, search, replace, namespace=NAMESPACE): root = tree.getroot() urls = root.findall("sitemaps:url", namespace) if len(urls) == 0: - raise ValueError("No sitemaps:url element found: check if the namespace in the XML file " - "is still xmlns='{0[sitemaps]}'".format(namespace)) + raise ValueError( + "No sitemaps:url element found: check if the namespace in the XML file " + "is still xmlns='{0[sitemaps]}'".format(namespace) + ) for url in urls: loc = url.find("sitemaps:loc", namespace) try: loc.text = loc.text.replace(search, replace) except AttributError: - raise ValueError("No sitemaps:loc element found: check if the namespace in the XML file " - "is still xmlns='{0[sitemaps]}'".format(namespace)) + raise ValueError( + "No sitemaps:loc element found: check if the namespace in the XML file " + "is still xmlns='{0[sitemaps]}'".format(namespace) + ) return tree if __name__ == "__main__": - parser = argparse.ArgumentParser(description='Change top level loc in sitemap.', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) - - parser.add_argument('sitemap', metavar="FILE", - help="path to sitemap.xml file, will be changed in place") - parser.add_argument('--output', '-o', metavar="FILE", - default="sitemap_release.xml", - help="write altered XML to output FILE") - parser.add_argument("--search", "-s", metavar="URL", - default=DEVELOP_URL, - help="search this URL in the loc elements") - parser.add_argument("--replace", "-r", metavar="URL", - default=RELEASE_URL, - help="replace the searched URL with this URL in the loc elements") - args = parser.parse_args() + parser = argparse.ArgumentParser( + description="Change top level loc in sitemap.", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + ) + parser.add_argument( + "sitemap", + metavar="FILE", + help="path to sitemap.xml file, will be changed in place", + ) + parser.add_argument( + "--output", + "-o", + metavar="FILE", + default="sitemap_release.xml", + help="write altered XML to output FILE", + ) + parser.add_argument( + "--search", + "-s", + metavar="URL", + default=DEVELOP_URL, + help="search this URL in the loc elements", + ) + parser.add_argument( + "--replace", + "-r", + metavar="URL", + default=RELEASE_URL, + help="replace the searched URL with this URL in the loc elements", + ) + args = parser.parse_args() with open(args.sitemap) as xmlfile: tree = ElementTree.parse(xmlfile) @@ -55,8 +76,16 @@ def replace_loc(tree, search, replace, namespace=NAMESPACE): tree = replace_loc(tree, args.search, args.replace) with open(args.output, "wb") as xmlfile: - tree.write(xmlfile, encoding="utf-8", xml_declaration=True, - default_namespace=NAMESPACE['sitemaps']) + tree.write( + xmlfile, + encoding="utf-8", + xml_declaration=True, + default_namespace=NAMESPACE["sitemaps"], + ) - print("adapt_sitemap.py: Created output file {} with change in loc:".format(args.output)) + print( + "adapt_sitemap.py: Created output file {} with change in loc:".format( + args.output + ) + ) print("adapt_sitemap.py: {0} --> {1}".format(args.search, args.replace)) diff --git a/maintainer/norm_version.py b/maintainer/norm_version.py index 6bc8748ea04..32e02cd9512 100644 --- a/maintainer/norm_version.py +++ b/maintainer/norm_version.py @@ -23,7 +23,7 @@ def norm_version(version_str: str): import argparse parser = argparse.ArgumentParser() - parser.add_argument('--file', type=str, help="file with version to parse") + parser.add_argument("--file", type=str, help="file with version to parse") args = parser.parse_args() with open(args.file) as filed: diff --git a/maintainer/update_json_stubs_sitemap.py b/maintainer/update_json_stubs_sitemap.py index f6b1436abc4..9ba36745141 100644 --- a/maintainer/update_json_stubs_sitemap.py +++ b/maintainer/update_json_stubs_sitemap.py @@ -9,43 +9,44 @@ # 3. Write a sitemap.xml file for the root directory # +import errno +import glob import json import os import shutil -import xml.etree.ElementTree as ET -import errno -import glob import textwrap -import shutil +import xml.etree.ElementTree as ET try: from urllib.request import Request, urlopen except ImportError: from urllib2 import Request, urlopen -URL = os.environ['URL'] -VERSION = os.environ['VERSION'] +URL = os.environ["URL"] +VERSION = os.environ["VERSION"] if "http" not in URL: - raise ValueError("URL should have the transfer protocol (HTTP/S). " - f"Given: $URL={URL}") + raise ValueError( + "URL should have the transfer protocol (HTTP/S). " f"Given: $URL={URL}" + ) try: int(VERSION[0]) except ValueError: - raise ValueError("$VERSION should start with a number. " - f"Given: $VERSION={VERSION}") from None + raise ValueError( + "$VERSION should start with a number. " f"Given: $VERSION={VERSION}" + ) from None def get_web_file(filename, callback, default): url = os.path.join(URL, filename) try: - page = Request(url, headers={'User-Agent': 'Mozilla/5.0'}) + page = Request(url, headers={"User-Agent": "Mozilla/5.0"}) data = urlopen(page).read().decode() except Exception as e: print(e) try: - with open(filename, 'r') as f: + with open(filename, "r") as f: return callback(f) except IOError as e: print(e) @@ -54,58 +55,62 @@ def get_web_file(filename, callback, default): return callback(data) -def write_redirect(file, version='', outfile=None): +def write_redirect(file, version="", outfile=None): if outfile is None: outfile = file url = os.path.join(URL, version, file) - REDIRECT = textwrap.dedent(f""" + REDIRECT = textwrap.dedent( + f""" Redirecting to {url} - """) - with open(outfile, 'w') as f: + """ + ) + with open(outfile, "w") as f: f.write(REDIRECT) print(f"Wrote redirect from {url} to {outfile}") # ========= WRITE JSON ========= # Update $root/versions.json with links to the right version -versions = get_web_file('versions.json', json.loads, []) -existing = [item['version'] for item in versions] +versions = get_web_file("versions.json", json.loads, []) +existing = [item["version"] for item in versions] already_exists = VERSION in existing -latest = 'dev' not in VERSION +latest = "dev" not in VERSION if not already_exists: if latest: for ver in versions: - ver['latest'] = False + ver["latest"] = False - versions.append({ - 'version': VERSION, - 'display': VERSION, - 'url': os.path.join(URL, VERSION), - 'latest': latest - }) + versions.append( + { + "version": VERSION, + "display": VERSION, + "url": os.path.join(URL, VERSION), + "latest": latest, + } + ) for ver in versions[::-1]: - if ver['latest']: - latest_version = ver['version'] + if ver["latest"]: + latest_version = ver["version"] break else: try: - latest_version = versions[-1]['version'] + latest_version = versions[-1]["version"] except IndexError: latest_version = None for ver in versions[::-1]: - if '-dev' in ver['version']: - dev_version = ver['version'] + if "-dev" in ver["version"]: + dev_version = ver["version"] break else: try: - dev_version = versions[-1]['version'] + dev_version = versions[-1]["version"] except IndexError: dev_version = None @@ -147,12 +152,14 @@ def add_or_update_version(version): ver["url"] = os.path.join(URL, version) break else: - versions.append({ - "version": version, - "display": version, - "url": os.path.join(URL, version), - "latest": False - }) + versions.append( + { + "version": version, + "display": version, + "url": os.path.join(URL, version), + "latest": False, + } + ) def copy_version(old_version, new_version): @@ -166,7 +173,7 @@ def copy_version(old_version, new_version): # Copy stable/ docs and write redirects from root level docs if latest: copy_version(VERSION, "stable") - html_files = glob.glob(f'stable/**/*.html', recursive=True) + html_files = glob.glob(f"stable/**/*.html", recursive=True) for file in html_files: # below should be true because we only globbed stable/* paths assert file.startswith("stable/") @@ -179,26 +186,26 @@ def copy_version(old_version, new_version): if exc.errno != errno.EEXIST: raise - write_redirect(file, '', outfile) + write_redirect(file, "", outfile) # Separate just in case we update versions.json or muck around manually # with docs if latest_version: - write_redirect('index.html', "stable") - write_redirect('index.html', latest_version, 'latest/index.html') + write_redirect("index.html", "stable") + write_redirect("index.html", latest_version, "latest/index.html") # Copy dev/ docs if dev_version and dev_version == VERSION: copy_version(VERSION, "dev") # update versions.json online -with open("versions.json", 'w') as f: +with open("versions.json", "w") as f: json.dump(versions, f, indent=2) # ========= WRITE SUPER SITEMAP.XML ========= # make one big sitemap.xml -ET.register_namespace('xhtml', "http://www.w3.org/1999/xhtml") +ET.register_namespace("xhtml", "http://www.w3.org/1999/xhtml") # so we could make 1 big sitemap as commented # below, but they must be max 50 MB / 50k URL. @@ -220,11 +227,10 @@ def copy_version(old_version, new_version): bigroot = ET.Element("sitemapindex") bigroot.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") for ver in versions: - path = os.path.join(URL, '{}/sitemap.xml'.format(ver['version'])) - sitemap = ET.SubElement(bigroot, 'sitemap') - ET.SubElement(sitemap, 'loc').text = path - -ET.ElementTree(bigroot).write('sitemap_index.xml', - xml_declaration=True, - encoding='utf-8', - method="xml") + path = os.path.join(URL, "{}/sitemap.xml".format(ver["version"])) + sitemap = ET.SubElement(bigroot, "sitemap") + ET.SubElement(sitemap, "loc").text = path + +ET.ElementTree(bigroot).write( + "sitemap_index.xml", xml_declaration=True, encoding="utf-8", method="xml" +)