Skip to content

Commit 759c4a4

Browse files
committed
Support NumPy 2
Not too long ago, NumPy 2.0 got released. cluster_utils didn't work with it out of the box, so we restricted the dependency to numpy<2. Looking into it now, it seems the only actual problem was in the `Discrete` distribution when using booleans: Using NumPy's `choice()` converted the list of native bools to an array of np.bool. Later one, this clashed when passing the parameters to the job script, where they are parsed with `ast.literal_eval()`. Simply converting back to list with `.tolist()` seems to be enough to fix this.
1 parent c73efde commit 759c4a4

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Moved documentation from GitHub Pages to Read the Docs. This allows to more easily
1515
manage docs for different versions.
1616

17+
### Added
18+
- Support for Numpy 2.
19+
20+
1721
## [3.0.0] - 2024-08-19
1822

1923
### Changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Issues = "https://github.com/martius-lab/cluster_utils/issues"
5353
runner = [
5454
"colorama",
5555
"gitpython>=3.0.5",
56-
"numpy<2",
56+
"numpy",
5757
"pandas[output_formatting]>=2.0.3",
5858
"scipy",
5959
"tqdm",

src/cluster_utils/server/distributions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,16 @@ def prepare_samples(self, howmany):
246246
howmany = min(
247247
10, howmany
248248
) # HACK: for smart rounding a reasonable sample size is needed
249-
self.samples = get_rng().choice(self.option_list, p=self.probs, size=howmany)
249+
250+
_samples = get_rng().choice(self.option_list, p=self.probs, size=howmany)
251+
# choice() returns a numpy array, implicitly converting value types to numpy
252+
# types (e.g. native bool becomes np.bool). This causes trouble later on, as
253+
# the parameters passed to the job script are parsed with ast.literal_eval,
254+
# which can only handle native types. Converting back to list here, should also
255+
# get us back to native types for the elements (at least successfully tested
256+
# with bool).
257+
self.samples = _samples.tolist()
258+
250259
super().prepare_samples(howmany)
251260

252261
def plot(self):

0 commit comments

Comments
 (0)