<s> """<STR_LIT>""" <EOL> import sys <EOL> import ctypes <EOL> import os <EOL> try : <EOL> file <EOL> except NameError : <EOL> import io <EOL> file = io . BufferedReader <EOL> import numpy as np <EOL> import quantities as pq <EOL> from neo . io . baseio import BaseIO <EOL> from neo . core import Segment , AnalogSignal , SpikeTrain , EventArray <EOL> ns_OK = <NUM_LIT:0> <EOL> ns_LIBERROR = - <NUM_LIT:1> <EOL> ns_TYPEERROR = - <NUM_LIT:2> <EOL> ns_FILEERROR = - <NUM_LIT:3> <EOL> ns_BADFILE = - <NUM_LIT:4> <EOL> ns_BADENTITY = - <NUM_LIT:5> <EOL> ns_BADSOURCE = - <NUM_LIT:6> <EOL> ns_BADINDEX = - <NUM_LIT:7> <EOL> class NeuroshareError ( Exception ) : <EOL> def __init__ ( self , lib , errno ) : <EOL> self . lib = lib <EOL> self . errno = errno <EOL> pszMsgBuffer = ctypes . create_string_buffer ( <NUM_LIT> ) <EOL> self . lib . ns_GetLastErrorMsg ( pszMsgBuffer , ctypes . c_uint32 ( <NUM_LIT> ) ) <EOL> errstr = '<STR_LIT>' . format ( errno , pszMsgBuffer . value ) <EOL> Exception . __init__ ( self , errstr ) <EOL> class DllWithError ( ) : <EOL> def __init__ ( self , lib ) : <EOL> self . lib = lib <EOL> def __getattr__ ( self , attr ) : <EOL> f = getattr ( self . lib , attr ) <EOL> return self . decorate_with_error ( f ) <EOL> def decorate_with_error ( self , f ) : <EOL> def func_with_error ( * args ) : <EOL> errno = f ( * args ) <EOL> if errno != ns_OK : <EOL> raise NeuroshareError ( self . lib , errno ) <EOL> return errno <EOL> return func_with_error <EOL> class NeurosharectypesIO ( BaseIO ) : <EOL> """<STR_LIT>""" <EOL> is_readable = True <EOL> is_writable = False <EOL> supported_objects = [ Segment , AnalogSignal , EventArray , SpikeTrain ] <EOL> readable_objects = [ Segment ] <EOL> writeable_objects = [ ] <EOL> has_header = False <EOL> is_streameable = False <EOL> read_params = { Segment : [ ] } <EOL> write_params = None <EOL> name = '<STR_LIT>' <EOL> extensions = [ ] <EOL> mode = '<STR_LIT:file>' <EOL> def __init__ ( self , filename = '<STR_LIT>' , dllname = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> BaseIO . __init__ ( self ) <EOL> self . dllname = dllname <EOL> self . filename = filename <EOL> def read_segment ( self , import_neuroshare_segment = True , <EOL> lazy = False , cascade = True ) : <EOL> """<STR_LIT>""" <EOL> seg = Segment ( file_origin = os . path . basename ( self . filename ) , ) <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> neuroshare = ctypes . windll . LoadLibrary ( self . dllname ) <EOL> elif sys . platform . startswith ( '<STR_LIT>' ) : <EOL> neuroshare = ctypes . cdll . LoadLibrary ( self . dllname ) <EOL> neuroshare = DllWithError ( neuroshare ) <EOL> info = ns_LIBRARYINFO ( ) <EOL> neuroshare . ns_GetLibraryInfo ( ctypes . byref ( info ) , ctypes . sizeof ( info ) ) <EOL> seg . annotate ( neuroshare_version = str ( info . dwAPIVersionMaj ) + '<STR_LIT:.>' + str ( info . dwAPIVersionMin ) ) <EOL> if not cascade : <EOL> return seg <EOL> hFile = ctypes . c_uint32 ( <NUM_LIT:0> ) <EOL> neuroshare . ns_OpenFile ( ctypes . c_char_p ( self . filename ) , ctypes . byref ( hFile ) ) <EOL> fileinfo = ns_FILEINFO ( ) <EOL> neuroshare . ns_GetFileInfo ( hFile , ctypes . byref ( fileinfo ) , ctypes . sizeof ( fileinfo ) ) <EOL> for dwEntityID in range ( fileinfo . dwEntityCount ) : <EOL> entityInfo = ns_ENTITYINFO ( ) <EOL> neuroshare . ns_GetEntityInfo ( hFile , dwEntityID , ctypes . byref ( entityInfo ) , ctypes . sizeof ( entityInfo ) ) <EOL> if entity_types [ entityInfo . dwEntityType ] == '<STR_LIT>' : <EOL> pEventInfo = ns_EVENTINFO ( ) <EOL> neuroshare . ns_GetEventInfo ( hFile , dwEntityID , ctypes . byref ( pEventInfo ) , ctypes . sizeof ( pEventInfo ) ) <EOL> if pEventInfo . dwEventType == <NUM_LIT:0> : <EOL> pData = ctypes . create_string_buffer ( pEventInfo . dwMaxDataLength ) <EOL> elif pEventInfo . dwEventType == <NUM_LIT:1> : <EOL> pData = ctypes . create_string_buffer ( pEventInfo . dwMaxDataLength ) <EOL> elif pEventInfo . dwEventType == <NUM_LIT:2> : <EOL> pData = ctypes . c_byte ( <NUM_LIT:0> ) <EOL> elif pEventInfo . dwEventType == <NUM_LIT:3> : <EOL> pData = ctypes . c_int16 ( <NUM_LIT:0> ) <EOL> elif pEventInfo . dwEventType == <NUM_LIT:4> : <EOL> pData = ctypes . c_int32 ( <NUM_LIT:0> ) <EOL> pdTimeStamp = ctypes . c_double ( <NUM_LIT:0.> ) <EOL> pdwDataRetSize = ctypes . c_uint32 ( <NUM_LIT:0> ) <EOL> ea = EventArray ( name = str ( entityInfo . szEntityLabel ) , ) <EOL> if not lazy : <EOL> times = [ ] <EOL> labels = [ ] <EOL> for dwIndex in range ( entityInfo . dwItemCount ) : <EOL> neuroshare . ns_GetEventData ( hFile , dwEntityID , dwIndex , <EOL> ctypes . byref ( pdTimeStamp ) , ctypes . byref ( pData ) , <EOL> ctypes . sizeof ( pData ) , ctypes . byref ( pdwDataRetSize ) ) <EOL> times . append ( pdTimeStamp . value ) <EOL> labels . append ( str ( pData . value ) ) <EOL> ea . times = times * pq . s <EOL> ea . labels = np . array ( labels , dtype = '<STR_LIT:S>' ) <EOL> else : <EOL> ea . lazy_shape = entityInfo . dwItemCount <EOL> seg . eventarrays . append ( ea ) <EOL> if entity_types [ entityInfo . dwEntityType ] == '<STR_LIT>' : <EOL> pAnalogInfo = ns_ANALOGINFO ( ) <EOL> neuroshare . ns_GetAnalogInfo ( hFile , dwEntityID , ctypes . byref ( pAnalogInfo ) , ctypes . sizeof ( pAnalogInfo ) ) <EOL> dwIndexCount = entityInfo . dwItemCount <EOL> if lazy : <EOL> signal = [ ] * pq . Quantity ( <NUM_LIT:1> , pAnalogInfo . szUnits ) <EOL> else : <EOL> pdwContCount = ctypes . c_uint32 ( <NUM_LIT:0> ) <EOL> pData = np . zeros ( ( entityInfo . dwItemCount , ) , dtype = '<STR_LIT>' ) <EOL> total_read = <NUM_LIT:0> <EOL> while total_read < entityInfo . dwItemCount : <EOL> dwStartIndex = ctypes . c_uint32 ( total_read ) <EOL> dwStopIndex = ctypes . c_uint32 ( entityInfo . dwItemCount - total_read ) <EOL> neuroshare . ns_GetAnalogData ( hFile , dwEntityID , dwStartIndex , <EOL> dwStopIndex , ctypes . byref ( pdwContCount ) , pData [ total_read : ] . ctypes . data_as ( ctypes . POINTER ( ctypes . c_double ) ) ) <EOL> total_read += pdwContCount . value <EOL> signal = pq . Quantity ( pData , units = pAnalogInfo . szUnits , copy = False ) <EOL> dwIndex = <NUM_LIT:0> <EOL> pdTime = ctypes . c_double ( <NUM_LIT:0> ) <EOL> neuroshare . ns_GetTimeByIndex ( hFile , dwEntityID , dwIndex , ctypes . byref ( pdTime ) ) <EOL> anaSig = AnalogSignal ( signal , <EOL> sampling_rate = pAnalogInfo . dSampleRate * pq . Hz , <EOL> t_start = pdTime . value * pq . s , <EOL> name = str ( entityInfo . szEntityLabel ) , <EOL> ) <EOL> anaSig . annotate ( probe_info = str ( pAnalogInfo . szProbeInfo ) ) <EOL> if lazy : <EOL> anaSig . lazy_shape = entityInfo . dwItemCount <EOL> seg . analogsignals . append ( anaSig ) <EOL> if entity_types [ entityInfo . dwEntityType ] == '<STR_LIT>' and import_neuroshare_segment : <EOL> pdwSegmentInfo = ns_SEGMENTINFO ( ) <EOL> if not str ( entityInfo . szEntityLabel ) . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> neuroshare . ns_GetSegmentInfo ( hFile , dwEntityID , <EOL> ctypes . byref ( pdwSegmentInfo ) , ctypes . sizeof ( pdwSegmentInfo ) ) <EOL> nsource = pdwSegmentInfo . dwSourceCount <EOL> pszMsgBuffer = ctypes . create_string_buffer ( "<STR_LIT:U+0020>" * <NUM_LIT> ) <EOL> neuroshare . ns_GetLastErrorMsg ( ctypes . byref ( pszMsgBuffer ) , <NUM_LIT> ) <EOL> for dwSourceID in range ( pdwSegmentInfo . dwSourceCount ) : <EOL> pSourceInfo = ns_SEGSOURCEINFO ( ) <EOL> neuroshare . ns_GetSegmentSourceInfo ( hFile , dwEntityID , dwSourceID , <EOL> ctypes . byref ( pSourceInfo ) , ctypes . sizeof ( pSourceInfo ) ) <EOL> if lazy : <EOL> sptr = SpikeTrain ( times , name = str ( entityInfo . szEntityLabel ) , t_stop = <NUM_LIT:0.> * pq . s ) <EOL> sptr . lazy_shape = entityInfo . dwItemCount <EOL> else : <EOL> pdTimeStamp = ctypes . c_double ( <NUM_LIT:0.> ) <EOL> dwDataBufferSize = pdwSegmentInfo . dwMaxSampleCount * pdwSegmentInfo . dwSourceCount <EOL> pData = np . zeros ( ( dwDataBufferSize ) , dtype = '<STR_LIT>' ) <EOL> pdwSampleCount = ctypes . c_uint32 ( <NUM_LIT:0> ) <EOL> pdwUnitID = ctypes . c_uint32 ( <NUM_LIT:0> ) <EOL> nsample = int ( dwDataBufferSize ) <EOL> times = np . empty ( ( entityInfo . dwItemCount ) , dtype = '<STR_LIT:f>' ) <EOL> waveforms = np . empty ( ( entityInfo . dwItemCount , nsource , nsample ) , dtype = '<STR_LIT:f>' ) <EOL> for dwIndex in range ( entityInfo . dwItemCount ) : <EOL> neuroshare . ns_GetSegmentData ( hFile , dwEntityID , dwIndex , <EOL> ctypes . byref ( pdTimeStamp ) , pData . ctypes . data_as ( ctypes . POINTER ( ctypes . c_double ) ) , <EOL> dwDataBufferSize * <NUM_LIT:8> , ctypes . byref ( pdwSampleCount ) , <EOL> ctypes . byref ( pdwUnitID ) ) <EOL> times [ dwIndex ] = pdTimeStamp . value <EOL> waveforms [ dwIndex , : , : ] = pData [ : nsample * nsource ] . reshape ( nsample , nsource ) . transpose ( ) <EOL> sptr = SpikeTrain ( times = pq . Quantity ( times , units = '<STR_LIT:s>' , copy = False ) , <EOL> t_stop = times . max ( ) , <EOL> waveforms = pq . Quantity ( waveforms , units = str ( pdwSegmentInfo . szUnits ) , copy = False ) , <EOL> left_sweep = nsample / <NUM_LIT> / float ( pdwSegmentInfo . dSampleRate ) * pq . s , <EOL> sampling_rate = float ( pdwSegmentInfo . dSampleRate ) * pq . Hz , <EOL> name = str ( entityInfo . szEntityLabel ) , <EOL> ) <EOL> seg . spiketrains . append ( sptr ) <EOL> if entity_types [ entityInfo . dwEntityType ] == '<STR_LIT>' : <EOL> pNeuralInfo = ns_NEURALINFO ( ) <EOL> neuroshare . ns_GetNeuralInfo ( hFile , dwEntityID , <EOL> ctypes . byref ( pNeuralInfo ) , ctypes . sizeof ( pNeuralInfo ) ) <EOL> if lazy : <EOL> times = [ ] * pq . s <EOL> t_stop = <NUM_LIT:0> * pq . s <EOL> else : <EOL> pData = np . zeros ( ( entityInfo . dwItemCount , ) , dtype = '<STR_LIT>' ) <EOL> dwStartIndex = <NUM_LIT:0> <EOL> dwIndexCount = entityInfo . dwItemCount <EOL> neuroshare . ns_GetNeuralData ( hFile , dwEntityID , dwStartIndex , <EOL> dwIndexCount , pData . ctypes . data_as ( ctypes . POINTER ( ctypes . c_double ) ) ) <EOL> times = pData * pq . s <EOL> t_stop = times . max ( ) <EOL> sptr = SpikeTrain ( times , t_stop = t_stop , <EOL> name = str ( entityInfo . szEntityLabel ) , ) <EOL> if lazy : <EOL> sptr . lazy_shape = entityInfo . dwItemCount <EOL> seg . spiketrains . append ( sptr ) <EOL> neuroshare . ns_CloseFile ( hFile ) <EOL> seg . create_many_to_one_relationship ( ) <EOL> return seg <EOL> class ns_FILEDESC ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:32> ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:8> ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:8> ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:16> ) , <EOL> ] <EOL> class ns_LIBRARYINFO ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:64> ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:64> ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ns_FILEDESC * <NUM_LIT:16> ) , <EOL> ] <EOL> class ns_FILEINFO ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:32> ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:64> ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT> ) , <EOL> ] <EOL> class ns_ENTITYINFO ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:32> ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ] <EOL> entity_types = { <NUM_LIT:0> : '<STR_LIT>' , <EOL> <NUM_LIT:1> : '<STR_LIT>' , <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:3> : '<STR_LIT>' , <EOL> <NUM_LIT:4> : '<STR_LIT>' , <EOL> } <EOL> class ns_EVENTINFO ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT> ) , <EOL> ] <EOL> class ns_ANALOGINFO ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT> ) , <EOL> ] <EOL> class ns_SEGMENTINFO ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:32> ) , <EOL> ] <EOL> class ns_SEGSOURCEINFO ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT:16> ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT> ) , <EOL> ] <EOL> class ns_NEURALINFO ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_char * <NUM_LIT> ) , <EOL> ] </s>
