<jupyter_start><jupyter_text>VISUALIZE REGISTERED REGIONS<jupyter_code>mask = np.load('/media/cat/4TBSSD/yuki/IA2/tif_files/IA2pm_Apr22_Week2_30Hz/IA2pm_Apr22_Week2_30Hz_aligned_maskwarp.npy')
if True:
    idx = np.where(mask!=0)
    mask[idx]=1

    idx = np.where(mask==0)
    mask[idx]=np.nan
#mask[~idx]=1
plt.imshow(mask)
plt.show()
# load REGISTERED MAP IN 679 x 586 resolution

dorsalMaps_unique = np.load('/home/cat/dorsalMaps_name.npy')
print (dorsalMaps_unique.shape)
print (np.unique(dorsalMaps_unique))
plt.imshow(dorsalMaps_unique)
plt.show()
# load REGISTERED MAP IN 128 x 128 resolution
# NOTE: This map contains average errors that need to be removed:
# so replace the values which are not in the dorsalMap list above with most frequent nearby values;

# fix mean maskwarp values
maskwarp = np.load('/home/cat/maskwarp.npy')
ax = plt.subplot(1,2,1)
plt.imshow(maskwarp)
print (np.unique(maskwarp))
maskwarp_unique_ids = np.unique(maskwarp)
for id_ in maskwarp_unique_ids:
    #print (id_)
    if id_ in dorsalMaps_unique:
        pass
    else:
        idx = np.where(maskwarp==id_)
        
        for k in range(len(idx[0])):
            maskwarp[idx[0][k],idx[1][k]] = np.nan

        for k in range(len(idx[0])):
            temp = maskwarp[max(0,idx[0][k]-1): idx[0][k]+2, max(0,idx[1][k]-1): idx[1][k]+2].ravel()
            uniques = np.unique(temp, return_counts=True)
            idx4 = np.argmax(uniques[1])
            val = uniques[0][idx4]  # Replace values with most frequent nearby vals;
            maskwarp[idx[0][k],idx[1][k]] = val

ax = plt.subplot(1,2,2)
plt.imshow(maskwarp)
print (np.unique(maskwarp))
plt.show()

