|
12 | 12 | ChartPlotter, |
13 | 13 | DEFAULT_HEIGHT |
14 | 14 | ) |
15 | | -from easydiffraction.experiments.experiments import Experiments |
16 | 15 | from easydiffraction.core.objects import ( |
17 | 16 | Descriptor, |
18 | 17 | Parameter |
19 | 18 | ) |
20 | | -from easydiffraction.core.singletons import ( |
21 | | - ConstraintsHandler, |
22 | | - UidMapHandler |
23 | | -) |
| 19 | +from easydiffraction.core.singletons import ConstraintsHandler |
| 20 | +from easydiffraction.experiments.experiments import Experiments |
24 | 21 |
|
25 | | -from .collections.aliases import ConstraintAliases |
26 | | -from .collections.constraints import ConstraintExpressions |
| 22 | +from .collections.aliases import Aliases |
| 23 | +from .collections.constraints import Constraints |
| 24 | +from .collections.joint_fit_experiments import JointFitExperiments |
27 | 25 | from .calculators.calculator_factory import CalculatorFactory |
28 | 26 | from .minimization import DiffractionMinimizer |
29 | 27 | from .minimizers.minimizer_factory import MinimizerFactory |
30 | | -from easydiffraction.analysis.collections.joint_fit_experiments import JointFitExperiments |
31 | 28 |
|
32 | 29 |
|
33 | 30 | class Analysis: |
34 | 31 | _calculator = CalculatorFactory.create_calculator('cryspy') |
35 | 32 |
|
36 | 33 | def __init__(self, project: Project) -> None: |
37 | 34 | self.project = project |
38 | | - self.aliases = ConstraintAliases() |
39 | | - self.constraints = ConstraintExpressions() |
| 35 | + self.aliases = Aliases() |
| 36 | + self.constraints = Constraints() |
40 | 37 | self.constraints_handler = ConstraintsHandler.get() |
41 | 38 | self.calculator = Analysis._calculator # Default calculator shared by project |
42 | 39 | self._calculator_key: str = 'cryspy' # Added to track the current calculator |
@@ -196,14 +193,16 @@ def how_to_access_parameters(self, show_description: bool = False) -> None: |
196 | 193 | if entry_id: |
197 | 194 | variable += f"['{entry_id}']" |
198 | 195 | variable += f".{param_key}" |
199 | | - rows.append({'variable': variable, |
200 | | - 'description': description}) |
| 196 | + uid = param._generate_human_readable_unique_id() |
| 197 | + rows.append({'Code variable': variable, |
| 198 | + 'Unique ID for CIF': uid, |
| 199 | + 'Description': description}) |
201 | 200 |
|
202 | 201 | dataframe = pd.DataFrame(rows) |
203 | 202 |
|
204 | | - column_headers = ['variable'] |
| 203 | + column_headers = ['Code variable', 'Unique ID for CIF'] |
205 | 204 | if show_description: |
206 | | - column_headers = ['variable', 'description'] |
| 205 | + column_headers.append('description') |
207 | 206 | dataframe = dataframe[column_headers] |
208 | 207 |
|
209 | 208 | indices = range(1, len(dataframe) + 1) # Force starting from 1 |
@@ -315,47 +314,31 @@ def show_constraints(self) -> None: |
315 | 314 | return |
316 | 315 |
|
317 | 316 | rows = [] |
318 | | - for id, constraint in constraints_dict.items(): |
| 317 | + for constraint in constraints_dict.values(): |
319 | 318 | row = { |
320 | | - 'id': id, |
321 | 319 | 'lhs_alias': constraint.lhs_alias.value, |
322 | 320 | 'rhs_expr': constraint.rhs_expr.value, |
323 | 321 | 'full expression': f'{constraint.lhs_alias.value} = {constraint.rhs_expr.value}' |
324 | 322 | } |
325 | 323 | rows.append(row) |
326 | 324 |
|
327 | 325 | dataframe = pd.DataFrame(rows) |
| 326 | + indices = range(1, len(dataframe) + 1) # Force starting from 1 |
328 | 327 |
|
329 | 328 | print(paragraph(f"User defined constraints")) |
330 | 329 | print(tabulate(dataframe, |
331 | 330 | headers=dataframe.columns, |
332 | 331 | tablefmt="fancy_outline", |
333 | | - showindex=False)) |
334 | | - |
335 | | - def _update_uid_map(self) -> None: |
336 | | - """ |
337 | | - Update the UID map for accessing parameters by UID. |
338 | | - This is needed for adding or removing constraints. |
339 | | - """ |
340 | | - sample_models_params = self.project.sample_models.get_all_params() |
341 | | - experiments_params = self.project.experiments.get_all_params() |
342 | | - params = sample_models_params + experiments_params |
343 | | - |
344 | | - UidMapHandler.get().set_uid_map(params) |
| 332 | + showindex=indices)) |
345 | 333 |
|
346 | | - def apply_constraints(self) -> None: |
| 334 | + def apply_constraints(self): |
347 | 335 | if not self.constraints._items: |
348 | 336 | print(warning(f"No constraints defined.")) |
349 | 337 | return |
350 | 338 |
|
351 | | - sample_models_params = self.project.sample_models.get_fittable_params() |
352 | | - experiments_params = self.project.experiments.get_fittable_params() |
353 | | - fittable_params = sample_models_params + experiments_params |
354 | | - |
355 | | - self._update_uid_map() |
356 | 339 | self.constraints_handler.set_aliases(self.aliases) |
357 | | - self.constraints_handler.set_expressions(self.constraints) |
358 | | - self.constraints_handler.apply(parameters=fittable_params) |
| 340 | + self.constraints_handler.set_constraints(self.constraints) |
| 341 | + self.constraints_handler.apply() |
359 | 342 |
|
360 | 343 | def show_calc_chart(self, expt_name: str, x_min: Optional[float] = None, x_max: Optional[float] = None) -> None: |
361 | 344 | self.calculate_pattern(expt_name) |
@@ -442,12 +425,22 @@ def fit(self) -> None: |
442 | 425 | # After fitting, get the results |
443 | 426 | self.fit_results = self.fitter.results |
444 | 427 |
|
445 | | - def as_cif(self) -> str: |
| 428 | + def as_cif(self): |
| 429 | + current_minimizer = self.current_minimizer |
| 430 | + if " " in current_minimizer: |
| 431 | + current_minimizer = f'"{current_minimizer}"' |
| 432 | + |
446 | 433 | lines = [] |
447 | 434 | lines.append(f"_analysis.calculator_engine {self.current_calculator}") |
448 | | - lines.append(f"_analysis.fitting_engine {self.current_minimizer}") |
| 435 | + lines.append(f"_analysis.fitting_engine {current_minimizer}") |
449 | 436 | lines.append(f"_analysis.fit_mode {self.fit_mode}") |
450 | 437 |
|
| 438 | + lines.append("") |
| 439 | + lines.append(self.aliases.as_cif()) |
| 440 | + |
| 441 | + lines.append("") |
| 442 | + lines.append(self.constraints.as_cif()) |
| 443 | + |
451 | 444 | return "\n".join(lines) |
452 | 445 |
|
453 | 446 | def show_as_cif(self) -> None: |
|
0 commit comments