|
20 | 20 | """
|
21 | 21 | __all__ = ["MonteCarlo", "AnnealingSchedule", "GlobalOptimType"]
|
22 | 22 |
|
| 23 | +import warnings |
| 24 | +try: |
| 25 | + import ipywidgets as widgets |
| 26 | +except ImportError: |
| 27 | + widgets = None |
23 | 28 | from pyobjcryst._pyobjcryst import MonteCarlo as MonteCarlo_orig, AnnealingSchedule, GlobalOptimType
|
24 | 29 | from .refinableobj import *
|
25 | 30 |
|
26 | 31 |
|
27 | 32 | class MonteCarlo(MonteCarlo_orig):
|
28 | 33 |
|
29 |
| - def MultiRunOptimize(self, nb_run: int, nb_step: int, final_cost=0, max_time=-1): |
30 |
| - # Fix parameters that should not be optimised in a MonterCarlo run |
31 |
| - self.SetParIsFixed(refpartype_unitcell, True); |
32 |
| - self.SetParIsFixed(refpartype_scattdata_scale, True); |
33 |
| - self.SetParIsFixed(refpartype_scattdata_profile, True); |
34 |
| - self.SetParIsFixed(refpartype_scattdata_corr, True); |
35 |
| - self.SetParIsFixed(refpartype_scattdata_background, True); |
36 |
| - self.SetParIsFixed(refpartype_scattdata_radiation, True); |
| 34 | + def Optimize(self, nb_step: int, final_cost=0, max_time=-1): |
| 35 | + self._fix_parameters_for_global_optim() |
| 36 | + super().Optimize(int(nb_step), True, final_cost, max_time) |
37 | 37 |
|
| 38 | + def MultiRunOptimize(self, nb_run: int, nb_step: int, final_cost=0, max_time=-1): |
| 39 | + self._fix_parameters_for_global_optim() |
38 | 40 | super().MultiRunOptimize(int(nb_run), int(nb_step), True, final_cost, max_time)
|
39 | 41 |
|
40 | 42 | def RunSimulatedAnnealing(self, nb_step: int, final_cost=0, max_time=-1):
|
41 |
| - # Fix parameters that should not be optimised in a MonterCarlo run |
42 |
| - self.SetParIsFixed(refpartype_unitcell, True); |
43 |
| - self.SetParIsFixed(refpartype_scattdata_scale, True); |
44 |
| - self.SetParIsFixed(refpartype_scattdata_profile, True); |
45 |
| - self.SetParIsFixed(refpartype_scattdata_corr, True); |
46 |
| - self.SetParIsFixed(refpartype_scattdata_background, True); |
47 |
| - self.SetParIsFixed(refpartype_scattdata_radiation, True); |
48 |
| - |
| 43 | + self._fix_parameters_for_global_optim() |
49 | 44 | super().RunSimulatedAnnealing(int(nb_step), True, final_cost, max_time)
|
50 | 45 |
|
51 | 46 | def RunParallelTempering(self, nb_step: int, final_cost=0, max_time=-1):
|
| 47 | + self._fix_parameters_for_global_optim() |
| 48 | + super().RunParallelTempering(int(nb_step), True, final_cost, max_time) |
| 49 | + |
| 50 | + def _fix_parameters_for_global_optim(self): |
52 | 51 | # Fix parameters that should not be optimised in a MonterCarlo run
|
53 |
| - self.SetParIsFixed(refpartype_unitcell, True); |
54 |
| - self.SetParIsFixed(refpartype_scattdata_scale, True); |
55 |
| - self.SetParIsFixed(refpartype_scattdata_profile, True); |
56 |
| - self.SetParIsFixed(refpartype_scattdata_corr, True); |
57 |
| - self.SetParIsFixed(refpartype_scattdata_background, True); |
58 |
| - self.SetParIsFixed(refpartype_scattdata_radiation, True); |
| 52 | + self.SetParIsFixed(refpartype_unitcell, True) |
| 53 | + self.SetParIsFixed(refpartype_scattdata_scale, True) |
| 54 | + self.SetParIsFixed(refpartype_scattdata_profile, True) |
| 55 | + self.SetParIsFixed(refpartype_scattdata_corr, True) |
| 56 | + self.SetParIsFixed(refpartype_scattdata_background, True) |
| 57 | + self.SetParIsFixed(refpartype_scattdata_radiation, True) |
59 | 58 |
|
60 |
| - super().RunParallelTempering(int(nb_step), True, final_cost, max_time) |
| 59 | + def widget(self): |
| 60 | + """ |
| 61 | + Display a simple widget for this MonteCarloObj, which only updates the current |
| 62 | + cost (log-likelihood). Requires ipywidgets |
| 63 | + """ |
| 64 | + if widgets is None: |
| 65 | + warnings.warn("You need to install ipywidgets to use MonteCarlo.widget()") |
| 66 | + return |
| 67 | + self._widget = widgets.Box() |
| 68 | + # See https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Styling.html |
| 69 | + self._widget_label = widgets.Label("", layout=widgets.Layout(max_width='25%', width='20em')) |
| 70 | + self._widget_llk = widgets.Text("", disabled=True, layout=widgets.Layout(max_width='50%', width='30em')) |
| 71 | + self._widget.children = [widgets.HBox([self._widget_label, self._widget_llk])] |
| 72 | + self._widget_update() |
| 73 | + return self._widget |
61 | 74 |
|
62 | 75 | def UpdateDisplay(self):
|
| 76 | + try: |
| 77 | + if self._display_update_disabled: |
| 78 | + return |
| 79 | + except: |
| 80 | + pass |
| 81 | + try: |
| 82 | + if self._widget is not None: |
| 83 | + self._widget_update() |
| 84 | + except AttributeError: |
| 85 | + # self._3d_widget does not exist |
| 86 | + pass |
| 87 | + |
| 88 | + def disable_display_update(self): |
| 89 | + """ Disable display (useful for multiprocessing)""" |
| 90 | + self._display_update_disabled = True |
| 91 | + |
| 92 | + def enable_display_update(self): |
| 93 | + """ Enable display""" |
| 94 | + self._display_update_disabled = False |
| 95 | + |
| 96 | + def _widget_update(self): |
| 97 | + self._widget_label.value = "MonteCarlo:%s" % self.GetName() |
| 98 | + self._widget_label.layout.width = '%dem' % len(self._widget_label.value) |
63 | 99 | if self.IsOptimizing():
|
64 |
| - print("Run %2d Trial %8d LLK=%12.2f" % (self.run, self.trial, self.llk)) |
| 100 | + self._widget_llk.value = "LLK=%12.2f Run %2d Trial %8d" % (self.llk, self.run, self.trial) |
65 | 101 | else:
|
66 |
| - print("MonteCarlo: current LLK=%12.2f" % self.llk) |
| 102 | + self._widget_llk.value = "LLK=%12.2f " % self.llk |
| 103 | + self._widget_llk.layout.width = '%dem' % len(self._widget_llk.value) |
0 commit comments