# load names/labels for data
ids = np.load('/home/cat/dorsalMaps_id.npy').squeeze()
print ("All Allen IDS: ", ids.shape[0])
import csv
names = []
with open('/home/cat/dorsalMaps_name.txt', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        names.append(row)
        
print ("All Allen area names: ", len(names[1:]))


# visualize annotated areas:
registered_ids = np.int32(np.unique(maskwarp))
print ("Registered area ids: ", registered_ids.shape)
print ("registered ids: ", registered_ids[:10])

ctr=1
areas = [15, 21, 43, 57,150, 249, 255,261, 268, 275]
area_masks = []
names_array = []
fig=plt.figure()
#for k in range(25,registered_ids.shape[0],25):
for k in range(0,registered_ids.shape[0],1):
    ax=plt.subplot(4,10,ctr)
    temp = maskwarp.copy()

    print ("searching for ", registered_ids[k])
    idx = np.where(temp!=registered_ids[k])
    temp*=1
    temp[idx]=0
    temp*=mask
    if registered_ids[k] in areas:
        area_masks.append(idx)
        names_array.append(names[registered_ids[k]+1])
    plt.imshow(temp)
    
    ctr+=1
    # label stuff
    
    titles = ''
    for p in range(len(names[registered_ids[k]+1])):
        titles = titles + names[registered_ids[k]+1][p]+'\n'
    
    plt.ylabel(str(k)+" "+str(registered_ids[k]))
    plt.xticks([])
    plt.yticks([])
    plt.title(titles,fontsize=8,y=0.85)
plt.show()
print (len(names_array))
print (names_array)

# grab masks for RS dorsal and RS ventral 
img = np.zeros((128,128),'float32') #*np.nan
print (len(area_masks))
masks_2D = []
for k in range(len(area_masks)):
    print (k)
    ax=plt.subplot(4,3,k+1)
    temp =img.copy()
    temp[area_masks[k]]=np.nan
    masks_2D.append(temp)
    plt.imshow(temp)
    plt.title(names_array[k])
plt.show()

np.save('/home/cat/areas.npy', masks_2D)
np.save('/home/cat/area_names.npy',names_array)
# USE stacks_ave_arrays ([# features, # times steps, width, height]) to visualize specific areas:
# to plot ROI vs time

temp_temp = np.array(stacks_ave_arrays)
print (temp_temp.shape)

selected_feature = 0
t=np.arange(temp_temp.shape[1])/30. - 3
ax = plt.subplot(2,1,1)
for k in range(1,registered_ids.shape[0],1):

    temp = maskwarp.copy()
    
    print ("searching for area ", registered_ids[k])
    # find indexes for particaulr ROI
    idx = np.where(temp==registered_ids[k])
    #time_series = temp_temp[selected_feature,:, :].mean(1).mean(1)#, idx[0], idx[1]].mean(0)
    time_series = temp_temp[selected_feature, :, idx[0], idx[1]].mean(0)
    print (time_series.shape)
    
    #for p in range(len(names[registered_ids[k]])):
    #    if "Retrosplenial" in names[registered_ids[k]][p]:
    plt.plot(t, time_series, label=names[registered_ids[k]])

#plt.legend(fontsize=10)
ax = plt.subplot(2,1,2)
time_series = temp_temp[selected_feature]
print (time_series.shape)
temp_show = np.zeros((128,128),'float32')
for k in range(time_series.shape[0]):
    #print (time_series[k].shape, mask.shape, (time_series[k]*mask).shape)
    temp_show= np.hstack((temp_show, time_series[k]*mask))

#temp_show = #np.hstack(temp_show)
print ("final: ", temp_show.shape)
plt.imshow(temp_show,aspect='auto')
    
    
plt.suptitle("Trigger: "+str(labels[selected_feature]))
plt.show()


# load the ROI areas used for SVM analysis
ROI_area_ids = np.load('/media/cat/4TBSSD/yuki/IA1/tif_files/IA1am_Mar4_30Hz/IA1am_Mar4_30Hz_code_04_lockout_10sec_trial_ROItimeCourses_10sec_area_ids.npy')
print (ROI_area_ids)

# load the names of all 840 allen ROIs
fname = '/media/cat/4TBSSD/yuki/meta_allenmap/dorsalMaps_name.txt'

# get the names of the ROI_area_ids
import csv
names = []
with open(fname) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        names.append(row)

# print the matched ids
names=np.array(names)
for id_ in ROI_area_ids:
    print (id_, names[id_])
# make visualizatoin of all the ROI areas

# set backend to not plot and save directly; allows for higher res figs
matplotlib.use('Agg')

# load the maskwarp we used
mask = np.load('/media/cat/4TBSSD/yuki/meta_allenmap/maskwarp.npy')

# plot data
ctr=1
fig=plt.figure(figsize=(30,30))
for k in ROI_area_ids:
    ax=plt.subplot(5,7,ctr)
    idx = np.where(mask==k)
    mask1 = mask.copy()
    mask1[idx]=np.nan
    plt.imshow(mask1)
    ctr+=1
    plt.xticks([])
    plt.yticks([])
    plt.title(str(k)+" "+str(names[k]), fontsize=9)
plt.savefig('/home/cat/maps.png')
plt.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15407023790257324, 'fraction_numerical': 0.030911150671629712, 'mean_word_length': 4.184563758389261, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '638487', 'license': 'MIT', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2414, 'n_words': 474, 'path': 'older/self_initiated_paper/3_Fig_3_calcium_motiffs.ipynb', 'repo_name': 'catubc/widefield', 'size': '6179'}