Exemplo n.º 1
0
 def __init__(self, infile, montage, *args, **kwargs):
     """Set montage and initialise EEGFile object"""
     if not montage in MONTAGES:
         msg = "Unrecognised montage: '{0}'".format(montage)
         raise UnrecognisedMontageError(msg)
     self.montage = MONTAGES[montage]
     EEGFile.__init__(self, infile, *args, **kwargs)
Exemplo n.º 2
0
    def __init__(self, chan_pars, rec_id="Generated Signals", duration=60, sample_rate=200, secsblock=5):
        """Generate and store header information.

        The EEGFile is deemed to have as many channels as there are items in
        chan_pars, which should be a list of dictionaries. The label for each
        channel is taken from chan_pars[n]['label'].
        """

        # Generate channel header from chan_pars item
        def chan_from_pars(cp):
            return Chan(label=cp["label"], dim="uV", calib=1, offset=0, nsblock=int(secsblock * sample_rate), finfo=cp)

        # Initialise
        chan = [chan_from_pars(cp) for cp in chan_pars]
        descr = [(c.label, "i2", c.nsblock) for c in chan]
        descr = BlockDescriptor(descr)

        # Store
        self._head = Head(
            tstart=datetime.datetime.today(),
            nblocks=int(duration // secsblock),
            secsblock=secsblock,
            nchan=len(chan_pars),
            chan=chan,
        )
        self._block_descriptor = descr
        self.nblocks = self._head.nblocks
        self.name = "<genfile>"

        # Pass self in to silence EEGFile
        EEGFile.__init__(self, self)
Exemplo n.º 3
0
    def __init__(self, eegfile, mode):
        """eegfile can be a path to a file or an open file object.
        mode should be 'r' or 'w'.
        
        1) On Windows if the file object is either sys.stdin or sys.stdout the
        mode will be changed to binary using msvcrt.setmode
        2) The file object must be a 'real' file object not a python
        'file-like' object.
        3) No other code should operate on the file.
        """
        # Accept only real files or file paths
        if not isinstance(eegfile, (file, basestring)):
            raise TypeError("eegfile must be either 'file' or 'string'")
        if not mode in ['r','w']:
            raise ValueError("Mode should be 'r' or 'w'")
        
        # Ensure eegfile is a binary file objects
        if isinstance(eegfile, basestring):
            _mode = mode
            if self._binary:
                _mode += 'b'
            eegfile = open(eegfile, _mode)
        # stdio is in text mode by default on windows
        elif eegfile in STDIO:
            if msvcrt and self._binary:
                msvcrt.setmode(eegfile.fileno(), os.O_BINARY)
        # Don't attempt to change mode if not STDIO
        elif self._binary and 'b' not in eegfile.mode:
            raise IOError("File not opened in binary mode")

        # Check if file object is a pipe
        st = os.fstat(eegfile.fileno())
        if st.st_mode & stat.S_IFIFO:
            seekable = False
            filesize = -1
        else:
            seekable = True
            filesize = st.st_size

        # Store relevant data
        self.__file = eegfile
        self.__fp = 0
        self.mode = mode
        self.seekable = seekable
        self.filesize = filesize

        # Store here just as in EEGFile
        EEGFile.__init__(self, eegfile)
Exemplo n.º 4
0
 def _get_blocks(self, n1, n2):
     """Wraps EEGFile._get_blocks to change the montage of the data"""
     block = EEGFile._get_blocks(self, n1, n2)
     new_block = self._block_descriptor.new(len(block))
     for label, elec1, elec2 in self.indices:
         new_block[label] = block[elec2] - block[elec1]
     return new_block
Exemplo n.º 5
0
 def _cache_blocks(self, n2):
     """Cache the first n blocks if not already done"""
     # Retrieve blocks
     n1 = self._last_block
     blockseg = EEGFile._get_blocks(self, n1, n2)
     block_ind = len(self._blocksegments)
     self._blocksegments.append((blockseg, n1))
     for n in range(n1, n2):
         self._blockrefs[n] = block_ind
     self._last_block = n2
Exemplo n.º 6
0
 def _get_blocks(self, n1, n2):
     block = EEGFile._get_blocks(self, n1, n2)
     block_new = block.copy()
     # Hopefully the filtering has previously ended on the block before
     initial_states = self._filter_states.get(n1, self._zero_states)
     final_states = []
     for name, f, zi in zip(block.keys(), self._filters, initial_states):
         x_old = block[name]
         x_new, zf = self._apply_filter(x_old, f, zi)
         block_new[name] = numpy.round(x_new)
         final_states.append(zf)
     self._filter_states[n2] = final_states
     # Return as normal
     return block_new
Exemplo n.º 7
0
 def __init__(self, *args, **kwargs):
     EEGFile.__init__(self, *args, **kwargs)
     self._blocksegments = []
     self._blockrefs = {}
     self._last_block = 0
Exemplo n.º 8
0
 def _get_blocks(self, n1, n2):
     """Wraps EEGFile._get_block to return a different block"""
     n1, n2 = [n + self._n1 for n in (n1, n2)]
     return EEGFile._get_blocks(self, n1, n2)
Exemplo n.º 9
0
 def __init__(self, eegfile, n1, n2):
     self._n1 = max(n1, 0)
     self._n2 = n2
     EEGFile.__init__(self, eegfile)