Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ml merge with beta version #1

Draft
wants to merge 205 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
205 commits
Select commit Hold shift + click to select a range
5004a7b
before reordering the calc_alpha_function
schuyann Jun 15, 2022
401aa96
ML workflow with only dummy_routines is working
schuyann Jun 15, 2022
ef53435
bin2xml for single orbital
schuyann Jun 17, 2022
6dac126
commented my module
schuyann Jun 17, 2022
2bb4f1f
debugging underflow bug
schuyann Jun 17, 2022
9477df5
hopefully now it is working until compute power
schuyann Jun 17, 2022
da52ee2
save before I checkout old pseudo-folder
schuyann Jun 20, 2022
a750b7f
problem with kwargs
schuyann Jun 21, 2022
eef3859
added ML as a field in master_calc_params
schuyann Jun 21, 2022
9cb4085
removed all binary files and Yannick_test
schuyann Jun 22, 2022
5243361
removing Yannick_testst
schuyann Jun 22, 2022
98f6685
rm cp_koopmans
schuyann Jun 22, 2022
2e4b2ea
towards consistency of the indices for the orbitals. The workflow run…
schuyann Jun 22, 2022
853f966
Implementation of RidgeRegression & Rewritten compute_power.py
schuyann Jun 22, 2022
fcc13f1
Implemented do_prediction() and write_predicted_alphas_to_file()
schuyann Jun 22, 2022
45035c7
redirect output?
schuyann Jun 23, 2022
03f8512
Added sanity checks, if-else-logic for use_ML, improved print-statements
schuyann Jun 26, 2022
48ec997
added dummy routine to test ML-workflow without computing alphas ab-i…
schuyann Jun 27, 2022
23fea58
adding debug statements in yannick.f90
schuyann Jun 27, 2022
3f0546c
store the unscaled vectors and only scale them when training
schuyann Jun 28, 2022
f1fd1c6
fixing a bug which has led to the fact the centers were already read …
schuyann Jun 28, 2022
450beae
commited because I want to chekcout the master branch
schuyann Jun 30, 2022
c317b3f
making sure that every orbital is only trained once
schuyann Jul 1, 2022
7720133
making sure that the number of points in each dimension is not larger…
schuyann Jul 1, 2022
ab6fa78
making sure that I take the largest possible r_cut
schuyann Jul 1, 2022
608175f
added Debug logic to be able to compare against my previous by-hand-r…
schuyann Jul 1, 2022
9b0ab56
making sure that I extract the correct orbitals
schuyann Jul 1, 2022
68347b5
Merge branch 'ML_workflow' of github.com:epfl-theos/koopmans into ML_…
schuyann Jul 1, 2022
760ded6
trying to make the WF-settings nicer
schuyann Jul 1, 2022
e2db2e1
trying to make the WF-settings nicer
schuyann Jul 1, 2022
676cfd9
ML no longer master_calc_params but parameters
schuyann Jul 2, 2022
a3f2020
remove debug statements
schuyann Jul 2, 2022
0f6597a
make the extraction of wannier centers more robus (like in unfold and…
schuyann Jul 2, 2022
81f9af8
removed bin2xml from cat_workflow because this was completely senseless
schuyann Jul 2, 2022
13557ae
deleted copy of _koopmans_dscf.py
schuyann Jul 2, 2022
1bf44e4
added correct typings
schuyann Jul 2, 2022
624e0c5
removed Debugs
schuyann Jul 2, 2022
dfe8593
added type declarations
schuyann Jul 2, 2022
9455ceb
added more attributes to the RidgeRegression class
schuyann Jul 2, 2022
dbccff3
added with open() logic
schuyann Jul 2, 2022
bde0861
correct band.index int and not float
schuyann Jul 2, 2022
1a98a39
changed ML_directory -> ML_dir to be consistent
schuyann Jul 3, 2022
f137b37
beginning to add restart statements
schuyann Jul 3, 2022
75d518b
merge master into this branch, refactoring directory names into one b…
schuyann Jul 3, 2022
66509a5
Merged with different ML_workflow branch to have a more stable extrac…
schuyann Jul 3, 2022
a8d0824
added sklearn to the requirements
schuyann Jul 3, 2022
3653709
changed ML to ml for variable names
schuyann Jul 3, 2022
b68dcf1
changed ML to ml for variable names
schuyann Jul 3, 2022
8c4d7d9
Set Debug_Yannick to false
schuyann Jul 5, 2022
53f9136
temporary solution to use pbesol
schuyann Jul 5, 2022
b2a5457
merged changes from master branch in functionals.f90
schuyann Jul 5, 2022
6149df7
moved use_ML from a ML to a workflow setting & formatting
schuyann Jul 5, 2022
0773b24
recovered the original pseudos
schuyann Jul 5, 2022
4ac42ab
removed minor wrong modifications
schuyann Jul 5, 2022
f3d4636
removed minor wrong modifications
schuyann Jul 5, 2022
a2a3346
somehow we have to differentiate if we also want to read in the ml-se…
schuyann Jul 5, 2022
22ea920
fixed a small mistake from merge with master
schuyann Jul 5, 2022
8977297
removed an old statement about setting prin_real_space_densities
schuyann Jul 6, 2022
f877ccd
working on code documentation and formatting
schuyann Jul 6, 2022
cf39065
some changes to the radial basis function: l now in (0,..,l_max) inst…
schuyann Jul 6, 2022
7f80712
added documentation to ml_utils
schuyann Jul 7, 2022
35b9340
added docstrings to the trajectory and ml workflow
schuyann Jul 7, 2022
62a0373
added more docstrings and I have rewritten the logic of use_ml and so…
schuyann Jul 7, 2022
8497dd5
more docstrings
schuyann Jul 7, 2022
a9fc14c
TODO -> TODO Yannick
schuyann Jul 7, 2022
7dcc024
added hasattr and more TODO-> TODO Yannick
schuyann Jul 7, 2022
eec565f
some adjustments and sanity checkings of the ml-settings
schuyann Jul 8, 2022
a3b155a
problem with dft_dummy
schuyann Jul 8, 2022
1c5b8d9
added annotation for alpha_indep_calcs
schuyann Jul 8, 2022
4635c81
added more sanity checks for the ml-workflow
schuyann Jul 8, 2022
875b587
add analytic expression for the normalization constant of the densities
schuyann Jul 8, 2022
87558d1
change filename
schuyann Jul 9, 2022
ef4326a
add to-and from dict and test for RidgeRegression
schuyann Jul 9, 2022
4eb517f
add DeepDiff as requirement
schuyann Jul 9, 2022
7d1b160
refined the test
schuyann Jul 9, 2022
3d4ac21
removed some todos that are done
schuyann Jul 9, 2022
63410c7
refined the logic and implemented 'on_the_fly' and 'alpha_from_file_k…
schuyann Jul 9, 2022
5175b4e
beginning to add possibitility to only extract bands_to_solve
schuyann Jul 12, 2022
2316f4b
continuing implementing bands_to_solve
schuyann Jul 13, 2022
b9f347c
towards spin-polarised and only bands_to_solve
schuyann Jul 14, 2022
a38eea8
now it should be possible to only use bands_to_extract, further check…
schuyann Jul 15, 2022
4bfd07c
implemented a convergence_workflow for the ml-model
schuyann Jul 19, 2022
e22707c
removed black-import
schuyann Jul 20, 2022
ebe4367
removed black-import
schuyann Jul 20, 2022
b2cd9da
change default setting for 'mode'
schuyann Jul 20, 2022
bc4562b
improved convergence workflow
schuyann Jul 21, 2022
8d4e14f
improved folder structure of results
schuyann Jul 22, 2022
d0b44be
index error
schuyann Jul 22, 2022
04a17d6
debugging
schuyann Jul 22, 2022
1f98e47
found error -> reset ml_model
schuyann Jul 22, 2022
18d1d46
add statistics
schuyann Jul 22, 2022
43226fb
rm final directory s.t. evs get recalculated
schuyann Jul 22, 2022
2da0c8c
just before I make convergence analysis a new workflow
schuyann Jul 23, 2022
0a3b895
made ml_convergence an own workflow
schuyann Jul 23, 2022
e721828
commit right before making ml-model abstract
schuyann Jul 23, 2022
f4ba5b7
made MLModelWrapper an ABC
schuyann Jul 23, 2022
10f99b8
fixed small bug for evs
schuyann Jul 24, 2022
2971cad
add the option to train occ and emp states independently
schuyann Jul 24, 2022
9edd663
added SH as valid input data
schuyann Jul 24, 2022
6895e9b
for SH -> Linear Regression instead of Ridge Regression
schuyann Jul 24, 2022
1341aea
first trial implementation of grid_search
schuyann Jul 24, 2022
2e97a18
small changes to get grid search running
schuyann Jul 25, 2022
c98763d
clean up code, especially convergence_ml
schuyann Jul 25, 2022
284d7e5
minor bugs for toinputjson(), still not completely working
schuyann Jul 26, 2022
04486f2
ongoing merge with master
schuyann Jul 26, 2022
921ae26
Merge branch 'master' into ML_workflow_merged_with_master
schuyann Jul 26, 2022
c85811a
getting closer to master
schuyann Jul 26, 2022
e8815a9
wrong indendation levels
schuyann Jul 26, 2022
3268a1b
getting closer to correctness after merge
schuyann Jul 26, 2022
5ac479e
finalized merge with master
schuyann Jul 27, 2022
6f7edf0
added keyword 'print_real_space_density'
schuyann Jul 27, 2022
9999547
dump result_dict for even easier plotting
schuyann Jul 31, 2022
db9b976
commented out file saving
schuyann Aug 4, 2022
4493bb3
corrected init of trajectory
schuyann Aug 4, 2022
ccc42fa
minor changes to be pushed before forking
schuyann Aug 16, 2022
03b4b1f
Merge remote-tracking branch 'origin/ML_workflow_merged_with_master' …
schuyann Aug 16, 2022
0f628e8
added extra combination necessary for working grid search
schuyann Aug 16, 2022
2acd7fe
Merge branch 'ML_workflow_merged_with_master' of github.com:epfl-theo…
schuyann Aug 16, 2022
5d0aef1
Merge remote-tracking branch 'origin/ML_workflow_merged_with_master' …
schuyann Aug 16, 2022
9e9782b
first try to push to private fork
schuyann Aug 16, 2022
6d85c64
koopmans/ml_utils/_compute_decomposition.py
schuyann Aug 16, 2022
df5cfa7
set debugging to false
schuyann Aug 16, 2022
521e2c7
Corrected one typing
schuyann Sep 2, 2022
50b3bb1
begin to write tutorial
schuyann Sep 2, 2022
1f662c6
improved plotting for convergence_ml
schuyann Sep 3, 2022
56460e3
added tutorial files
schuyann Sep 3, 2022
03f8500
perturbed positions for tutorial
schuyann Sep 3, 2022
fcebeb7
json example files for tutorial_5
schuyann Sep 3, 2022
8dc08fd
deleted old folders
schuyann Sep 3, 2022
c9993e3
added the convergence plots
schuyann Sep 3, 2022
f4cf637
also trajectory for tutorial
schuyann Sep 3, 2022
0c7e130
alternative version
schuyann Sep 3, 2022
165289f
Merge remote-tracking branch 'refs/remotes/origin/ML_merge_with_beta_…
schuyann Sep 3, 2022
e338389
updated tutorial
schuyann Sep 3, 2022
7fe214a
added tests and worked on settings
schuyann Sep 5, 2022
5d4e577
add sanity checks to ml workflow
schuyann Sep 5, 2022
5ffc3be
more testing, power spectrum to array and minor fixes
schuyann Sep 5, 2022
53e347c
commit before I rewrite init and to-and -and fromdict
schuyann Sep 6, 2022
81fda2c
all tests should work now
schuyann Sep 6, 2022
1ccea12
rerunning tests and tutorials
schuyann Sep 6, 2022
bec1c55
updated tutorial-figures
schuyann Sep 6, 2022
498f9ca
updated the tutorials with new figures
schuyann Sep 6, 2022
fa61310
rewrite tutorial 5
schuyann Sep 16, 2022
1aee587
plot bardiagram tutorial 5
schuyann Sep 17, 2022
cb7e44d
different figure size
schuyann Sep 17, 2022
572ac1a
new figure sizes
schuyann Sep 17, 2022
b1d9164
adapted tutorials docs
schuyann Sep 17, 2022
b7ad413
finishing the TODOs
schuyann Sep 17, 2022
84de655
added again the benchmark files from the final calculations
schuyann Sep 20, 2022
510e842
Merge with epfl-theos/koopmans/master
elinscott Oct 7, 2022
778f0ac
Switch kcp submodule to version with ML modifications
elinscott Oct 7, 2022
cf9719e
Updated kcp submodule
elinscott Oct 7, 2022
1779fde
Corrected path to source code in pre-commit-config
elinscott Oct 7, 2022
e894615
Change tests and tutorials to run on pull_request_target cf pull_requ…
elinscott Oct 7, 2022
6419c90
Experimenting with removing all secrets from tests action
elinscott Oct 7, 2022
e658eaf
Experimenting with removing all secrets from tutorials action
elinscott Oct 7, 2022
08b6407
Changing tutorials back from pull_request_target to pull_request
elinscott Oct 7, 2022
41ec6e0
Switch generate_benchmarks to use write_encoded_json, and remove bad …
elinscott Oct 7, 2022
e130da4
WIP: bugfixing after the merge
elinscott Oct 7, 2022
aca2fdc
Restructure tests so that "tests" is not treated as a python module
elinscott Oct 10, 2022
9e8d97a
Tweak to benchmark_diff
elinscott Oct 10, 2022
e791d28
Change so that the path to bin2xml is unspecified
elinscott Oct 10, 2022
2bf4bfc
Fix path in utils
elinscott Oct 11, 2022
12507fd
Updated benchmarks for ML
elinscott Oct 11, 2022
f438a07
Updated outdated references in benchmarks
elinscott Oct 20, 2022
cd2db01
updated alt names in the ml tutorial
Oct 24, 2022
3aa91be
deleted dummy function that is not needed anymore
Oct 24, 2022
0d66fc7
added sanity checks
Oct 24, 2022
324d2a5
Undo changes to gitignore and gitmodules
elinscott Nov 15, 2022
59d5ba7
Updated scikit-learn requirements
elinscott Nov 15, 2022
94860f6
More typing for commands.py
elinscott Nov 15, 2022
d21b703
Adding some typing to ml_models
elinscott Nov 16, 2022
0addc39
Move from using bin2xml_real_space_density.x to bin2xml.x
elinscott Nov 16, 2022
3aceadf
Added various typing and bugfixes
elinscott Nov 16, 2022
7b8c499
Typehints
elinscott Nov 16, 2022
020ca3d
Renamed link to bin2xml
elinscott Nov 17, 2022
be0ff31
Fix typo
elinscott Nov 17, 2022
89bd625
Tweaking the ml utils to be more flexible
elinscott Nov 17, 2022
6327b5f
Renamed debugging.py _misc.py
elinscott Nov 17, 2022
7d69904
More code tidying
elinscott Nov 17, 2022
17c7937
Fix typo Hilmanen -> Himanen
elinscott Nov 17, 2022
340c94e
Remove Debug from src/koopmans/ml_utils/_precompute_parameters_of_bas…
elinscott Nov 17, 2022
cdc1de1
Fixing descriptions of ML settings
elinscott Nov 17, 2022
4ca10a0
Add description of ML block to docs
elinscott Nov 17, 2022
f962f72
More tidying
elinscott Nov 17, 2022
854cdc3
Tidying xsf writer
elinscott Nov 17, 2022
2a6d518
Add note about orthorhombic cell assumptions
elinscott Nov 17, 2022
d17ffe3
More tidying
elinscott Nov 17, 2022
3fc7963
More tidying
elinscott Nov 17, 2022
3248a2e
pep8 compliance
elinscott Nov 17, 2022
4845853
More pep8 compliance
elinscott Nov 17, 2022
55d9a5f
Even more pep8 compliance
elinscott Nov 17, 2022
153d310
More tidying
elinscott Nov 17, 2022
04e64e3
Updated gitmodules so that tests work from forks
elinscott Nov 18, 2022
5bbf71f
Tidied convergence_ml workflow
elinscott Nov 18, 2022
bb05702
Checked _ml.py
elinscott Nov 18, 2022
05a6d47
More checking of _ml.py
elinscott Nov 18, 2022
54e2aa0
Checked _trakectory.py
elinscott Nov 18, 2022
4e9c9cc
Tidying output of ML workflow
elinscott Nov 18, 2022
c93fbeb
Updated tutorial files
elinscott Nov 18, 2022
ffdac12
Updated version number
elinscott Nov 18, 2022
8347821
Rename ml_utils as ml
elinscott Nov 18, 2022
79f44cd
More renaming ml_utils to ml
elinscott Nov 18, 2022
509a24d
Update ml keywords docs
elinscott Nov 18, 2022
9a91a7d
Tweaked tutorial text
elinscott Nov 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More code tidying
  • Loading branch information
elinscott committed Nov 17, 2022
commit 7d69904c31068a49f63378775b6fff075c81ace4
92 changes: 2 additions & 90 deletions src/koopmans/ml_utils/_compute_decomposition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import math
from functools import partial
from pathlib import Path
from typing import Callable, Dict, List, Tuple
Expand All @@ -7,41 +6,19 @@
from ase import Atoms, units
from numpy.linalg import norm

from koopmans import utils
from koopmans.bands import Bands
from koopmans.utils import read_xml_array, read_xml_nr

from ._basis_functions import g as g_basis
from ._basis_functions import \
real_spherical_harmonics as real_spherical_harmonics_basis_functions
from ._misc import cart2sph_array, compute_3d_integral_naive

# Functions for the basis functions
# Basis functions types
RadialBasisFunctions = Callable[[np.ndarray, int, int, int], np.ndarray]
SphericalBasisFunctions = Callable[[np.ndarray, np.ndarray, int, int], np.ndarray]


# def radial_basis_function(r: np.ndarray, n: int, n_max: int, l: int, betas: np.ndarray, alphas: np.ndarray) -> np.ndarray:
# """
# Wrapper for the radial basis function.
#
# Currently only Gaussian Type Basis Functions (see Hilmanen et al 2020) are implemented as radial basis functions.
# This wrapper allows to simply exchange them with other choices of radial basis functions.
# """
#
# return g_basis(r, n, n_max, l, betas, alphas)
#
#
# def spherical_basis_function(theta: np.ndarray, phi: np.ndarray, l: int, m: int) -> np.ndarray:
# """
# Wrapper for the spherical basis function.
#
# Currently only real spherical harmonics (see Hilmanen et al 2020) are implemented as spherical basis functions.
# This wrapper allows to simply exchange them with other choices of spherical basis functions.
# """
#
# return real_spherical_harmonics_basis(theta, phi, l, m)


def precompute_basis_function(radial_basis_functions: RadialBasisFunctions,
spherical_basis_functions: SphericalBasisFunctions,
r_cartesian: np.ndarray, r_spherical: np.ndarray, n_max: int, l_max: int) -> np.ndarray:
Expand Down Expand Up @@ -76,35 +53,6 @@ def precompute_basis_function(radial_basis_functions: RadialBasisFunctions,
return total_basis_function_array


# Functions to convert cartesian to spherical coordinates

def cart2sph(x: float, y: float, z: float) -> Tuple[float, float, float]:
"""
Converts one cartesian coordinate into the corresponding spherical one.
"""

XsqPlusYsq = x**2 + y**2
r = math.sqrt(XsqPlusYsq + z**2)
theta = math.atan2(z, math.sqrt(XsqPlusYsq))+np.pi/2.0
phi = math.atan2(y, x) + np.pi
return r, theta, phi


def cart2sph_array(r_cartesian: np.ndarray) -> np.ndarray:
"""
Converts an array of cartesian coordinates into the corresponding spherical ones.
"""

(k_max, j_max, i_max, _) = np.shape(r_cartesian)
r_spherical = np.zeros_like(r_cartesian)
for k in range(k_max):
for j in range(j_max):
for i in range(i_max):
r_spherical[k, j, i, :] = cart2sph(
r_cartesian[k, j, i, 2], r_cartesian[k, j, i, 1], r_cartesian[k, j, i, 0])
return r_spherical


# functions to make sure that integration domain is chosen such that the center of the orbital density is the center of the integration domain

def get_index(r: np.ndarray, vec: np.ndarray) -> Tuple[int, int, int]:
Expand Down Expand Up @@ -163,18 +111,6 @@ def translate_to_new_integration_domain(f: np.ndarray, wfc_center_index: Tuple[i

# functions to compute the expansion coefficients

def compute_3d_integral_naive(f: np.ndarray, r: np.ndarray) -> np.ndarray:
"""
Computes the 3d-integral of an array of functions f on r with a simple trapezoidal rule.
"""

z = r[:, 0, 0, 0]
y = r[0, :, 0, 1]
x = r[0, 0, :, 2]
result_n = np.sum(f, axis=(0, 1, 2))*(x[-1]-x[0])*(y[-1]-y[0])*(z[-1]-z[0])/((len(x)-1)*(len(y)-1)*(len(z)-1))
return result_n


def get_coefficients(rho: np.ndarray, rho_total: np.ndarray, r_cartesian: np.ndarray, total_basis_function_array: np.ndarray) -> Tuple[List[float], List[float]]:
"""
Computes the expansion coefficients of rho and rho_total wrt the basis defined in total_basis_function_array.
Expand Down Expand Up @@ -271,27 +207,3 @@ def compute_decomposition(n_max: int, l_max: int, r_min: float, r_max: float, r_
# save the decomposition coefficients in files
np.savetxt(dirs['coeff_orb'] / f'coff.orbital.{filled_str}.{band.index}.txt', coefficients_orbital)
np.savetxt(dirs['coeff_tot'] / f'coff.total.{filled_str}.{band.index}.txt', coefficients_total)


def get_reconstructed_orbital_densities(total_basis_array: np.ndarray, coefficients: List[float]) -> np.ndarray:
"""
Reconstruct the density with the truncated expansion coefficients multiplied with the corresponding basis functions.
"""

rho_r_reconstruced = np.einsum('ijkl,l->ijk', total_basis_array, coefficients)
return rho_r_reconstruced


def map_again_to_original_grid(f_new: np.ndarray, wfc_center_index: Tuple[int, int, int], nr_xml: Tuple[int, int, int], nr_new_integration_domain: Tuple[int, int, int]):
"""
Maps the function f_new defined on the new integration domain back to the unit cell.
"""

f_on_reg_grid = np.zeros((nr_xml[2]-1, nr_xml[1]-1, nr_xml[0]-1), dtype=float)

for k_new, k in enumerate(range(wfc_center_index[0]-nr_new_integration_domain[2], wfc_center_index[0]+nr_new_integration_domain[2]+1)):
for j_new, j in enumerate(range(wfc_center_index[1]-nr_new_integration_domain[1], wfc_center_index[1]+nr_new_integration_domain[1]+1)):
for i_new, i in enumerate(range(wfc_center_index[2]-nr_new_integration_domain[0], wfc_center_index[2]+nr_new_integration_domain[0]+1)):
f_on_reg_grid[k % (nr_xml[2]-1), j % (nr_xml[1]-1), i % (nr_xml[0]-1)] = f_new[k_new, j_new, i_new]

return f_on_reg_grid
27 changes: 27 additions & 0 deletions src/koopmans/ml_utils/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
import numpy as np


def cart2sph_array(r_cartesian: np.ndarray) -> np.ndarray:
"""
Converts an array of cartesian coordinates into the corresponding spherical ones.

Note that cartesian is z, y, x; spherical is r, theta, phi
"""

xy2 = r_cartesian[:, :, :, 2]**2 + r_cartesian[:, :, :, 1]**2
r_spherical = np.zeros_like(r_cartesian)
r_spherical[:, :, :, 0] = np.linalg.norm(r_cartesian, axis=-1)
r_spherical[:, :, :, 1] = np.arctan2(r_cartesian[:, :, :, 0], np.sqrt(xy2)) + np.pi / 2.0
r_spherical[:, :, :, 2] = np.arctan2(r_cartesian[:, :, :, 1], r_cartesian[:, :, :, 2]) + np.pi
return r_spherical


def compute_3d_integral_naive(f: np.ndarray, r: np.ndarray) -> np.ndarray:
"""
Computes the 3d-integral of an array of functions f on r with a simple trapezoidal rule.
"""

z = r[:, 0, 0, 0]
y = r[0, :, 0, 1]
x = r[0, 0, :, 2]
result_n = np.sum(f, axis=(0, 1, 2))*(x[-1]-x[0])*(y[-1]-y[0])*(z[-1]-z[0])/((len(x)-1)*(len(y)-1)*(len(z)-1))
return result_n


def reconstruct_orbital_densities(total_basis_array: np.ndarray, coefficients: List[float]) -> np.ndarray:
"""
Reconstruct the density with the truncated expansion coefficients multiplied with the corresponding basis functions
Expand Down