Read EDF Bio-Electricity Datafile

I was studying about sleeping steps, using this cap-sleeping dataset: https://physionet.org/content/capslpdb/1.0.0/ as reference.

However I encountered some problems decoding the EEG sequencial datafile which ends with *.edf.

This datasets contains several kinds of files, including *.edf, *.m, *.xlsx, *.st, *.txt. The following explainations are about these types of files:


*.edf:

Multi-Channels raw-signal datafile, and the full name of edf was European Data Format, whose name should be widely known in the realm of signal-processing. For python users, we can use pyEDFlib to analyze this file type.

https://pyedflib.readthedocs.io/en/latest/

pyEDFlib is a python library to read/write EDF+/BDF+ files based on EDFlib.

EDF means [European Data Format](http://www.edfplus.info/) and was firstly published [1992](http://www.sciencedirect.com/science/article/pii/0013469492900097). In 2003, an improved version of the file protokoll named EDF+ has been published and can be found [here](http://www.sciencedirect.com/science/article/pii/0013469492900097).

The EDF/EDF+ format saves all data with 16 Bit. A version which saves all data with 24 Bit, was introduces by the compony [BioSemi](http://www.biosemi.com/faq/file_format.htm).

The definition of the EDF/EDF+/BDF/BDF+ format can be found under [edfplus.info](http://www.edfplus.info/).

This python toolbox is a fork of the [toolbox from Christopher Lee-Messer](https://bitbucket.org/cleemesser/python-edf/) and uses the [EDFlib](http://www.teuniz.net/edflib/) from Teunis van Beelen. The EDFlib is able to read and write EDF/EDF+/BDF/BDF+ files.

*.st: 

The scores for each recording in PhysioBank-compatible format.

*.txt:

The scores for each recording in REMlogic report format.

The .txt score files have the following fields:

  • Sleep stage (W=wake, S1-S4=sleep stages, R=REM, MT=body movements)
  • Body position (Left, Right, Prone, or Supine; not recorded in some subjects)
  • Time of day [hh:mm:ss]
  • Event (either a sleep stage (SLEEP-S0..S4, REM, MT), or a phase A of CAP)
  • Duration (in seconds)
  • Location (the signal(s) in which the event can be observed)

*.m:

  Matlab scripts. for reading the .txt score files easily. By launching ScoringReader.m, an array containing the scoring of the macrostructure, and three arrays containing the starting time of the CAP A phases, their duration and their subtype, are generated. The hypnogram is reported as 0 for wake, 1 to 4 for the sleep stages, according to R&K rules, and 5 for REM. The Matlab function CAP.m computes, starting from these variables, the CAP time and CAP rate according to Terzano’s rules.

gender-age.xlsx:

report of age and gender of the subjects. The first row is what is wrong with the subjects. Here is a list. These names also showed in txt files.


Since we get the definitions, we can use tools to step out for reaching the benchmark.

for python user, use pip to install the edf analyze library:

pip install pyedflib

Then run the test code to draw a fragment of one sequencial data file:

>>> import pyedflib
>>> import numpy as np
>>> file_name = r"\\192.168.1.103\ssd\cap-sleep-database-1.0.0\n1.edf"
>>> f = pyedflib.EdfReader(file_name)
>>> n = f.signals_in_file
>>> f
<pyedflib.edfreader.EdfReader object at 0x000001AEDC0488D8>
>>> n
21
>>> signal_labels = f.getSignalLabels()
>>> signal_labels
['ROC-LOC', 'LOC-ROC', 'F2-F4', 'F4-C4', 'C4-P4', 'P4-O2', 'F1-F3', 'F3-C3', 'C3-P3', 'P3-O1', 'C4-A1', 'EMG1-EMG2', 'ECG1-ECG2', 'TERMISTORE', 'TORACE', 'ADDOME', 'Dx1-DX2', 'SX1-SX2', 'Posizione', 'HR', 'SpO2']
>>> len(signal_labels)
21
>>> sample = f.getNSamples()
>>> len(sample)
21
>>> sample
array([17725440, 17725440, 17725440, 17725440, 17725440, 17725440,
       17725440, 17725440, 17725440, 17725440, 17725440,  8862720,
       17725440,  2215680,  2215680,  2215680,  8862720,  8862720,
        2215680,    34620,    34620])
>>> f.readSignal(1)
array([ -28.96970238,  -19.93384158,   -8.21164377, ..., -244.94509158,
       -242.44191392, -241.61769689])
>>> signal_1 = f.readSignal(1)
>>> len(signal_1)
17725440
>>> import matplotlib.pyplot as plt
>>> plt.scatter([_ for _ in range(10000)], signal_1[:10000])
<matplotlib.collections.PathCollection object at 0x000001AEEF75BC50>
>>> plt.show()

 

Now with the labels, you can run test on this dataset. 

<think>好的,我现在需要解决用户遇到的Python脚本未识别参数的问题,具体是train_Kfold_CV.py报错unrecognized arguments。根据用户提供的引用内容,特别是引用[2]和[3],可能的原因包括参数解析库argparse的使用问题、重复导入或环境依赖缺失。 首先,用户提到的错误信息类似于引用[2]中的情况,即参数未被识别。引用[2]中问题是因为在多个文件中导入了argparse,导致解析冲突。因此,我需要检查用户的代码中是否存在重复的argparse导入,或者在多个模块中定义参数解析器,导致主脚本的参数被覆盖或冲突。 其次,引用[3]提到缺少pytest-cov库导致参数错误,虽然用户的问题不是关于pytest,但可能类似的库依赖问题存在。需要确认用户是否在脚本中使用了需要额外库支持的参数选项,比如某些插件或扩展参数,但没有安装对应的包。 接下来,根据常见解决方法,首先应该检查argparse的参数定义是否正确。用户可能在脚本中定义了某些参数,但实际运行时传递的参数名称不匹配,或者参数类型错误。例如,是否在add_argument时指定了dest或type,但传递的参数不符合要求。 另外,用户可能在运行脚本时传递了未在argparse中声明的参数,比如拼写错误或多余的参数。比如,是否在命令行中写成了--loader而不是脚本中定义的--load,或者参数需要等号连接如--data=value而不是空格分隔。 还需要考虑脚本是否在正确的位置解析参数。如果参数是在其他模块中被提前解析,可能会导致主脚本无法识别后续参数。例如,在导入的模块中调用了parse_args(),导致主脚本的参数被截断。 此外,用户的环境问题也可能导致此错误,如引用[5]提到的虚拟环境问题。可能需要检查Python环境是否一致,是否存在多个版本的argparse或冲突的包。用户是否在正确的虚拟环境中运行脚本,依赖是否安装正确。 最后,建议用户检查代码结构,确保所有参数都在主脚本中正确声明,避免在导入的模块中重复定义解析器。如果存在多个文件使用argparse,需要确保参数正确聚合,或者使用父解析器来避免冲突。 可能的解决方案步骤包括:检查argparse的参数定义,确认参数名称和类型正确;检查命令行输入是否有拼写错误;确保没有在导入的模块中提前解析参数;检查环境依赖是否安装;尝试在另一个IDE或环境中运行以排除环境问题。</think>### 解决Python脚本未识别参数错误的步骤 #### 1. 检查参数定义与命令行输入是否匹配 确保在脚本中使用`argparse`定义的参数与命令行输入的参数**完全一致**(包括大小写、短横线数量): ```python import argparse parser = argparse.ArgumentParser() parser.add_argument("--data_path", type=str, required=True) # 定义必须参数 parser.add_argument("--epochs", type=int, default=10) # 定义可选参数 args = parser.parse_args() ``` 若命令行输入为: ```bash python train_Kfold_CV.py --data_path ./dataset --epochs 20 ``` 则参数匹配正确。若参数名拼写错误(如`--data-path`写成`--datapath`),会触发`unrecognized arguments`错误[^2]。 #### 2. 避免重复导入argparse导致的冲突 若在多个模块中导入并使用了`argparse`(如预处理脚本和主脚本),可能导致参数解析冲突: ```python # preprocess.py import argparse # 错误示例:在子模块中重复定义解析器 parser = argparse.ArgumentParser() parser.add_argument("--input_dir") # 该参数可能覆盖主脚本定义 args = parser.parse_args() ``` **正确做法**:仅在主脚本中定义参数解析器,子模块通过函数参数传递配置[^2]。 #### 3. 验证参数解析顺序 确保在脚本中**没有提前调用**`parser.parse_args()`,例如: ```python # 错误示例:在定义所有参数前解析 args = parser.parse_args() # 提前解析 parser.add_argument("--batch_size") # 此参数不会被识别 ``` 应始终在所有`add_argument()`之后调用`parse_args()`。 #### 4. 检查依赖库是否完整 若脚本使用需要第三方库支持的参数(如`--cov`需要`pytest-cov`),需安装对应库: ```bash pip install pytest-cov # 示例:解决--cov相关错误[^3] ``` #### 5. 验证虚拟环境配置 当使用PyCharm等IDE时,可能因虚拟环境配置错误导致参数未被识别: - 检查Python解释器路径是否正确 - 尝试在终端中直接运行脚本: ```bash /path/to/venv/bin/python train_Kfold_CV.py --参数 ``` - 或切换至VSCode测试环境兼容性[^5] #### 6. 使用`--help`验证参数列表 通过以下命令查看脚本支持的合法参数: ```bash python train_Kfold_CV.py --help ``` 输出应包含所有已定义的参数说明,若缺失则说明参数定义有误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值