-
Notifications
You must be signed in to change notification settings - Fork 57
/
brain_util.py
39 lines (33 loc) · 980 Bytes
/
brain_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import brain
import numpy as np
import random
import copy
import pickle
from collections import OrderedDict
# Save obj (could be Brain object, list of saved winners, etc) as file_name
def sim_save(file_name, obj):
with open(file_name,'wb') as f:
pickle.dump(obj, f)
def sim_load(file_name):
with open(file_name,'rb') as f:
return pickle.load(f)
# Compute item overlap between two lists viewed as sets.
def overlap(a,b,percentage=False):
o = len(set(a) & set(b))
if percentage:
return (float(o)/float(len(b)))
else:
return o
# Compute overlap of each list of winners in winners_list
# with respect to a specific winners set, namely winners_list[base]
def get_overlaps(winners_list,base,percentage=False):
overlaps = []
base_winners = winners_list[base]
k = len(base_winners)
for i in xrange(len(winners_list)):
o = overlap(winners_list[i],base_winners)
if percentage:
overlaps.append(float(o)/float(k))
else:
overlaps.append(o)
return overlaps