2121# SOFTWARE.
2222
2323from dataclasses import dataclass , field
24- from typing import Optional
2524
2625from ansys .systemcoupling .core .charts .chart_datatypes import InterfaceInfo , SeriesType
2726
@@ -31,6 +30,7 @@ class DataTransferSpec:
3130 # It's not ideal, but we have to work in terms of display names for transfers,
3231 # as that is all we have in the data (the CSV data, at least).
3332 # TODO: add optional internal name field which we will use if provided.
33+ name : str
3434 display_name : str
3535 show_convergence : bool = True
3636 show_transfer_values : bool = True
@@ -100,12 +100,15 @@ class SubplotDefinition:
100100
101101
102102class SubplotManager :
103- def __init__ (self , is_transient : bool , intf_spec : InterfaceSpec ):
103+ def __init__ (
104+ self , is_transient : bool , intf_spec : InterfaceSpec , is_csv_source : bool = False
105+ ):
104106 self ._is_time : bool = is_transient
105107 self ._intf_spec = intf_spec
108+ self ._is_csv_source = is_csv_source
106109
107110 self ._conv_subplot : SubplotDefinition | None = None
108- self ._transfer_subplots : dict [tuple [ str , int ] , SubplotDefinition ] = {}
111+ self ._transfer_subplots : dict [str , SubplotDefinition ] = {}
109112 self ._subplots : list [SubplotDefinition ] = []
110113 self ._data_index_map : dict [int , tuple [SubplotDefinition , int ]] = {}
111114 self ._allocate_subplots ()
@@ -116,7 +119,7 @@ def subplots(self) -> list[SubplotDefinition]:
116119
117120 def subplot_for_data_index (
118121 self , data_index : int
119- ) -> tuple [Optional [ SubplotDefinition ] , int ]:
122+ ) -> tuple [SubplotDefinition | None , int ]:
120123 """Return the subplot definition, and the line index within the
121124 subplot, corresponding to a given ``data_index``.
122125
@@ -166,10 +169,16 @@ def _allocate_subplots(self):
166169 keep_conv = False
167170 transfer_disambig : dict [str , int ] = {}
168171 for transfer in self ._intf_spec .transfers :
169- if transfer .display_name in transfer_disambig :
170- transfer_disambig [transfer .display_name ] += 1
171- else :
172- transfer_disambig [transfer .display_name ] = 0
172+ if self ._is_csv_source :
173+ # In the CSV case, we have to create a unique ID for each transfer
174+ # based on display name and an integer suffix, because we don't
175+ # have internal names in the data. 'transfer_disambig' keeps track of
176+ # how many times we've seen a given display name so far and we use that
177+ # to create the unique ID.
178+ if transfer .display_name in transfer_disambig :
179+ transfer_disambig [transfer .display_name ] += 1
180+ else :
181+ transfer_disambig [transfer .display_name ] = 0
173182 if transfer .show_convergence :
174183 keep_conv = True
175184 if transfer .show_transfer_values :
@@ -180,12 +189,12 @@ def _allocate_subplots(self):
180189 x_axis_label = "Time" if self ._is_time else "Iteration" ,
181190 y_axis_label = "" ,
182191 )
183- transfer_subplots [
184- (
185- transfer .display_name ,
186- transfer_disambig [ transfer . display_name ],
187- )
188- ] = transfer_value
192+ if self . _is_csv_source :
193+ disambig = transfer_disambig . get ( transfer . display_name , 0 )
194+ transfer_id = f" { transfer .display_name } : { disambig } "
195+ else :
196+ transfer_id = transfer . name
197+ transfer_subplots [ transfer_id ] = transfer_value
189198 subplots .append (transfer_value )
190199 if keep_conv :
191200 self ._conv_subplot = conv
@@ -194,9 +203,7 @@ def _allocate_subplots(self):
194203 self ._subplots = [subplot for subplot in subplots if subplot is not None ]
195204 for i , subplot in enumerate (self ._subplots ):
196205 subplot .index = i
197- self ._transfer_subplots : dict [tuple [str , int ], SubplotDefinition ] = (
198- transfer_subplots
199- )
206+ self ._transfer_subplots : dict [str , SubplotDefinition ] = transfer_subplots
200207
201208 def set_metadata (self , metadata : InterfaceInfo ):
202209 """Reconcile the metadata for a single interface with the pre-allocated
@@ -231,10 +238,7 @@ def set_metadata(self, metadata: InterfaceInfo):
231238 transfer_value_line_count : dict [tuple [str , int ], int ] = {}
232239
233240 for data_index , transfer in enumerate (metadata .transfer_info ):
234- transfer_key = (
235- transfer .transfer_display_name ,
236- transfer .disambiguation_index ,
237- )
241+ transfer_key = transfer .transfer_id
238242 if transfer .series_type == SeriesType .CONVERGENCE :
239243 if transfer .transfer_display_name not in active_transfers :
240244 # We don't want this transfer on the convergence plot
@@ -252,9 +256,7 @@ def set_metadata(self, metadata: InterfaceInfo):
252256 self ._conv_subplot .series_labels .append (transfer .transfer_display_name )
253257 iconv += 1
254258 else :
255- transfer_value_subplot = self ._transfer_subplots .get (
256- (transfer_key [0 ], transfer_key [1 ])
257- )
259+ transfer_value_subplot = self ._transfer_subplots .get (transfer_key )
258260 if transfer_value_subplot :
259261 value_type = (
260262 "Sum"
@@ -282,14 +284,16 @@ def set_metadata(self, metadata: InterfaceInfo):
282284
283285
284286class PlotDefinitionManager :
285- def __init__ (self , spec : PlotSpec ):
287+ def __init__ (self , spec : PlotSpec , is_csv_source : bool = False ):
286288 self ._subplot_mgrs : dict [str , SubplotManager ] = {}
287- self ._init (spec )
289+ self ._init (spec , is_csv_source )
288290
289- def _init (self , spec : PlotSpec ):
291+ def _init (self , spec : PlotSpec , is_csv_source : bool ):
290292 is_time = spec .plot_time
291293 for interface in spec .interfaces :
292- self ._subplot_mgrs [interface .name ] = SubplotManager (is_time , interface )
294+ self ._subplot_mgrs [interface .name ] = SubplotManager (
295+ is_time , interface , is_csv_source
296+ )
293297
294298 @property
295299 def interface_names (self ) -> list [str ]:
0 commit comments