Esempio n. 1
0
def read_edf_file(filename):
    f = EdfReader(filename)
    chan = 0
    data = f.readSignal(chan) / 1000.
    sample_freq = f.getSampleFrequency(chan)
    f._close()
    return data, sample_freq
Esempio n. 2
0
    def _load_file(self, input_file, return_missing=False):
        """
        Parameters
        ----------
        input_file : str
                    path of the file which to load
        return_missing : bool
                    if true, returns triple (x, y, missing)
                    where missing is true if input_file does 
                    contain one or more of the requested channels.
        Returns
        -------
        tuple (
            x : {'channelname': {'sampling_frequency': 100, 'data': np.array}, ... }
            y : {'channelname': {'sampling_frequency': 100, 'data': np.array}, ... }
        )
        """

        reader = EdfReader(input_file)
        channel_names = reader.getSignalLabels()
        channel_names_dict = {
            channel_names[i]: i
            for i in range(len(channel_names))
        }
        x_channels_to_process = set(channel_names).intersection(
            set(self.x_channels))
        y_channels_to_process = set(channel_names).intersection(
            set(self.y_channels))

        x_not_present = set(self.x_channels).difference(
            set(self.x_channels).intersection(set(channel_names)))
        y_not_present = set(self.y_channels).difference(
            set(self.y_channels).intersection(set(channel_names)))
        not_present = x_not_present.union(y_not_present)

        #return this with x and y to detect missing channels.
        missing = len(not_present) != 0
        #report missing channels
        if missing:
            not_present = [i for i in not_present]
            print(
                f'File \'{input_file}\' does not have channels: {not_present}')

        # Create data dictionaries
        x_channel_data_dict = {}
        for i in x_channels_to_process:
            x_channel_data_dict[i] = {
                "sampling_frequency":
                reader.getSampleFrequency(channel_names_dict[i]),
                "data":
                reader.readSignal(channel_names_dict[i])
            }

        y_channel_data_dict = {}
        for i in y_channels_to_process:
            y_channel_data_dict[i] = {
                "sampling_frequency":
                reader.getSampleFrequency(channel_names_dict[i]),
                "data":
                reader.readSignal(channel_names_dict[i])
            }

        if return_missing:
            return x_channel_data_dict, y_channel_data_dict, missing
        else:
            return x_channel_data_dict, y_channel_data_dict
class ChbEdfFile(object):
    """
    Edf reader using pyedflib
    """
    def __init__(self, filename, patient_id=None):
        self._filename = filename
        self._patient_id = patient_id
        self._file = EdfReader(filename)

    def get_filename(self):
        return self._filename

    def get_n_channels(self):
        """
        Number of channels
        """
        return len(self._file.getSampleFrequencies())

    def get_n_data_points(self):
        """
        Number of data points
        """
        if len(self._file.getNSamples()) < 1:
            raise ValueError("Number of channels is less than 1")
        return self._file.getNSamples()[0]

    def get_channel_names(self):
        """
        Names of channels
        """
        return self._file.getSignalLabels()

    def get_channel_scalings(self):
        """
        Channel scalings as an array
        :return:
        """
        out = np.zeros(self.get_n_channels())
        for i in range(self.get_n_channels()):
            out[i] = self._file.getPhysicalMaximum(
                i) - self._file.getPhysicalMinimum(i)
        return out

    def get_file_duration(self):
        """
        Returns the file duration in seconds
        """
        return self._file.getFileDuration()

    def get_sampling_rate(self):
        """
        Get the frequency
        """
        if len(self._file.getSampleFrequencies()) < 1:
            raise ValueError("Number of channels is less than 1")
        return self._file.getSampleFrequency(0)

    def get_channel_data(self, channel_id):
        """
        Get raw data for a single channel
        """
        if channel_id >= self.get_n_channels() or channel_id < 0:
            raise ValueError("Illegal channel id selected %d" % channel_id)
        return self._file.readSignal(channel_id)

    def get_data(self):
        """
        Get raw data for all channels
        """
        output_data = np.zeros(
            (self.get_n_data_points(), self.get_n_channels()))
        for i in range(self.get_n_channels()):
            output_data[:, i] = self._file.readSignal(i)
        return output_data

    def get_start_datetime(self):
        """
        Get the starting date and time
        """
        return self._file.getStartdatetime()

    def get_end_datetime(self):
        return self._file.getStartdatetime() + datetime.timedelta(
            seconds=self._file.getFileDuration())