Exemple #1
0
 def compute_indexes(self, length):
     # compute frequency of word,and left/right entropy
     self.tf = self.freq
     self.freq /= length
     self.length = length
     self.left = compute_entropy(self.left)
     self.right = compute_entropy(self.right)
    def run(self):

        # get username
        username = raw_input('Enter a username: '******'Pair your mindwave with your laptop. Just flip the switch on the side of the device. Press ENTER when it\'s paired.')

        # connect to the server
        with SocketIO('http://indra.coolworld.me') as socket:
            # send username,get server data 
            print('connecting...')
            socket.emit('hello', username, self.on_hello_response)
            socket.on('disconnect', self.on_disconnect)
            socket.on('clientlist', self.on_clientlist)
            socket.wait_for_callbacks(seconds=1)

            # logging.basicConfig(level=logging.DEBUG)
            for pkt in ThinkGearProtocol('/dev/tty.MindWaveMobile-DevA').get_packets():

                for d in pkt:

                    if isinstance(d, ThinkGearRawWaveData): 
         
                        self.raw_log.append(float(str(d))) #how/can/should we cast this data beforehand?

                        # compute and ship entropy when we have > 512 raw values
                        if len(self.raw_log) > self.entropy_window:
                            entropy = compute_entropy(self.raw_log)
                            #print entropy
                            #ship_biodata(socket,'entropy',entropy)
                            self.raw_log = []

                    if isinstance(d, ThinkGearEEGPowerData): 
                            # TODO: this cast is really embarrassing
                            reading = eval(str(d).replace('(','[').replace(')',']'))
                            print reading
    def run_mindwave(self, use_port="COM%s"%windows_port):

        # get username
        self.username = '******'
        '''self.username = raw_input('Enter a username: '******'Windows' in platform.system():
            port_number = raw_input('Windows OS detected. Please select proper COM part number (default is %s):'%windows_port)
            use_port= "COM%s"%port_number if len(port_number)>0 else "COM%s"%windows_port
        raw_input('Pair your mindwave with your laptop. Just flip the switch on the side of the device. Press ENTER when it\'s paired.')
        '''
        print '\nconnecting...',
        # set the timediff before doing anything
        self.set_timediff(
            requests.get('http://indra.coolworld.me').json()['time']
        )
        print('connected! starting to read mindwave data....') 
        if print_output:
            output_file_writer = open(output_file, 'w')
        try:
            for pkt in ThinkGearProtocol(use_port).get_packets():

                for d in pkt:

                    if isinstance(d, ThinkGearRawWaveData): 
                        self.raw_log.append(float(str(d))) #how/can/should we cast this data beforehand?

                        # compute and ship entropy when we have > 512 raw values
                        if len(self.raw_log) > self.entropy_window:
                            entropy = compute_entropy(self.raw_log)
                            #print entropy
                            #ship_biodata('entropy',entropy)
                            self.raw_log = []

                    if isinstance(d, ThinkGearEEGPowerData): 
                        # TODO: this cast is really embarrassing
                        reading = eval(str(d).replace('(','[').replace(')',']'))
                        reading2 = '"%s": %s'%(datetime.utcnow().replace(tzinfo=None), reading)
                        time_now = datetime.now()
                        entry = { 'time':str(time_now), 'values' : reading}
                        #print entry
                        self.data[:0]=[entry]
                        print reading2
                        if print_output:
                            output_file_writer.write(str(reading2)+'\n')
                            output_file_writer.flush()
                        self.ship_biodata('eeg_power',reading)
        except Exception as e:
            print 'ERROR', e.strerror
            self.alive = False
            self.started = False
            self.status = e.strerror
            pass
Exemple #4
0
    def test_entropy(self):
        frequency_maps = [
            get_wikipedia_input(),
            get_degenerated_input(),
            get_uniform_input(),
            get_real_world_input()
        ]

        for frequency_map in frequency_maps:
            huffman_entropy = compute_huffman_entropy(frequency_map)
            min_entropy = compute_entropy(frequency_map.values())
            self.assertTrue(huffman_entropy >= min_entropy)
            self.assertTrue(huffman_entropy <= min_entropy + 1)
