# Path: util/avg_model.py
# def compute_avg_user(users):
#   """ Creates an average user model for mean imputation.
# 
#       Args:
#         users: the modeled dictionary of users.
# 
#       Returns:
#         A dictionary representing a user with average feature values.
#   """
#   avg_user = {}
#   for feature in users.itervalues().next():
#     avg_user[feature] = 0.0
#   missing = []
#   for feature in avg_user:
#     if feature in ['id', 'ratings', '_id', 'similars', 'trustees', 'trustors']:
#       continue
#     avg_user[feature] = 0.0
#     count = 0
#     for user in users.itervalues():
#       if not isnan(user[feature]):
#         try:
#           avg_user[feature] += user[feature]
#         except:
#           print feature
#           print avg_user
#           print user
#         count += 1
#     if count == 0:
#       missing.append(feature)
#     else:
#       avg_user[feature] /= float(count)
#   for feature in missing:
#     if feature == 'avg_help_giv_sim' or feature == 'avg_help_giv_tru_net':
#       avg_user[feature] = avg_user['avg_help_giv']
#     elif feature == 'avg_rating_sim' or feature == 'avg_rating_fir_net':
#       avg_user[feature] = avg_user['avg_rating']
#   avg_user['id'] = avg_user['_id'] = None
#   avg_user['ratings'] = {}
#   avg_user['similars'] = []
#   avg_max = []
#   avg_min = []
#   for user in users.itervalues():
#     if user['ratings']:
#       avg_max.append(max(user['ratings'].values()))
#       avg_min.append(min(user['ratings'].values()))
#   avg_user['avg_max'] = mean(avg_max)
#   avg_user['avg_min'] = mean(avg_min)
#   return avg_user
# 
# def compute_avg_model(model):
#   """ Creates an average user model for mean imputation.
# 
#       Args:
#         users: the modeled dictionary of users.
# 
#       Returns:
#         A dictionary representing a user with average feature values.
#   """
#   avg_entity = {}
#   for feature in model.itervalues().next():
#     avg_entity[feature] = 0.0
#   for feature in avg_entity:
#     count = 0.0
#     for user in model.itervalues():
#       if not isnan(user[feature]):
#         avg_entity[feature] += user[feature]
#         count += 1.0
#     avg_entity[feature] /= count
#   return avg_entity


from numpy import reshape, array, isnan
from util.avg_model import compute_avg_user, compute_avg_model
  model = []
  for feature in features:
    if isnan(users_conn[feature]):
      model.append(avg_conn[feature])
      print ' -- Imputed on conn'
    else:
      model.append(users_conn[feature])
  model = array(model)
  return model 


def map_features(votes, reviews, users, users_sim, users_conn, trusts):
  """ Maps all features related to each vote, in the same order.

      Args:
        votes: list of vote dictionaries.
        reviews: dictionary of review dictionaries indexed by review id.
        users: dictionary of user dictionaries indexed by user id.
        users_sim: dictionary of user similarity dictionaries indexed by a
          2-tuple of user ids.
        users_conn: dictionary of user connection dictionaries indexed by a
          2-tuple of user ids.
        trusts: networkx DiGraph with trust network.

      Returns:
        A dictionary of features indexed by related entity name (e.g.: voter)
      and containing a list with a feature array for each vote.
  """
  avg_user = compute_avg_user(users)
  avg_sim = compute_avg_model(users_sim)

# Path: util/avg_model.py
# def compute_avg_user(users):
#   """ Creates an average user model for mean imputation.
# 
#       Args:
#         users: the modeled dictionary of users.
# 
#       Returns:
#         A dictionary representing a user with average feature values.
#   """
#   avg_user = {}
#   for feature in users.itervalues().next():
#     avg_user[feature] = 0.0
#   missing = []
#   for feature in avg_user:
#     if feature in ['id', 'ratings', '_id', 'similars', 'trustees', 'trustors']:
#       continue
#     avg_user[feature] = 0.0
#     count = 0
#     for user in users.itervalues():
#       if not isnan(user[feature]):
#         try:
#           avg_user[feature] += user[feature]
#         except:
#           print feature
#           print avg_user
#           print user
#         count += 1
#     if count == 0:
#       missing.append(feature)
#     else:
#       avg_user[feature] /= float(count)
#   for feature in missing:
#     if feature == 'avg_help_giv_sim' or feature == 'avg_help_giv_tru_net':
#       avg_user[feature] = avg_user['avg_help_giv']
#     elif feature == 'avg_rating_sim' or feature == 'avg_rating_fir_net':
#       avg_user[feature] = avg_user['avg_rating']
#   avg_user['id'] = avg_user['_id'] = None
#   avg_user['ratings'] = {}
#   avg_user['similars'] = []
#   avg_max = []
#   avg_min = []
#   for user in users.itervalues():
#     if user['ratings']:
#       avg_max.append(max(user['ratings'].values()))
#       avg_min.append(min(user['ratings'].values()))
#   avg_user['avg_max'] = mean(avg_max)
#   avg_user['avg_min'] = mean(avg_min)
#   return avg_user
# 
# def compute_avg_model(model):
#   """ Creates an average user model for mean imputation.
# 
#       Args:
#         users: the modeled dictionary of users.
# 
#       Returns:
#         A dictionary representing a user with average feature values.
#   """
#   avg_entity = {}
#   for feature in model.itervalues().next():
#     avg_entity[feature] = 0.0
#   for feature in avg_entity:
#     count = 0.0
#     for user in model.itervalues():
#       if not isnan(user[feature]):
#         avg_entity[feature] += user[feature]
#         count += 1.0
#     avg_entity[feature] /= count
#   return avg_entity


# Path: algo/cap/map_features.py
from numpy import reshape, array, isnan
from util.avg_model import compute_avg_user, compute_avg_model

  model = []
  for feature in features:
    if isnan(users_conn[feature]):
      model.append(avg_conn[feature])
      print ' -- Imputed on conn'
    else:
      model.append(users_conn[feature])
  model = array(model)
  return model 


def map_features(votes, reviews, users, users_sim, users_conn, trusts):
  """ Maps all features related to each vote, in the same order.

      Args:
        votes: list of vote dictionaries.
        reviews: dictionary of review dictionaries indexed by review id.
        users: dictionary of user dictionaries indexed by user id.
        users_sim: dictionary of user similarity dictionaries indexed by a
          2-tuple of user ids.
        users_conn: dictionary of user connection dictionaries indexed by a
          2-tuple of user ids.
        trusts: networkx DiGraph with trust network.

      Returns:
        A dictionary of features indexed by related entity name (e.g.: voter)
      and containing a list with a feature array for each vote.
  """
  avg_user = compute_avg_user(users)
  avg_sim = compute_avg_model(users_sim)

  avg_conn = compute_avg_model(users_conn)