# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ### This script is designed to take an input of location of a beacon in X and Y coordinates and bin size of historgram. It knows already the size of the arena and will produce a histogram/ matrix of numbers for occupancy/area per each bin.
#

import matplotlib.pyplot as plt 
import numpy as np
import matplotlib.patches as patches
from PIL import Image
import matplotlib.image as mpimg
import pandas as pd
import math
figures = 'C:/Users/Fabian/Desktop/Analysis/Round3_FS03_FS06/Figures/'
processed= 'C:/Users/Fabian/Desktop/Analysis/Round3_FS03_FS06/processed/'

# ### 1. input arena size

cut = 0 # keeping the cut where rectangle of arena ends
X_cut_min = -.59
Y_cut_max = 1.61
X_cut_max = .12
Y_cut_min = .00
print("area %s M*2" %((X_cut_max-X_cut_min)*(Y_cut_max-Y_cut_min))) 

# +
xcut_offset=0
ycut_offset=0

fig, ax1 = plt.subplots(1, 1, sharex=True,dpi=300, sharey=True)
fig.suptitle("Normalization visual")
ax1.plot([(X_cut_min+cut)-xcut_offset,(X_cut_max-cut)-xcut_offset],[(Y_cut_max-cut)+ycut_offset,(Y_cut_max-cut)+ycut_offset] ,'r-')
ax1.plot([(X_cut_min+cut)-xcut_offset,(X_cut_min+cut)-xcut_offset],[(Y_cut_min+cut)+ycut_offset,(Y_cut_max-cut)+ycut_offset] ,'r-')
ax1.plot([(X_cut_max-cut)-xcut_offset,(X_cut_max-cut)-xcut_offset],[(Y_cut_min+cut)+ycut_offset,(Y_cut_max-cut)+ycut_offset] ,'r-')
ax1.plot([(X_cut_max-cut)-xcut_offset,(X_cut_min+cut)-xcut_offset],[(Y_cut_min+cut)+ycut_offset,(Y_cut_min+cut)+ycut_offset] ,'r-')
ax1.plot(-.45,.4,"go")
rectangle = patches.Rectangle((-.59,0), .71,1.61, color="green")
ax1.add_patch(rectangle)
k=reversed(range(10))
print(k)
color=iter(plt.cm.rainbow(np.linspace(0,1,10)))
for i in reversed(range(10)):
    c=next(color)
    patch = patches.Circle((-.45,.4), radius=.15*i,color=c)
    ax1.add_patch(patch)
    patch.set_clip_path(rectangle)                  
    #get_clip_path(self)[source]
ax1.axis("equal") 
plt.show()
# -

# ## 2. Now define the area of circles mathematically 

# ## a. get area of each circle withouth subtracting rectangle 

from math import pi
r = float(input ("Input the radius of the circle : "))
print ("The area of the circle with radius " + str(r) + " is: " + str(pi * r**2))

hist=[]
prev=0
for i in (range(10)):
    res=(pi * (.15*i)**2)
    hist.append(res-prev)
    prev=res
print(hist)
plt.bar(range(10),hist)

# ### b. subract the area from the rectangle area

# ## Tried mathematical approach on paper but there were at least 4 other combination for which it would have to be calculated.So it is possible but would take too long to write the code hence decided to use the pictures that canbe generated and eastimaed the pixels using different grayscale values 

# +
xcut_offset=0
ycut_offset=0
def visualization(center=(-.45,.4),X_cut_min = -.59,Y_cut_max = 1.61,X_cut_max = .12,Y_cut_min = .00 ):
    """Makes a visual represetation of a banded rectangle with circles. to be exported and then can be counted""" 
    
    fig, ax1 = plt.subplots(1, 1, sharex=True,dpi=400,)
    fig.suptitle("Normalization visual")
    ax1.plot(center[0],center[1],"go")
    rectangle = patches.Rectangle((X_cut_min,Y_cut_min), (abs(X_cut_min)+abs(X_cut_max)),Y_cut_max , color="green")
    ax1.add_patch(rectangle)
    k=reversed(range(10))
    print(k)
    color=iter(plt.cm.rainbow(np.linspace(0,1,10)))
    for i in reversed(range(10)):
        c=next(color)
        patch = patches.Circle((center[0],center[1]), radius=.15*i,color=c)
        ax1.add_patch(patch)
        patch.set_clip_path(rectangle)                  

    ax1.axis("equal") 
    return plt.show()
    
visualization()