def add_entropies(energydb,
                  aminos,
                  include_absolute_entropy=False,
                  include_boltzman_entropy=False,
                  include_absolute_boltzman_entropy=False):
    """For a given energy database calculated in energy.py, calculate
    the entropy for the site"""
    for k in energydb.keys():
        energies = list()
        for amino in aminos:
            if amino not in energydb[k]:
                logging.error('Missing amino energy from {}: {}'.format(
                    k, amino))
                continue
            energies.append(energydb[k][amino])
        boltzman_probs = compute_boltzmann(energies)
        # TODO: we could add the probabilities to the dict here...
        energydb[k]['shannon_entropy'] = compute_entropy(boltzman_probs)
        if include_absolute_entropy:
            abs_boltzman_probs = compute_boltzmann([abs(e) for e in energies])
            energydb[k]['absolute_shannon_entropy'] = compute_entropy(
                abs_boltzman_probs)
        if include_boltzman_entropy:
            boltzman_classic_probs = compute_boltzmann([
                (e * 4184 / ((1.38e-23 * 6.02e+23) * 310.15)) for e in energies
            ])
            energydb[k]['boltzman_shannon_entropy'] = compute_entropy(
                boltzman_classic_probs)
        if include_absolute_boltzman_entropy:
            abs_boltzman_classic_probs = compute_boltzmann([
                abs(e * 4184 / ((1.38e-23 * 6.02e+23) * 310.15))
                for e in energies
            ])
            energydb[k]['absolute_boltzman_shannon_entropy'] = compute_entropy(
                abs_boltzman_classic_probs)
    return energydb
Exemple #6
0
    def getCurrentEntropy(self):
        #print MW.raw_data[-10:]
        rawEEG = zip(*MW.raw_data[-self.entropy_window:])[2]

        try:
            if np.median(rawEEG) > 150:
                ''' If raw data exhibits large deviation, take the median of the entropy on the last 10 measures'''
                self.entropy.append(np.median(self.entropy[-10:]))
            else:
                #print rawEEG
                #print compute_entropy(rawEEG,1)
                self.entropy.append(compute_entropy(rawEEG,1))

            self.normalized_entropy.append(normalize(self.entropy[-self.deque:])[-1])
            self.currentEntropy = self.normalized_entropy[-1]
        except:
            pass
Exemple #7
0
    def readEEG(self):
        
        Median = []
        Std = []
        for pkt in ThinkGearProtocol(self.port).get_packets():
            #print pkt
            for d in pkt:
 
                if isinstance(d, ThinkGearPoorSignalData):
                    self.signal_quality += int(str(d))
                    self.poorSignal = int(str(d))
                    #print self.poorSignal
                    
                if isinstance(d, ThinkGearAttentionData):
                    self.attention_esense = int(str(d))
 
                if isinstance(d, ThinkGearMeditationData):
                    self.meditation_esense = int(str(d))
 
                if isinstance(d, ThinkGearEEGPowerData):
                    # this cast is both amazing and embarrassing
                    self.eeg_power = eval(str(d).replace('(','[').replace(')',']'))
 
                if isinstance(d, ThinkGearRawWaveData): 
                    # record a reading
                    # how/can/should we cast this data beforehand?
                    self.raw_log.append(float(str(d))) 
                    
                    if len(self.raw_log) == self.entropy_window:
                        
                        Median.append(np.median(self.raw_log))
                        Std.append(np.std(self.raw_log))              
                        
                        if Median[-1] > 150:
                            self.entropy.append(self.entropy[-1])
                        else:
                            self.entropy.append(compute_entropy(self.raw_log,1))
                        
                        self.normalized_entropy.append(normalize(self.entropy[-self.deque:])[-1])

                        self.currentEntropy = self.normalized_entropy[-1]
                        #print self.currentEntropy
                        self.raw_log = []

            if self.onText == False:
                break
    words = []
    with open(filename, "r") as file:
        for line in file:
            for c in line:
                if c not in frequency_map:
                    frequency_map[c] = 0
                frequency_map[c] += 1
                ccount += 1
            text += line
            words += [w.strip(' \n.,”“') for w in line.split()]

    print('= Stats =')
    print('Number of characters', ccount)
    print('Number of words', len(words))

    min_entropy = compute_entropy(frequency_map.values())
    print('Minimum entropy', min_entropy)

    huffman_entropy = compute_huffman_entropy(frequency_map)
    print('Huffman entropy', huffman_entropy)

    tree = build_tree(frequency_map)
    encoded_text = encode(text, tree)
    print('Length of raw text: {} bytes'.format(len(text)))
    print('Length of encoded text: {} bytes'.format(len(encoded_text)/8))
    print('Compression rate: {}'.format(len(text)*8/len(encoded_text)))

    print('= Word-based =')
    text_length = 0

    frequency_map = {}
Exemple #9
0
 def compute_indexes(self, length):
     #compute frequency of word,and left/right entropy
     # length是整个doc的长度
     self.freq /= length
     self.left = compute_entropy(self.left)
     self.right = compute_entropy(self.right)
Exemple #10
0
 def compute_entropies(self):
     self.left_entropy = compute_entropy(self.left)
     self.right_entropy = compute_entropy(self.right)