-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.py
More file actions
101 lines (89 loc) · 3.04 KB
/
util.py
File metadata and controls
101 lines (89 loc) · 3.04 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Module containing miscellaneous utility functions without a home anywhere else.
:Author: Kjetil Valle <kjetilva@stud.ntnu.no>"""
import time
import operator
import numpy as np
def fill_matrix_diagonal(matrix, value):
"""Implementation of fill_diagonal from numpy"""
matrix.flat[::matrix.shape[1]+1] = value
return matrix
def flatten(list):
"""Returen flattened version of *list*"""
return [item for sublist in list for item in sublist]
def _sorted_centralities(cents):
"""Sort a centralities dictionary"""
return sorted(cents.iteritems(), key = operator.itemgetter(1), reverse = True)
def load_words(path):
"""Return document as list of words from a file *path*"""
with open(path, 'r') as f:
words = f.read().split()
return words
def timed(f):
"""Decorator for measuring time used in functions"""
def wrapper(*args):
tic = time.time()
result = f(*args)
toc = time.time()
print ' -- time used in', f.__name__,':', (toc-tic)
return result
wrapper.__name__ = f.__name__
wrapper.__doc__ = f.__doc__
wrapper.__dict__.update(f.__dict__)
return wrapper
def interrogate(item):
"""Print useful information about item."""
if hasattr(item, '__name__'):
print "NAME: ", item.__name__
if hasattr(item, '__class__'):
print "CLASS: ", item.__class__.__name__
print "ID: ", id(item)
print "TYPE: ", type(item)
print "VALUE: ", repr(item)
print "CALLABLE:",
if callable(item):
print "Yes"
else:
print "No"
if hasattr(item, '__doc__'):
doc = getattr(item, '__doc__')
if not doc: return
doc = doc.strip() # Remove leading/trailing whitespace.
firstline = doc.split('\n')[0]
print "DOC: ", firstline
def to_latex_table(data, write_file=True, prt=True, start=None, end=None):
if write_file: f = open('output/experiment', 'a')
if start:
if prt:
print start
if write_file:
f.write(start+'\n')
for metric in data['forward'].keys():
line = ' '+metric+' & '+'%1.3f' % data['forward'][metric]+' & '+'%1.3f' % data['undirected'][metric]+' & '+'%1.3f' % data['backward'][metric]+' \\\\'
if prt:
print line
if write_file:
f.write(line+'\n')
if end:
if prt:
print end
if write_file:
f.write(end+'\n')
def test_unique(path='../data/reuters1000/'):
"""Check if there are any documents with multiple categories in dataset on given *path*."""
import os
d = {}
for root, cats, docs in os.walk(path):
category = root.split(os.sep)[-1]
if not category: continue
d[category] = set(docs)
for cat in d.keys():
for other in d.keys():
in_both = list(d[cat].intersection(other))
if in_both and cat!=other:
print cat+' and '+other
print in_both
print 'done'
if __name__=='__main__':
#~ test_unique('../data/reuters90')
pass