OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [remove_cycle/] [file_io.py] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
import pickle
2
def read_from_pickle(pickle_file):
3
        try:
4
                with open(pickle_file,"rb") as f:
5
                        print("File loaded from: %s" % pickle_file)
6
                        output = pickle.load(f)
7
                return output
8
        except Exception as e:
9
                print("******Exception: %s" % e)
10
                return {}
11
 
12
def write_dict_to_file(data_dict,output_file):
13
        tuple_list = [(k,v) for k,v in data_dict.iteritems()]
14
        write_pairs_to_file(tuple_list,output_file)
15
 
16
def write_pairs_to_file(edges_list,edges_list_file):
17
        f = open(edges_list_file, 'w')
18
        for e in edges_list:
19
                u,v = e
20
                f.write(str(u) + " " + str(v) + " \n")
21
        f.close()
22
 
23
def write_edges_to_file(edges_list,edges_list_file):
24
        write_pairs_to_file(edges_list,edges_list_file)
25
 
26
def write_set_to_txt(output,output_file):
27
        with open(output_file,"w") as f:
28
                for k in output:
29
                        f.write(str(k) + "\n")
30
 
31
def write_dict_to_txt(output,output_file):
32
        with open(output_file,"w") as f:
33
                for k,v in output.iteritems():
34
                        f.write(str(k) + " " + str(v) + " \n")
35
 
36
 
37
def write_to_pickle(output_pickle,output_file):
38
        with open(output_file,"wb") as f:
39
                print("File saved to: %s" % output_file)
40
                pickle.dump(output_pickle,f)
41
 
42
def read_pairs_from_file(edges_list_file,first_type = int, second_type = int):
43
        try:
44
                edges = []
45
                f = open(edges_list_file, 'r')
46
                for line in f:
47
                        elements = line.split()
48
                        s = elements[0]
49
                        t = elements[1]
50
                        s = first_type(s)
51
                        t = second_type(t)
52
                        edges.append((s,t))
53
                return edges
54
        except Exception as e:
55
                return []
56
 
57
 
58
def read_edges_from_file(edges_list_file,first_type = int, second_type = int):
59
        return read_pairs_from_file(edges_list_file,first_type = int, second_type = int)
60
 
61
 
62
def reverse_edges(edges_list_file,output_file = None,first_type = int, second_type = int):
63
        edges = read_edges_from_file(edges_list_file,first_type = first_type,second_type = second_type)
64
        new_edges = [ (v,u) for (u,v) in edges]
65
        if output_file:
66
                write_edges_to_file(new_edges,output_file)
67
        return new_edges
68
#reverse_edges("data/sx_stackoverflow/sx_stackoverflow_a2q.edges",output_file = "data/sx_stackoverflow/sx_stackoverflow_q2a.edges")
69
 
70
 
71
def read_dict_from_file(file_name,key_type = int, value_type = int):
72
        input_file = open(file_name,"r")
73
        d = {}
74
        for line in input_file.readlines():
75
                k,v = line.split()
76
                if (key_type is not None) and (value_type is not None):
77
                        try:
78
                                k=key_type(k)
79
                                v=value_type(v)
80
                                d[k] = v
81
                        except Exception as e:
82
                                print e
83
        return d
84
 
85
 
86
def read_dict_from_csv(file_name,key_type = int,value_type = int,key_index = 0, value_index = 1):
87
        import pandas as pd
88
        df = pd.read_csv(file_name,sep = ",")
89
        d = {}
90
        for k,v in zip(df.iloc[:,key_index],df.iloc[:,value_index]):
91
                try:
92
                        k = key_type(k)
93
                        v = key_type(v)
94
                        d[k] = v
95
                except Exception as e:
96
                        pass
97
        return d
98
 
99
def read_dict_list_from_csv(file_name,key_type = int, value_type = int, key_index = 0, value_index = 1):
100
        import pandas as pd
101
        df = pd.read_csv(file_name,sep = ",")
102
        d = {}
103
        for k,v in zip(df.iloc[:,key_index],df.iloc[:,value_index]):
104
                k = key_type(k)
105
                v = key_type(v)
106
                if k in d:
107
                        d[k] = d[k] + [v]
108
                else:
109
                        d[k] = [v]
110
        return d
111
 
112
 
113
 
114
def switch_key_value_dict_list(d):
115
        new_d = {}
116
        for k,v in d.iteritems():
117
                if v in new_d:
118
                        new_d[v] += [k]
119
                else:
120
                        new_d[v] = [k]
121
        return new_d
122
 
123
def switch_key_value_dict_value(d):
124
        new_d = {}
125
        for k,v in d.iteritems():
126
                new_d[v] = k
127
        return new_d
128
 
129
 
130
 
131
def read_dict_pair_from_csv(file_name,key_type = int, value_type = int):
132
        import pandas as pd
133
        df = pd.read_csv(file_name,sep = ",")
134
        d = {}
135
        for k,v,days in zip(df.iloc[:,1],df.iloc[:,0],df.iloc[:,2]):
136
                k = key_type(k)
137
                v = value_type(v)
138
                if k in d:
139
                        d[k].append((v,days))
140
                else:
141
                        d[k] = [(v,days)]
142
        for k,v in d.iteritems():
143
                v = sorted(v,key = lambda x: x[1])
144
                d[k] =v
145
        return d
146
 
147
def read_dict_pair_from_csv_2(file_name,key_type = int, value_type = int):
148
        import pandas as pd
149
        df = pd.read_csv(file_name,delim_whitespace = True)
150
        d = {}
151
        for k,v,simi in zip(df.iloc[:,0],df.iloc[:,1],df.iloc[:,2]):
152
                k = key_type(k)
153
                v = value_type(v)
154
                if k in d:
155
                        d[k] = d[k] + [(v,simi)]
156
                else:
157
                        d[k] = [(v,simi)]
158
        for k,v in d.iteritems():
159
                v = sorted(v,key = lambda x: x[1],reverse = True)
160
                d[k] =v
161
        return d

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.