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 testSaving2(self):

        channel = "C4-A1"
        fpath = parentdir  #u'C:/DATA/Vickie_spindles/'
        fileName = "/test.bdf"  #'PETCHn1.bdf'
        AmpEventName = 'SpindleAmp'
        detectionStages = ["Sleep stage 2", "Sleep stage N2"]

        #print "Converting .sig file to .bdf file..."
        #readerSIG =  HarmonieReader(fpath + 'PETCHn1.SIG')
        #readerSIG.saveAsEDF(fpath + fileName, "BDF", verbose=False)

        reader1 = EDFReader(fpath + fileName)
        # Delete any present event with the name AmpEventName
        reader1.events = [e for e in reader1.events if e.name != AmpEventName]
        detector = SpindleDectectorAmp(reader1, usePickled=False)
        detector.setDetectionStages(detectionStages)
        detector.detectSpindles(channelList=[channel])
        detector.saveSpindle(reader1, AmpEventName, "Spindle")

        reader2 = EDFReader(fpath + fileName)

        events1 = [
            e for e in reader1.events
            if e.name == AmpEventName and e.channel == channel
        ]
        events2 = [
            e for e in reader2.events
            if e.name == AmpEventName and e.channel == channel
        ]

        for event1, event2 in zip(events1, events2):
            self.assertEqual(event1, event2)