/* SleepLib ResMed Loader Implementation Author: Mark Watkins License: GPL */ #include #include #include #include #include #include #include #include #include #include "resmed_loader.h" #include "SleepLib/session.h" extern QProgressBar *qprogress; QHash RMS9ModelMap; QHash > resmed_codes; EDFSignal * EDFParser::lookupSignal(ChannelID ch) { QHash >::iterator ci; QHash::iterator jj; ci=resmed_codes.find(ch); if (ci==resmed_codes.end()) return NULL; for (int i=0;i::iterator s; for (s=edfsignals.begin();s!=edfsignals.end();s++) { if ((*s)->data) delete [] (*s)->data; delete *s; } if (buffer) delete [] buffer; } qint16 EDFParser::Read16() { unsigned char *buf=(unsigned char *)buffer; if (pos>=filesize) return 0; qint16 res=*(qint16 *)&buf[pos]; //qint16 res=(buf[pos] ^128)<< 8 | buf[pos+1] ^ 128; pos+=2; return res; } QString EDFParser::Read(int si) { QString str; if (pos>=filesize) return ""; for (int i=0;idata=NULL; edfsignals[i]->label=Read(16); lookup[edfsignals[i]->label]=signal; } for (int i=0;itransducer_type=Read(80); for (int i=0;iphysical_dimension=Read(8); for (int i=0;iphysical_minimum=Read(8).toDouble(&ok); for (int i=0;iphysical_maximum=Read(8).toDouble(&ok); for (int i=0;idigital_minimum=Read(8).toDouble(&ok); for (int i=0;iprefiltering=Read(80); for (int i=0;inr=Read(8).toLong(&ok); for (int i=0;ireserved=Read(32); // allocate the buffers for (int i=0;ilabel << endl; EDFSignal & sig=*edfsignals[i]; long recs=sig.nr * num_data_records; if (num_data_records<0) return false; sig.data=new qint16 [recs]; sig.pos=0; } for (int x=0;x ml=profile->GetMachines(MT_CPAP); bool found=false; QVector::iterator i; for (i=ml.begin(); i!=ml.end(); i++) { if (((*i)->GetClass()==resmed_class_name) && ((*i)->properties["Serial"]==serial)) { ResmedList[serial]=*i; //static_cast(*i); found=true; break; } } if (found) return *i; qDebug() << "Create ResMed Machine" << serial; Machine *m=new CPAP(profile,0); m->SetClass(resmed_class_name); ResmedList[serial]=m; profile->AddMachine(m); m->properties["Serial"]=serial; m->properties["Brand"]="ResMed"; QString a; a.sprintf("%i",resmed_data_version); m->properties["DataVersion"]=a; return m; } long event_cnt=0; int ResmedLoader::Open(QString & path,Profile *profile) { QString newpath; QString dirtag="DATALOG"; if (path.endsWith(QDir::separator()+dirtag)) { return 0; // id10t user.. //newpath=path; } else { newpath=path+QDir::separator()+dirtag; } QString idfile=path+QDir::separator()+"Identification.tgt"; QFile f(idfile); QHash idmap; if (f.open(QIODevice::ReadOnly)) { if (!f.isReadable()) return 0; while (!f.atEnd()) { QString line=f.readLine().trimmed(); QString key,value; if (!line.isEmpty()) { key=line.section(" ",0,0); value=line.section(" ",1); key=key.section("#",1); idmap[key]=value; } } } QString strfile=path+QDir::separator()+"STR.edf"; EDFParser stredf(strfile); if (!stredf.Parse()) { qDebug() << "Faulty file" << strfile; return 0; } qint64 duration=stredf.GetNumDataRecords()*stredf.GetDuration(); int days=duration/86400000L; QDateTime dt1=QDateTime::fromTime_t(stredf.startdate/1000L); QDateTime dt2=QDateTime::fromTime_t(stredf.enddate/1000L); //QDate dd1=dt1.date(); //QDate dd2=dt2.date(); for (int s=0;snr*stredf.GetNumDataRecords(); qDebug() << "STREDF:" << stredf.edfsignals[s]->label << recs; } QDir dir(newpath); if ((!dir.exists() || !dir.isReadable())) return 0; qDebug() << "ResmedLoader::Open newpath=" << newpath; dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); dir.setSorting(QDir::Name); QFileInfoList flist=dir.entryInfoList(); QMap > sessfiles; QString ext,rest,datestr,s,codestr; SessionID sessionid; QDateTime date; QString filename; int size=flist.size(); for (int i=0;isetValue((float(i+1)/float(size)*33.0)); QApplication::processEvents(); } Machine *m=NULL; QString fn; Session *sess; int cnt=0; size=sessfiles.size(); for (QMap >::iterator si=sessfiles.begin();si!=sessfiles.end();si++) { sessionid=si.key(); //qDebug() << "Parsing Session " << sessionid; bool done=false; bool first=true; sess=NULL; for (int i=0;i::iterator i=idmap.begin();i!=idmap.end();i++) { if (i.key()=="SRN") { if (edf.serialnumber!=i.value()) { qDebug() << "edf Serial number doesn't match Identification.tgt"; } if (edf.serialnumber!=stredf.serialnumber) { qDebug() << "edf Serial number doesn't match STR.edf!"; } } else if (i.key()=="PNA") { m->properties["Model"]=i.value(); } else if (i.key()=="PCD") { bool ok; int j=i.value().toInt(&ok); if (RMS9ModelMap.find(j)!=RMS9ModelMap.end()) { m->properties["SubModel"]=RMS9ModelMap[j]; } } else { m->properties[i.key()]=i.value(); } } if (m->SessionExists(sessionid)) { done=true; break; } sess=new Session(m,sessionid); first=false; } if (!done) { if (fn=="eve.edf") LoadEVE(sess,edf); else if (fn=="pld.edf") LoadPLD(sess,edf); else if (fn=="brp.edf") LoadBRP(sess,edf); else if (fn=="sad.edf") LoadSAD(sess,edf); //if (first) { //first=false; //} } } if (qprogress) qprogress->setValue(33.0+(float(++cnt)/float(size)*33.0)); QApplication::processEvents(); EDFSignal *sig; if (!sess) continue; if (!sess->first()) { delete sess; continue; } else { sess->SetChanged(true); qint64 dif=sess->first()-stredf.startdate; int dn=dif/86400000L; if (dndata[dn]; } else mode=0; // AutoSV machines don't have both fields sig=stredf.lookupSignal("EPR"); if (sig) { sess->settings["EPR"]=sig->data[dn]; } sig=stredf.lookupSignal("EPRLevel"); if (sig) { sess->settings["EPRSet"]=sig->data[dn]; } if (mode==0) { sess->settings[CPAP_Mode]=MODE_CPAP; sig=stredf.lookupSignal("Set Pressure"); if (sig) { EventDataType pressure=sig->data[dn]*sig->gain; sess->settings[CPAP_Pressure]=pressure; sess->setWavg(CPAP_Pressure,pressure); sess->setAvg(CPAP_Pressure,pressure); sess->set90p(CPAP_Pressure,pressure); sess->setMax(CPAP_Pressure,pressure); sess->setMin(CPAP_Pressure,pressure); } } else { if (mode>5) { sess->settings[CPAP_Mode]=MODE_BIPAP; } else { sess->settings[CPAP_Mode]=MODE_APAP; } sig=stredf.lookupSignal(CPAP_PressureMin); if (sig) sess->setMin(CPAP_Pressure,sig->data[dn]*sig->gain); sig=stredf.lookupSignal(CPAP_PressureMax); if (sig) sess->setMax(CPAP_Pressure,sig->data[dn]*sig->gain); } } m->AddSession(sess,profile); // Adding earlier than I really like here.. } } if (m) { m->Save(); } if (qprogress) qprogress->setValue(100); qDebug() << "Total Events " << event_cnt; return 1; } bool ResmedLoader::LoadEVE(Session *sess,EDFParser &edf) { // EVEnt records have useless duration record. QString t; long recs; double duration; char * data; char c; long pos; bool sign,ok; double d; double tt; ChannelID code; //Event *e; //totaldur=edf.GetNumDataRecords()*edf.GetDuration(); EventList *EL[4]={NULL}; sess->updateFirst(edf.startdate); //if (edf.enddate>edf.startdate) sess->set_last(edf.enddate); for (int s=0;snr*edf.GetNumDataRecords()*2; //qDebug() << edf.edfsignals[s]->label << " " << t; data=(char *)edf.edfsignals[s]->data; pos=0; tt=edf.startdate; sess->updateFirst(tt); duration=0; while (posAddEventList(CPAP_Obstructive,EVL_Event))) return false; } EL[0]->AddEvent(tt,duration); } else if (t=="hypopnea") { if (!EL[1]) { if (!(EL[1]=sess->AddEventList(CPAP_Hypopnea,EVL_Event))) return false; } EL[1]->AddEvent(tt,duration+10); // Only Hyponea's Need the extra duration??? } else if (t=="apnea") { if (!EL[2]) { if (!(EL[2]=sess->AddEventList(CPAP_Apnea,EVL_Event))) return false; } EL[2]->AddEvent(tt,duration); } else if (t=="central apnea") { code=CPAP_ClearAirway; if (!EL[3]) { if (!(EL[3]=sess->AddEventList(CPAP_ClearAirway,EVL_Event))) return false; } EL[3]->AddEvent(tt,duration); } else { if (t!="recording starts") { qDebug() << "Unobserved ResMed annotation field: " << t; } } } if (pos>=recs) { qDebug() << "Short EDF EVE file" << edf.filename; break; } // pos++; } while ((data[pos]==0) && pos=recs) break; } sess->updateLast(tt); // qDebug(data); } return true; } bool ResmedLoader::LoadBRP(Session *sess,EDFParser &edf) { QString t; sess->updateFirst(edf.startdate); qint64 duration=edf.GetNumDataRecords()*edf.GetDuration(); sess->updateLast(edf.startdate+duration); for (int s=0;snr*edf.GetNumDataRecords(); ChannelID code; if (edf.edfsignals[s]->label=="Flow") { es.gain*=60; es.physical_dimension="L/M"; code=CPAP_FlowRate; } else if (edf.edfsignals[s]->label.startsWith("Mask Pres")) { code=CPAP_MaskPressureHi; } else if (es.label.startsWith("Resp Event")) { code=CPAP_RespEvent; } else { qDebug() << "Unobserved ResMed BRP Signal " << edf.edfsignals[s]->label; continue; } double rate=double(duration)/double(recs); EventList *a=sess->AddEventList(code,EVL_Waveform,es.gain,es.offset,0,0,rate); a->setDimension(es.physical_dimension); a->AddWaveform(edf.startdate,es.data,recs,duration); sess->setMin(code,a->min()); sess->setMax(code,a->max()); //delete edf.edfsignals[s]->data; //edf.edfsignals[s]->data=NULL; // so it doesn't get deleted when edf gets trashed. } return true; } EventList * ResmedLoader::ToTimeDelta(Session *sess,EDFParser &edf, EDFSignal & es, ChannelID code, long recs, qint64 duration,EventDataType min,EventDataType max) { bool first=true; double rate=(duration/recs); // milliseconds per record double tt=edf.startdate; //sess->UpdateFirst(tt); EventDataType c,last; EventList *el=sess->AddEventList(code,EVL_Event,es.gain,es.offset,min,max); int startpos=0; if ((code==CPAP_Pressure) || (code==CPAP_IPAP) || (code==CPAP_EPAP)) { startpos=20; // Shave the first 20 seconds of pressure data tt+=rate*startpos; } for (int i=startpos;iAddEvent(tt,c); first=false; } else { if (last!=c) { el->AddEvent(tt,c); } } tt+=rate; last=c; } el->AddEvent(tt,c); sess->updateLast(tt); return el; } bool ResmedLoader::LoadSAD(Session *sess,EDFParser &edf) { QString t; sess->updateFirst(edf.startdate); qint64 duration=edf.GetNumDataRecords()*edf.GetDuration(); sess->updateLast(edf.startdate+duration); for (int s=0;snr*edf.GetNumDataRecords(); ChannelID code; if (edf.edfsignals[s]->label.startsWith("Puls")) { code=OXI_Pulse; } else if (edf.edfsignals[s]->label=="SpO2") { code=OXI_SPO2; } else { qDebug() << "Unobserved ResMed SAD Signal " << edf.edfsignals[s]->label; continue; } bool hasdata=false; for (int i=0;isetMin(code,a->min()); sess->setMax(code,a->max()); } } } return true; } bool ResmedLoader::LoadPLD(Session *sess,EDFParser &edf) { // Is it save to assume the order does not change here? enum PLDType { MaskPres=0, TherapyPres, ExpPress, Leak, RR, Vt, Mv, SnoreIndex, FFLIndex, U1, U2 }; qint64 duration=edf.GetNumDataRecords()*edf.GetDuration(); sess->updateFirst(edf.startdate); sess->updateLast(edf.startdate+duration); QString t; int emptycnt=0; EventList *a; double rate; long recs; ChannelID code; for (int s=0;ssettings[CPAP_Mode]=MODE_BIPAP; a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if ((es.label=="MV") || (es.label=="VM")){ code=CPAP_MinuteVent; a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if ((es.label=="RR") || (es.label=="AF") || (es.label=="FR")) { code=CPAP_RespRate; a=sess->AddEventList(code,EVL_Waveform,es.gain,es.offset,0,0,rate); a->AddWaveform(edf.startdate,es.data,recs,duration); } else if ((es.label=="Vt") || (es.label=="VC")) { code=CPAP_TidalVolume; es.physical_maximum=es.physical_minimum=0; es.gain*=1000.0; a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if ((es.label=="Leak") || (es.label.startsWith("Leck"))) { code=CPAP_Leak; es.gain*=60; es.physical_dimension="L/M"; a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label=="FFL Index") { code=CPAP_FLG; a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label.startsWith("Mask Pres")) { code=CPAP_MaskPressure; a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label.startsWith("Exp Press")) { code=CPAP_EPAP;//ExpiratoryPressure a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label.startsWith("I:E")) { code=CPAP_IE;//I:E ratio? a=sess->AddEventList(code,EVL_Waveform,es.gain,es.offset,0,0,rate); a->AddWaveform(edf.startdate,es.data,recs,duration); //a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label.startsWith("Ti")) { code=CPAP_Ti; a=sess->AddEventList(code,EVL_Waveform,es.gain,es.offset,0,0,rate); a->AddWaveform(edf.startdate,es.data,recs,duration); //a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label.startsWith("Te")) { code=CPAP_Te; a=sess->AddEventList(code,EVL_Waveform,es.gain,es.offset,0,0,rate); a->AddWaveform(edf.startdate,es.data,recs,duration); //a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label.startsWith("TgMV")) { code=CPAP_TgMV; a=sess->AddEventList(code,EVL_Waveform,es.gain,es.offset,0,0,rate); a->AddWaveform(edf.startdate,es.data,recs,duration); //a=ToTimeDelta(sess,edf,es, code,recs,duration,0,0); } else if (es.label=="") { if (emptycnt==0) { code=RMS9_E01; a=ToTimeDelta(sess,edf,es, code,recs,duration); } else if (emptycnt==1) { code=RMS9_E02; a=ToTimeDelta(sess,edf,es, code,recs,duration); } else { qDebug() << "Unobserved Empty Signal " << es.label; } emptycnt++; } else { qDebug() << "Unobserved ResMed PLD Signal " << es.label; a=NULL; } if (a) { sess->setMin(code,a->min()); sess->setMax(code,a->max()); a->setDimension(es.physical_dimension); } } return true; } void ResInitModelMap() { // Courtesy Troy Schultz RMS9ModelMap[36001]="ResMed S9 Escape"; RMS9ModelMap[36002]="ResMed S9 Escape Auto"; RMS9ModelMap[36003]="ResMed S9 Elite"; RMS9ModelMap[36004]="ResMed S9 VPAP S"; RMS9ModelMap[36005]="ResMed S9 AutoSet"; RMS9ModelMap[36006]="ResMed S9 VPAP Auto"; RMS9ModelMap[36007]="ResMed S9 VPAP Adapt"; RMS9ModelMap[36008]="ResMed S9 VPAP ST"; /* S8 Series RMS9ModelMap[33007]="ResMed S8 Escape"; RMS9ModelMap[33039]="ResMed S8 Elite II"; RMS9ModelMap[33051]="ResMed S8 Escape II"; RMS9ModelMap[33064]="ResMed S8 Escape II AutoSet"; RMS9ModelMap[33064]="ResMed S8 Escape II AutoSet"; RMS9ModelMap[33129]="ResMed S8 AutoSet II"; */ resmed_codes[CPAP_FlowRate].push_back("Flow"); resmed_codes[CPAP_MaskPressureHi].push_back("Mask Pres"); resmed_codes[CPAP_MaskPressureHi].push_back("Mask Pressure"); // vpap resmed_codes[CPAP_RespEvent].push_back("Resp Event"); resmed_codes[CPAP_MaskPressure].push_back("Mask Pres"); resmed_codes[CPAP_MaskPressure].push_back("Mask Pressure"); // vpap resmed_codes[CPAP_Pressure].push_back("Therapy Pres"); // not on vpap resmed_codes[CPAP_IPAP].push_back("Insp Pressure"); // on vpap resmed_codes[CPAP_EPAP].push_back("Exp Press"); resmed_codes[CPAP_EPAP].push_back("Exp Pressure"); // vpap resmed_codes[CPAP_Leak].push_back("Leak"); resmed_codes[CPAP_Leak].push_back("Leck."); resmed_codes[CPAP_RespRate].push_back("RR"); resmed_codes[CPAP_RespRate].push_back("AF"); resmed_codes[CPAP_RespRate].push_back("FR"); resmed_codes[CPAP_TidalVolume].push_back("Vt"); resmed_codes[CPAP_TidalVolume].push_back("VC"); resmed_codes[CPAP_MinuteVent].push_back("MV"); resmed_codes[CPAP_MinuteVent].push_back("VM"); resmed_codes[CPAP_IE].push_back("I:E"); // vpap resmed_codes[CPAP_Snore].push_back("Snore Index"); resmed_codes[CPAP_FLG].push_back("FFL Index"); resmed_codes[CPAP_RespEvent].push_back("RE"); resmed_codes[CPAP_Ti].push_back("Ti"); resmed_codes[CPAP_Te].push_back("Te"); // Sad (oximetry) resmed_codes[OXI_Pulse].push_back("Pulse"); resmed_codes[OXI_Pulse].push_back("Puls"); resmed_codes[OXI_SPO2].push_back("SpO2"); // Event annotations resmed_codes[CPAP_Obstructive].push_back("Obstructive apnea"); resmed_codes[CPAP_Hypopnea].push_back("Hypopnea"); resmed_codes[CPAP_Apnea].push_back("Apnea"); resmed_codes[CPAP_ClearAirway].push_back("Central apnea"); resmed_codes[CPAP_Mode].push_back("Mode"); resmed_codes[CPAP_Mode].push_back("Modus"); resmed_codes["Set Pressure"].push_back("Eingest. Druck"); resmed_codes["Set Pressure"].push_back("Set Pressure"); // Prescription resmed_codes["Set Pressure"].push_back("Pres. prescrite"); resmed_codes["EPR"].push_back("EPR"); resmed_codes["EPRLevel"].push_back("EPR Level"); resmed_codes["EPRLevel"].push_back("EPR-Stufe"); resmed_codes["EPRLevel"].push_back("Niveau EPR"); resmed_codes[CPAP_PressureMax].push_back("Max Pressure"); resmed_codes[CPAP_PressureMax].push_back("Max. Druck"); resmed_codes[CPAP_PressureMax].push_back("Pression max."); resmed_codes[CPAP_PressureMin].push_back("Min Pressure"); resmed_codes[CPAP_PressureMin].push_back("Min. Druck"); resmed_codes[CPAP_PressureMin].push_back("Pression min."); // STR.edf } bool resmed_initialized=false; void ResmedLoader::Register() { if (resmed_initialized) return; qDebug() << "Registering ResmedLoader"; RegisterLoader(new ResmedLoader()); ResInitModelMap(); resmed_initialized=true; }