@@ -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,6 +238,54 @@ def plot_ref_sld_helper(
234238 plt .pause (0.005 )
235239
236240
241+ def _align_profiles (data : PlotEventData , confidence_intervals : dict | None = None ):
242+ """Align SLD profiles and resampled layers.
243+
244+ Aligns the A/L SLD profiles so that the substrates line up by padding the
245+ start of any shorter than the longest profile.
246+
247+ Parameters
248+ ----------
249+ data : PlotEventData
250+ The plot event data that contains all the information
251+ 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)
255+ """
256+ slds = data .sldProfiles
257+ size = (len (slds ), len (slds [0 ]))
258+
259+ # Find the length of the longest profile.
260+ lengths = [[sld .shape [0 ] for sld in sld_row ] for sld_row in slds ]
261+ max_value = np .max (lengths )
262+ max_index = np .unravel_index (np .argmax (lengths ), shape = size )
263+
264+ max_x = slds [max_index [0 ]][max_index [1 ]][:, 0 ]
265+ max_x_value = max_x [- 1 ]
266+
267+ for i in range (size [0 ]):
268+ for j in range (size [1 ]):
269+ cur_sld = slds [i ][j ]
270+ diff = max_value - cur_sld .shape [0 ]
271+ if diff :
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 ))
275+
276+ cur_resample_layer = data .resampledLayers [i ][j ]
277+ if not np .all (cur_resample_layer ):
278+ total_length = sum (cur_resample_layer [:, 0 ])
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+
288+
237289def plot_ref_sld (
238290 project : ratapi .Project ,
239291 results : ratapi .outputs .Results | ratapi .outputs .BayesResults ,
@@ -294,7 +346,7 @@ def plot_ref_sld(
294346 data .reflectivity = copy .deepcopy (results .reflectivity )
295347 data .shiftedData = results .shiftedData
296348 data .sldProfiles = copy .deepcopy (results .sldProfiles )
297- data .resampledLayers = results .resampledLayers
349+ data .resampledLayers = copy . deepcopy ( results .resampledLayers )
298350 data .dataPresent = ratapi .inputs .make_data_present (project )
299351 data .subRoughs = results .contrastParams .subRoughs
300352 data .resample = ratapi .inputs .make_resample (project )
@@ -355,6 +407,7 @@ def plot_ref_sld(
355407 show_grid = show_grid ,
356408 show_legend = show_legend ,
357409 shift_value = shift_value ,
410+ align_profile = (project .geometry == "air/substrate" and project .model != "custom xy" ),
358411 )
359412
360413 if return_fig :
@@ -533,13 +586,12 @@ def update_foreground(self, data):
533586 self .figure .canvas .restore_region (self .bg )
534587 plot_data = _extract_plot_data (data , self .q4 , self .show_error_bar , self .shift_value )
535588
536- offset = 2
537- for i in range (
538- 0 ,
539- len (self .figure .axes [0 ].lines ),
540- ):
541- self .figure .axes [0 ].lines [i ].set_data (plot_data ["ref" ][i // offset ][0 ], plot_data ["ref" ][i // offset ][1 ])
542- 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
543595
544596 i = 0
545597 for j in range (len (plot_data ["sld" ])):
@@ -553,12 +605,14 @@ def update_foreground(self, data):
553605 self .figure .axes [1 ].draw_artist (self .figure .axes [1 ].lines [i ])
554606 i += 1
555607
556- for i , container in enumerate (self .figure .axes [0 ].containers ):
557- self .adjust_error_bar (
558- container , plot_data ["error" ][i ][0 ], plot_data ["error" ][i ][1 ], plot_data ["error" ][i ][2 ]
559- )
560- self .figure .axes [0 ].draw_artist (container [2 ][0 ])
561- 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
562616
563617 self .figure .canvas .blit (self .figure .bbox )
564618 self .figure .canvas .flush_events ()
0 commit comments