-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
153 lines (109 loc) · 5.17 KB
/
dataloader.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 21 01:57:31 2021
@author: rodrigosandon
"""
import numpy as np
import pandas as pd
import os
import csv
import pickle
class DataLoader:
def __init__(self, path, to_search):
self.regions = to_search
# print("New Dataloader")
self.path = path
self.data = self.convertToDataFrame(self.path)
#self.data, self.test = self.cross_validation_split(self.data, 0.75)
self.summaries = dict()
self.class_count = dict()
self.imp_features = self.all_features()
#self.load_obj("features_mean")
def all_features(self):
features = []
for i in range(0, 1024):
features.append((i, 0, 0))
return features
def load_obj(self, name):
with open('obj/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
def cross_validation_split(self, data, train_size):
train = data.sample(frac=train_size)
test = data.drop(train.index).sample(frac=1.0)
return train, test
def convertToDataFrame(self, path):
df = pd.DataFrame(columns=("Class", "Data"))
for subdir, dirs, files in os.walk(path):
for file in files:
reader = csv.reader(open(os.path.join(subdir, file), "rt"), delimiter=",")
x = list(reader)
result = np.array(x).astype("float")
row = pd.DataFrame({"Class" : os.path.basename(os.path.normpath(subdir)), "Data" : [result] })
df = df.append(row, ignore_index=True)
return df
def summarizeData(self, label, important_features):
mean_matrix = np.zeros([32, 32])
std_matrix = np.zeros([32, 32])
label_count = 0
for index, row in self.data.iterrows():
#if self.shouldAnalyze(row["Class"], label):
if row["Class"] == label:
label_count += 1
data = row["Data"]
for feature in important_features:
row = int(feature[0] / mean_matrix.shape[0])
col = feature[0] % std_matrix.shape[1]
mean_matrix[row][col] += (1 - feature[1]) * (1 - feature[2]) * data[row][col]
mean_matrix = mean_matrix / label_count
for index, row in self.data.iterrows():
#if self.shouldAnalyze(row["Class"], label):
if row["Class"] == label:
data = row["Data"]
for feature in important_features:
row = int(feature[0] / std_matrix.shape[0])
col = feature[0] % std_matrix.shape[1]
std_matrix[row][col] += (((1 - feature[1]) * (1 - feature[2]) * data[row][col]) - mean_matrix[row][col]) ** 2
std_matrix = std_matrix / (label_count - 1)
std_matrix = np.sqrt(std_matrix)
return mean_matrix, std_matrix, label_count
def generateClassSummaries(self):
for region in self.regions:
means, stds, class_count = self.summarizeData(region, self.imp_features)
self.summaries[region] = [means, stds]
self.class_count[region] = class_count
loader = DataLoader("Data\\allBrainRegionsraw", ["visal", "visam", "visl", "visp", "vispm", "visrl"])
loader.generateClassSummaries()
print(loader.summaries)
# import numpy as np
# import pandas as pd
# import os
# import csv
# import pickle
# class DataLoader:
# def __init__(self, path):
# self.path = path
# self.data = self.csvToDataframe(self.path)
# def load_object(self, name):
# with open('obj/' + name + '.pkl', 'rb') as f:
# return pickle.load(f)
# def csvToDataframe(self, path):
# df = pd.DataFrame(columns = ["Class", "Data"])
# # we cans still label the csv but we don't have to follow through with it
# for subdir, folders in os.walk(path): #path is the general input to the whole class
# for csv_files in folders:
# reader = csv.reader(open(os.path.join(subdir,csv_files), "rt"), delimiter = ",")
# x = list(reader) #convert what was read into an iterable list
# result = np.array(x).astype("float") #convert all numbers in list to floats
# row = pd.DataFrame({ #adding on the values directly to the columns
# "Class": os.path.basename(os.path.normpath(subdir)),
# "Data": [result] #result is the list of floats
# })
# df = df.append(row, ignore_index = True)
# #we are going to have a large dataframe with
# #all of the matrices represented as a flattened list
# return df
#dataloader = DataLoader("/Volumes/Passport/ResearchDataChen/Code/Data")
#dataloader.generateClassSummaries()
#print(dataloader.summaries)
#print(dataloader.class_count)