Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
psimpy
Manage
Activity
Members
Labels
Plan
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
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
mbd
psimpy
Commits
746df515
Commit
746df515
authored
2 years ago
by
Hu Zhao
Browse files
Options
Downloads
Patches
Plain Diff
feat: add check_bounds function
parent
c999fe7d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/psimpy/utility/util_funcs.py
+27
-13
27 additions, 13 deletions
src/psimpy/utility/util_funcs.py
with
27 additions
and
13 deletions
src/psimpy/utility/util_funcs.py
+
27
−
13
View file @
746df515
import
numpy
as
np
import
numpy
as
np
from
beartype
import
beartype
from
beartype
import
beartype
@beartype
def
check_bounds
(
ndim
:
int
,
bounds
:
np
.
ndarray
)
->
None
:
"""
Check if bounds are valid.
Parameters
----------
ndim : int
Parameter dimension.
bounds: numpy array
Bounds of the `ndim` parameters, where bounds[:, 0] and bounds[:, 1]
correspond to lower and upper bounds, respectively. Shape (ndim, 2).
"""
if
bounds
.
ndim
!=
2
:
raise
ValueError
(
"
bounds must be a 2d numpy array
"
)
elif
bounds
.
shape
[
0
]
!=
ndim
or
bounds
.
shape
[
1
]
!=
2
:
raise
ValueError
(
"
bounds must be of shape (ndim, 2)
"
)
lower_bounds
=
bounds
[:,
0
]
upper_bounds
=
bounds
[:,
1
]
if
np
.
any
(
lower_bounds
>=
upper_bounds
):
raise
ValueError
(
"
Lower bounds must be smaller than corresponding upper bounds
"
)
@beartype
@beartype
def
scale_samples
(
samples
:
np
.
ndarray
,
bounds
:
np
.
ndarray
)
->
np
.
ndarray
:
def
scale_samples
(
samples
:
np
.
ndarray
,
bounds
:
np
.
ndarray
)
->
np
.
ndarray
:
"""
Scale samples from a unit hypercube to arbitrary `bounds`.
"""
Scale samples from a unit hypercube to arbitrary `bounds`.
...
@@ -23,22 +46,13 @@ def scale_samples(samples: np.ndarray, bounds: np.ndarray) -> np.ndarray:
...
@@ -23,22 +46,13 @@ def scale_samples(samples: np.ndarray, bounds: np.ndarray) -> np.ndarray:
if
not
samples
.
ndim
==
2
:
if
not
samples
.
ndim
==
2
:
raise
ValueError
(
"
samples must be a 2D array
"
)
raise
ValueError
(
"
samples must be a 2D array
"
)
if
not
bounds
.
ndim
==
2
:
ndim
=
samples
.
shape
[
1
]
raise
ValueError
(
"
bounds must be a 2D array
"
)
check_bounds
(
ndim
,
bounds
)
if
not
samples
.
shape
[
1
]
==
bounds
.
shape
[
0
]:
raise
ValueError
(
"
The dimension of parameters in samples must match
"
"
that in bounds
"
)
lower_bounds
=
bounds
[:,
0
]
lower_bounds
=
bounds
[:,
0
]
upper_bounds
=
bounds
[:,
1
]
upper_bounds
=
bounds
[:,
1
]
if
np
.
any
(
lower_bounds
>=
upper_bounds
):
raise
ValueError
(
"
All lower_bounds (bounds[:,0]) must be smaller
"
"
than upper_bounds (bounds[:,1])
"
)
scaled_samples
=
samples
*
(
upper_bounds
-
lower_bounds
)
+
lower_bounds
scaled_samples
=
samples
*
(
upper_bounds
-
lower_bounds
)
+
lower_bounds
return
scaled_samples
return
scaled_samples
\ No newline at end of file
\ No newline at end of file
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