Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gext
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michele Nottoli
gext
Commits
8685c877
Commit
8685c877
authored
1 year ago
by
Michele Nottoli
Browse files
Options
Downloads
Patches
Plain Diff
Test fitting done.
parent
a7068085
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
gext/fitting.py
+10
-9
10 additions, 9 deletions
gext/fitting.py
tests/test_descriptor_fitting.py
+43
-0
43 additions, 0 deletions
tests/test_descriptor_fitting.py
with
53 additions
and
9 deletions
gext/fitting.py
+
10
−
9
View file @
8685c877
...
@@ -62,14 +62,15 @@ class DiffFitting(AbstractFitting):
...
@@ -62,14 +62,15 @@ class DiffFitting(AbstractFitting):
"""
Given a set of vectors and a target return the fitting
"""
Given a set of vectors and a target return the fitting
coefficients.
"""
coefficients.
"""
if
len
(
vectors
)
==
1
:
if
len
(
vectors
)
==
1
:
raise
ValueError
(
"
DiffFit does not work for one vector
"
)
raise
ValueError
(
"
DiffFit
ting
does not work for one vector
"
)
target
=
target
-
vectors
[
-
1
]
target
=
target
-
vectors
[
-
1
]
target
=
target
.
flatten
()
VECTORS
=
[]
diff_vectors
=
[]
for
i
in
range
(
1
,
len
(
vectors
)):
for
i
in
range
(
1
,
len
(
vectors
)):
each_vector
=
vectors
[
i
-
1
]
-
vectors
[
-
1
]
each_vector
=
vectors
[
i
-
1
]
-
vectors
[
-
1
]
VECTORS
.
append
(
each_vector
.
flatten
())
diff_vectors
.
append
(
each_vector
)
matrix
=
np
.
array
(
VECTORS
).
T
matrix
=
np
.
array
(
diff_vectors
).
T
a
=
matrix
.
T
@
matrix
a
=
matrix
.
T
@
matrix
b
=
matrix
.
T
@
target
b
=
matrix
.
T
@
target
if
self
.
options
[
"
regularization
"
]
>
0.0
:
if
self
.
options
[
"
regularization
"
]
>
0.0
:
...
@@ -82,12 +83,12 @@ class DiffFitting(AbstractFitting):
...
@@ -82,12 +83,12 @@ class DiffFitting(AbstractFitting):
"""
Given a set of vectors (or matrices) and the corresponding
"""
Given a set of vectors (or matrices) and the corresponding
coefficients, build their linear combination.
"""
coefficients, build their linear combination.
"""
if
len
(
vectors
)
==
1
:
if
len
(
vectors
)
==
1
:
raise
ValueError
(
"
DiffFit does not work for one vector
"
)
raise
ValueError
(
"
DiffFit
ting
does not work for one vector
"
)
result
=
np
.
zeros
(
vectors
[
0
].
shape
,
dtype
=
np
.
float64
)
result
=
np
.
zeros
(
vectors
[
0
].
shape
,
dtype
=
np
.
float64
)
VECTORS_DiffFitting
=
[]
diff_vectors
=
[]
for
i
in
range
(
1
,
len
(
vectors
)):
for
i
in
range
(
1
,
len
(
vectors
)):
VECTORS_DiffFitting
.
append
(
vectors
[
i
-
1
]
-
vectors
[
-
1
])
diff_vectors
.
append
(
vectors
[
i
-
1
]
-
vectors
[
-
1
])
for
coeff
,
vector
in
zip
(
coefficients
,
VECTORS_DiffFitting
):
for
coeff
,
vector
in
zip
(
coefficients
,
diff_vectors
):
result
+=
vector
*
coeff
result
+=
vector
*
coeff
result
=
result
+
vectors
[
-
1
]
result
=
result
+
vectors
[
-
1
]
return
result
return
result
...
...
This diff is collapsed.
Click to expand it.
tests/test_descriptor_fitting.py
+
43
−
0
View file @
8685c877
...
@@ -130,3 +130,46 @@ def test_time_reversibility(datafile):
...
@@ -130,3 +130,46 @@ def test_time_reversibility(datafile):
# check the time reversibility
# check the time reversibility
assert
np
.
linalg
.
norm
(
fitted_target
-
fitted_target_reverse
,
ord
=
np
.
inf
)
<
SMALL
assert
np
.
linalg
.
norm
(
fitted_target
-
fitted_target_reverse
,
ord
=
np
.
inf
)
<
SMALL
@pytest.mark.parametrize
(
"
datafile
"
,
[
"
urea.json
"
,
"
glucose.json
"
])
@pytest.mark.parametrize
(
"
regularization
"
,
[
0.0
,
0.01
,
0.05
])
def
test_diff_fitting
(
datafile
,
regularization
):
# load test data from json file
data
=
utils
.
load_json
(
f
"
tests/
{
datafile
}
"
)
nelectrons
=
data
[
"
nelectrons
"
]
natoms
=
data
[
"
trajectory
"
].
shape
[
1
]
nbasis
=
data
[
"
overlaps
"
].
shape
[
1
]
nframes
=
data
[
"
trajectory
"
].
shape
[
0
]
# initialize an extrapolator
extrapolator
=
gext
.
Extrapolator
(
nelectrons
,
nbasis
,
natoms
,
nsteps
=
nframes
,
fitting
=
"
diff
"
,
fitting_regularization
=
regularization
,
descriptor
=
"
distance
"
)
# load data in the extrapolator
for
(
coords
,
coeff
,
overlap
)
in
zip
(
data
[
"
trajectory
"
],
data
[
"
coefficients
"
],
data
[
"
overlaps
"
]):
extrapolator
.
load_data
(
coords
,
coeff
,
overlap
)
descriptors
=
extrapolator
.
descriptors
.
get
(
10
)
target
=
descriptors
[
-
1
]
fitting_calculator
=
extrapolator
.
fitting_calculator
# check if things are reasonable
for
start
in
range
(
0
,
8
):
vectors
=
descriptors
[
start
:
-
1
]
fit_coefficients
=
fitting_calculator
.
fit
(
vectors
,
target
)
fitted_target
=
fitting_calculator
.
linear_combination
(
vectors
,
fit_coefficients
)
error
=
np
.
linalg
.
norm
(
target
-
fitted_target
,
ord
=
np
.
inf
)
assert
error
<
THRESHOLD
# if we put the target in the vectors used for the fitting,
# check that we get an error smaller than the regularization
vectors
=
descriptors
[:
-
1
]
vectors
[
0
]
=
target
fit_coefficients
=
fitting_calculator
.
fit
(
vectors
,
target
)
fitted_target
=
fitting_calculator
.
linear_combination
(
vectors
,
fit_coefficients
)
assert
np
.
linalg
.
norm
(
target
-
fitted_target
,
ord
=
np
.
inf
)
<
max
(
SMALL
,
regularization
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment