class AeonUtility(SDKUtility):
	"""
	Including Utility Functions for Aeon Usage

	DataStorage Content:
		self.wp_filepath = wp_filepath
		self.wifi_filepath = wifi_filepath
		self.wifi_list = wifi_list
		self.wp_pos = wp_pos
		self.wp_ind = wp_ind
		self.wifi_matrix = wifi_matrix

	"""
	def __init__(self):
		SDKUtility.__init__(self)
		self.loader = LoadWifiData()
		self.map_ratio = 10

	def load_wifi(self, wp_path, wifi_path, ref_list=[]):
		if ref_list:
			wifi = self.loader.extract_with_ref(wp_path, wifi_path, ref_list)
		else:
			wifi = self.loader.extract(wp_path, wifi_path)
		return wifi

	def extract_aeon(self, wifi_path, ref_list):
		file_list = os.listdir(wifi_path)
		route = []
		for fn in file_list:
			if 'journal' in fn:
				continue
			data = self.extract_wifi(os.path.join(wifi_path,fn), ref_list)
			route += data
		return route

	def accuracy(self, res, test_pos, train_pos):
		RMSE = []
		for i,n in enumerate(res):
			pos_res = array(train_pos[n]);
			pos_tar = array(test_pos[i]);
			pos_diff = pos_res
			se = sqrt(((pos_tar - pos_diff) ** 2).sum()) / self.map_ratio
			RMSE.append(se)
		return RMSE

	def pos_weighted(self, w_arr, cls_pos):
		t_pos = zeros(array(cls_pos[0]).shape)
		for i, w in enumerate(w_arr):
			t_pos += array(cls_pos[i]) * w
		t_pos = t_pos / w_arr.sum()
		return t_pos

	def accuracy_proba(self, res, test_pos, train_pos):
		RMSE = []
		for n in range(res.shape[0]):
			w_arr = res[n,:]
			pos_res = self.pos_weighted(w_arr, train_pos)
			pos_tar = test_pos[n]
			se = sqrt(((pos_tar - pos_res) ** 2).sum()) / self.map_ratio
			RMSE.append(se)
		return RMSE

	def res_to_coord(self, res, train_pos, proba=False):
		# print res
		pos = []
		for x_res in res:
			if proba:
				pos_res = self.pos_weighted(x_res, train_pos)
			else:
				pos_res = array(train_pos[x_res])
			pos.append(pos_res)
		return pos
class AeonUtility(SDKUtility):
	"""
	Including Utility Functions for Aeon Usage

	DataStorage Content:
		self.wp_filepath = wp_filepath
		self.wifi_filepath = wifi_filepath
		self.wifi_list = wifi_list
		self.wp_pos = wp_pos
		self.wp_ind = wp_ind
		self.wifi_matrix = wifi_matrix

	"""
	def __init__(self):
		SDKUtility.__init__(self)
		self.loader = LoadWifiData()
		self.map_ratio = 1

	def load_wifi(self, wp_path, wifi_path, ref_list=[]):
		if ref_list:
			wifi = self.loader.extract_with_ref(wp_path, wifi_path, ref_list)
		else:
			wifi = self.loader.extract(wp_path, wifi_path)
		return wifi

	def extract_aeon(self, wifi_path, ref_list):
		file_list = os.listdir(wifi_path)
		route = []
		for fn in file_list:
			if 'journal' in fn:
				continue
			data = self.extract_wifi(os.path.join(wifi_path,fn), ref_list)
			route += data
		return route

	def accuracy(self, res, test_pos, train_pos):
		RMSE = []
		for i,n in enumerate(res):
			pos_res = array(train_pos[n]);
			pos_tar = array(test_pos[i]);
			pos_diff = pos_res
			se = sqrt(((pos_tar - pos_diff) ** 2).sum()) / self.map_ratio
			RMSE.append(se)
		return RMSE

	def pos_weighted(self, w_arr, cls_pos):
		t_pos = zeros(array(cls_pos[0]).shape)
		for i, w in enumerate(w_arr):
			t_pos += array(cls_pos[i]) * w
		t_pos = t_pos / w_arr.sum()
		return t_pos

	def accuracy_proba(self, res, test_pos, train_pos):
		RMSE = []
		for n in range(res.shape[0]):
			w_arr = res[n,:]
			pos_res = self.pos_weighted(w_arr, train_pos)
			pos_tar = test_pos[n]
			se = sqrt(((pos_tar - pos_res) ** 2).sum()) / self.map_ratio
			RMSE.append(se)
		return RMSE

	def res_to_coord(self, res, train_pos, proba=False):
		# print res
		pos = []
		for x_res in res:
			if proba:
				pos_res = self.pos_weighted(x_res, train_pos)
			else:
				pos_res = array(train_pos[x_res])
			pos.append(pos_res)
		return pos