Skip to content

Commit 7b6e7db

Browse files
committed
Refactor factory methods, etc.
1 parent 0c3e5cc commit 7b6e7db

54 files changed

Lines changed: 1317 additions & 987 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/user-guide/analysis-workflow/analysis.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,21 @@ An example of setting aliases for parameters in a structure:
269269

270270
```python
271271
# Set aliases for the atomic displacement parameters
272-
project.analysis.aliases.add(
272+
project.analysis.aliases.add_from_scratch(
273273
label='biso_La',
274274
param_uid=project.structures['lbco'].atom_sites['La'].b_iso.uid,
275275
)
276-
project.analysis.aliases.add(
276+
project.analysis.aliases.add_from_scratch(
277277
label='biso_Ba',
278278
param_uid=project.structures['lbco'].atom_sites['Ba'].b_iso.uid,
279279
)
280280

281281
# Set aliases for the occupancies of the atom sites
282-
project.analysis.aliases.add(
282+
project.analysis.aliases.add_from_scratch(
283283
label='occ_La',
284284
param_uid=project.structures['lbco'].atom_sites['La'].occupancy.uid,
285285
)
286-
project.analysis.aliases.add(
286+
project.analysis.aliases.add_from_scratch(
287287
label='occ_Ba',
288288
param_uid=project.structures['lbco'].atom_sites['Ba'].occupancy.uid,
289289
)
@@ -300,12 +300,12 @@ other aliases.
300300
An example of setting constraints for the aliases defined above:
301301

302302
```python
303-
project.analysis.constraints.add(
303+
project.analysis.constraints.add_from_scratch(
304304
lhs_alias='biso_Ba',
305305
rhs_expr='biso_La',
306306
)
307307

308-
project.analysis.constraints.add(
308+
project.analysis.constraints.add_from_scratch(
309309
lhs_alias='occ_Ba',
310310
rhs_expr='1 - occ_La',
311311
)

docs/user-guide/analysis-workflow/experiment.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ experiment = Experiment(
153153
radiation_probe='neutron',
154154
scattering_type='bragg',
155155
)
156-
project.experiments.add(experiment)
156+
project.experiments.add_from_scratch(experiment)
157157
```
158158

159159
## Modifying Parameters
@@ -188,8 +188,8 @@ project.experiments['hrpt'].instrument.calib_twotheta_offset = 0.6
188188

189189
```python
190190
# Add excluded regions to the experiment
191-
project.experiments['hrpt'].excluded_regions.add(start=0, end=10)
192-
project.experiments['hrpt'].excluded_regions.add(start=160, end=180)
191+
project.experiments['hrpt'].excluded_regions.add_from_scratch(start=0, end=10)
192+
project.experiments['hrpt'].excluded_regions.add_from_scratch(start=160, end=180)
193193
```
194194

195195
### 3. Peak Category { #peak-category }
@@ -213,18 +213,18 @@ project.experiments['hrpt'].peak.broad_lorentz_y = 0.1
213213
project.experiments['hrpt'].background_type = 'line-segment'
214214

215215
# Add background points
216-
project.experiments['hrpt'].background.add(x=10, y=170)
217-
project.experiments['hrpt'].background.add(x=30, y=170)
218-
project.experiments['hrpt'].background.add(x=50, y=170)
219-
project.experiments['hrpt'].background.add(x=110, y=170)
220-
project.experiments['hrpt'].background.add(x=165, y=170)
216+
project.experiments['hrpt'].background.add_from_scratch(x=10, y=170)
217+
project.experiments['hrpt'].background.add_from_scratch(x=30, y=170)
218+
project.experiments['hrpt'].background.add_from_scratch(x=50, y=170)
219+
project.experiments['hrpt'].background.add_from_scratch(x=110, y=170)
220+
project.experiments['hrpt'].background.add_from_scratch(x=165, y=170)
221221
```
222222

223223
### 5. Linked Phases Category { #linked-phases-category }
224224

225225
```python
226226
# Link the structure defined in the previous step to the experiment
227-
project.experiments['hrpt'].linked_phases.add(id='lbco', scale=10.0)
227+
project.experiments['hrpt'].linked_phases.add_from_scratch(id='lbco', scale=10.0)
228228
```
229229

230230
### 6. Measured Data Category { #measured-data-category }

docs/user-guide/analysis-workflow/model.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ reference it later.
6464
```python
6565
# Add a structure with default parameters
6666
# The structure name is used to reference it later.
67-
project.structures.add(name='nacl')
67+
project.structures.add_from_scratch(name='nacl')
6868
```
6969

7070
The `add` method creates a new structure with default parameters. You can then

src/easydiffraction/analysis/analysis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def fit_mode(self, strategy: str) -> None:
416416
# Pre-populate all experiments with weight 0.5
417417
self.joint_fit_experiments = JointFitExperiments()
418418
for id in self.project.experiments.names:
419-
self.joint_fit_experiments.add(id=id, weight=0.5)
419+
self.joint_fit_experiments.add_from_scratch(id=id, weight=0.5)
420420
console.paragraph('Current fit mode changed to')
421421
console.print(self._fit_mode)
422422

@@ -554,7 +554,7 @@ def fit(self):
554554
# parameters can be resolved correctly during fitting.
555555
object.__setattr__(dummy_experiments, '_parent', self.project)
556556

557-
dummy_experiments._add(experiment)
557+
dummy_experiments.add(experiment)
558558
self.fitter.fit(
559559
structures,
560560
dummy_experiments,

src/easydiffraction/core/category.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,27 @@ def from_cif(self, block):
103103
"""Populate this collection from a CIF block."""
104104
category_collection_from_cif(self, block)
105105

106-
@checktype
107-
def _add(self, item) -> None:
108-
"""Add an item to the collection."""
106+
def add(self, item) -> None:
107+
"""Insert a pre-built item into the collection.
108+
109+
Args:
110+
item: A ``CategoryItem`` instance to add.
111+
"""
109112
self[item._identity.category_entry_name] = item
110113

111114
@checktype
112-
def add(self, **kwargs) -> None:
113-
"""Create and add a new child instance from the provided
114-
arguments.
115+
def add_from_scratch(self, **kwargs) -> None:
116+
"""Create a new item with the given attributes and add it.
117+
118+
A default instance of the collection's item type is created,
119+
then each keyword argument is applied via ``setattr``.
120+
121+
Args:
122+
**kwargs: Attribute names and values for the new item.
115123
"""
116124
child_obj = self._item_type()
117125

118126
for attr, val in kwargs.items():
119127
setattr(child_obj, attr, val)
120128

121-
self._add(child_obj)
129+
self.add(child_obj)

src/easydiffraction/core/datablock.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
from __future__ import annotations
55

6-
from typeguard import typechecked
7-
86
from easydiffraction.core.category import CategoryCollection
97
from easydiffraction.core.category import CategoryItem
108
from easydiffraction.core.collection import CollectionBase
@@ -21,9 +19,17 @@ def __init__(self):
2119

2220
def __str__(self) -> str:
2321
"""Human-readable representation of this component."""
24-
name = self._log_name
25-
items = getattr(self, '_items', None)
26-
return f'<{name} ({items})>'
22+
name = self.unique_name
23+
cls = type(self).__name__
24+
categories = '\n'.join(f' - {c}' for c in self.categories)
25+
return f"{cls} datablock '{name}':\n{categories}"
26+
27+
def __repr__(self) -> str:
28+
"""Developer-oriented representation of this component."""
29+
name = self.unique_name
30+
cls = type(self).__name__
31+
num_categories = len(self.categories)
32+
return f'<{cls} datablock "{name}" ({num_categories} categories)>'
2733

2834
def _update_categories(
2935
self,
@@ -88,8 +94,21 @@ class DatablockCollection(CollectionBase):
8894
Experiments).
8995
9096
Each item is a DatablockItem.
97+
98+
Subclasses provide explicit ``add_from_*`` convenience methods
99+
that delegate to the corresponding factory classmethods, then
100+
call :meth:`add` with the resulting item.
91101
"""
92102

103+
def add(self, item) -> None:
104+
"""Add a pre-built item to the collection.
105+
106+
Args:
107+
item: A ``DatablockItem`` instance (e.g. a ``Structure``
108+
or ``ExperimentBase`` subclass).
109+
"""
110+
self[item._identity.datablock_entry_name] = item
111+
93112
def __str__(self) -> str:
94113
"""Human-readable representation of this component."""
95114
name = self._log_name
@@ -124,8 +143,3 @@ def as_cif(self) -> str:
124143
from easydiffraction.io.cif.serialize import datablock_collection_to_cif
125144

126145
return datablock_collection_to_cif(self)
127-
128-
@typechecked
129-
def _add(self, item) -> None:
130-
"""Add an item to the collection."""
131-
self[item._identity.datablock_entry_name] = item

src/easydiffraction/core/factory.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/easydiffraction/datablocks/experiment/categories/peak/factory.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from easydiffraction.datablocks.experiment.item.enums import ScatteringTypeEnum
99

1010

11-
# TODO: Consider inheriting from FactoryBase
1211
class PeakFactory:
1312
"""Factory for creating peak profile objects.
1413

0 commit comments

Comments
 (0)