Skip to content

Commit 7ff2a53

Browse files
committed
classmethod thin functions
1 parent 3c41f8e commit 7ff2a53

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

getdist/chains.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,22 @@ def thin_indices(self, factor, weights=None):
844844
"""
845845
if weights is None:
846846
weights = self.weights
847+
return WeightedSamples.thin_indices_single_samples(factor, weights)
848+
849+
@staticmethod
850+
def thin_indices_and_weights(factor, weights):
851+
"""
852+
Returns indices and new weights for use when thinning samples.
853+
854+
:param factor: thin factor
855+
:param weights: initial weight (counts) per sample point
856+
:return: (unique index, counts) tuple of sample index values to keep and new weights
857+
"""
858+
thin_ix = WeightedSamples.thin_indices_single_samples(factor, weights)
859+
return np.unique(thin_ix, return_counts=True)
860+
861+
@staticmethod
862+
def thin_indices_single_samples(factor, weights):
847863
numrows = len(weights)
848864
norm1 = np.sum(weights)
849865
weights = weights.astype(int)
@@ -914,8 +930,7 @@ def weighted_thin(self, factor):
914930
This function also preserves separate chains.
915931
:param factor: The (integer) factor to thin by
916932
"""
917-
thin_ix = self.thin_indices(factor)
918-
unique, counts = np.unique(thin_ix, return_counts=True)
933+
unique, counts = self.thin_indices_and_weights(factor, self.weights)
919934
self.setSamples(self.samples[unique, :],
920935
loglikes=None if self.loglikes is None
921936
else self.loglikes[unique],
@@ -1137,7 +1152,8 @@ def _getParamIndices(self):
11371152
:return: A dict mapping the param name to the parameter index.
11381153
"""
11391154
if self.samples is not None and len(self.paramNames.names) != self.n:
1140-
raise WeightedSampleError("paramNames size does not match number of parameters in samples")
1155+
raise WeightedSampleError("paramNames size (%s) does not match number of "
1156+
"parameters in samples (%s)" % (len(self.paramNames.names), self.n))
11411157
index = dict()
11421158
for i, name in enumerate(self.paramNames.names):
11431159
index[name.name] = i

getdist/paramnames.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ def numberOfName(self, name):
257257
return i
258258
return -1
259259

260+
def hasParam(self, name):
261+
return self.numberOfName(name) != -1
262+
260263
def parsWithNames(self, names, error=False, renames=None):
261264
"""
262265
gets the list of :class:`ParamInfo` instances for given list of name strings.

0 commit comments

Comments
 (0)