Skip to content

Commit f3cdec0

Browse files
authored
Remove raw_dict methods from ArrayStore (#575)
## Description <!-- Provide a brief description of the PR's purpose here. --> These methods have never been used outside of the ArrayStore tests. Furthermore, they introduce unnecessary complexity as part of implementing the Array API in #571 (it becomes tricky to consider `xp` and the `device`, in particular). ## TODO <!-- Notable points that this PR has either accomplished or will accomplish. --> - [x] Remove implementation - [x] Remove tests ## Status - [x] I have read the guidelines in [CONTRIBUTING.md](https://github.com/icaros-usc/pyribs/blob/master/CONTRIBUTING.md) - [x] I have formatted my code using `yapf` - [x] I have tested my code by running `pytest` - [x] I have linted my code with `pylint` - [x] I have added a one-line description of my change to the changelog in `HISTORY.md` - [x] This PR is ready to go
1 parent 193707d commit f3cdec0

File tree

3 files changed

+2
-126
lines changed

3 files changed

+2
-126
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#### API
88

99
- Support array backends via Python array API Standard ({pr}`573`)
10+
- **Backwards-incompatible:** Remove raw_dict methods from ArrayStore
11+
({pr}`575`)
1012

1113
## 0.8.1
1214

ribs/archives/_array_store.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -523,64 +523,3 @@ def resize(self, capacity):
523523
new_shape = (capacity,) + cur_arr.shape[1:]
524524
self._fields[name] = np.empty(new_shape, cur_arr.dtype)
525525
self._fields[name][:cur_capacity] = cur_arr
526-
527-
def as_raw_dict(self):
528-
"""Returns the raw data in the ArrayStore as a one-level dictionary.
529-
530-
To collapse the dict, we prefix each key with ``props.`` or ``fields.``,
531-
so the result looks as follows::
532-
533-
{
534-
"props.capacity": ...,
535-
"props.occupied": ...,
536-
...
537-
"fields.objective": ...,
538-
...
539-
}
540-
541-
Returns:
542-
dict: See description above.
543-
"""
544-
d = {}
545-
for prefix, attr in [("props", self._props), ("fields", self._fields)]:
546-
for name, val in attr.items():
547-
if isinstance(val, np.ndarray):
548-
val = readonly(val.view())
549-
d[f"{prefix}.{name}"] = val
550-
return d
551-
552-
@staticmethod
553-
def from_raw_dict(d):
554-
"""Loads an ArrayStore from a dict of raw info.
555-
556-
Args:
557-
d (dict): Dict returned by :meth:`as_raw_dict`.
558-
Returns:
559-
ArrayStore: The new ArrayStore created from d.
560-
Raises:
561-
ValueError: The loaded props dict has the wrong keys.
562-
"""
563-
# pylint: disable = protected-access
564-
565-
store = ArrayStore({}, 0) # Create an empty store.
566-
567-
props = {
568-
name[len("props."):]: arr
569-
for name, arr in d.items()
570-
if name.startswith("props.")
571-
}
572-
if props.keys() != store._props.keys():
573-
raise ValueError(
574-
f"Expected props to have keys {store._props.keys()} but "
575-
f"only found {props.keys()}")
576-
577-
fields = {
578-
name[len("fields."):]: arr
579-
for name, arr in d.items()
580-
if name.startswith("fields.")
581-
}
582-
583-
store._props = props
584-
store._fields = fields
585-
586-
return store

tests/archives/array_store_test.py

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -364,71 +364,6 @@ def test_resize_to_double_capacity(store):
364364
assert np.all(store._fields["objective"][[3, 5]] == [1.0, 2.0])
365365

366366

367-
def test_as_raw_dict(store):
368-
store.add(
369-
[3, 5],
370-
{
371-
"objective": [1.0, 2.0],
372-
"measures": [[1.0, 2.0], [3.0, 4.0]],
373-
"solution": [np.zeros(10), np.ones(10)],
374-
},
375-
)
376-
377-
d = store.as_raw_dict()
378-
379-
assert d.keys() == set([
380-
"props.capacity",
381-
"props.occupied",
382-
"props.n_occupied",
383-
"props.occupied_list",
384-
"props.updates",
385-
"fields.objective",
386-
"fields.measures",
387-
"fields.solution",
388-
])
389-
assert d["props.capacity"] == 10
390-
assert np.all(d["props.occupied"] == [0, 0, 0, 1, 0, 1, 0, 0, 0, 0])
391-
assert d["props.n_occupied"] == 2
392-
assert np.all(np.sort(d["props.occupied_list"][:2]) == [3, 5])
393-
assert np.all(d["props.updates"] == [1, 0]) # 1 add, 0 clear.
394-
assert np.all(d["fields.objective"][[3, 5]] == [1.0, 2.0])
395-
assert np.all(d["fields.measures"][[3, 5]] == [[1.0, 2.0], [3.0, 4.0]])
396-
assert np.all(d["fields.solution"][[3, 5]] == [np.zeros(10), np.ones(10)])
397-
398-
399-
def test_from_raw_dict_invalid_props(store):
400-
d = store.as_raw_dict()
401-
del d["props.capacity"]
402-
with pytest.raises(ValueError):
403-
ArrayStore.from_raw_dict(d)
404-
405-
406-
def test_from_raw_dict(store):
407-
store.add(
408-
[3, 5],
409-
{
410-
"objective": [1.0, 2.0],
411-
"measures": [[1.0, 2.0], [3.0, 4.0]],
412-
"solution": [np.zeros(10), np.ones(10)],
413-
},
414-
)
415-
416-
new_store = ArrayStore.from_raw_dict(store.as_raw_dict())
417-
418-
assert len(new_store) == 2
419-
assert np.all(new_store.occupied == [0, 0, 0, 1, 0, 1, 0, 0, 0, 0])
420-
assert np.all(np.sort(new_store.occupied_list) == [3, 5])
421-
422-
occupied, data = new_store.retrieve([5, 3])
423-
424-
assert np.all(occupied == [True, True])
425-
assert data.keys() == set(["objective", "measures", "solution", "index"])
426-
assert np.all(data["objective"] == [2.0, 1.0])
427-
assert np.all(data["measures"] == [[3.0, 4.0], [1.0, 2.0]])
428-
assert np.all(data["solution"] == [np.ones(10), np.zeros(10)])
429-
assert np.all(data["index"] == [5, 3])
430-
431-
432367
def test_data(store):
433368
store.add(
434369
[3, 5],

0 commit comments

Comments
 (0)