|
1 | 1 | from __future__ import annotations
|
2 |
| -from dataclasses import field |
3 |
| -from typing import Optional |
| 2 | +from typing import Dict, Optional |
| 3 | +from attr import define, field |
4 | 4 |
|
5 |
| -from dataclasses_json.cfg import config |
6 |
| -from cellengine.utils.dataclass_mixin import DataClassMixin, ReadOnly |
7 |
| - |
8 |
| -from dataclasses import dataclass |
9 | 5 | import cellengine as ce
|
| 6 | +from cellengine.utils import converter |
| 7 | +from cellengine.utils.readonly import readonly |
10 | 8 |
|
11 | 9 |
|
12 |
| -@dataclass |
13 |
| -class Population(DataClassMixin): |
| 10 | +@define |
| 11 | +class Population: |
| 12 | + _id: str = field(on_setattr=readonly) |
| 13 | + experiment_id: str = field(on_setattr=readonly) |
14 | 14 | name: str
|
15 | 15 | gates: str
|
| 16 | + unique_name: Optional[str] = field(default=None, on_setattr=readonly) |
16 | 17 | parent_id: Optional[str] = None
|
17 | 18 | terminal_gate_gid: Optional[str] = None
|
18 |
| - _id: str = field( |
19 |
| - metadata=config(field_name="_id"), default=ReadOnly() |
20 |
| - ) # type: ignore |
21 |
| - experiment_id: str = field(default=ReadOnly()) # type: ignore |
22 |
| - unique_name: str = field(default=ReadOnly()) # type: ignore |
23 | 19 |
|
24 | 20 | def __repr__(self):
|
25 | 21 | return f"Population(_id='{self._id}', name='{self.name}')"
|
26 | 22 |
|
27 |
| - @classmethod |
28 |
| - def get(cls, experiment_id: str, _id: str = None, name: str = None): |
29 |
| - kwargs = {"name": name} if name else {"_id": _id} |
30 |
| - return ce.APIClient().get_population(experiment_id, **kwargs) |
| 23 | + @property |
| 24 | + def client(self): |
| 25 | + return ce.APIClient() |
| 26 | + |
| 27 | + @property |
| 28 | + def path(self): |
| 29 | + return f"experiments/{self.experiment_id}/populations/{self._id}".rstrip( |
| 30 | + "/None" |
| 31 | + ) |
31 | 32 |
|
32 | 33 | @classmethod
|
33 |
| - def create(cls, experiment_id: str, population: dict) -> Population: |
34 |
| - return ce.APIClient().post_population(experiment_id, population) |
| 34 | + def from_dict(cls, data: Dict): |
| 35 | + return converter.structure(data, cls) |
| 36 | + |
| 37 | + def to_dict(self) -> Dict: |
| 38 | + return converter.unstructure(self) |
35 | 39 |
|
36 | 40 | def update(self):
|
37 | 41 | """Save changes to this Population to CellEngine."""
|
38 |
| - res = ce.APIClient().update_entity( |
39 |
| - self.experiment_id, self._id, "populations", self.to_dict() |
40 |
| - ) |
41 |
| - self.__dict__.update(Population.from_dict(res).__dict__) |
| 42 | + res = self.client.update(self) |
| 43 | + self.__setstate__(res.__getstate__()) # type: ignore |
42 | 44 |
|
43 | 45 | def delete(self):
|
44 |
| - ce.APIClient().delete_entity(self.experiment_id, "populations", self._id) |
| 46 | + self.client.delete_entity(self.experiment_id, "populations", self._id) |
0 commit comments