私はPythonで幼虫の段階にあり、C ++で卵前の段階にありますが、特に「Don'tRepeatYourself」の原則で最善を尽くそうとしています。
開くマルチチャネルRAWファイル形式があります。メインのASCIIヘッダーには、文字列と整数として表現できるフィールドがあります(常に空白で埋められた文字としてコード化されています)。2番目の部分はN個のヘッダーで、Nはメインヘッダーのフィールドであり、これらの各ヘッダーには、実際の16ビットマルチチャネルストリームの長さとサイズを参照する、より多くのテキストフィールドと数値フィールド(ASCIIとしてコード化)があります。ファイルの残りの部分を構成します。
これまでのところ、C++でこの動作するコードがあります。
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
struct Header {
string version;
string patinfo;
string recinfo;
string start_date;
string start_time;
int header_bytes;
string reserved;
int nrecs;
double rec_duration;
int nchannels;
};
struct Channel {
string label;
string transducertype;
string phys_dim;
int pmin;
int pmax;
int dmin;
int dmax;
string prefiltering;
int n_samples;
string reserved;
};
int main()
{
ifstream edf("/home/helton/Dropbox/01MIOTEC/06APNÉIA/Samples/Osas2002plusQRS.rec", ios::binary);
// prepare to read file header
Header header;
char buffer[80];
// reads header fields into the struct 'header'
edf.read(buffer, 8);
header.version = string(buffer, 8);
edf.read(buffer, 80);
header.patinfo = string(buffer, 80);
edf.read(buffer, 80);
header.recinfo = string(buffer, 80);
edf.read(buffer, 8);
header.start_date = string(buffer, 8);
edf.read(buffer, 8);
header.start_time = string(buffer, 8);
edf.read(buffer, 8);
stringstream(buffer) >> header.header_bytes;
edf.read(buffer, 44);
header.reserved = string(buffer, 44);
edf.read(buffer, 8);
stringstream(buffer) >> header.nrecs;
edf.read(buffer,8);
stringstream(buffer) >> header.rec_duration;
edf.read(buffer,4);
stringstream(buffer) >> header.nchannels;
/*
cout << "'" << header.version << "'" << endl;
cout << "'" << header.patinfo << "'" << endl;
cout << "'" << header.recinfo << "'" << endl;
cout << "'" << header.start_date << "'" << endl;
cout << "'" << header.start_time << "'" << endl;
cout << "'" << header.header_bytes << "'" << endl;
cout << "'" << header.reserved << "'" << endl;
cout << "'" << header.nrecs << "'" << endl;
cout << "'" << header.rec_duration << "'" << endl;
cout << "'" << header.nchannels << "'" << endl;
*/
// prepare to read channel headers
int ns = header.nchannels; // ns tells how much channels I have
char title[16]; // 16 is the specified length of the "label" field of each channel
for (int n = 0; n < ns; n++)
{
edf >> title;
cout << title << endl; // and this successfully echoes the label of each channel
}
return 0;
};
私がすでにしなければならないいくつかの発言:
- フォーマット仕様が非常にハードコーディングされているため、構造体を使用することにしました。
- 読み取るバイト数とタイプはかなり恣意的であるように思われたため、メインヘッダーフィールドを反復処理しませんでした。
- 各チャネルのラベルを取得できたので、実際には各チャネルのフィールドの構造体を作成します。これ自体は、おそらくマップに保存する必要があります。
私の(うまくいけば簡単な)質問は次のとおりです。
「この種のコードをより「Pythonic」(より抽象的で反復性の低いもの)にするために手抜きをすることを心配する必要がありますか、それともこれはC ++での動作方法ではありませんか?」
多くのPythonエバンジェリスト(私はそれが大好きなので、私自身もそうですが)は、その使いやすさなどを強調しています。ですから、私はしばらくの間、愚かなことをしているのか、それとも正しいことだけをしているのか疑問に思いますが、C ++の性質上、それほど「自動」ではありません。
読んでくれてありがとう
ヘルトン