<commit_before># -*- coding: utf-8 -*-
"""
Class for reading data from WinWCP, a software tool written by
<NAME>.

WinWCP is free:
http://spider.science.strath.ac.uk/sipbs/software.htm

Author : sgarcia
Author: <NAME>
"""
from __future__ import unicode_literals, print_function, division, absolute_import

from .baserawio import (BaseRawIO, _signal_channel_dtype, _unit_channel_dtype,
                        _event_channel_dtype)

import numpy as np

import os
import sys
import struct


class WinWcpRawIO(BaseRawIO):
    extensions = ['wcp']
    rawmode = 'one-file'

    def __init__(self, filename=''):
        BaseRawIO.__init__(self)
        self.filename = filename

    def _source_name(self):
        return self.filename

    def _parse_header(self):
        SECTORSIZE = 512

        with open(self.filename, 'rb') as fid:

            headertext = fid.read(1024)
            headertext = headertext.decode('ascii')
            header = {}
            for line in headertext.split('\r\n'):
                if '=' not in line:
                    continue
                # print '#' , line , '#'
                key, val = line.split('=')
                if key in ['NC', 'NR', 'NBH', 'NBA', 'NBD', 'ADCMAX', 'NP', 'NZ', ]:
                    val = int(val)
                elif key in ['AD', 'DT', ]:
                    val = val.replace(',', '.')
                    val = float(val)
                header[key] = val

            nb_segment = header['NR']
            self._raw_signals = {}
            all_sampling_interval = []
            # loop for record number
            for seg_index in range(header['NR']):
                # print 'record ',i
                offset = 1024 + seg_index * (SECTORSIZE * header['NBD'] + 1024)

                # read analysis zone
                analysisHeader = HeaderReader(fid, AnalysisDescription).read_f(offset=offset)

                # read data
                NP = (SECTORSIZE * header['NBD']) // 2
                NP = NP - NP % header['NC']
                NP = NP // header['NC']

                self._raw_signals[seg_index] = np.memmap(self.filename, dtype='int16', mode='r',
                                                         shape=(NP, header['NC'],),
                                                         offset=offset + header['NBA'] * SECTORSIZE)

                all_sampling_interval.append(analysisHeader['SamplingInterval'])

        assert np.unique(all_sampling_interval).size == 1

        self._sampling_rate = 1. / all_sampling_interval[0]

        sig_channels = []
        for c in range(header['NC']):<commit_msg>Fix bug "error: [Errno 24] Too many open files". Open only one memmap for all segmengts.<commit_after># -*- coding: utf-8 -*-
"""
Class for reading data from WinWCP, a software tool written by
<NAME>.

WinWCP is free:
http://spider.science.strath.ac.uk/sipbs/software.htm

Author : sgarcia
Author: <NAME>
"""
from __future__ import unicode_literals, print_function, division, absolute_import

from .baserawio import (BaseRawIO, _signal_channel_dtype, _unit_channel_dtype,
                        _event_channel_dtype)

import numpy as np

import os
import sys
import struct


class WinWcpRawIO(BaseRawIO):
    extensions = ['wcp']
    rawmode = 'one-file'

    def __init__(self, filename=''):
        BaseRawIO.__init__(self)
        self.filename = filename

    def _source_name(self):
        return self.filename

    def _parse_header(self):
        SECTORSIZE = 512
        
        # only one memmap for all segment to avoid
        # "error: [Errno 24] Too many open files"
        self._memmap = np.memmap(self.filename, dtype='uint8', mode='r')

        with open(self.filename, 'rb') as fid:

            headertext = fid.read(1024)
            headertext = headertext.decode('ascii')
            header = {}
            for line in headertext.split('\r\n'):
                if '=' not in line:
                    continue
                # print '#' , line , '#'
                key, val = line.split('=')
                if key in ['NC', 'NR', 'NBH', 'NBA', 'NBD', 'ADCMAX', 'NP', 'NZ', ]:
                    val = int(val)
                elif key in ['AD', 'DT', ]:
                    val = val.replace(',', '.')
                    val = float(val)
                header[key] = val

            nb_segment = header['NR']
            self._raw_signals = {}
            all_sampling_interval = []
            # loop for record number
            for seg_index in range(header['NR']):
                offset = 1024 + seg_index * (SECTORSIZE * header['NBD'] + 1024)

                # read analysis zone
                analysisHeader = HeaderReader(fid, AnalysisDescription).read_f(offset=offset)

                # read data
                NP = (SECTORSIZE * header['NBD']) // 2
                NP = NP - NP % header['NC']
                NP = NP // header['NC']
                NC = header['NC']
                ind0 = offset + header['NBA'] * SECTORSIZE
                ind1 = ind0 + NP * NC * 2
                sigs = self._memmap[ind0:ind1].view('int16').reshape(NP, NC)
                self._raw_signals[seg_index] = sigs
                
                all_sampling_interval.append(analysisHeader['SamplingInterval'])

        assert np.unique(all_sampling_interval).size == 1

        self._sampling_rate = 1. / all_sampling_interval[0]

        sig_channels = []
        for c in range(header['NC']):
{'fraction_non_alphanumeric': 0.1202426916712631, 'fraction_numerical': 0.010112152969295827, 'mean_word_length': 1.7474747474747474, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3794464', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1516, 'n_words': 389}