@@ -18,6 +18,8 @@ def __init__(self, controller=None, parent=None, backend="qt"):
1818 ViewBase .__init__ (self , controller = controller , parent = parent , backend = backend )
1919
2020 self .ccg , self .bins = self .controller .get_correlograms ()
21+ self .figure_cache = {}
22+ self .max_cache_size = 20
2123
2224
2325 def _on_settings_changed (self ):
@@ -64,24 +66,33 @@ def _qt_refresh(self):
6466
6567 for r in range (n ):
6668 for c in range (r , n ):
67-
68- i = unit_ids .index (visible_unit_ids [r ])
69- j = unit_ids .index (visible_unit_ids [c ])
70- count = ccg [i , j , :]
71-
72- plot = pg .PlotItem ()
73- if not self .settings ['display_axis' ]:
74- plot .hideAxis ('bottom' )
75- plot .hideAxis ('left' )
76-
77- if r == c :
78- unit_id = visible_unit_ids [r ]
79- color = colors [unit_id ]
69+ unit_id1 = visible_unit_ids [r ]
70+ unit_id2 = visible_unit_ids [c ]
71+ if (unit_id1 , unit_id2 ) in self .figure_cache :
72+ plot = self .figure_cache [(unit_id1 , unit_id2 )]
8073 else :
81- color = (120 ,120 ,120 ,120 )
82-
83- curve = pg .PlotCurveItem (bins , count , stepMode = 'center' , fillLevel = 0 , brush = color , pen = color )
84- plot .addItem (curve )
74+ # create new plot
75+ i = unit_ids .index (visible_unit_ids [r ])
76+ j = unit_ids .index (visible_unit_ids [c ])
77+ count = ccg [i , j , :]
78+
79+ plot = pg .PlotItem ()
80+ if not self .settings ['display_axis' ]:
81+ plot .hideAxis ('bottom' )
82+ plot .hideAxis ('left' )
83+
84+ if r == c :
85+ unit_id = visible_unit_ids [r ]
86+ color = colors [unit_id ]
87+ else :
88+ color = (120 , 120 , 120 , 120 )
89+
90+ curve = pg .PlotCurveItem (bins , count , stepMode = 'center' , fillLevel = 0 , brush = color , pen = color )
91+ plot .addItem (curve )
92+ # cache plot
93+ if len (self .figure_cache ) >= self .max_cache_size :
94+ self .figure_cache .pop (next (iter (self .figure_cache )))
95+ self .figure_cache [(unit_id1 , unit_id2 )] = plot
8596 self .grid .addItem (plot , row = r , col = c )
8697
8798 ## panel ##
@@ -102,18 +113,12 @@ def _panel_make_layout(self):
102113 self .empty_plot_pane ,
103114 sizing_mode = "stretch_both" ,
104115 )
105- self .is_warning_active = False
106-
107- self .plots = []
108116
109117 def _panel_refresh (self ):
110118 import panel as pn
111119 import bokeh .plotting as bpl
112120 from bokeh .layouts import gridplot
113- from .utils_panel import _bg_color , insert_warning , clear_warning
114-
115- # clear previous plot
116- self .plots = []
121+ from .utils_panel import _bg_color
117122
118123 if self .ccg is None :
119124 return
@@ -127,67 +132,75 @@ def _panel_refresh(self):
127132 }
128133 ccg = self .ccg
129134 bins = self .bins
130-
135+ figures = []
131136 first_fig = None
132137 for r in range (n ):
133138 row_plots = []
134139 for c in range (r , n ):
135- i = unit_ids .index (visible_unit_ids [r ])
136- j = unit_ids .index (visible_unit_ids [c ])
137- count = ccg [i , j , :]
140+ unit1 = visible_unit_ids [r ]
141+ unit2 = visible_unit_ids [c ]
138142
139- # Create Bokeh figure
140- if first_fig is not None :
141- extra_kwargs = dict (x_range = first_fig .x_range )
143+ if (unit1 , unit2 ) in self .figure_cache :
144+ fig = self .figure_cache [(unit1 , unit2 )]
142145 else :
143- extra_kwargs = dict ()
144- fig = bpl .figure (
145- width = 250 ,
146- height = 250 ,
147- tools = "pan,wheel_zoom,reset" ,
148- active_drag = "pan" ,
149- active_scroll = "wheel_zoom" ,
150- background_fill_color = _bg_color ,
151- border_fill_color = _bg_color ,
152- outline_line_color = "white" ,
153- ** extra_kwargs ,
154- )
155- fig .toolbar .logo = None
156-
157- # Get color from controller
158- if r == c :
159- unit_id = visible_unit_ids [r ]
160- color = colors [unit_id ]
161- fill_alpha = 0.7
162- else :
163- color = "lightgray"
164- fill_alpha = 0.4
165-
166- fig .quad (
167- top = count ,
168- bottom = 0 ,
169- left = bins [:- 1 ],
170- right = bins [1 :],
171- fill_color = color ,
172- line_color = color ,
173- alpha = fill_alpha ,
174- )
175- if first_fig is None :
176- first_fig = fig
177-
146+ # create new figure
147+ i = unit_ids .index (unit1 )
148+ j = unit_ids .index (unit2 )
149+ count = ccg [i , j , :]
150+
151+ # Create Bokeh figure
152+ if first_fig is not None :
153+ extra_kwargs = dict (x_range = first_fig .x_range )
154+ else :
155+ extra_kwargs = dict ()
156+ fig = bpl .figure (
157+ width = 250 ,
158+ height = 250 ,
159+ tools = "pan,wheel_zoom,reset" ,
160+ active_drag = "pan" ,
161+ active_scroll = "wheel_zoom" ,
162+ background_fill_color = _bg_color ,
163+ border_fill_color = _bg_color ,
164+ outline_line_color = "white" ,
165+ ** extra_kwargs ,
166+ )
167+ fig .toolbar .logo = None
168+
169+ # Get color from controller
170+ if r == c :
171+ unit_id = visible_unit_ids [r ]
172+ color = colors [unit_id ]
173+ fill_alpha = 0.7
174+ else :
175+ color = "lightgray"
176+ fill_alpha = 0.4
177+
178+ fig .quad (
179+ top = count ,
180+ bottom = 0 ,
181+ left = bins [:- 1 ],
182+ right = bins [1 :],
183+ fill_color = color ,
184+ line_color = color ,
185+ alpha = fill_alpha ,
186+ )
187+ if first_fig is None :
188+ first_fig = fig
189+ # Cache figure
190+ if len (self .figure_cache ) >= self .max_cache_size :
191+ self .figure_cache .pop (next (iter (self .figure_cache )))
192+ self .figure_cache [(unit1 , unit2 )] = fig
178193 row_plots .append (fig )
179194 # Fill row with None for proper spacing
180195 full_row = [None ] * r + row_plots + [None ] * (n - len (row_plots ))
181- self .plots .append (full_row )
182-
183- if len (self .plots ) > 0 :
184- grid = gridplot (self .plots , toolbar_location = "right" , sizing_mode = "stretch_both" )
185- self .layout [0 ] = pn .Column (
186- grid ,
187- styles = {'background-color' : f'{ _bg_color } ' }
188- )
189- else :
190- self .layout [0 ] = self .empty_plot_pane
196+ figures .append (full_row )
197+
198+ grid = gridplot (figures , toolbar_location = "right" , sizing_mode = "stretch_both" )
199+ grid .toolbar .logo = None
200+ self .layout [0 ] = pn .Column (
201+ grid ,
202+ styles = {'background-color' : f'{ _bg_color } ' }
203+ )
191204
192205
193206
0 commit comments