Skip to content

Commit

Permalink
allow to not copy data (and hence save memory)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdevis committed Apr 2, 2024
1 parent 06b0c4e commit ee82fac
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion comet_maths/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.21.2"
__version__ = "0.22.3"
19 changes: 15 additions & 4 deletions comet_maths/generate_sample/generate_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def generate_sample(
corr_x = np.array([corr_x])
i = 0

if np.any(u_x[i] < 0):
if (not comp_list) and (not u_x[i] is None) and np.any(u_x[i] < 0):
raise ValueError(
"comet_maths.generate_sample: u_x cannot have any negative values"
)
Expand Down Expand Up @@ -650,7 +650,13 @@ def generate_sample_cov(


def correlate_sample_corr(
sample, corr, mean=None, std=None, dtype=None, iterate_sample=False
sample,
corr,
mean=None,
std=None,
dtype=None,
iterate_sample=False,
maintain_sample_unmodified=False,
):
"""
Method to correlate independent sample of input quantities using correlation matrix and Cholesky decomposition.
Expand All @@ -666,7 +672,9 @@ def correlate_sample_corr(
:param dtype: dtype of the produced sample
:type dtype: numpy.dtype, optional
:param iterate_sample: boolean to indicate if comet_maths should iterate over the different samples when introducing correlation. (This is more time-consuming but might be necessary if the different samples have different error correlations), defaults to False.
:type iterate_sample: bool
:type iterate_sample: bool, optional
:param maintain_sample_unmodified: boolean to indicate if the provided sample must remain unchanged (as opposed to introducing correlation). This requires a full copy of the sample, and thus doubles the memory required. Defaults to False.
:type maintain_sample_unmodified: bool, optional
:return: correlated sample of input quantities
:rtype: array[array]
"""
Expand All @@ -693,7 +701,10 @@ def correlate_sample_corr(
except:
L = cm.nearestPD_cholesky(corr, corr=True)

sample_out = copy.deepcopy(sample)
if maintain_sample_unmodified:
sample_out = copy.deepcopy(sample)
else:
sample_out = sample

if iterate_sample:
sample_out_iter = np.empty(len(sample), dtype=object)
Expand Down
6 changes: 4 additions & 2 deletions docs/content/random_generator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ generate_sample_random(), generate_sample_systematic() and generate_sample_corre
There is generate_sample_same() function that trivially generates a sample where u_x is zero and thus consists of repeats of the provided x array.
Then, there is the generate_sample_cov() function where a covariance matrix is provided instead of the u_x and corr_x (which contain the same information).

A somewhat more useful function is correlate_sample_corr(). This function can be used to add correlation to a previously generated sample.
This can particularly be useful when multiple MC samples where generated (e.g. for different variables, optionally each with their own internal
Another useful function is correlate_sample_corr(). This function can be used to add correlation to a previously generated sample.
This can particularly be useful when multiple MC samples were generated (e.g. for different variables, optionally each with their own internal
correlation) and these different samples need to be correlated (e.g. because the different variables themselves are correlated).
Note that by default the provided samples will be modified, though there is a keyword that allows to maintain the samples unchanged (at the cost of higher memory usage).

Finally, there is the generate_error_sample() function, which gives the differences between the sample generated by generate_sample() and x itself.

.. _pdf:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# required modules
numpy
numpy>=0.44.2
matplotlib
numdifftools
scikit-learn
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def read(filename):
"scikit-learn",
"numdifftools",
"scipy",
"punpy",
"punpy>=0.44.2",
"matplotlib",
],
extras_require={
Expand Down

0 comments on commit ee82fac

Please sign in to comment.