def main():
    #what size images are we working with??
    width = 256
    height = width
    frame_ref=400
    f_rate=30.
    f_low=[.1,.3]
    f_high=[1.,3.]
    #define the base dirs for the data and analysis
    #note that these should become network locations
    base_dir_data='/media/cornelis/DataCDH/Raw-data/Test Data'
    base_dir_analysis='/media/cornelis/DataCDH/data/Test Data Output'
    to_analyze=jf.check_dirs(base_dir_data,base_dir_analysis)
    print(to_analyze)
    
    #start a loop on the folders to analyze and do some things.
    #we need to make a local copy for speed of loading and saving
    base_dir_tmp='/home/cornelis/PycharmProjects/BrainImageAnalyzer/data'
    for f in to_analyze:
        #Check if the tmp dir exists or not, copy over the dirs in the date folder
        if os.path.isdir(base_dir_tmp):
            print('tmp found, deleting')
            shutil.rmtree(base_dir_tmp)
            shutil.copytree(base_dir_data+f,base_dir_tmp)
        else:
            shutil.copytree(base_dir_data+f,base_dir_tmp)
            print('tmp not found, just copying')
            
        #figure out which cages are present
        #print('These are the cages '+str(jf.check_cages(base_dir_tmp))+' in'+f)
        the_cages=jf.check_cages(base_dir_tmp)
        
        #continue doing stuff
        #step one is to read in all the raw data and save all the green videos
        if len(the_cages)>0:
            for c in the_cages:
                #setup the dir for reading the raw data
                data_dir=base_dir_tmp+str(c)+'/Videos/'
                #which mice are in the cage?
                the_mice=jf.check_mice(data_dir)
                #print(data_dir)
                os.chdir(data_dir)
                raw_list=glob.glob('*.raw')
                green_dir=base_dir_tmp+c+'/Green/'
                os.makedirs(green_dir)
                for rf in raw_list:
                    frames=fj.get_frames(data_dir+rf,width,height)
                    out_green_fn=rf[:-4]+'.g'
                    fj.save_to_file(green_dir,out_green_fn,frames,np.uint8)
                print('waiting...')
                #copy the resulting folder to the analysis location
                green_dir_to_copy=base_dir_analysis+f+'/'+c
                #print(green_dir_to_copy)
                if os.path.isdir(green_dir_to_copy+'/Green/'):
                    shutil.rmtree(green_dir_to_copy+'/Green/')
                    shutil.copytree(green_dir,green_dir_to_copy+'/Green/')
                else:
                    shutil.copytree(green_dir,green_dir_to_copy+'/Green/')

                #Do alignments!!
                print "Doing alignments..."
                aligned_dir=base_dir_tmp+c+'/Aligned/'
                os.makedirs(aligned_dir)
                for m in the_mice:
                    lof, lofilenames=dj.get_file_list(green_dir, m)
                    print(lof)
                    lp=dj.get_distance_var(lof,width,height,frame_ref)
                    for i in range(len(lp)):
                        print('Working on this file: ')+str(lof[i])
                        #tmp_lof=[]
                        #tmp_lof.append(lof[i])
                        #print('creating 1 element list')
                        #print(type(tmp_lof))
                        frames=dj.get_green_frames(str(lof[i]),width,height)
                        frames=dj.shift_frames(frames,lp[i])
                        #save it!
                        out_aligned_fn=str(lofilenames[i])
                        out_aligned_fn=out_aligned_fn[:-4]+'_aligned.g'
                        fj.save_to_file(aligned_dir,out_aligned_fn,frames,np.uint8)
                #copy the resulting folder to the analysis location
                aligned_dir_to_copy=base_dir_analysis+f+'/'+c
                if os.path.isdir(aligned_dir_to_copy+'/Aligned/'):
                    shutil.rmtree(aligned_dir_to_copy+'/Aligned/')
                    shutil.copytree(aligned_dir,aligned_dir_to_copy+'/Aligned/')
                else:
                    shutil.copytree(aligned_dir,aligned_dir_to_copy+'/Aligned/')
    
                #Do temporal filters, apply and calculate dff
                #each filter band
                for i in range(len(f_low)):
                    #set up a folder for this band
                    band_dir=base_dir_tmp+c+'/DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/'
                    #and for GSR version
                    gsrband_dir=base_dir_tmp+c+'/GSR_DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/'
                    os.makedirs(band_dir)
                    os.makedirs(gsrband_dir)
                    #go to the aligned tmp dir
                    os.chdir(aligned_dir)
                    aligned_list=glob.glob('*.g')
                    for af in aligned_list:
                        frames=dj.get_green_frames(aligned_dir+af,width,height)
                        avg_frames=fj.calculate_avg(frames)
                        frames=fj.cheby_filter(frames, f_low[i], f_high[i], f_rate)
                        frames+=avg_frames
                        frames=fj.calculate_df_f0(frames)
                        #save this
                        out_dff_fn=af[:-4]+'_DFF_'+str(f_low[i])+'-'+str(f_high[i])+'Hz.raw'
                        fj.save_to_file(band_dir,out_dff_fn,frames,np.float32)
                        #do gsr
                        frames=fj.gsr(frames,width,height)
                        #save this
                        out_gsr_fn=af[:-4]+'_GSR_DFF_'+str(f_low[i])+'-'+str(f_high[i])+'Hz.raw' 
                        fj.save_to_file(gsrband_dir,out_gsr_fn,frames,np.float32)
                    #copy the resulting folder to the analysis location
                    dff_dir_to_copy=base_dir_analysis+f+'/'+c
                    if os.path.isdir(dff_dir_to_copy+'/DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/'):
                        shutil.rmtree(dff_dir_to_copy+'/DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/')
                        shutil.copytree(band_dir,dff_dir_to_copy+'/DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/')
                    else:
                        shutil.copytree(band_dir,dff_dir_to_copy+'/DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/')
                    gsr_dir_to_copy=base_dir_analysis+f+'/'+c
                    if os.path.isdir(gsr_dir_to_copy+'/GSR_DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/'):
                        shutil.rmtree(gsr_dir_to_copy+'/GSR_DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/')
                        shutil.copytree(gsrband_dir,gsr_dir_to_copy+'/GSR_DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/')
                    else:
                        shutil.copytree(gsrband_dir,gsr_dir_to_copy+'/GSR_DFF_Freq_Band_'+str(f_low[i])+'-'+str(f_high[i])+'Hz/')
                        
                    
                
        else:
            print('No cages present, sry :(')
            
        
        #at end of loop make sure to clean up tmp folder
        shutil.rmtree(base_dir_tmp)
    def getImage(self,params):
        import pandas as pd
        print("getImage entered")
        choose_map = params['choose_map']
        preprocessed_frames_dict = {}

        if choose_map == 'spc_map':
            # Retrieve list of all x,y values for what was ticked
            tups=[(k,v) for (k,v) in params.items() if k[2:] in params['regions_check']]
            coords={}
            for x_tup in tups:
                for y_tup in tups:
                    # print(str(x_tup) + " and " + str(y_tup) + "?")
                    if x_tup[0][0] == 'x' and y_tup[0][0] == 'y' and x_tup[0][2:] == y_tup[0][2:]:
                        # print(str(x_tup) + " and " + str(y_tup))
                        coords[x_tup[0][2:]] = (x_tup[1],y_tup[1])


		# 5,6,3,2,1,4

        images = {}
        for coord in coords.items():
            if coord[1][0]!='' and coord[1][1]!='':

                print("Entered the loop")
                region = coord[0]
                x = float(coord[1][0])
                y = float(coord[1][1])
                f_low = float(params['f_low'])
                f_high = float(params['f_high'])
                frame_ref = float(params['frame_ref'])
                raw_file_folder = params['raw_file_folder']
                raw_filename_to_align = params['raw_file_to_align']


                if 'preprocessed_frames' not in globals():
                    # The first file added to lof is raw_file_to_align
                    lof=[]
                    for root, dirs, files in os.walk(raw_file_folder):
                        for file in files:
                            if(raw_filename_to_align.lower() in file.title().lower()):
                                lof.append((os.path.join(root, file)))
                    for root, dirs, files in os.walk(raw_file_folder):
                        for file in files:
                            if ((file.endswith(".raw") or file.endswith(".g")) and (os.path.join(root, file)) not in lof):
                                lof.append((os.path.join(root, file)))

                    # Uncomment to check result immdediately after import
                    # frames=dj.get_green_frames(str(lof[1]),width,height)
                    # return(frames[0])

                    # CorrelationMapDisplayer = fj.CorrelationMapDisplayer(frames)
                    # image = CorrelationMapDisplayer.get_correlation_map(x, y, frames)
                    #return(frames[0])
                    #print("Well F**k")

                    # First element in aligned_frames_list is all frames aligned to user selected raw
                    if len(lof) > 1:
                        print("Doing alignments...")
                        aligned_frames_list = []
                        lp=dj.get_distance_var(lof,width,height,frame_ref)
                        if 'all_aligns' in params['all_alignments_check']:
                            for i in range(len(lp)):
                                print('Working on this file: ')+str(lof[i])
                                frames=fj.get_frames(str(lof[i]),width,height)
                                frames_aligned=dj.shift_frames(frames,lp[i])
                                aligned_frames_list.append(frames_aligned)
                        else:
                            print('Working on this file: ')+str(lof[0])
                            frames=dj.get_frames(str(lof[0]),width,height)
                            frames_aligned=dj.shift_frames(frames,lp[0])
                            aligned_frames_list.append(frames_aligned)
                    else:
                        aligned_frames_list=[dj.get_frames(str(lof[0]),width,height)]
                        frames = aligned_frames_list[0]

                    # Uncomment to check result immdediately after alignment
                    #print('MADE IT')
                    #return(aligned_frames_list[0][0])

                    # CorrelationMapDisplayer = fj.CorrelationMapDisplayer(aligned_frames_list[0])
                    # image = CorrelationMapDisplayer.get_correlation_map(x, y, aligned_frames_list[0])
                    # return(image)
                    #print("Well F**k")

                    # Do temporal filters, apply and calculate dff
                    # each filter band
                    # Again, the first element in preprocessed_frames_list is cheby filter applied to all frames aligned to user selected
                    # raw and then df/f0 computed and then gsr applied if option was selected
                    preprocessed_frames_list = []
                    for f in aligned_frames_list:
                        avg_frames=fj.calculate_avg(frames)
                        frames=fj.cheby_filter(frames, f_low, f_high, frame_rate)
                        #return(np.gradient(frames[0]))[0]
                        frames+=avg_frames

                        frames=fj.calculate_df_f0(frames)
                        preprocessed_frames_list.append(frames)
                        #do gsr
                        # if 'gsr' in params['gsr_check']:
                        # 	frames=fj.gsr(frames,width,height)
                        # 	preprocessed_frames_list.append(frames)
                        # else:
                        # 	preprocessed_frames_list.append(frames)

                    preprocessed_frames = preprocessed_frames_list[0]
                    global preprocessed_frames

                 #do gsr
                if 'gsr' in params['gsr_check']:
                    #gsr_memory = True
                    #global gsr_memory
                    print('gsr commence')
                    preprocessed_frames=fj.gsr(preprocessed_frames,width,height)


                #pickle.dump( favorite_color, open( "save.p", "wb" ) )

                seed_indicator = 0.5
                # output selected map
                if choose_map == 'spc_map':
                    print('x = '+str(x))
                    print('y = '+ str(y))
                    CorrelationMapDisplayer = fj.CorrelationMapDisplayer(preprocessed_frames)
                    image = CorrelationMapDisplayer.get_correlation_map(y, x, preprocessed_frames)
                    preprocessed_frames_dict[region] = preprocessed_frames
                    # Make pixels around the seed equal to the maximum value 1 for the red dot to show
                    image[y,x]=seed_indicator
                    image[y+1,x]=seed_indicator
                    image[y,x+1]=seed_indicator
                    image[y+1,x+1]=seed_indicator
                    image[y-1,x]=seed_indicator
                    image[y,x-1]=seed_indicator
                    image[y-1,x-1]=seed_indicator
                    image[y-1,x+1]=seed_indicator
                    image[y+1,x-1]=seed_indicator
                    images[region] = image

                else:
                    image = fj.standard_deviation(preprocessed_frames)
                    # Make pixels around the seed equal to the maximum value 1 for the red dot to show
                    image[y,x]=seed_indicator
                    image[y+1,x]=seed_indicator
                    image[y,x+1]=seed_indicator
                    image[y+1,x+1]=seed_indicator
                    image[y-1,x]=seed_indicator
                    image[y,x-1]=seed_indicator
                    image[y-1,x-1]=seed_indicator
                    image[y-1,x+1]=seed_indicator
                    image[y+1,x-1]=seed_indicator
                    images[region] = image

        # You have a dictionary of all the images. Concatenate them vertically in a logical order
        global preprocessed_frames_dict
        images_list = []

        for region in params['regions_check']:
            for image_and_key in images.items():
                if image_and_key[0] == region:
                    print(image_and_key[0])
                    images_list = images_list + [image_and_key[1]]

        combined_image = np.vstack(images_list)
        return combined_image