def testDoubleSave(self): fpath = parentdir #u'C:/DATA/Vickie_spindles/' fileName = "/test.bdf" #'PETCHn1.bdf' print("Converting .sig file to .bdf file...") readerSIG = HarmonieReader(fpath + fileName[:-3] + 'SIG') readerSIG.saveAsEDF(fpath + fileName, "BDF", verbose=False) reader = EDFReader(fpath + fileName) reader.saveAs(fpath + "temp.bdf") reader.saveAs(fpath + "temp2.bdf") del reader with io.open(fpath + "temp.bdf", 'rb') as file2: with io.open(fpath + "temp2.bdf", 'rb') as file3: while 1: bytes3 = file3.read(100) bytes2 = file2.read(100) if not bytes3: break self.assertEqual(bytes3, bytes2) os.remove(fpath + "temp.bdf") os.remove(fpath + "temp2.bdf")
def testLoadDetectSaveLoad(self): fpath = parentdir #u'C:/DATA/Vickie_spindles/' fileName = "/test.bdf" #'PETCHn1.bdf' AmpEventName = 'SpindleAmp' RMSEventName = 'SpindleRMS' detectionStages = ["Sleep stage 2", "Sleep stage N2"] channels = ["C4-A1"] print("Converting .sig file to .bdf file...") readerSIG = HarmonieReader(fpath + fileName[:-3] + 'SIG') readerSIG.saveAsEDF(fpath + fileName, "BDF", verbose=False) reader = EDFReader(fpath + fileName) # Delete any present event with the name AmpEventName reader.events = [e for e in reader.events if e.name != AmpEventName] reader.events = [e for e in reader.events if e.name != RMSEventName] detector = SpindleDectectorAmp(reader, usePickled=False) detector.quantileThreshold = 0.925 detector.setDetectionStages(detectionStages) detector.computeRMS = False detector.computeFreq = False detector.computeSlopeFreq = False detector.detectSpindles(channelList=channels) detector.saveSpindle(reader, AmpEventName, "Spindle") copyfile(fpath + fileName, fpath + "temp.bdf") reader = EDFReader(fpath + fileName) # Delete any present event with the name AmpEventName reader.events = [e for e in reader.events if e.name != AmpEventName] reader.events = [e for e in reader.events if e.name != RMSEventName] detector = SpindleDectectorAmp(reader, usePickled=False) detector.quantileThreshold = 0.925 detector.setDetectionStages(detectionStages) detector.computeRMS = False detector.computeFreq = False detector.computeSlopeFreq = False detector.detectSpindles(channelList=channels) detector.saveSpindle(reader, AmpEventName, "Spindle") with io.open(fpath + fileName, 'rb') as file1: with io.open(fpath + "temp.bdf", 'rb') as file2: byte1 = file1.read(1) byte2 = file2.read(1) self.assertEqual(byte1, byte2) os.remove(fpath + "temp.bdf")
def testDoubleDetection(self): AmpEventName = 'SpindleAmp' RMSEventName = 'SpindleRMS' fpath = parentdir #u'C:/DATA/Vickie_spindles/' fileName = "/test.bdf" #'PETCHn1.bdf' detectionStages = ["Sleep stage 2", "Sleep stage N2"] channels = ["C4-A1"] print("Converting .sig file to .bdf file...") readerSIG = HarmonieReader(fpath + fileName[:-3] + 'SIG') readerSIG.saveAsEDF(fpath + fileName, "BDF", verbose=False) reader = EDFReader(fpath + fileName) # Delete any present event with the name AmpEventName #reader.events = [e for e in reader.events if e.name != AmpEventName] #reader.events = [e for e in reader.events if e.name != RMSEventName] detector = SpindleDectectorAmp(reader, usePickled=False) detector.quantileThreshold = 0.925 detector.setDetectionStages(detectionStages) detector.computeRMS = False detector.computeFreq = False detector.computeSlopeFreq = False detector.detectSpindles(channelList=channels) nbSpins1 = len(detector.detectedSpindles) detector.saveSpindle(reader, AmpEventName, "Spindle") detector.detectSpindles(channelList=channels) nbSpins2 = len(detector.detectedSpindles) detector.saveSpindle(reader, RMSEventName, "Spindle") try: self.assertEqual(nbSpins1, nbSpins2) except AssertionError: print(nbSpins1, nbSpins2) raise evaluatorAmp = DetectorEvaluator() evaluatorAmp.computeStatistics(detectionStages, RMSEventName, AmpEventName, reader, channelList=channels) for channel in channels: if channel in evaluatorAmp.TP: self.assertEqual(evaluatorAmp.sensitivity(channel), 1.0) self.assertEqual(evaluatorAmp.specificity(channel), 1.0) self.assertEqual(evaluatorAmp.PPV(channel), 1.0) self.assertEqual(evaluatorAmp.NPV(channel), 1.0)
def testSaving1(self): print("Converting .sig file to .bdf file...") readerSIG = HarmonieReader(parentdir + "/test.SIG") readerSIG.saveAsEDF(parentdir + "/test.BDF", "BDF", verbose=False) print("Reading the .bdf file...") reader = EDFReader(parentdir + "/test.BDF") detectionStages = ["Sleep stage N2", "Sleep stage 2"] channelList = ['F3-A1', 'F4-A1'] detector = SpindleDectectorAmp(reader, usePickled=False) detector.setDetectionStages(detectionStages) detector.detectSpindles(channelList=channelList) nbEvents = len(reader.events) + len(detector.detectedSpindles) detector.saveSpindle(reader, "SpindleRMS", "Spindle") reader2 = EDFReader(parentdir + "/test.BDF") self.failIf(nbEvents != len(reader2.events)) evaluator = DetectorEvaluator() evaluator.computeStatistics(detectionStages, "SpindleRMS", "SpindleRMS", reader, reader2, channelList) for channel in channelList: try: self.assertEqual(evaluator.sensitivity(channel), 1.0) self.assertEqual(evaluator.specificity(channel), 1.0) self.assertEqual(evaluator.PPV(channel), 1.0) self.assertEqual(evaluator.NPV(channel), 1.0) except AssertionError: print("channel=", channel, "; sensitivity=", evaluator.sensitivity(channel), end=' ') print("; specificity=", evaluator.specificity(channel), "; PPV=", end=' ') print(evaluator.PPV(channel), "; NPV=", evaluator.NPV(channel)) raise
def testConversion_readChannel(self): #loadFiles() print("Reading the .sig file...") readerSIG = HarmonieReader(os.path.join(parentdir, "test.SIG")) print("Saving a copy in the BDF format...") readerSIG.saveAsEDF(os.path.join(parentdir, "test.bdf"), "BDF", annotationFileName=os.path.join( parentdir, "test Annotations.bdf")) print("Reading the saved BDF file...") readerEDF = EDFReader(os.path.join(parentdir, "test.bdf"), annotationFileName=os.path.join( parentdir, "test Annotations.bdf")) channelListSig = readerSIG.getChannelLabels() channelListEdf = readerEDF.getChannelLabels() for channelSig, channelEdf in zip(channelListSig, channelListEdf): data1 = readerSIG.readChannel(channelSig, False) Y1 = data1.signal T1 = readerSIG.getChannelTime(channelSig) #fsSIG = data.samplingRate data2 = readerEDF.readChannel(channelEdf) Y2 = data2.signal T2 = readerEDF.getChannelTime(channelEdf) #fsEDF = data.samplingRate print(("1:", np.mean(abs(Y1)), np.mean(abs(Y2))))
def testConversion(self): #loadFiles() readerSIG = HarmonieReader(os.path.join(parentdir, "test.SIG")) print("Saving a copy in the BDF format...") readerSIG.saveAsEDF(os.path.join(parentdir, "test.bdf"), "BDF", annotationFileName=os.path.join( parentdir, "test Annotations.bdf")) print("Reading the saved BDF file...") readerEDF = EDFReader(os.path.join(parentdir, "test.BDF"), annotationFileName=os.path.join( parentdir, "test Annotations.bdf")) channelListSig = readerSIG.getChannelLabels() channelListEdf = readerEDF.getChannelLabels() # For each pages, verify that the signals in reader and readerEDF # are essentially the same by verifying their signal-to-noise ratio for noPage in range(readerSIG.getNbRecords()): # We test only complete pages since incomplete pages will be # different for the two formats, the EDF format padding zeros # at the end of the page to make complete records. if readerSIG.getInfoRecords(noPage + 1).isComplete: pageSIG = readerSIG.readRecord(channelListSig, noPage + 1) pageEDF = readerEDF.readRecord(channelListEdf, noPage + 1) for channelSig, channelEdf in zip(channelListSig, channelListEdf): Y1 = pageSIG.getSignal(channelSig) Y2 = pageEDF.getSignal(channelEdf) SNR = 10.0 * log10(trapz(Y1**2) / trapz((Y1 - Y2)**2)) print(("noPage=", noPage + 1, "(of ", readerSIG.getNbRecords(), ")", "channel=", channelSig, "SNR=", SNR)) if channelSig != "Mic-Mic" and SNR < 50.0: import pylab pylab.plot(list(range(len(Y1))), Y1) pylab.plot(list(range(len(Y2))), Y2) pylab.show() self.failIf(channelSig != "Mic-Mic" and SNR < 50.0) else: print(("Skiping incomplete page " + str(noPage + 1)))
def testDoubleDetection(self): fpath = parentdir #u'C:/DATA/Vickie_spindles/' fileName = "/test.bdf" #'PETCHn1.bdf' channels = ["C4-A1"] print("Converting .sig file to .bdf file...") readerSIG = HarmonieReader(fpath + fileName[:-3] + 'SIG') readerSIG.saveAsEDF(fpath + fileName, "BDF", verbose=False) reader = EDFReader(fpath + fileName) data = reader.readChannel(channels[0], usePickled=False) signal = data.signal print(signal[[1, 100, 200, 300, 400, 500]]) data = reader.readChannel(channels[0], usePickled=False) signal = data.signal print(signal[[1, 100, 200, 300, 400, 500]])