44from pathlib import Path
55from one .alf .spec import QC
66from datetime import date
7+ from typing import Tuple
78
89from neuropixel import trace_header
910import spikeglx
@@ -275,13 +276,8 @@ def compute_alignment_status(self):
275276
276277 return results
277278
278- def upload_channels (self , alignment_key , upload_alyx , upload_flatiron ):
279- """
280- Upload channels to alyx and flatiron based on the alignment specified by the alignment key
281- """
282-
283- feature = np .array (self .alignments [alignment_key ][0 ])
284- track = np .array (self .alignments [alignment_key ][1 ])
279+ def get_channels_mlapdv (self , alignment_key : str ) -> Tuple [np .ndarray , np .ndarray , np .array ]:
280+ feature , track = np .array (self .alignments [alignment_key ][:2 ])
285281
286282 try :
287283 meta_dset = self .one .list_datasets (self .insertion ['session' ], '*ap.meta' ,
@@ -291,25 +287,82 @@ def upload_channels(self, alignment_key, upload_alyx, upload_flatiron):
291287 collection = f'raw_ephys_data/{ self .insertion ["name" ]} ' ,
292288 download_only = True )
293289 geometry = spikeglx .read_geometry (meta_file )
294- chns = np .c_ [geometry ['x' ], geometry ['y' ]]
290+ channels_xy = np .c_ [geometry ['x' ], geometry ['y' ]]
295291 except Exception as err :
296292 self .log .warning (f"Could not compute channel locations from meta file, errored with message: { err } . "
297293 f"Will use default Neuropixel 1 channels" )
298294 geometry = trace_header (version = 1 )
299- chns = np .c_ [geometry ['x' ], geometry ['y' ]]
295+ channels_xy = np .c_ [geometry ['x' ], geometry ['y' ]]
300296
301- ephysalign = EphysAlignment (self .xyz_picks , chns [:, 1 ],
297+ ephysalign = EphysAlignment (self .xyz_picks , channels_xy [:, 1 ],
302298 track_prev = track ,
303299 feature_prev = feature ,
304300 brain_atlas = self .brain_atlas )
305301 channels_mlapdv = np .int32 (ephysalign .get_channel_locations (feature , track ) * 1e6 )
306302 channels_atlas_id = ephysalign .get_brain_locations (channels_mlapdv / 1e6 )['id' ]
303+ return channels_mlapdv , channels_atlas_id , channels_xy
304+
305+ def create_electrode_datasets (self , alignment_key : str ) -> list :
306+ """
307+ When an insertion alignment is resolved, we write datasets containing electrode locations
308+ :return:
309+ """
310+ channels_mlapdv , channels_atlas_id , channels_xy = self .get_channels_mlapdv (alignment_key )
311+ files_to_register = []
312+ alf_path = self .one .eid2path (self .insertion ['session' ]).joinpath ('alf' , self .insertion ["name" ])
313+ alf_path .mkdir (exist_ok = True , parents = True )
314+
315+ f_name = alf_path .joinpath ('electrodeSites.mlapdv.npy' )
316+ np .save (f_name , channels_mlapdv )
317+ files_to_register .append (f_name )
318+
319+ f_name = alf_path .joinpath ('electrodeSites.brainLocationIds_ccf_2017.npy' )
320+ np .save (f_name , channels_atlas_id )
321+ files_to_register .append (f_name )
322+
323+ f_name = alf_path .joinpath ('electrodeSites.localCoordinates.npy' )
324+ np .save (f_name , channels_xy )
325+ files_to_register .append (f_name )
326+ probe_collections = self .one .list_collections (self .insertion ['session' ], filename = 'channels*' ,
327+ collection = f'alf/{ self .insertion ["name" ]} *' )
328+ feature , track = np .array (self .alignments [alignment_key ][:2 ])
329+ for collection in probe_collections :
330+ chns = self .one .load_dataset (self .insertion ['session' ], 'channels.localCoordinates' , collection = collection )
331+
332+ ephysalign = EphysAlignment (self .xyz_picks , chns [:, 1 ],
333+ track_prev = track ,
334+ feature_prev = feature ,
335+ brain_atlas = self .brain_atlas )
336+ channels_mlapdv = np .int32 (ephysalign .get_channel_locations (feature , track ) * 1e6 )
337+ channels_atlas_id = ephysalign .get_brain_locations (channels_mlapdv / 1e6 )['id' ]
338+
339+ alf_path = self .one .eid2path (self .insertion ['session' ]).joinpath (collection )
340+ alf_path .mkdir (exist_ok = True , parents = True )
341+
342+ f_name = alf_path .joinpath ('channels.mlapdv.npy' )
343+ np .save (f_name , channels_mlapdv )
344+ files_to_register .append (f_name )
345+
346+ f_name = alf_path .joinpath ('channels.brainLocationIds_ccf_2017.npy' )
347+ np .save (f_name , channels_atlas_id )
348+ files_to_register .append (f_name )
349+
350+ self .log .info ("The following files have been saved:" )
351+ for f in files_to_register :
352+ self .log .info (f )
307353
354+ return files_to_register
355+
356+ def upload_channels (self , alignment_key , upload_alyx , upload_flatiron ):
357+ """
358+ Upload channels to alyx and flatiron based on the alignment specified by the alignment key
359+ """
360+ channels_mlapdv , channels_atlas_id , channels_xy = self .get_channels_mlapdv (alignment_key )
308361 # Need to change channels stored on alyx as well as the stored key is not the same as the latest key
309362 if upload_alyx :
310363 if alignment_key != self .align_keys_sorted [0 ]:
311364 histology .register_aligned_track (self .eid , channels_mlapdv / 1e6 ,
312- chn_coords = chns , one = self .one ,
365+ chn_coords = channels_xy , one = self .one ,
313366 overwrite = True , channels = self .channels_flag ,
314367 brain_atlas = self .brain_atlas )
315368
@@ -322,47 +375,9 @@ def upload_channels(self, alignment_key, upload_alyx, upload_flatiron):
322375
323376 files_to_register = []
324377 if upload_flatiron :
378+ files_to_register = self .create_electrode_datasets ()
379+ self .log .info (f"Registering { len (files_to_register )} datasets on FTP patcher" )
325380 ftp_patcher = FTPPatcher (one = self .one )
326-
327- alf_path = self .one .eid2path (self .insertion ['session' ]).joinpath ('alf' , self .insertion ["name" ])
328- alf_path .mkdir (exist_ok = True , parents = True )
329-
330- f_name = alf_path .joinpath ('electrodeSites.mlapdv.npy' )
331- np .save (f_name , channels_mlapdv )
332- files_to_register .append (f_name )
333-
334- f_name = alf_path .joinpath ('electrodeSites.brainLocationIds_ccf_2017.npy' )
335- np .save (f_name , channels_atlas_id )
336- files_to_register .append (f_name )
337-
338- f_name = alf_path .joinpath ('electrodeSites.localCoordinates.npy' )
339- np .save (f_name , chns )
340- files_to_register .append (f_name )
341-
342- probe_collections = self .one .list_collections (self .insertion ['session' ], filename = 'channels*' ,
343- collection = f'alf/{ self .insertion ["name" ]} *' )
344-
345- for collection in probe_collections :
346- chns = self .one .load_dataset (self .insertion ['session' ], 'channels.localCoordinates' , collection = collection )
347- ephysalign = EphysAlignment (self .xyz_picks , chns [:, 1 ],
348- track_prev = track ,
349- feature_prev = feature ,
350- brain_atlas = self .brain_atlas )
351- channels_mlapdv = np .int32 (ephysalign .get_channel_locations (feature , track ) * 1e6 )
352- channels_atlas_id = ephysalign .get_brain_locations (channels_mlapdv / 1e6 )['id' ]
353-
354- alf_path = self .one .eid2path (self .insertion ['session' ]).joinpath (collection )
355- alf_path .mkdir (exist_ok = True , parents = True )
356-
357- f_name = alf_path .joinpath ('channels.mlapdv.npy' )
358- np .save (f_name , channels_mlapdv )
359- files_to_register .append (f_name )
360-
361- f_name = alf_path .joinpath ('channels.brainLocationIds_ccf_2017.npy' )
362- np .save (f_name , channels_atlas_id )
363- files_to_register .append (f_name )
364-
365- self .log .info ("Writing datasets to FlatIron" )
366381 ftp_patcher .create_dataset (path = files_to_register ,
367382 created_by = self .one .alyx .user )
368383
0 commit comments