From 131ee6cd8141bb1ff5d62df62b07de75db7167dc Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Tue, 1 Aug 2023 17:44:58 +0200 Subject: [PATCH] CI: Enable flake8 for the scripts/ directory --- .github/workflows/pythonapp.yml | 2 +- scripts/convert_legacy_hdf5.py | 6 ++- scripts/hamiltonians/plot_mos_energies.py | 3 -- scripts/pyxaid/iconds_excess_energy.py | 5 ++- scripts/pyxaid/plot_cooling.py | 45 +++++++++++++++++------ scripts/pyxaid/plot_spectra_pyxaid.py | 2 +- scripts/qmflows/convolution.py | 6 ++- scripts/qmflows/coordination_ldos.py | 8 +++- scripts/qmflows/dos_cp2k.py | 3 +- 9 files changed, 56 insertions(+), 24 deletions(-) diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index b2ec3a0e..e9dfac63 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -201,7 +201,7 @@ jobs: run: pip list - name: Run flake8 - run: flake8 nanoqm test setup.py + run: flake8 nanoqm test setup.py scripts - name: Run pydocstyle run: pydocstyle nanoqm diff --git a/scripts/convert_legacy_hdf5.py b/scripts/convert_legacy_hdf5.py index becb4e6f..6831f285 100644 --- a/scripts/convert_legacy_hdf5.py +++ b/scripts/convert_legacy_hdf5.py @@ -53,8 +53,10 @@ def copy_orbitals(self) -> None: points = [k for k in self.source[self.project].keys() if k.startswith("point_")] keys = {"coefficients", "eigenvalues", "energy"} # "project/point_x/cp2k/mo/" - old_names = chain(*[[f"{self.project}/{point}/cp2k/mo/{k}" for point in points] for k in keys]) - new_names = chain(*[[f"{k}/{point}" for point in points] for k in keys]) + old_names = chain.from_iterable( + [f"{self.project}/{point}/cp2k/mo/{k}" for point in points] for k in keys + ) + new_names = chain.from_iterable([f"{k}/{point}" for point in points] for k in keys) self.copy_node_values(old_names, new_names) def copy_couplings(self) -> None: diff --git a/scripts/hamiltonians/plot_mos_energies.py b/scripts/hamiltonians/plot_mos_energies.py index d9afbe4c..3a1ec21f 100644 --- a/scripts/hamiltonians/plot_mos_energies.py +++ b/scripts/hamiltonians/plot_mos_energies.py @@ -58,9 +58,6 @@ def read_cmd_line(parser: argparse.ArgumentParser) -> tuple[str, str, int, int, Parse Command line options. """ args = parser.parse_args() - - attributes = ['p', 'ts', 'ihomo', 'nhomos', 'nlumos'] - return (args.p, args.ts, args.ihomo, args.nhomos, args.nlumos) diff --git a/scripts/pyxaid/iconds_excess_energy.py b/scripts/pyxaid/iconds_excess_energy.py index 7f52d497..91e873ba 100644 --- a/scripts/pyxaid/iconds_excess_energy.py +++ b/scripts/pyxaid/iconds_excess_energy.py @@ -1,8 +1,9 @@ #! /usr/bin/env python """ -This program finds the indexes of the states for electron and hole cooling calculations (ONLY!) -extrapolated at a desired initial condition. It reads the me_energies0 file from pyxaid [at initial condition t=0]. +This program finds the indexes of the states for electron and hole cooling calculations (ONLY!) \ +extrapolated at a desired initial condition. It reads the me_energies0 file from \ +pyxaid [at initial condition t=0]. Example: diff --git a/scripts/pyxaid/plot_cooling.py b/scripts/pyxaid/plot_cooling.py index 6c0e9f6c..238f6ab2 100644 --- a/scripts/pyxaid/plot_cooling.py +++ b/scripts/pyxaid/plot_cooling.py @@ -1,5 +1,6 @@ #!/usr/bin/env python -"""This program plots the average electronic energy during a NAMD simulation averaged over several initial conditions. +"""This program plots the average electronic energy during a NAMD simulation \ +averaged over several initial conditions. It plots both the SH and SE population based energies. @@ -46,7 +47,11 @@ def plot_stuff( plt.xlabel('Time (fs)') plt.ylabel('Energy (eV)') for iconds in range(nconds): - plt.imshow(y_grid[iconds, :, :].T, aspect='auto', extent=(0, len(ts)*dt, np.min(x_grid), np.max(x_grid)), origin='lower', interpolation='bicubic', cmap='hot') + plt.imshow( + y_grid[iconds, :, :].T, aspect='auto', + extent=(0, len(ts)*dt, np.min(x_grid), np.max(x_grid)), + origin='lower', interpolation='bicubic', cmap='hot', + ) plt.plot(ts * dt, w_en, 'w') interactive(True) plt.show() @@ -55,7 +60,11 @@ def plot_stuff( plt.xlabel('Time (fs)') plt.ylabel('Excess Energy (eV)') for iconds in range(nconds): - plt.imshow(y_grid_scaled[iconds, :, :].T, aspect='auto', extent=(0, len(ts)*dt, np.min(x_grid_scaled), np.max(x_grid_scaled)), origin='lower', interpolation='bicubic', cmap='hot') + plt.imshow( + y_grid_scaled[iconds, :, :].T, aspect='auto', + extent=(0, len(ts)*dt, np.min(x_grid_scaled), np.max(x_grid_scaled)), + origin='lower', interpolation='bicubic', cmap='hot', + ) plt.plot(ts * dt, w_en_scaled, 'w') interactive(True) plt.show() @@ -64,7 +73,10 @@ def plot_stuff( plt.xlabel('Time (fs)') plt.ylabel('State Number') for iconds in range(nconds): - plt.imshow(outs[:, :, iconds].T, aspect='auto', origin='lower', extent = ( 0, len(ts) * dt, 0, outs.shape[1]), interpolation='bicubic', cmap='hot') + plt.imshow( + outs[:, :, iconds].T, aspect='auto', origin='lower', + extent=(0, len(ts) * dt, 0, outs.shape[1]), interpolation='bicubic', cmap='hot' + ) interactive(True) plt.show() @@ -94,7 +106,11 @@ def plot_stuff( plt.xlabel('Freqencies cm-1') sd_int = sd[:, 0, :int(sd.shape[2]/2)] sd_freq = sd[0, 1, :int(sd.shape[2]/2)] - plt.imshow(sd_int, aspect='auto', origin='lower', extent = (np.min(sd_freq), np.max(sd_freq), 0, sd_int.shape[0] ), interpolation='bicubic', cmap='hot') + plt.imshow( + sd_int, aspect='auto', origin='lower', + extent=(np.min(sd_freq), np.max(sd_freq), 0, sd_int.shape[0]), + interpolation='bicubic', cmap='hot', + ) interactive(False) plt.show() @@ -120,12 +136,16 @@ def main(path_output: str, nstates: int, nconds: int, dt: float, sigma: float) - energies_scaled = np.stack([ energies[:, istate, :] - lowest_hl_gap for istate in range(nstates-1) ]) - energies_scaled = energies_scaled.swapaxes(0,1) # Just reshape to have the energies shape (time, nstates, nconds) + # Just reshape to have the energies shape (time, nstates, nconds) + energies_scaled = energies_scaled.swapaxes(0, 1) x_grid_scaled = np.linspace(0, np.max(energies_scaled), 100) y_grid_scaled = np.stack([ np.stack([ - convolute(energies_scaled[time, :, iconds], outs[time, :, iconds], x_grid_scaled, sigma) - for time in range(energies_scaled.shape[0]) + convolute( + energies_scaled[time, :, iconds], + outs[time, :, iconds], + x_grid_scaled, sigma, + ) for time in range(energies_scaled.shape[0]) ]) for iconds in range(nconds) ]) # This part is done @@ -135,9 +155,9 @@ def main(path_output: str, nstates: int, nconds: int, dt: float, sigma: float) - el_ene_outs = np.sum(eav_outs, axis=1) # Scale them to the lowest excitation energy ene_outs_ref0 = el_ene_outs - lowest_hl_gap - #Average over initial conditions + # Average over initial conditions ene_outs_ref0 = np.average(ene_outs_ref0, axis=1) - el_ene_av = np.average(el_ene_outs, axis = 1) + el_ene_av = np.average(el_ene_outs, axis=1) ################################## # Compute autocorrelation function for consecutive pair of states d_en = np.stack( @@ -150,7 +170,10 @@ def main(path_output: str, nstates: int, nconds: int, dt: float, sigma: float) - ################################# # Call plotting function ts = np.arange(energies.shape[0]) - plot_stuff(x_grid, y_grid, x_grid_scaled, y_grid_scaled, sd, el_ene_av, ene_outs_ref0, nconds, outs, energies, ts, dt) + plot_stuff( + x_grid, y_grid, x_grid_scaled, y_grid_scaled, sd, el_ene_av, + ene_outs_ref0, nconds, outs, energies, ts, dt, + ) def read_cmd_line(parser: argparse.ArgumentParser) -> tuple[str, int, int, float, float]: diff --git a/scripts/pyxaid/plot_spectra_pyxaid.py b/scripts/pyxaid/plot_spectra_pyxaid.py index 9e2f082f..bd8cc239 100644 --- a/scripts/pyxaid/plot_spectra_pyxaid.py +++ b/scripts/pyxaid/plot_spectra_pyxaid.py @@ -147,7 +147,7 @@ def main(path_output: str, s1: int, s2: int, dt: float, wsd: int, wdeph: int) -> # Take the transpose to have the correct shape for spectral_density acf = np.stack([autocorrelate(en_states[i, :]) for i in range(en_states.shape[0])]).T # Compute the spectral density for each column using the normalized acf - sd = np.stack([spectral_density(acf[:, 1, i], dt) for i in range(en_states.shape[0])]) + sd = np.stack([spectral_density(acf[:, 1, i], dt) for i in range(en_states.shape[0])]) # Compute the dephasing time for the uncorrelated acf between two states deph, rate = dephasing(acf[:, 0, 2], dt) # Plot stuff diff --git a/scripts/qmflows/convolution.py b/scripts/qmflows/convolution.py index 0236ca51..7e1d4eb9 100644 --- a/scripts/qmflows/convolution.py +++ b/scripts/qmflows/convolution.py @@ -1,5 +1,6 @@ #! /usr/bin/env python -""" This script convolutes the calculated oscillator strengts for different snapshots in a MD trajectory and plot the average spectrum. +""" This script convolutes the calculated oscillator strengts for different snapshots \ +in a MD trajectory and plot the average spectrum. Usage: convolution.py -sigma 0.05 -n 2 -nm True -write True @@ -94,7 +95,8 @@ def read_cmd_line(parser) -> tuple[float, int, bool, bool]: # ============<>=============== if __name__ == "__main__": - msg = """convolution.py -sigma -n + msg = """convolution.py -sigma \ + -n \ -nm -write """ parser = argparse.ArgumentParser(description=msg) diff --git a/scripts/qmflows/coordination_ldos.py b/scripts/qmflows/coordination_ldos.py index e0d7cea1..64a8017d 100644 --- a/scripts/qmflows/coordination_ldos.py +++ b/scripts/qmflows/coordination_ldos.py @@ -101,7 +101,13 @@ def create_ldos_lists(coord: NestedDict) -> Settings: return s -def compute_ldos(mol: Molecule, coord: NestedDict, name: str, workdir: str, path_results: str) -> None: +def compute_ldos( + mol: Molecule, + coord: NestedDict, + name: str, + workdir: str, + path_results: str, +) -> None: """Compute the DOS projected on subsets of atoms given through lists. These lists are divided by atom type and coordination number. diff --git a/scripts/qmflows/dos_cp2k.py b/scripts/qmflows/dos_cp2k.py index 8083d283..db600e59 100644 --- a/scripts/qmflows/dos_cp2k.py +++ b/scripts/qmflows/dos_cp2k.py @@ -1,5 +1,6 @@ #! /usr/bin/env python -"""This programs performs a density of states (DOS) calculation with cp2k using generic settings, i.e. DFT/PBE +"""This programs performs a density of states (DOS) calculation with cp2k \ +using generic settings, i.e. DFT/PBE Note that is mandatory to define a cell_parameter, and a xyz structure. If you have a restart file, a basis set and you can also define it