# +
xcut_offset=0
ycut_offset=0
def visualization_grey(center=(-.45,.4),dpi=500,X_cut_min = -.59,Y_cut_max = 1.61,X_cut_max = .12,Y_cut_min = .00 ):
    """Makes a visual represetation of a banded rectangle with circles. to be exported and then can be counted""" 
    
    fig, ax1 = plt.subplots(1, 1, sharex=True,dpi=dpi,)
    fig.patch.set_visible(False)
    #fig.suptitle("Normalization visual")
    #ax1.plot(center[0],center[1],"go")
    rectangle = patches.Rectangle((X_cut_min,Y_cut_min), (abs(X_cut_min)+abs(X_cut_max)),Y_cut_max , color="black")
    ax1.add_patch(rectangle)
    k=reversed(range(10))
    print(k)
    color=iter(plt.cm.binary(np.linspace(.01,.99,20)))
    for i in reversed(range(20)):
        c=next(color)
        patch = patches.Circle((center[0],center[1]), radius=.075*i,color=c)
        ax1.add_patch(patch)
        patch.set_clip_path(rectangle)                  

    ax1.axis("equal")
    ax1.axis("off")
    mng = plt.get_current_fig_manager()
    mng.full_screen_toggle()
    fig.savefig(figures + 'norm_graph.png', dpi=dpi, transparent=True)
    return plt.show()
    
visualization_grey()
# -

img = mpimg.imread(figures + 'norm_graph.png')
counts, bins, bars = plt.hist(img.ravel(), bins=18, range=(0.01, .99),)
plt.hist(img.ravel(), bins=18, range=(0.01, .99), fc='k', ec='w')
plt.show()
imgplot = plt.imshow(img)
counts


img.ravel()

norm= []
int(sum(counts))
for count in counts: 
    k= count/int(sum(counts))
    norm.append(k)
norm

# ### Now take the counts and multiply the distributions correctly - so make a histogram for each time beacon changes 

FS04=pd.read_excel(processed +'FS04_rears_new.xlsx', index_col=0)
FS04.head()


# +

def get_rear_distance_from_beacon(df_rears_corrected):
    dist=[]
    for row in df_rears_corrected.iterrows():
        #print(row[1][1])
        #print(row[1][4])
        #print(row[1][2])
        #print(row[1][5])
        dist.append(math.sqrt((row[1][1] - row[1][4])**2 + (row[1][2] - row[1][5])**2))

    return dist 
plt.hist(get_rear_distance_from_beacon(FS04))


# +
def make_simple_graphs (animal_ID,rearing):
    binwidth=.075
    plt.tight_layout
    bins = np.arange(0, 1.425, binwidth)
    interval=bins
    print(bins)
    bins[1]= 0.075
    fig, ax = plt.subplots(4,dpi=800,sharex=False)
    fig.suptitle(animal_ID +' rearing distance from beacons ephys',y=1)

    N, bins, patches=ax[0].hist(get_rear_distance_from_beacon(rearing.loc[rearing['Visibility']==1]),bins=bins,ec='w')
    
    print(len(norm))
    print(len(bins))
    print(N*norm)
    
    ax[0].set_title('Visible attempt')
    for i in range(0,1):
        patches[i].set_facecolor('g')
    for i in range(1, len(patches)):
        patches[i].set_facecolor('blue')
        fig.tight_layout(pad=1.5)
        
    N1, bins, patches=ax[1].hist(get_rear_distance_from_beacon(rearing.loc[rearing['Visibility']==0]),bins=bins,ec='w')
    print(N1*norm)
    ax[1].set_title('Invisible attempt')
    for i in range(0,1):
        patches[i].set_facecolor('g')
    for i in range(1, len(patches)):
        patches[i].set_facecolor('blue')
        fig.tight_layout(pad=1.5)
    
    patches = ax[2].bar(N*norm,bins)
    ax[2].set_title('Visible attempt Normalized')
    for i in range(0,1):
        patches[i].set_facecolor('g')
    for i in range(1, len(patches)):
        patches[i].set_facecolor('blue')
        fig.tight_layout(pad=1.5)
    
    
    number, bins, patches = ax[3].hist((N1*norm),bins=bins,ec='w')
    ax[3].set_title('invisible attempt Normalized')
    for i in range(0,1):
        patches[i].set_facecolor('g')
    for i in range(1, len(patches)):
        patches[i].set_facecolor('blue')
        fig.tight_layout(pad=1.5)
    
    
    
        
    plt.savefig('%srat_rearing_distance_from_beacons_norm%s.png'%(figures,animal_ID), dpi = 100)

    
    
make_simple_graphs('FS04' ,FS04) 

# -



StarcoderdataJupyterScriptsDedupFiltered