-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.py
55 lines (51 loc) · 1.68 KB
/
helper.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# @Author: ThomasO
import random
import numpy as np
import scipy.sparse as sparse
def read_csr_matrix(path, comments="#", delimiter=None, weight=False,
make_sym=False):
""" """
dtype = [('f0', int), ('f1', int)]
usecols = (0, 1)
if weight:
type.append(('weight', float))
usecols = (0, 1, 2)
arr_list = np.loadtxt(path, comments=comments, delimiter=delimiter,
dtype=dtype, usecols=usecols, unpack=True)
if make_sym:
tmp = arr_list[0]
arr_list[0] = np.concatenate((arr_list[0], arr_list[1]), axis=0)
arr_list[1] = np.concatenate((arr_list[1], tmp), axis=0)
del tmp
#
n = max(arr_list[0].max() + 1, arr_list[1].max() + 1)
if len(usecols) == 2:
arr_list.append(np.ones([arr_list[0].shape[0]]))
#
csr = sparse.csr_matrix((arr_list[2], (arr_list[0], arr_list[1])),
shape=(n, n), dtype=float)
return csr
def itershuffle(iterable, bufsize=1000):
"""
Shuffle an iterator. This works by holding `bufsize` items back
and yielding them sometime later. This is NOT 100% random,
proved or anything.
"""
iterable = iter(iterable)
buf = []
try:
while True:
for i in xrange(random.randint(1, bufsize - len(buf))):
buf.append(iterable.next())
random.shuffle(buf)
for i in xrange(random.randint(1, bufsize)):
if buf:
yield buf.pop()
else:
break
except StopIteration:
random.shuffle(buf)
while buf:
yield buf.pop()
raise StopIteration