8787from neo .io .baseio import BaseIO
8888
8989# to import from core
90- from neo .core import Block , Segment , AnalogSignal , Event , SpikeTrain
90+ from neo .core import Block , Segment , AnalogSignal , Event , SpikeTrain , NeoReadWriteError
9191
9292
9393# --------------------------------------------------------
@@ -328,7 +328,8 @@ def __init__(
328328 super ().__init__ (layout , episode , number , x_unit , n_events , name )
329329 self .wf_samples = wf_samples
330330 self .wf_sampling_frequency = wf_sampling_frequency
331- assert wf_sampling_frequency , "bad sampling frequency"
331+ if not wf_sampling_frequency :
332+ raise ValueError ("bad sampling frequency" )
332333 self .wf_sampling_period = 1.0 / wf_sampling_frequency
333334 self .wf_units = [unit_x_wf , unit_y_wf ]
334335 self .t_start = t_start
@@ -532,7 +533,8 @@ def detect_protocol_from_name(self, path):
532533 match = re .search (pattern , path )
533534 if hasattr (match , "end" ):
534535 code = codes .get (path [match .end () - 1 ].lower (), None )
535- assert code != "m" , "multistim file detected"
536+ if code == "m" :
537+ raise ValueError ("multistim file detected" )
536538 return code
537539 elif "spt" in filename .lower ():
538540 return "spontaneousactivity"
@@ -897,7 +899,8 @@ def __init__(self, layout):
897899
898900 # extract episode properties
899901 n_channels = read_from_char (fileobj , "B" )
900- assert not ((n_channels < 1 ) or (n_channels > 16 )), "bad number of channels"
902+ if (n_channels < 1 ) or (n_channels > 16 ):
903+ raise ValueError (f"`n_channels` must be between 1 and 15. It is currently { n_channels } " )
901904 nbpt = read_from_char (fileobj , "h" )
902905 l_xu , x_unit = struct .unpack ("<B3s" , fileobj .read (4 ))
903906 if hasattr (x_unit , "decode" ):
@@ -1001,7 +1004,8 @@ def __init__(self, layout):
10011004
10021005 n_ep = read_from_char (fileobj , "h" )
10031006 tpData = read_from_char (fileobj , "h" )
1004- assert tpData in [3 , 2 , 1 , 0 ], "bad sample size"
1007+ if tpData not in [3 , 2 , 1 , 0 ]:
1008+ raise ValueError (f"`tpData` must be 3, 2, 1, or 0, but is { tpData } " )
10051009 no_analog_data = read_from_char (fileobj , "?" )
10061010
10071011 self .n_ep = n_ep
@@ -1784,8 +1788,10 @@ def get_signal(self, episode, channel):
17841788 Return the signal description relative
17851789 to the specified episode and channel.
17861790 """
1787- assert episode in range (1 , self .n_episodes + 1 )
1788- assert channel in range (1 , self .n_channels (episode ) + 1 )
1791+ if episode not in range (1 , self .n_episodes + 1 ):
1792+ raise ValueError (f"`episode must be in { range (1 , self .n_episodes + 1 )} , but is { episode } " )
1793+ if channel not in range (1 , self .n_channels (episode ) + 1 ):
1794+ raise ValueError (f"`channel` must be in { range (1 , self .n_channels (episode )+ 1 )} but is { channel } " )
17891795 t_start = 0
17901796 sampling_period = self .sampling_period (episode , channel )
17911797 t_stop = sampling_period * self .n_samples (episode , channel )
@@ -1863,7 +1869,8 @@ def reshape_bytes(self, databytes, reshape, datatypes, order="<"):
18631869 """
18641870 Reshape a numpy array containing a set of databytes.
18651871 """
1866- assert datatypes and len (datatypes ) == len (reshape ), "datatypes are not well defined"
1872+ if not datatypes and len (datatypes ) == len (reshape ):
1873+ raise NeoReadWriteError ("The `datatypes` are not well-defined" )
18671874
18681875 l_bytes = len (databytes )
18691876
@@ -2080,7 +2087,8 @@ def get_episode_blocks(self):
20802087
20812088 def set_info_block (self ):
20822089 i_blks = self .get_blocks_of_type ("USER INFO" )
2083- assert len (i_blks ) < 2 , "too many info blocks"
2090+ if len (i_blks ) >= 2 :
2091+ raise ValueError (f"There are too many info blocks: { i_blks } " )
20842092 if len (i_blks ):
20852093 self .info_block = i_blks [0 ]
20862094
@@ -2205,7 +2213,8 @@ def set_episode_blocks(self):
22052213
22062214 def set_info_block (self ):
22072215 i_blks = self .get_blocks_of_type ("USER INFO" )
2208- assert len (i_blks ) < 2 , "too many info blocks"
2216+ if len (i_blks ) >= 2 :
2217+ raise ValueError (f"There are too many info blocks: { i_blks } " )
22092218 if len (i_blks ):
22102219 self .info_block = i_blks [0 ]
22112220
@@ -2243,7 +2252,8 @@ def sample_type(self, ep, ch):
22432252
22442253 def sample_size (self , ep , ch ):
22452254 size = super ().sample_size (ep , ch )
2246- assert size == 2 , "sample size is always 2 bytes for DAC2/GS/2000 format"
2255+ if size != 2 :
2256+ raise ValueError (f"sample size is always 2 bytes for DAC2/GS/2000 format, but size is calculated as { size } " )
22472257 return size
22482258
22492259 def sampling_period (self , ep , ch ):
@@ -2297,10 +2307,12 @@ def file_duration(self):
22972307 return self .main_block .dX * self .n_samples
22982308
22992309 def get_tag (self , episode , tag_channel ):
2300- assert episode in range (1 , self .n_episodes + 1 )
2310+ if episode not in range (1 , self .n_episodes + 1 ):
2311+ raise ValueError (f"`episode` must be within { range (1 + self .n_episodes + 1 )} and is { episode } " )
23012312 # there are none or 2 tag channels
23022313 if self .tag_mode (episode ) == 1 :
2303- assert tag_channel in range (1 , 3 ), "DAC2/GS/2000 format support only 2 tag channels"
2314+ if tag_channel not in range (1 , 3 ):
2315+ raise ValueError ("DAC2/GS/2000 format support only 2 tag channels" )
23042316 block = self .episode_block (episode )
23052317 t_stop = self .main_block .n_samples * block .dX
23062318 return ElphyTag (self , episode , tag_channel , block .x_unit , 1.0 / block .dX , 0 , t_stop )
@@ -2384,7 +2396,8 @@ def set_episode_blocks(self):
23842396 def set_info_block (self ):
23852397 # in fact the file info are contained into a single sub-block with an USR identifier
23862398 i_blks = self .get_blocks_of_type ("B_Finfo" )
2387- assert len (i_blks ) < 2 , "too many info blocks"
2399+ if len (i_blks ) >= 2 :
2400+ raise ValueError (f"There are too many info blocks: { i_blks } " )
23882401 if len (i_blks ):
23892402 i_blk = i_blks [0 ]
23902403 sub_blocks = i_blk .sub_blocks
@@ -2658,7 +2671,8 @@ def get_tag(self, episode, tag_channel):
26582671 Return a :class:`ElphyTag` which is a
26592672 descriptor of the specified event channel.
26602673 """
2661- assert episode in range (1 , self .n_episodes + 1 )
2674+ if episode not in range (1 , self .n_episodes + 1 ):
2675+ raise ValueError (f"`episode` must be in { range (1 , self .n_episodes + 1 )} and is { episode } " )
26622676
26632677 # there are none, 2 or 16 tag
26642678 # channels depending on tag_mode
@@ -2669,11 +2683,11 @@ def get_tag(self, episode, tag_channel):
26692683
26702684 # verify the validity of the tag channel
26712685 if tag_mode == 1 :
2672- assert tag_channel in range (1 , 3 ), "Elphy format support only 2 tag channels for tag_mode == 1"
2673- elif tag_mode == 2 :
2674- assert tag_channel in range ( 1 , 17 ), "Elphy format support only 16 tag channels for tag_mode == 2"
2675- elif tag_mode == 3 :
2676- assert tag_channel in range ( 1 , 17 ), "Elphy format support only 16 tag channels for tag_mode == 3"
2686+ if tag_channel not in range (1 , 3 ):
2687+ raise ValueError ( "Elphy format support only 2 tag channels for tag_mode == 1" )
2688+ elif tag_mode == 2 or tag_mode == 3 :
2689+ if tag_channel not in range ( 1 , 17 ) :
2690+ raise ValueError ( "Elphy format support only 16 tag channels for tag_mode == 2 or tag_mode == 3" )
26772691
26782692 smp_period = block .ep_block .dX
26792693 smp_freq = 1.0 / smp_period
@@ -2707,12 +2721,15 @@ def get_event(self, ep, ch, marked_ks):
27072721 Return a :class:`ElphyEvent` which is a
27082722 descriptor of the specified event channel.
27092723 """
2710- assert ep in range (1 , self .n_episodes + 1 )
2711- assert ch in range (1 , self .n_channels + 1 )
2724+ if ep not in range (1 , self .n_episodes + 1 ):
2725+ raise ValueError (f"`ep` must be in { range (1 , self .n_episodes + 1 )} , but is { ep } " )
2726+ if ch not in range (1 , self .n_channels + 1 ):
2727+ raise ValueError (f"`ch` must be in { range (1 , self .n_channels + 1 )} , but is { ch } " )
27122728
27132729 # find the event channel number
27142730 evt_channel = np .where (marked_ks == - 1 )[0 ][0 ]
2715- assert evt_channel in range (1 , self .n_events (ep ) + 1 )
2731+ if evt_channel not in range (1 , self .n_events (ep ) + 1 ):
2732+ raise ValueError (f"`evt_channel` must be in { range (1 , self .n_events (ep ) + 1 )} , but is { evt_channel } " )
27162733
27172734 block = self .episode_block (ep )
27182735 ep_blocks = self .get_blocks_stored_in_episode (ep )
@@ -2792,8 +2809,10 @@ def get_spiketrain(self, episode, electrode_id):
27922809 Return a :class:`Spike` which is a
27932810 descriptor of the specified spike channel.
27942811 """
2795- assert episode in range (1 , self .n_episodes + 1 )
2796- assert electrode_id in range (1 , self .n_spiketrains (episode ) + 1 )
2812+ if episode not in range (1 , self .n_episodes + 1 ):
2813+ raise ValueError (f"`episode` must be in { range (1 , self .n_episodes + 1 )} , but is { episode } " )
2814+ if electrode_id not in range (1 , self .n_spiketrains (episode ) + 1 ):
2815+ raise ValueError (f"`eletrodee_id` must be in { range (1 , self .n_spiketrains (episode )+ 1 )} , but is { electrode_id } " )
27972816 # get some properties stored in the episode sub-block
27982817 block = self .episode_block (episode )
27992818 x_unit = block .ep_block .x_unit
@@ -3748,7 +3767,8 @@ def read_block(
37483767 lazy : bool
37493768 Postpone actual reading of the file.
37503769 """
3751- assert not lazy , "Do not support lazy"
3770+ if lazy :
3771+ raise NeoReadWriteError ("This IO does not support lazy reading" )
37523772
37533773 # basic
37543774 block = Block (name = None )
0 commit comments