Commit a80535db authored by Laura Cancedda's avatar Laura Cancedda
Browse files

Upload New File

parent 60a6179e
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 16 10:32:53 2016
@author: EBalzani
"""
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from std_grafical_tools import *
import scipy.stats as sts
from scipy.cluster.hierarchy import *
from copy import copy
import seaborn as sns; sns.set()
from parse_lab_book_RenamedLitters import *
save_folder = '.'
import pandas as pd
def subject_clust_visited(cluster_array, num_pups=10):
num_diff_clust = np.zeros(num_pups)
all_diff_clust = []
for frame in cluster_array:
for j in range(frame.group_number):
if not frame.groups[j] in all_diff_clust:
all_diff_clust += [frame.groups[j]]
for i in range(1,num_pups+1):
for gr in all_diff_clust:
if i in gr:
num_diff_clust[i-1] = num_diff_clust[i-1] + 1
return num_diff_clust
def time_in_a_cluster(cluster_array, num_pups=10,frame_rate=30):
time_in_cluster = np.zeros(num_pups)
for frame in cluster_array:
for j in range(frame.group_number):
for i in frame.groups[j]:
time_in_cluster[i-1] = time_in_cluster[i-1] + frame_rate
return time_in_cluster
def perc_treat_per_cluster(cluster_array,litter,dict_treat,pre_or_post, num_pups=10, frame_rate=30,
merge_blank_and_control=True):
group_dict = copy(dict_treat[litter])
if merge_blank_and_control:
blank = group_dict.pop('blank')
group_dict['control'] = np.hstack((group_dict['control'], blank))
perc_in_cluster = np.zeros(len(cluster_array),dtype={'names':('Measure','Litter','PRE/POST','Num DREADDs','Num Controls','Frame','Percentage'),
'formats':('S100','S100','S4',int,int,int,float)})
i_frame = 0
for frame in cluster_array:
vector_perc = np.zeros(frame.group_number)
for j in range(frame.group_number):
gr_size = len(frame.groups[j])
treat = 0.0
for i in frame.groups[j]:
if i in group_dict['DREADDs']:
treat += 1.0
vector_perc[j] = treat / gr_size
perc_in_cluster['Percentage'][i_frame] = np.mean(vector_perc)
perc_in_cluster['Frame'][i_frame] = i_frame
i_frame += 1
perc_in_cluster['Litter'] = litter
perc_in_cluster['Measure'] = 'Percentage of DREADDs mice per cluster'
perc_in_cluster['PRE/POST'] = pre_or_post
perc_in_cluster['Num DREADDs'] = len(group_dict['DREADDs'])
perc_in_cluster['Num Controls'] = num_pups - len(group_dict['DREADDs'])
return perc_in_cluster
def matrix_time_in_cluster(cluster_array,num_pups):
matrix_time = np.zeros((num_pups,num_pups))
for i in range(10):
for j in range(0,i):
for frame in cluster_array:
for group in frame.groups:
if i+1 in group and j+1 in group:
matrix_time[i,j] = matrix_time[i,j] + 1
matrix_time[j,i] = matrix_time[i,j]
for i in range(10):
matrix_time[i,i] = len(cluster_array)
return matrix_time
def transform_to_pdist_type(matrix_time):
vector = np.nan*np.zeros(matrix_time.shape[0]*(matrix_time.shape[0]-1)//2,dtype=float)
iv = 0
for i in range(10):
for j in range(i+1,10):
vector[iv] = matrix_time[i,j]
iv+=1
return vector
###############################################################################
# formula per tradurre: se matrix_time e' nxn e j>i
# matrix_time[i,j] = vector[i*n+j-(i+1)*(i+2)/2]
###############################################################################
lambda_clust_switch = lambda ca : np.sum(ca[1:]/ca[:-1])
dict_sheet = parsing_excel_labbook('score.xlsx')
list_allLitters = ['LITTER 1']
all_res = np.zeros(len(list_allLitters)*10*2,dtype={'names':('Litter','PRE/POST','Group','Subject','Value'),
'formats':('U50','U4','U30',int,float)})
num_pups = 10
kk=0
for litter in list_allLitters:
litter_data = dict_sheet[litter]
for key in litter_data.dtype.names:
if 'PRE' in key:
pre_key = key
elif 'POST' in key:
post_key = key
matrix_pre = matrix_time_in_cluster(litter_data[pre_key],10)
all_res[kk*10:(kk+1)*10]['Value'] = np.nanmean(matrix_pre - np.diag(np.nan*np.diagonal(matrix_pre)),axis=0)
all_res[kk*10:(kk+1)*10]['Subject'] = range(1,11)
group_list = []
for num in range(1,11):
group_list += [getGroup(num,dict_groups[litter])] # dict_groups e' global definita in parse_lab_book_RenamedLitters.py
all_res[kk*10:(kk+1)*10]['Group'] = group_list
all_res[kk*10:(kk+1)*10]['PRE/POST'] = 'PRE'
all_res[kk*10:(kk+1)*10]['Litter'] = litter
vector_dist = transform_to_pdist_type(matrix_pre)
# res=pd.DataFrame(np.max(matrix_pre)-matrix_pre,index=range(1,11),columns=range(1,11))
kk+=1
matrix_post = matrix_time_in_cluster(litter_data[post_key],10)
all_res[kk*10:(kk+1)*10]['Value'] = np.nanmean(matrix_post - np.diag(np.nan*np.diagonal(matrix_post)),axis=0)
all_res[kk*10:(kk+1)*10]['Subject'] = range(1,11)
group_list=[]
for num in range(1,11):
group_list += [getGroup(num,dict_groups[litter])]
all_res[kk*10:(kk+1)*10]['Group'] = group_list
all_res[kk*10:(kk+1)*10]['PRE/POST'] = 'POST'
all_res[kk*10:(kk+1)*10]['Litter'] = litter
kk+=1
np.save('Dreadds_mixed_time_spent_togheter.npy',all_res)
df = pd.DataFrame(all_res)
writer = pd.ExcelWriter('time_spent_togheter.xlsx')
df.to_excel(writer,index=False)
writer.save()
writer.close()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment