Example #1
0
    def generate_files_for_io_able_to_write(self):
        """
        
        """
        localdir = self.create_local_dir_if_not_exists()
        shortname = self.ioclass.__name__.lower().strip('io')
        higher = self.ioclass.supported_objects[0]
        
        if not(higher in self.ioclass.readable_objects and higher in self.ioclass.writeable_objects):
            return
        if not(higher == neo.Block or higher == neo.Segment):
            return
        # when io need external knowldge for writting or read such as sampling_rate (RawBinaryIO...)
        # the test is too much complex too design genericaly. 
        if higher in self.ioclass.read_params and len(self.ioclass.read_params[higher]) != 0 : return

        ob = generate_from_supported_objects(self.ioclass.supported_objects)
        
        if self.ioclass.mode == 'file':
            filename = localdir+'/Generated0_'+self.ioclass.__name__
            if len(self.ioclass.extensions)>=1:
                filename += '.'+self.ioclass.extensions[0]
            writer = self.ioclass(filename = filename)
            self.files_generated.append( filename )
        elif self.ioclass.mode == 'dir':
            dirname = localdir+'/Generated0_'+self.ioclass.__name__
            writer = self.ioclass(dirname = dirname)
            self.files_generated.append( dirname )
        else:
            return
        
        ob = generate_from_supported_objects(self.ioclass.supported_objects)
        if higher == neo.Block:
            writer.write_block(ob)
        elif higher == neo.Segment:
            writer.write_segment(ob)

        try: # for HDF5IO file should be closed before being opened again in test
            writer.close()
        except: pass
Example #2
0
    def test_write_then_read(self):
        """
        Test for IO that are able to write and read - here %s:
          1 - Generate a full schema with supported objects.
          2 - Write to a file
          3 - Read from the file
          4 - Check the hierachy
          5 - Check data
        
        Work only for IO for Block and Segment for the highest object (main cases).
        """ % self.ioclass.__name__
        localdir = self.create_local_dir_if_not_exists()
        shortname = self.ioclass.__name__.lower().strip('io')
        
        # Find the highest object that is supported by the IO
        # Test only if it is a Block or Segment, and if it can both read
        # and write this object.
        higher = self.ioclass.supported_objects[0]
        if not(higher in self.ioclass.readable_objects and 
            higher in self.ioclass.writeable_objects):
            return
        if not(higher == neo.Block or higher == neo.Segment):
            return
        
        if not self.read_and_write_is_bijective:
            return
        
        # When the highest-level object requires parameters, such as
        # sampling rate, this test is too complicated to write generically,
        # so return.
        if higher in self.ioclass.read_params and \
            len(self.ioclass.read_params[higher]) != 0 : return
        
        # Create writers and readers for a temporary location.
        if self.ioclass.mode == 'file':
            # Operates on files, so create a temporary filename with the
            # first filename extension in `extensions`, if any.
            filename = os.path.join(localdir, 
                'Generated0_%s' % self.ioclass.__name__)
            if len(self.ioclass.extensions) >= 1:
                filename += '.' + self.ioclass.extensions[0]
            # Create reader and writer for that file
            writer = self.ioclass(filename = filename)
            reader = self.ioclass(filename = filename)
        elif self.ioclass.mode == 'dir':
            dirname = localdir+'/Generated0_'+self.ioclass.__name__
            writer = self.ioclass(dirname = dirname)
            reader = self.ioclass(dirname = dirname)
        else:
            return
        
        # Get an object to write
        ob = generate_from_supported_objects(self.ioclass.supported_objects)
        
        # Write and read with the IO and ensure it is the same.
        if higher == neo.Block:
            writer.write_block(ob)
            if writer.__class__.name == 'Hdf5': # need to read what was saved
                ob2 = reader.read_block(ob.hdf5_path)
            else:
                ob2 = reader.read_block()
        elif higher == neo.Segment:
            writer.write_segment(ob)
            ob2 = reader.read_segment()

        assert_same_sub_schema(ob, ob2)
        assert_neo_object_is_compliant(ob2)

        try: # for HDF5IO file should be closed before being opened again in test
            reader.close()
            writer.close()
        except: pass