@@ -60,6 +60,7 @@ def _extract_plot_data(event_data: PlotEventData, q4: bool, show_error_bar: bool
6060 # Plot the reflectivity on plot (1,1)
6161 results ["ref" ].append ([r [:, 0 ], r [:, 1 ] * mult ])
6262
63+ results ["error" ].append ([])
6364 if event_data .dataPresent [i ]:
6465 sd_x = data [:, 0 ]
6566 sd_y , sd_e = map (lambda x : x * mult , (data [:, 1 ], data [:, 2 ]))
@@ -73,7 +74,7 @@ def _extract_plot_data(event_data: PlotEventData, q4: bool, show_error_bar: bool
7374 valid = np .ones (len (sd_e )).astype (bool )
7475 sd_e = errors
7576
76- results ["error" ]. append ([sd_x [valid ], sd_y [valid ], sd_e [valid ]])
77+ results ["error" ][ - 1 ]. extend ([sd_x [valid ], sd_y [valid ], sd_e [valid ]])
7778
7879 results ["sld" ].append ([])
7980 for j in range (len (sld )):
@@ -111,6 +112,7 @@ def plot_ref_sld_helper(
111112 show_legend : bool = True ,
112113 shift_value : float = 100 ,
113114 animated = False ,
115+ align_profile = False ,
114116):
115117 """Clear the previous plots and updates the ref and SLD plots.
116118
@@ -160,6 +162,8 @@ def plot_ref_sld_helper(
160162 ref_plot .cla ()
161163 sld_plot .cla ()
162164
165+ if align_profile :
166+ _align_profiles (data , confidence_intervals )
163167 plot_data = _extract_plot_data (data , q4 , show_error_bar , shift_value )
164168 for i , name in enumerate (data .contrastNames ):
165169 ref_plot .plot (plot_data ["ref" ][i ][0 ], plot_data ["ref" ][i ][1 ], label = name , linewidth = 1 , animated = animated )
@@ -234,45 +238,53 @@ def plot_ref_sld_helper(
234238 plt .pause (0.005 )
235239
236240
237- def _align_profiles (data : PlotEventData ):
241+ def _align_profiles (data : PlotEventData , confidence_intervals : dict | None = None ):
238242 """Align SLD profiles and resampled layers.
239243
240- Aligns the A/L SLD profiles so that the substrates line up by padding the
244+ Aligns the A/L SLD profiles so that the substrates line up by padding the
241245 start of any shorter than the longest profile.
242246
243247 Parameters
244248 ----------
245249 data : PlotEventData
246250 The plot event data that contains all the information
247251 to generate the ref and sld plots
252+ confidence_intervals : dict or None, default None
253+ The Bayesian confidence intervals for reflectivity and SLD.
254+ Only relevant if the procedure used is Bayesian (NS or DREAM)
248255 """
249256 slds = data .sldProfiles
250257 size = (len (slds ), len (slds [0 ]))
251258
252259 # Find the length of the longest profile.
253- lengths = [[sld .shape [0 ] for sld in sld_row ] for sld_row in slds ]
260+ lengths = [[sld .shape [0 ] for sld in sld_row ] for sld_row in slds ]
254261 max_value = np .max (lengths )
255262 max_index = np .unravel_index (np .argmax (lengths ), shape = size )
256263
257- max_x = slds [max_index [0 ]][max_index [1 ]][:, 0 ]
264+ max_x = slds [max_index [0 ]][max_index [1 ]][:, 0 ]
258265 max_x_value = max_x [- 1 ]
259-
266+
260267 for i in range (size [0 ]):
261268 for j in range (size [1 ]):
262269 cur_sld = slds [i ][j ]
263270 diff = max_value - cur_sld .shape [0 ]
264271 if diff :
265- pad = np .zeros ((max_value , 2 ))
266- pad [:, 0 ] = max_x
267- pad [diff :, 1 ] = cur_sld [:, 1 ]
268- slds [i ][j ] = pad
272+ pad = np .zeros (diff )
273+ max_y = np .concatenate ((pad , cur_sld [:, 1 ]))
274+ slds [i ][j ] = np .column_stack ((max_x , max_y ))
269275
270276 cur_resample_layer = data .resampledLayers [i ][j ]
271277 if not np .all (cur_resample_layer ):
272278 total_length = sum (cur_resample_layer [:, 0 ])
273- pad = max_x_value - total_length
274- data .resampledLayers [i ][j ] = np .vstack ([[pad , 0 , 0 ], cur_resample_layer ])
275-
279+ offset = max_x_value - total_length
280+ data .resampledLayers [i ][j ] = np .vstack (([offset , 0 , 0 ], cur_resample_layer ))
281+
282+ if confidence_intervals is not None :
283+ cur_ci = confidence_intervals ["sld" ][i ][j ]
284+ inter_a = np .concatenate ((pad , cur_ci [0 ]))
285+ inter_b = np .concatenate ((pad , cur_ci [1 ]))
286+ confidence_intervals ["sld" ][i ][j ] = (inter_a , inter_b )
287+
276288
277289def plot_ref_sld (
278290 project : ratapi .Project ,
@@ -334,7 +346,7 @@ def plot_ref_sld(
334346 data .reflectivity = copy .deepcopy (results .reflectivity )
335347 data .shiftedData = results .shiftedData
336348 data .sldProfiles = copy .deepcopy (results .sldProfiles )
337- data .resampledLayers = results .resampledLayers
349+ data .resampledLayers = copy . deepcopy ( results .resampledLayers )
338350 data .dataPresent = ratapi .inputs .make_data_present (project )
339351 data .subRoughs = results .contrastParams .subRoughs
340352 data .resample = ratapi .inputs .make_resample (project )
@@ -395,6 +407,7 @@ def plot_ref_sld(
395407 show_grid = show_grid ,
396408 show_legend = show_legend ,
397409 shift_value = shift_value ,
410+ align_profile = (project .geometry == "air/substrate" and project .model != "custom xy" ),
398411 )
399412
400413 if return_fig :
@@ -573,13 +586,12 @@ def update_foreground(self, data):
573586 self .figure .canvas .restore_region (self .bg )
574587 plot_data = _extract_plot_data (data , self .q4 , self .show_error_bar , self .shift_value )
575588
576- offset = 2
577- for i in range (
578- 0 ,
579- len (self .figure .axes [0 ].lines ),
580- ):
581- self .figure .axes [0 ].lines [i ].set_data (plot_data ["ref" ][i // offset ][0 ], plot_data ["ref" ][i // offset ][1 ])
582- self .figure .axes [0 ].draw_artist (self .figure .axes [0 ].lines [i ])
589+ offset = 0
590+ for i in range (len (data .contrastNames )):
591+ for _ in range (int (data .dataPresent [i ]) + 1 ):
592+ self .figure .axes [0 ].lines [offset ].set_data (plot_data ["ref" ][i ][0 ], plot_data ["ref" ][i ][1 ])
593+ self .figure .axes [0 ].draw_artist (self .figure .axes [0 ].lines [offset ])
594+ offset += 1
583595
584596 i = 0
585597 for j in range (len (plot_data ["sld" ])):
@@ -593,12 +605,14 @@ def update_foreground(self, data):
593605 self .figure .axes [1 ].draw_artist (self .figure .axes [1 ].lines [i ])
594606 i += 1
595607
596- for i , container in enumerate (self .figure .axes [0 ].containers ):
597- self .adjust_error_bar (
598- container , plot_data ["error" ][i ][0 ], plot_data ["error" ][i ][1 ], plot_data ["error" ][i ][2 ]
599- )
600- self .figure .axes [0 ].draw_artist (container [2 ][0 ])
601- self .figure .axes [0 ].draw_artist (container [0 ])
608+ i = 0
609+ for error in plot_data ["error" ]:
610+ if error :
611+ container = self .figure .axes [0 ].containers [i ]
612+ self .adjust_error_bar (container , error [0 ], error [1 ], error [2 ])
613+ self .figure .axes [0 ].draw_artist (container [2 ][0 ])
614+ self .figure .axes [0 ].draw_artist (container [0 ])
615+ i += 1
602616
603617 self .figure .canvas .blit (self .figure .bbox )
604618 self .figure .canvas .flush_events ()
0 commit comments