text:
from exptools.core.session import MRISession
from LearningTrial import LearningTrial, InstructionTrial, PracticeTrial #, EndOfExperimentTrial, EndOfBlockTrial
from LearningStimulus import LearningStimulus, FixationCross, LearningStimulusSingle
from psychopy import visual, data
import datetime
import glob
import pandas as pd
import numpy as np
import os
import copy
import cPickle as pkl

class LearningSession(MRISession):

    def __init__(self, subject_initials, index_number, tr, start_block, config, debug=False, practice=False):
        super(LearningSession, self).__init__(subject_initials,
                                              index_number,
                                              tr=tr,
                                              simulate_mri_trigger=False,
                                              # NB: DO NOT use this MRI simulation option, but rather another!
                                              mri_trigger_key=config.get('mri', 'mri_trigger_key'))

        self.config = config
        self.start_block = start_block  # allows for starting at a later block than 1
        self.warmup_trs = config.get('mri', 'warmup_trs')
        self.restart_block = False
        self.debug = debug
        self.practice = practice

        self.response_button_signs = [config.get('input', 'response_button_left'),
                                      config.get('input', 'response_button_right')]

        _ = self.create_screen(engine='psychopy',
                               size=config.get('screen', 'size'),
                               full_screen=config.get('screen', 'full_screen'),
                               background_color=config.get('screen', 'background_color'),
                               gamma_scale=config.get('screen', 'gamma_scale'),
                               physical_screen_distance=config.get('screen', 'physical_screen_distance'),
                               physical_screen_size=config.get('screen', 'physical_screen_size'),
                               max_lums=config.get('screen', 'max_lums'),
                               wait_blanking=config.get('screen', 'wait_blanking'),
                               screen_nr=config.get('screen', 'screen_nr'),
                               mouse_visible=config.get('screen', 'mouse_visible'))

        # TODO: think about really including this?
        self.screen.recordFrameIntervals = True

        # negative durations below are jittered and will be determined per trial (except phase 0)
        # note that all these durations are default values and can and will be overwritten before each trial
        self.phase_durations = np.array([-0.0001,  # phase 0: wait for scan pulse
                                         -0.1,  # phase 1: fix cross
                                         1,     # phase 2: cue
                                         -0.1,  # phase 3: fix cross
                                         2,     # phase 4: stimulus
                                         0.5,   # phase 5: choice highlight
                                         0.5,   # phase 6: feedback
                                         -.01   # phase 7: ITI
                                         ])
        self.total_points = 0
        self.total_trials = 0
        self.instruction_trial_n = -1
        self.load_design()
        self.prepare_objects(counterbalance=True)

    def load_design(self):

        if self.practice:
            fn = 'sub-PRACTICE_design'
        elif self.debug:
            fn = 'sub-DEBUG_design'
        else:
            fn = 'sub-' + str(self.index_number).zfill(2) + '_design'

        self.design = pd.read_csv(os.path.join('designs', fn + '.csv'), sep='\t', index_col=False)
        self.p_wins = self.design.p_win_correct.unique()

        # For practice sessions, we want to annotate some stuff but only in the first three trials per block (?)
        self.design['annotate'] = False
        if self.practice:
            for block in [1, 4]:
                idx = self.design.block == block
                idx = (np.cumsum(idx) > 0) & (np.cumsum(idx) < 2)  # run only the first trial annotated
                self.design.loc[idx, 'annotate'] = True

    def estimate_bonus(self, return_final=False):
        """
        simple linear combination
        y = a*x + b
        a = 10/max_points
        b = -10/2
        """

        if return_final:
            # n points if *always* chosen the right answer, based on *all* trials
            max_points = self.design.shape[0] * np.mean(self.p_wins) * 100.
        else:
            # expected n points if *always* chosen the right answer, based on the number of trials so far
            max_points = self.total_trials * np.mean(self.p_wins) * 100.
        n_moneys = self.total_points * (10. / max_points) - 10 / 2.
        n_moneys_capped = np.min([np.max([n_moneys, 0]), 5])  # cap at [0, 5]

        if return_final:
            return max_points, n_moneys_capped
        else:
            return n_moneys_capped

    def update_instruction_screen(self, task='SAT',
                                  block=0,
                                  show_upcoming_stimuli=False,
                                  experiment_start=False,
                                  end_of_block=False,
                                  end_of_session=False):
        """
        Updates instruction screen based on upcoming block
        :param task: ['SAT', 'vanilla']
        :param block: [0, 1, 2]
        :param experiment_start: bool  (is this the start of the experiment? If true, shows welcome screen)
        """

        if experiment_start:
            # Session starts
            if self.practice:
                txt = 'Welcome to this practice session!'
            else:
                txt = 'Welcome to this experiment!'
            self.current_instruction_screen = [
                visual.TextStim(win=self.screen, text=txt,
                                height=self.config.get('text', 'height'),
                                units=self.config.get('text', 'units'),
                                pos=(0, 2)),
                visual.TextStim(win=self.screen, text='Press <space bar> to continue',
                                height=self.config.get('text', 'height'),
                                units=self.config.get('text', 'units'),
                                pos=(0, -2))
            ]
        elif end_of_session:
            # Session ends
            if self.practice:
                txt = 'This is the end of the practice session\n\nPress <space bar> to continue to the real experiment'
            else:
                txt = 'This is the end of experiment\n\nPlease inform the experiment leader now'
            self.current_instruction_screen = [
                visual.TextStim(self.screen,
                                pos=(0, 0),
                                units=self.config.get('text', 'units'),
                                height=self.config.get('text', 'height'),
                                text=txt,
                                wrapWidth=80
                                )]

        elif block in [1, 4] and not show_upcoming_stimuli:
            # Announce vanilla / SAT task
            if task == 'SAT':
                if self.practice:
                    self.current_instruction_screen = [
                        visual.TextStim(self.screen,
                                        text='You will next practice the task with speed/accuracy cues. We will '
                                             'again first show you what all of the stimuli in the upcoming block look '
                                             'like.',
                                        height=self.config.get('text', 'height'),
                                        units=self.config.get('text', 'units'),
                                        pos=(0, 0),
                                        wrapWidth=self.config.get('text', 'wrap_width')
                                        ),
                        visual.TextStim(self.screen,
                                        text='Press <space bar> to continue',
                                        pos=(0, -8), italic=True,
                                        units=self.config.get('text', 'units'),
                                        height=self.config.get('text', 'height'),
                                        wrapWidth=self.config.get('text', 'wrap_width')
                                        )
                    ]
                else:
                    # prep screen here
                    self.current_instruction_screen = [
                        self.instruction_screens[1]
                    ]
            elif task == 'vanilla':
                if self.practice:
                    self.current_instruction_screen = [
                        visual.TextStim(self.screen,
                                        text='We will first introduce the task without any speed/accuracy cues.\n\n'
                                             'To start with, we will show you what all of the stimuli in the upcoming '
                                             'block look like',
                                        height=self.config.get('text', 'height'),
                                        units=self.config.get('text', 'units'),
                                        pos=(0, 0),
                                        wrapWidth=self.config.get('text', 'wrap_width')
                                        ),
                        visual.TextStim(self.screen,
                                        text='Press <space bar> to continue',
                                        pos=(0, -8), italic=True,
                                        units=self.config.get('text', 'units'),
                                        height=self.config.get('text', 'height'),
                                        wrapWidth=self.config.get('text', 'wrap_width')
                                        )
                    ]
                else:
                    self.current_instruction_screen = [
                        self.instruction_screens[0]
                    ]

        elif show_upcoming_stimuli:
            # show all upcoming stimuli
            idx = (self.design.block == block)
            all_upcoming_pairs = self.design.loc[idx].groupby(['stimulus_set'])[['stim_left', 'stim_right']].last().reset_index()[
                ['stim_left', 'stim_right']].values.tolist()
            # all_upcoming_pairs = [self.design.loc[idx, 'stim_left'].unique(), self.design.loc[idx, 'stim_right'].unique()]
            y_positions = [4, 0, -4]

            if self.practice:
                self.current_instruction_screen = [
                    visual.TextStim(self.screen,
                                    text='The figures below will be your choice options in the next block. Have a '
                                         'look at them, and try to remember what they look like',
                                    pos=(0, 8),
                                    units=self.config.get('text', 'units'),
                                    height=self.config.get('text', 'height'),
                                    wrapWidth=self.config.get('text', 'wrap_width')
                                    )
                ]
            else:
                self.current_instruction_screen = [
                    visual.TextStim(self.screen,
                                    text='In the upcoming new block, you will see new choice pairs, illustrated '
                                         'below. As '
                                         'before, you will need to learn which choice options are most valuable. '
                                         'Have a look at them, and try to remember what they look like',
                                    pos=(0, 9),
                                    units=self.config.get('text', 'units'),
                                    height=self.config.get('text', 'height'),
                                    wrapWidth=self.config.get('text', 'wrap_width')
                                    )
                ]

            for i, pair in enumerate(all_upcoming_pairs):
                self.current_instruction_screen.append(
                    visual.TextStim(self.screen, pos=(self.config.get('stimulus', 'x_pos')[0], y_positions[i]),
                                    text=pair[0], height=self.config.get('stimulus', 'text_height'),
                                    units=self.config.get('text', 'units'),
                                    font='Agathodaimon', fontFiles=['./lib/AGATHODA.TTF'])
                )
                self.current_instruction_screen.append(
                    visual.TextStim(self.screen, pos=(self.config.get('stimulus', 'x_pos')[1], y_positions[i]),
                                    text=pair[1], height=self.config.get('stimulus', 'text_height'),
                                    units=self.config.get('text', 'units'),
                                    font='Agathodaimon', fontFiles=['./lib/AGATHODA.TTF'])
                )

            if self.practice:
                self.current_instruction_screen.append(
                    visual.TextStim(self.screen,
                                    text='Next, we will show an example trial, with step-by-step explanations of what to '
                                         'do.',
                                    pos=(0, -8),
                                    units=self.config.get('text', 'units'),
                                    height=self.config.get('text', 'height'),
                                    wrapWidth=self.config.get('text', 'wrap_width')
                                    ))
            self.current_instruction_screen.append(
                visual.TextStim(self.screen,
                                text='To start the task, press <space bar>!',
                                pos=(0, -10), italic=True,
                                units=self.config.get('text', 'units'),
                                height=self.config.get('text', 'height'),
                                wrapWidth=self.config.get('text', 'wrap_width')
                                )
            )

        elif end_of_block:
            # guestimate the amount of money to be earned
            estimated_moneys = self.estimate_bonus()
            end_str = '!' if estimated_moneys > 0 else ''
            break_str = 'You can take a short break now. ' if not self.practice else ''

            self.current_instruction_screen = [
                visual.TextStim(self.screen, pos=(0, 4),
                                units=self.config.get('text', 'units'),
                                height=self.config.get('text', 'height'),
                                text='End of block. So far, you earned %d points%s' % (
                                self.total_points, end_str),
                                wrapWidth=self.config.get('text', 'wrap_width')
                                ),
                visual.TextStim(self.screen, pos=(0, 0),
                                units=self.config.get('text', 'units'),
                                height=self.config.get('text', 'height'),
                                text='Based on your performance so far, it looks like you will receive a bonus of approximately %.2f euro%s' % (estimated_moneys, end_str),
                                wrapWidth=self.config.get('text', 'wrap_width')
                                ),
                visual.TextStim(self.screen, pos=(0, -4),
                                units=self.config.get('text', 'units'),
                                height=self.config.get('text', 'height'),
                                text='%sPress <space bar> to continue.' % break_str,
                                wrapWidth=self.config.get('text', 'wrap_width')
                                )
            ]

        #     def counterbalancounterbalancece_stimuli(self, all_sets):
#         """
#         For counterbalancing: determine 'order' of the provided stimulus set. That is, we want to make sure that a
#         specific character has varying probabilities of 'winning' over participants. E.g., assuming your stimuli are
#         ABCDEFGHIJKL, and you want ABCDEF and GHIJKL never to intermix, you may want this:
#
# pp  stim_1    stim_2    stim_3    stim_4    stim_5    stim_6    stim_7    stim_8    stim_9    stim_10    stim_11    stim_12
# --  ----  --------  --------  --------  --------  --------  --------  --------  --------  --------  ---------  ---------  ---------
#  0     1  A         B         C         D         E         F         G         H         I         J          K          L
#  1     2  B         A         D         C         F         E         H         G         J         I          L          K
#  2     3  C         D         E         F         A         B         I         J         K         L          G          H
#  3     4  D         C         F         E         B         A         J         I         L         K          H          G
#  4     5  E         F         A         B         C         D         K         L         G         H          I          J
#  5     6  F         E         B         A         D         C         L         K         H         G          J          I
#  6     7  G         H         I         J         K         L         A         B         C         D          E          F
#  7     8  H         G         J         I         L         K         B         A         D         C          F          E
#  8     9  I         J         K         L         G         H         C         D         E         F          A          B
#  9    10  J         I         L         K         H         G         D         C         F         E          B          A
# 10    11  K         L         G         H         I         J         E         F         A         B          C          D
# 11    12  L         K         H         G         J         I         F         E         B         A          D          C
#         """
#         import itertools
#         from copy import deepcopy
#
#         n_shifts = [0, 1, 2]  # assuming 6 stimuli, but you could do more, or less...
#         rev_inner = [False, True]
#         switch_sets = [False, True]
#
#         cb_df = pd.DataFrame(list(itertools.product(switch_sets, n_shifts, rev_inner)),
#                              columns=['switch_sets', 'n_shifts', 'rev_inner'])
#         cb_df['pp'] = np.arange(1, cb_df.shape[0] + 1)
#         for set_n in range(1, 13):
#             cb_df['stim_%d' % set_n] = None
#
#         for pp in cb_df['pp']:
#             idx = cb_df.pp == pp
#             switch_sets = cb_df.loc[idx, 'switch_sets'].iloc[0]
#             reverse_inner = cb_df.loc[idx, 'rev_inner'].iloc[0]
#             n_shifts = cb_df.loc[idx, 'n_shifts'].iloc[0]
#
#             if switch_sets:
#                 sets = deepcopy([all_sets[3:], all_sets[:3]])
#             else:
#                 sets = deepcopy([all_sets[:3], all_sets[3:]])
#
#             sets_allocated = 0
#             for set_n, set_ in enumerate(sets):
#                 for i in range(n_shifts):
#                     set_.insert(len(set_), set_.pop(0))  # move first item to last place
#
#                 if reverse_inner:
#                     set_ = [x[::-1] for x in set_]  # reverse inner order
#
#                 # print('pp %d, %d, %s' % (pp, set_n, set_))
#                 #### NB: you could just use set_ as a final result; the placing in the dataframe and then reverting
#                 # back to a nested list is definitely not necessary but may help clarify what's going on here...
#                 for to_allocate in [0, 1, 2]:
#                     for to_allocate_i in [0, 1]:
#                         cb_df.loc[idx, 'stim_%d' % (sets_allocated + 1)] = set_[to_allocate][to_allocate_i]
#                         sets_allocated += 1
#
#         pp_zero_based = self.index_number - 1
#         row_iloc = int(pp_zero_based - np.floor(pp_zero_based / 12) * 12)
#         colnames = cb_df.columns
#         stim_list = cb_df.iloc[row_iloc][[x for x in colnames if 'stim' in x]].values.tolist()
#         stim_nested_list = [[stim_list[0 + y * 2], stim_list[1 + y * 2]] for y in range(6)]
#         print('Stimuli/set order for this pp: %s' % stim_nested_list)
#         return stim_nested_list

    def prepare_objects(self, counterbalance=True):
        """
        Prepares all visual objects (instruction/feedback texts, stimuli)
        """

        config = self.config

        # Fixation cross
        self.fixation_cross = FixationCross(self.screen,
                                            outer_radius=config.get('fixation_cross', 'outer_radius'),
                                            inner_radius=config.get('fixation_cross', 'inner_radius'),
                                            bg=config.get('fixation_cross', 'bg'))

        # checkout if stimulus type is interpreted
        if not config.get('stimulus', 'type') in ['colors', 'agathodaimon']:
            raise(IOError('No idea what stimulus type I should draw. You entered %s' % config.get('stimulus', 'type')))
        # checkout if colors exist
        if config.get('stimulus', 'type') == 'colors':
            import matplotlib.colors as mcolors
            for set in ['set_1', 'set_2', 'set_3']:
                for col in config.get('stimulus', set):
                    if not col in mcolors.CSS4_COLORS.keys():
                        raise(IOError('I dont understand color %s that was provided to stimulus set %s...' %(col,
                                                                                set)))

        # load all possible stimuli
        # all_stim = []
        # for set_n in range(10):
        #     # assume a maximum of 10 sets, bit arbitrary but seems enough
        #     if config.has_option('stimulus', 'set_%d' % set_n):
        #         all_stim.append(config.get('stimulus', 'set_%d' % set_n))
        #
        # if counterbalance:
        #     # counterbalance, based on index_num
        #     ###### WARNING: This is set-up to be specific for my experiment #######
        #     all_stim = self.counterbalance_stimuli(all_stim)

        # let's find all stimuli first from dataframe
        all_stimuli = np.unique(np.hstack([self.design.stim_high.unique(), self.design.stim_low.unique()]))

        self.stimuli = {'left': {}, 'right': {}}
        for stimulus in all_stimuli:
            for i, location in enumerate(['left', 'right']):
                self.stimuli[location][stimulus] = LearningStimulusSingle(
                    screen=self.screen,
                    stimulus=stimulus,
                    stimulus_type=config.get('stimulus', 'type'),
                    width=config.get('stimulus', 'width'),
                    height=config.get('stimulus', 'height'),
                    text_height=config.get('stimulus', 'text_height'),
                    units=config.get('stimulus', 'units'),
                    x_pos=config.get('stimulus', 'x_pos')[i],
                    rect_line_width=config.get('stimulus',
                                               'rect_line_width'))

        # # Stimuli
        # self.stimuli = []
        # for stim in all_stim:
        #     self.stimuli.append(
        #         LearningStimulus(self.screen,
        #                          stimulus_type=config.get('stimulus', 'type'),
        #                          width=config.get('stimulus', 'width'),
        #                          height=config.get('stimulus', 'height'),
        #                          set=stim,
        #                          text_height=config.get('stimulus', 'text_height'),
        #                          units=config.get('stimulus', 'units'),
        #                          x_pos=config.get('stimulus', 'x_pos'),
        #                          rect_line_width=config.get('stimulus', 'rect_line_width')))

        # load instruction screen pdfs
        self.instruction_screens = [
            visual.ImageStim(self.screen, image='./lib/vanilla.png'),
            visual.ImageStim(self.screen, image='./lib/sat.png')
        ]

        # Prepare feedback stimuli. Rendering of text is supposedly slow so better to do this once only (not every
        # trial)
        # 1. Feedback on outcomes
        self.feedback_outcome_objects = [
            visual.TextStim(win=self.screen, text='Outcome: 0',  color='darkred',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos=(0, config.get('text', 'feedback_y_pos')[0])),
            visual.TextStim(win=self.screen, text='Outcome: +100', color='darkgreen',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos=(0, config.get('text', 'feedback_y_pos')[0])),
            visual.TextStim(win=self.screen, text='No choice made', color='darkred',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos=(0, config.get('text', 'feedback_y_pos')[0]))
        ]
        # 2. Feedback on earnings
        self.feedback_earnings_objects = [
            visual.TextStim(win=self.screen, text='Reward: 0', color='darkred',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos = (0, config.get('text', 'feedback_y_pos')[1])),
            visual.TextStim(win=self.screen, text='Reward: +100', color='darkgreen',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos=(0, config.get('text', 'feedback_y_pos')[1])),
        ]
        # 3. Feedback on timing
        self.feedback_timing_objects = [
            visual.TextStim(win=self.screen, text='Too slow!', color='darkred',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos=(0, config.get('text', 'feedback_y_pos')[2])),
            visual.TextStim(win=self.screen, text='Too fast!', color='darkred',
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'),
                            pos=(0, config.get('text', 'feedback_y_pos')[2])),
        ]

        # Prepare cue texts. Rendering of text is supposedly slow so better to do this once only (not every
        # trial)
        self.cues = [
            # 0 = SPD
            visual.TextStim(win=self.screen, text="SPD",
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height')),

            # 1 = ACC
            visual.TextStim(win=self.screen, text="ACC",
                            units=config.get('text', 'units'),
                            height=config.get('text', 'height'))
        ]

        # Waiting for scanner screen
        self.scanner_wait_screen = visual.TextStim(win=self.screen,
                                                   text='Waiting for scanner...',
                                                   name='scanner_wait_screen',
                                                   units=config.get('text', 'units'), font='Helvetica Neue', pos=(0, 0),
                                                   italic=True,
                                                   height=config.get('text', 'height'), alignHoriz='center')

        if self.debug:
             pos = -config.get('screen', 'size')[0]/3, config.get('screen', 'size')[1]/3
             self.debug_txt = visual.TextStim(win=self.screen,
                                              alignVert='top',
                                              text='debug mode\n',
                                              name='debug_txt',
                                              units='pix',  # config.get('text', 'units'),
                                              font='Helvetica Neue',
                                              pos=pos,
                                              height=14,  # config.get('text', 'height'),
                                              alignHoriz='center')

    def save_data(self, trial_handler=None, block_n='all'):

        output_fn_dat = self.output_file + '_block-' + str(block_n)
        output_fn_frames = self.output_file + '_block-' + str(block_n)

        if trial_handler is not None:
            trial_handler.saveAsPickle(output_fn_dat)
            trial_handler.saveAsWideText(output_fn_dat + '.csv', )

        if self.screen.recordFrameIntervals:
            # Save frame intervals to file
            self.screen.saveFrameIntervals(fileName=output_fn_frames + '_frameintervals.log', clear=False)

            # import matplotlib.pyplot as plt
            # # Make a nice figure
            # intervals_ms = np.array(self.screen.frameIntervals) * 1000
            # m = np.mean(intervals_ms)
            # sd = np.std(intervals_ms)
            #
            # msg = "Mean=%.1fms, s.d.=%.2f, 99%%CI(frame)=%.2f-%.2f"
            # dist_string = msg % (m, sd, m - 2.58 * sd, m + 2.58 * sd)
            # n_total = len(intervals_ms)
            # n_dropped = sum(intervals_ms > (1.5 * m))
            # msg = "Dropped/Frames = %i/%i = %.3f%%"
            # dropped_string = msg % (n_dropped, n_total, 100 * n_dropped / float(n_total))
            #
            # # plot the frame intervals
            # plt.figure(figsize=[12, 8])
            # plt.subplot(1, 2, 1)
            # plt.plot(intervals_ms, '-')
            # plt.ylabel('t (ms)')
            # plt.xlabel('frame N')
            # plt.title(dropped_string)
            #
            # plt.subplot(1, 2, 2)
            # plt.hist(intervals_ms, 50, normed=0, histtype='stepfilled')
            # plt.xlabel('t (ms)')
            # plt.ylabel('n frames')
            # plt.title(dist_string)
            # plt.savefig(output_fn_frames + '_frameintervals.png')

    def close(self):
        """ Saves stuff and closes """

        points = self.total_points
        max_points, bonus = self.estimate_bonus(return_final=True)
        print('Total points: %d (maximum: %d), total bonus earned: %.3f' %(points, max_points, bonus))

        self.save_data()
        super(LearningSession, self).close()

    def run_trial(self, ID, parameters, phase_durations, session, screen, annotate=False):

        if annotate:
            # adjust trial parameters to allow for annotation phases
            phase_2 = np.Inf if parameters['cue'] in ['SPD', 'ACC'] else -0.001
            phase_durations = np.array([-0.0001,   # phase 0: wait for scan pulse
                                        np.Inf,     # phase 1: fix cross
                                        phase_2,   # phase 2: cue
                                        -0.1,     # phase 3: fix cross
                                        np.Inf,   # phase 4: stimulus
                                        np.Inf,   # phase 5: choice highlight
                                        np.Inf,   # phase 6: feedback, highlight 1 (self-paced)
                                        np.Inf,   # phase 7: feedback, highlight 2 (self-paced)
                                        np.Inf,   # phase 8: feedback, highlight 3 (self-paced)
                                        0.5    # phase 9: ITI
                                        ])

            return PracticeTrial(ID, parameters, phase_durations, session=session, screen=screen)
        else:
            return LearningTrial(ID, parameters, phase_durations, session=session, screen=screen)

    def run(self):
        """ Runs this Instrumental Learning task"""
        # start by showing welcome screen, but only if the experiment is started from block 0
        if self.start_block == 1:
            self.update_instruction_screen(block=0, experiment_start=True)
            _ = InstructionTrial(ID=self.instruction_trial_n,
                                 parameters={},
                                 phase_durations=[0.5, 1000],
                                 session=self,
                                 screen=self.screen).run()
            self.instruction_trial_n -= 1

        # # Set start time of block 0 to 0 (useful if you want to calculate durations on the fly, otherwise not so
        # important
        # self.block_start_time = 0

        all_blocks = np.unique(self.design.block)
        current_block_id = -1
        while True:   # while loop to allow for moving between blocks
            current_block_id += 1
            if len(all_blocks) <= current_block_id:
                print('End of experiment!')
                # self.stopped = True
                break
            block_n = all_blocks[current_block_id]

            if block_n < self.start_block:
                continue

            # Get all trials for the next block, create trial handler
            this_block_design = self.design.loc[self.design.block == block_n]
            trial_handler = data.TrialHandler(this_block_design.to_dict('records'),
                                              nReps=1,
                                              method='sequential')

            # Loop over trials
            for block_trial_ID, this_trial_info in enumerate(trial_handler):

                # show instruction screen (do this in inner loop so we can peek into the next cue)
                if block_n in [1, 4] and block_trial_ID == 0:
                    if this_trial_info['cue'] in ['SPD', 'ACC']:
                        self.update_instruction_screen(block=block_n, task='SAT', experiment_start=False)
                    else:
                        self.update_instruction_screen(block=block_n, task='vanilla', experiment_start=False)
                    _ = InstructionTrial(ID=self.instruction_trial_n,
                                         parameters={},
                                         phase_durations=[0.5, 1000],
                                         session=self,
                                         screen=self.screen).run()
                    self.instruction_trial_n -= 1

                    # also, show upcoming stimuli
                    self.update_instruction_screen(block=block_n, show_upcoming_stimuli=True)
                    _ = InstructionTrial(ID=self.instruction_trial_n,
                                         parameters={},
                                         phase_durations=[0.5, 1000],
                                         session=self,
                                         screen=self.screen).run()
                    self.instruction_trial_n -= 1

                # Actual trial
                this_trial_parameters = {'stim_left': this_trial_info['stim_left'],
                                         'stim_right': this_trial_info['stim_right'],
                                         'correct_response': this_trial_info['correct_stim_lr'],
                                         'block': block_n,
                                         'block_trial_ID': block_trial_ID,
                                         'p_win': [this_trial_info['p_win_left'], this_trial_info['p_win_right']],
                                         'cue': this_trial_info['cue']}

                these_phase_durations = self.phase_durations.copy()
                for phase_n in np.arange(8):
                    if 'phase_' + str(phase_n) in this_trial_info.keys():
                        these_phase_durations[phase_n] = this_trial_info['phase_' + str(phase_n)]

                # NB we stop the trial 0.5s before the start of the new trial, to allow sufficient computation time
                # for preparing the next trial. (but never below 0.1s)
                these_phase_durations[-1] = np.max([0.1, these_phase_durations[-1]-0.5])

                this_trial = self.run_trial(ID=int(this_trial_info.trial_ID),
                                            annotate=this_trial_info.annotate,
                                            parameters=this_trial_parameters,
                                            phase_durations=these_phase_durations,
                                            session=self,
                                            screen=self.screen)

                # run the prepared trial
                this_trial.run()

                # Record some stuff
                trial_handler.addData('rt', this_trial.response['rt'])
                trial_handler.addData('response', this_trial.response['button'])
                trial_handler.addData('deadline', this_trial.deadline)

                trial_handler.addData('points_earned', this_trial.points_earned)
                trial_handler.addData('outcome', this_trial.won)
                self.total_points += this_trial.points_earned
                self.total_trials += 1
                trial_handler.addData('total_points_earned', self.total_points)

                # absolute times since session start
                for time_name in ['start_time', 't_time', 'jitter_time_1', 'cue_time', 'jitter_time_2',
                                  'stimulus_time', 'selection_time', 'feedback_time', 'iti_time']:
                    trial_handler.addData(time_name, getattr(this_trial, time_name))

                # durations / time since actual start of the block. These are useful to create events-files later for
                #  convolving. Can also grab these from the eventArray though.
                # trial_handler.addData('trial_t_time_block_measured', this_trial.t_time - self.block_start_time)
                # trial_handler.addData('stimulus_onset_time_block_measured', this_trial.jitter_time -
                #                       self.block_start_time)
                # Counter-intuitive, but jitter_time is END of the jitter period = onset of stim

                if self.restart_block:
                    current_block_id -= 1
                    break

                # check for last trial?
                # if this_trial_info.trial_ID == self.design.trial_ID.max():
                #     self.stopped = True

                if self.stopped:
                    # out of trial
                    break

            # Save
            self.save_data(trial_handler, block_n)

            if self.stopped:
                # out of block
                break

            if not self.restart_block:
                if not self.stopped:
                    # end of block, show expected score
                    self.update_instruction_screen(end_of_block=True)
                    _ = InstructionTrial(ID=self.instruction_trial_n,
                                         parameters={},
                                         phase_durations=[0.5, 1000],
                                         session=self,
                                         screen=self.screen).run()
                    self.instruction_trial_n -= 1
            else:
                self.restart_block = False

        if not self.stopped:
            # End of experiment
            print('yes?')
            self.update_instruction_screen(end_of_session=True)
            _ = InstructionTrial(ID=self.instruction_trial_n,
                                 parameters={},
                                 phase_durations=[0.5, 1000],
                                 session=self,
                                 screen=self.screen).run()
            self.instruction_trial_n -= 1

        self.close()

if __name__ == '__main__':
    from psychopy import core

    # Load config
    from exptools.utils.config import ExpToolsConfig
    config = ExpToolsConfig()

    # Set-up monitor on the fly
    from psychopy import monitors
    my_monitor = monitors.Monitor(name=config.get('screen', 'monitor_name'))
    my_monitor.setSizePix(config.get('screen', 'size'))
    my_monitor.setWidth(config.get('screen', 'physical_screen_size')[0])
    my_monitor.setDistance(config.get('screen', 'physical_screen_distance'))
    my_monitor.saveMon()

    # sub_id = 'PRACTICE'
    # Set-up session
    sess = LearningSession('1',
                           1,
                           tr=0,
                           start_block=0,
                           config=config,
                           debug=True,
                           practice=True)

    # EMULATOR
    # from psychopy.hardware.emulator import launchScan
    # scanner_emulator = launchScan(win=sess.screen, settings={'TR': 0.5, 'volumes': 30000, 'sync': 't'}, mode='Test')

    # run
    sess.run()

    # Load & dump data
    import cPickle as pkl
    from pprint import pprint

    with open(sess.output_file + '_outputDict.pkl', 'r') as f:
        a = pkl.load(f)
    pprint(a)

    core.quit()

max_stars_repo_name: ACTrutti/IMCN-learningtask

max_issues_repo_name: ACTrutti/IMCN-learningtask

max_forks_repo_name: ACTrutti/IMCN-learningtask


text:
import numpy as np
import pandas as pd

class BayesianBandits(object):

    def __init__(self,
                 arms=[0,0.5],
                 window=50,
                 rolling=1,
                 prev=5):

        self.arms = arms
        self.a = {i:1 for i in range(len(self.arms))}
        self.b = {i:1 for i in range(len(self.arms))}
        self.window = window
        self.rolling = rolling
        self.arm = 0   #当前选择的arm
        self.value = self.arms[self.arm]    #当前选择的arm的值
        self.prev = prev

        self.choices = [self.arm]  #所有选过的arm
        self.data = [-99999]

    def sample(self):
        #先随机生成prev+1个

        if len(self.choices) > self.prev:
            samples = []
            #i对应于第i个arm
            for i in range(len(self.arms)):
                samples.append(np.random.beta(self.a[i], self.b[i]))

            best = np.argmax(samples)
            self.arm = best
        else:
            self.arm = int(np.random.uniform() * len(self.arms))

        self.value = self.arms[self.arm]
        self.choices.append(self.arm)

        return(self.value)

    def update_dists(self, prev):

        self.data.append(prev)

        choices = self.choices.copy()
        choices.append(9999)

        if len(choices) > self.prev:
            df = pd.DataFrame({'Choice': choices, 'Reward': self.data})

            df['Max_0'] =  df.Reward.rolling(5).max().shift(1)
            df['Max_Ahead'] = df.Reward.rolling(self.rolling).max().shift(-self.rolling)
            df['Feedback'] = df['Max_Ahead'] - df['Max_0']
            df = df[~df['Feedback'].isna()].reset_index(drop=True)
            df['Bern'] = (np.sign(df.Feedback)+1)/2
            df = df.iloc[1:, :].reset_index(drop=True)
            df = df.iloc[-self.window:].reset_index(drop=True)

            self.a = {i:(1+df[df['Choice'] ==i].sum()['Bern']) for i in range(len(self.arms))}
            self.b = {i:(1+df[df['Choice'] ==i].count()['Bern'] - df[df['Choice'] ==i].sum()['Bern']) for i in range(len(self.arms))}

if __name__ == '__main__':
    BB = BayesianBandits()
    samples = []
    for i in range(10):
        samples.append(BB.sample())
        BB.update_dists(5)

ext: py

reason: import numpy


text:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import requests
import time

df = pd.read_csv('covid 2.csv',sep=';', header=0)
cabecera = ["Fiebre","Cansancio","Tos","Ahogo","Dolorgarganta","Dolor","Congestion","Goteo","Diarrea","Infante","Menor","Joven","Adulto","Mayor","Mujer","Hombre","Trans","CS","CN","CNS","Resultado"]
df.columns = cabecera
df.head()

df.drop(["Trans"],axis=1,inplace=True)
df.head()

x1 = df.values[:,0]
x2 = df.values[:,1]
x3 = df.values[:,2]
x4 = df.values[:,3]
x5 = df.values[:,4]
x6 = df.values[:,5]
x7 = df.values[:,6]
x8 = df.values[:,7]
x9 = df.values[:,8]
x10 = df.values[:,9]
x11 = df.values[:,10]
x12 = df.values[:,11]
x13 = df.values[:,12]
x14 = df.values[:,13]
x15 = df.values[:,14]
x16 = df.values[:,15]
x17 = df.values[:,16]
x18 = df.values[:,17]
x19 = df.values[:,18]
y = df.values[:,19]
x0 = np.ones(x1.shape)

X = np.matrix([x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19]).T
features = np.matrix([x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17,x18,x19])
Y = np.matrix (y).T
labels = np.matrix (y)
X.shape

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/1.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z1=[]
for x in field_1:
   z1.append(x['field1'])
z1=z1[1]
z1=int(z1)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/2.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z2=[]
for x in field_1:
   z2.append(x['field2'])
z2=z2[1]
z2=int(z2)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/3.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z3=[]
for x in field_1:
   z3.append(x['field3'])
z3=z3[1]
z3=int(z3)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/4.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z4=[]
for x in field_1:
   z4.append(x['field4'])
z4=z4[1]
z4=int(z4)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/5.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z5=[]
for x in field_1:
   z5.append(x['field5'])
z5=z5[1]
z5=int(z5)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/6.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z6=[]
for x in field_1:
   z6.append(x['field6'])
z6=z6[1]
z6=int(z6)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/7.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z7=[]
for x in field_1:
   z7.append(x['field7'])
z7=z7[1]
z7=int(z7)

NEW_URL = 'https://api.thingspeak.com/channels/1402727/fields/8.json?api_key=4ULGHLRFIKX30FU8&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z8=[]
for x in field_1:
   z8.append(x['field8'])
z8=z8[1]
z8=int(z8)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/1.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z9=[]
for x in field_1:
   z9.append(x['field1'])
z9=z9[1]
z9=int(z9)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/2.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z10=[]
for x in field_1:
   z10.append(x['field2'])
z10=z10[1]
z10=int(z10)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/3.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z11=[]
for x in field_1:
   z11.append(x['field3'])
z11=z11[1]
z11=int(z11)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/4.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z12=[]
for x in field_1:
   z12.append(x['field4'])
z12=z12[1]
z12=int(z12)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/5.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z13=[]
for x in field_1:
   z13.append(x['field5'])
z13=z13[1]
z13=int(z13)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/6.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z14=[]
for x in field_1:
   z14.append(x['field6'])
 z14=z14[1]
 z14=int(z14)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/7.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z15=[]
for x in field_1:
   z15.append(x['field7'])
z15=z15[1]
z15=int(z15)

NEW_URL = 'https://api.thingspeak.com/channels/1402728/fields/8.json?api_key=7T4EMCJHQ6TTCYD2&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z16=[]
for x in field_1:
   z16.append(x['field8'])
z16=z16[1]
z16=int(z16)

NEW_URL = 'https://api.thingspeak.com/channels/1402729/fields/1.json?api_key=MV3TJWU6TZQCIV4O&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z17=[]
for x in field_1:
   z17.append(x['field1'])
z17=z17[1]
z17=int(z17)

NEW_URL = 'https://api.thingspeak.com/channels/1402729/fields/2.json?api_key=MV3TJWU6TZQCIV4O&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z18=[]
for x in field_1:
   z18.append(x['field2'])
z18=z18[1]
z18=int(z18)

NEW_URL = 'https://api.thingspeak.com/channels/1402729/fields/3.json?api_key=MV3TJWU6TZQCIV4O&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
z19=[]
for x in field_1:
   z19.append(x['field3'])
z19=z19[1]
z19=int(z19)

z0=1
X1=np.array([z0,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10,z11,z12,z13,z14,z15,z16,z17,z18,z19]).reshape(1,-1)
prediccion = clf.predict(X1)
print(prediccion)

NEW_URL = 'https://api.thingspeak.com/channels/1344250/fields/3.json?api_key=SKF96HHC05WSCCXA&results=2'

get_data = requests.get(NEW_URL).json()

channel_id=get_data['channel']['id']
field_1=get_data['feeds']
T=[]
for x in field_1:
   T.append(x['field3'])
T=T[1]
T=float(T)

NEW_URL1 = 'https://api.thingspeak.com/channels/1344250/fields/5.json?api_key=SKF96HHC05WSCCXA&results=2'

get_data1 = requests.get(NEW_URL1).json()

channel_id=get_data1['channel']['id']
field_5=get_data1['feeds']
P=[]
for x in field_5:
   P.append(x['field5'])
P=P[1]
P=float(P)

NEW_URL2 = 'https://api.thingspeak.com/channels/1344250/fields/7.json?api_key=SKF96HHC05WSCCXA&results=2'

get_data2 = requests.get(NEW_URL2).json()

channel_id=get_data2['channel']['id']
field_7=get_data2['feeds']
O=[]
for x in field_7:
   O.append(x['field7'])
O=O[1]
O=float(O)

if(prediccion==1 and T<37.5 and P<130 and O>=90):
     C=1
if((prediccion==1 and ((T>=37.5 and T<38.5) or (O<90 and O>=87) or (P>=130 and P<300))) or (prediccion==0 and T<37.5 and P<130 and O>=90)):
     C=2
if(prediccion==0 and ((T>=37.5 and T<39.5) or (O<90 and O>=84))):
     C=3
if(O<83 or P>=300 or P<=30 or T>=39.5):
     C=4
print(C)
enviar = requests.get("https://api.thingspeak.com/update?api_key=6TSBI19FP6L81E62&field8="+str(C))


text:
import numpy as np
import numpy.random as npr
import matplotlib.pyplot as plt
from scipy.stats import norm, poisson, binom, uniform

# Computes the percentage confidence intervals corresponding to one standard deviation from the mean for different distributions, to see how they differ
#      e.g. verify the well-known 1 SD = ~65% CL for a Gaussian distribution
#           and compare it to other distributions

# Choose the distribution of interest

npoints = 1E5

# Gaussian
setmean = 3
setsd = 1
x = npr.normal(setmean, setsd, npoints)
## Expected [mean-1SD,mean+1SD] CDF: The full integral (1) minus twice the tail integral between (mean + 1 SD) and infinity
expected = 1 - 2 * norm.cdf(x=setmean - setsd, loc=setmean, scale=setsd)

# Binomial
#n = 100
#p = 0.5
#x = npr.binomial(n, p, npoints)
#setmean = n*p
#setsd = np.sqrt(n*p*(1-p))
##   For one-tailed (e.g. Poisson distribution) expected [mean-1SD,mean+1SD] CDF: the tail integral from leftmost bound (setmean+setsd) minus tail integral from rightmost bound (setmean-setsd)
#expected = binom.cdf(setmean+setsd, n=n, p=p) - binom.cdf(setmean-setsd, n=n, p=p)

# Uniform
#a = 0
#b = 1
#x = npr.uniform(a,b,npoints)
#setmean = 0.5*(a+b)
#setsd = np.sqrt(1./12. * (b-a)**2)
#expected = uniform.cdf(setmean+setsd, loc=a, scale=(b-a)) - uniform.cdf(setmean-setsd, loc=a, scale=(b-a))

# Poisson
#mu = 10
#x = npr.poisson(mu,npoints)
#setmean = mu
#setsd = np.sqrt(mu)
#expected = poisson.cdf(setmean+setsd,setmean) - poisson.cdf(setmean-setsd,setmean)

mean = np.mean(x)
se = np.std(x)

inCI = 0.
for i in x:
    if i >= (mean - se) and i <= (mean + se):
        inCI += 1.

print "\nMean: {0:.3g}".format(setmean)
print "Standard error: {0:.3g}".format(setsd)
print "Percentage of points {0:.3g} sampled points within 1 SD of the mean ( [{1:.3g}, {2:.3g}] ):\n{3:.3g}%\n".format(
    npoints, mean - se, mean + se, 100 * inCI / float(len(x)))
print "Exact CDF within +/- 1 SD of mean:\n{0:.3g}%".format(100 * expected)

max_stars_repo_name: ilanfri/StatsML

max_issues_repo_name: ilanfri/StatsML

max_forks_repo_name: ilanfri/StatsML

reason: import numpy,from scipy


text:
#!/usr/bin/env python

import pylab

import numpy

from mvn import Mvn

from matplotlib.gridspec import GridSpec

def triax():
    """
    return 3 :py:class:`matplotlib.axes.AxesSubplot`, stacked horizontally
    """
    #create a figures of the apropriate size
    pylab.figure(1, figsize=(12,4))

    #use a grid of 4x4 cells
    grid = GridSpec(1,3)

    #create axis
    ax0  = pylab.subplot(grid[:,0])
    ax1  = pylab.subplot(grid[:,1])
    ax2  = pylab.subplot(grid[:,2])

    return ax0,ax1,ax2

def main():
    axes = triax()

    N=int(10.0**(1+2*numpy.random.rand()))

    for dims in range(3):
        ax = axes[dims]
        ax.set_title("Mvn.rand((%s,2))" % dims)
        for n in range(N):
            M = Mvn.rand((dims,2))
            M.plot(ax,facecolor = numpy.random.rand(3))

    pylab.tight_layout()
    pylab.show()

if __name__ == '__main__':
    main()

max_stars_repo_name: MarkDaoust/mvn

max_issues_repo_name: MarkDaoust/mvn

max_forks_repo_name: MarkDaoust/mvn


text:
import numpy as np
import pandas as pd

from calibration_functions import *

datas = []
metadatas = []

for folder in ["pw_nn4_bs", "pw_nn4_ce", "pw_nn6_logit_bs", "other", "pwlf"
               "pw_nn6_logit_ce"]:
    path = "results_" + folder
    print(folder)
    for cf in calibration_functions: #
        print(cf.__name__)
        for n_data in [1000, 3000, 10000]:

            for seed in [0,1,2,3,4]:

                dist = "uniform"
                for ce in np.arange(0.0, 0.1001, 0.005):
                    try:
                        data = pd.read_pickle(f"{path}/d{dist}_n{n_data}_s{seed}_{cf.__name__}_ce{ce}_{folder}.pkl")
                        metadata = pd.read_pickle(
                            f"{path}/META_d{dist}_n{n_data}_s{seed}_{cf.__name__}_ce{ce}_{folder}.pkl")

                        if folder == "pw_nn4_bs":
                            data.loc[data.binning.str[-2:] != "bs", "binning"] = data[data.binning.str[-2:] != "bs"].binning + "_bs"
                            metadata.loc[metadata.binning.str[-2:] != "bs", "binning"] = metadata[metadata.binning.str[-2:] != "bs"].binning + "_bs"
                        if folder == "pw_nn4_ce":
                            data.loc[data.binning.str[-2:] != "ce", "binning"] = data[data.binning.str[-2:] != "ce"].binning + "_ce"
                            metadata.loc[metadata.binning.str[-2:] != "ce", "binning"] = metadata[metadata.binning.str[-2:] != "ce"].binning + "_ce"
                        if folder == "pw_nn4_mono_bs":
                            data.loc[data.binning.str[-2:] != "bs", "binning"] = data[data.binning.str[-2:] != "bs"].binning + "_bs"
                            metadata.loc[metadata.binning.str[-2:] != "bs", "binning"] = metadata[metadata.binning.str[-2:] != "bs"].binning + "_bs"
                        if folder == "pw_nn4_mono_ce":
                            data.loc[data.binning.str[-2:] != "ce", "binning"] = data[data.binning.str[-2:] != "ce"].binning + "_ce"
                            metadata.loc[metadata.binning.str[-2:] != "ce", "binning"] = metadata[metadata.binning.str[-2:] != "ce"].binning + "_ce"
                        if folder == "pw_nn4_mono_logit_bs":
                            data.loc[data.binning.str[-2:] != "bs", "binning"] = data[data.binning.str[-2:] != "bs"].binning + "_bs"
                            metadata.loc[metadata.binning.str[-2:] != "bs", "binning"] = metadata[metadata.binning.str[-2:] != "bs"].binning + "_bs"
                        if folder == "pw_nn4_mono_logit_ce":
                            data.loc[data.binning.str[-2:] != "ce", "binning"] = data[data.binning.str[-2:] != "ce"].binning + "_ce"
                            metadata.loc[metadata.binning.str[-2:] != "ce", "binning"] = metadata[metadata.binning.str[-2:] != "ce"].binning + "_ce"

                        datas.append(data)
                        metadatas.append(metadata)
                    except:
                        print(f"{path}/d{dist}_n{n_data}_s{seed}_{cf.__name__}_ce{ce}_{folder}.pkl")

                dist = "beta"
                for ce in np.arange(0.0, 0.0301, 0.005):
                    try:
                        data = pd.read_pickle(f"{path}/d{dist}_n{n_data}_s{seed}_{cf.__name__}_ce{ce}_{folder}.pkl")
                        metadata = pd.read_pickle(
                            f"{path}/META_d{dist}_n{n_data}_s{seed}_{cf.__name__}_ce{ce}_{folder}.pkl")

                        if folder == "pw_nn4_bs":
                            data.loc[data.binning.str[-2:] != "bs", "binning"] = data[data.binning.str[-2:] != "bs"].binning + "_bs"
                            metadata.loc[metadata.binning.str[-2:] != "bs", "binning"] = metadata[metadata.binning.str[-2:] != "bs"].binning + "_bs"
                        if folder == "pw_nn4_ce":
                            data.loc[data.binning.str[-2:] != "ce", "binning"] = data[data.binning.str[-2:] != "ce"].binning + "_ce"
                            metadata.loc[metadata.binning.str[-2:] != "ce", "binning"] = metadata[metadata.binning.str[-2:] != "ce"].binning + "_ce"
                        if folder == "pw_nn4_mono_bs":
                            data.loc[data.binning.str[-2:] != "bs", "binning"] = data[data.binning.str[-2:] != "bs"].binning + "_bs"
                            metadata.loc[metadata.binning.str[-2:] != "bs", "binning"] = metadata[metadata.binning.str[-2:] != "bs"].binning + "_bs"
                        if folder == "pw_nn4_mono_ce":
                            data.loc[data.binning.str[-2:] != "ce", "binning"] = data[data.binning.str[-2:] != "ce"].binning + "_ce"
                            metadata.loc[metadata.binning.str[-2:] != "ce", "binning"] = metadata[metadata.binning.str[-2:] != "ce"].binning + "_ce"
                        if folder == "pw_nn4_mono_logit_bs":
                            data.loc[data.binning.str[-2:] != "bs", "binning"] = data[data.binning.str[-2:] != "bs"].binning + "_bs"
                            metadata.loc[metadata.binning.str[-2:] != "bs", "binning"] = metadata[metadata.binning.str[-2:] != "bs"].binning + "_bs"
                        if folder == "pw_nn4_mono_logit_ce":
                            data.loc[data.binning.str[-2:] != "ce", "binning"] = data[data.binning.str[-2:] != "ce"].binning + "_ce"
                            metadata.loc[metadata.binning.str[-2:] != "ce", "binning"] = metadata[metadata.binning.str[-2:] != "ce"].binning + "_ce"

                        datas.append(data)
                        metadatas.append(metadata)

                    except:
                        print(f"{path}/d{dist}_n{n_data}_s{seed}_{cf.__name__}_ce{ce}_{folder}.pkl")

df_all_metadata = pd.concat(metadatas)
df_all_data = pd.concat(datas)

df_all_data.to_pickle("DATANAME.pkl", protocol=4)
df_all_metadata.to_pickle("METADATANAME.pkl", protocol=4)

#####

from os import walk
import re

_, _, filenames = next(walk("./"))

times_taken = np.zeros(shape=(10, 10))

for f_name in filenames:

    if f_name[-3:] != "out":
        continue

    with open("./" + f_name) as f:
        data = f.read()
        der_done = [m.start() for m in re.finditer('Derivate done in', data)]

        if data.find("Traceback") != -1:
            print("Error in")
            print(f_name)

        for done_idx, done in enumerate(der_done):
            time_taken = float(data[done + 17:done + 25])
            times_taken[len(der_done)][done_idx] += time_taken

print(times_taken)
print(np.sum(times_taken) / 60 / 60)
print(np.sum(times_taken, axis=1) / 60 / 60)
print(np.sum(times_taken, axis=0) / 60 / 60)


text:
import os
import math
import numpy as np

root_path = '/home/project/I3D/data/Kinetics/train_256'
num_frames = 16
data_list = []
id_list = []
label_list = []
erro_data = []
label = 0
id = 0

for file_path in sorted(os.listdir(root_path)):
    for video_path in sorted(os.listdir(os.path.join(root_path, file_path))):
        frame_num = len(os.listdir(os.path.join(root_path, file_path, video_path)))
        print('Process: ' + os.path.join(root_path, file_path, video_path), frame_num)
        if frame_num > 0:
            data_list.append(os.path.join(root_path, file_path, video_path))
            id_list.append(id)
            label_list.append(label)
            id += 1
        else:
            erro_data.append(os.path.join(root_path, file_path, video_path))
    label += 1
    if label == 100:
        break
print(erro_data)
print(len(data_list))
print(len(id_list))
print(len(label_list))

np.save('./train_data_list_%d.npy'%label, data_list)
np.save('./train_label_list_%d.npy'%label, label_list)


text:
import numpy as np
import pyLDAvis
#from biterm.cbtm import oBTM
from biterm.btm import oBTM
from sklearn.feature_extraction.text import CountVectorizer
from biterm.utility import vec_to_biterms, topic_summuary

if __name__ == "__main__":

    texts = open('./data/reuters.titles').read().splitlines()

    # vectorize texts
    vec = CountVectorizer(stop_words='english')
    X = vec.fit_transform(texts).toarray()

    # get vocabulary
    vocab = np.array(vec.get_feature_names())

    # get biterms
    biterms = vec_to_biterms(X)

    # create btm
    btm = oBTM(num_topics=20, V=vocab)

    print("\n\n Train Online BTM ..")
    for i in range(0, len(biterms), 100): # prozess chunk of 200 texts
        biterms_chunk = biterms[i:i + 100]
        btm.fit(biterms_chunk, iterations=50)
    topics = btm.transform(biterms)

    print("\n\n Visualize Topics ..")
    vis = pyLDAvis.prepare(btm.phi_wz.T, topics, np.count_nonzero(X, axis=1), vocab, np.sum(X, axis=0))
    pyLDAvis.save_html(vis, './vis/online_btm.html')

    print("\n\n Topic coherence ..")
    topic_summuary(btm.phi_wz.T, X, vocab, 10)

    print("\n\n Texts & Topics ..")
    for i in range(len(texts)):
        print("{} (topic: {})".format(texts[i], topics[i].argmax()))

max_stars_repo_name: markoarnauto/biterm

max_issues_repo_name: markoarnauto/biterm

max_forks_repo_name: markoarnauto/biterm


text:
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import filecmp
import os
import random
import shutil

from sagemaker.tensorflow.estimator import Tensorboard

def create_test_directory(directory, variable_content="hello world"):
    """Create dummy data for testing Tensorboard._sync_directories with the
    following structure:

    <directory>
    |_ child_directory
        |_ hello.txt
    |_ foo1.txt
    |_ foo2.txt

    Args:
        directory (str): The path to a directory to create with fake files
        variable_content (str): Content to put in one of the files
    """
    child_dir = os.path.join(directory, 'child_directory')
    os.mkdir(child_dir)
    with open(os.path.join(directory, 'foo1.txt'), 'w') as f:
        f.write('bar1')
    with open(os.path.join(directory, 'foo2.txt'), 'w') as f:
        f.write('bar2')
    with open(os.path.join(child_dir, 'hello.txt'), 'w') as f:
        f.write(variable_content)

def same_dirs(a, b):
    """Check that structure and files are the same for directories a and b

    Args:
        a (str): The path to the first directory
        b (str): The path to the second directory
    """
    comp = filecmp.dircmp(a, b)
    common = sorted(comp.common)
    left = sorted(comp.left_list)
    right = sorted(comp.right_list)
    if left != common or right != common:
        return False
    if len(comp.diff_files):
        return False
    for subdir in comp.common_dirs:
        left_subdir = os.path.join(a, subdir)
        right_subdir = os.path.join(b, subdir)
        return same_dirs(left_subdir, right_subdir)
    return True

def test_to_directory_doesnt_exist():
    with Tensorboard._temporary_directory() as from_dir:
        create_test_directory(from_dir)
        to_dir = './not_a_real_place_{}'.format(random.getrandbits(64))
        Tensorboard._sync_directories(from_dir, to_dir)
        assert same_dirs(from_dir, to_dir)
        shutil.rmtree(to_dir)

def test_only_root_of_to_directory_exists():
    with Tensorboard._temporary_directory() as from_dir:
        with Tensorboard._temporary_directory() as to_dir:
            create_test_directory(from_dir)
            assert not same_dirs(from_dir, to_dir)
            Tensorboard._sync_directories(from_dir, to_dir)
            assert same_dirs(from_dir, to_dir)

def test_files_are_overwritten_when_they_already_exist():
    with Tensorboard._temporary_directory() as from_dir:
        with Tensorboard._temporary_directory() as to_dir:
            create_test_directory(from_dir)
            create_test_directory(to_dir, "foo bar")
            assert not same_dirs(from_dir, to_dir)
            Tensorboard._sync_directories(from_dir, to_dir)
            assert same_dirs(from_dir, to_dir)

max_stars_repo_name: besirkurtulmus/sagemaker-python-sdk

max_issues_repo_name: besirkurtulmus/sagemaker-python-sdk

max_forks_repo_name: besirkurtulmus/sagemaker-python-sdk

reason: from sage


text:
"""Broadcast any number of arrays against each other."""
import numpy
import numpoly

from .common import implements

@implements(numpy.broadcast_arrays)
def broadcast_arrays(*args, **kwargs):
    """
    Broadcast any number of arrays against each other.

    Args:
        args (numpoly.ndpoly):
            The arrays to broadcast.
        subok (bool):
            If True, then sub-classes will be passed-through, otherwise the
            returned arrays will be forced to be a base-class array (default).

    Returns:
        (List[numpoly.ndpoly]):
            These arrays are views on the original arrays.  They are typically
            not contiguous.  Furthermore, more than one element of a
            broadcasted array may refer to a single memory location. If you
            need to write to the arrays, make copies first. While you can set
            the ``writable`` flag True, writing to a single output value may
            end up changing more than one location in the output array.

    Examples:
        >>> poly1 = numpoly.monomial(3).reshape(1, 3)
        >>> poly1
        polynomial([[1, q, q**2]])
        >>> poly2 = numpoly.monomial(2).reshape(2, 1)
        >>> poly2
        polynomial([[1],
                    [q]])
        >>> result1, result2 = numpoly.broadcast_arrays(poly1, poly2)
        >>> result1
        polynomial([[1, q, q**2],
                    [1, q, q**2]])
        >>> result2
        polynomial([[1, 1, 1],
                    [q, q, q]])

    """
    args = [numpoly.aspolynomial(arg) for arg in args]
    results = numpy.broadcast_arrays(*[arg.values for arg in args], **kwargs)
    return [numpoly.aspolynomial(result, names=arg.indeterminants)
            for result, arg in zip(results, args)]

max_stars_repo_name: FredrikMeyer/numpoly

max_issues_repo_name: FredrikMeyer/numpoly

max_forks_repo_name: FredrikMeyer/numpoly


text:
from types import SimpleNamespace
from typing import Any, Callable, Dict, List, Optional, Tuple

import numpy as np
import torch
from torch.distributions import constraint_registry, constraints

ModelBlockFunctionType = Callable[[SimpleNamespace, SimpleNamespace, torch.Tensor], Any]
InternalParametersType = Dict[
    str, Tuple[Tuple[int], torch.distributions.Transform, slice]
]
DataType = Dict[str, Any]
ParametersType = Dict[str, torch.Tensor]
ParametersNumPyType = Dict[str, np.ndarray]

class Model:
    """Holds the probabilistic model.

    This class holds knowledge about the parameters of the model and
    how to calculate the unnormalized log probability of the model given
    some data.

    Attributes:
        parameters: Parameter names (keys) and internal information
            (sizes, transformations, and slice in packed parameters tensor).
        model_block_f: List of function that define the unnormalized log
            probability.
        number_of_parameters: Number of parameters.
        dtype: Dtype (for torch) for the parameters.
        device: Device (for torch) for holding the parameters.
    """

    def __init__(
        self,
        dtype: torch.dtype = torch.float,
        device: torch.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu"
        ),
    ):
        """Initialize the model object.

        Args:
            dtype: Dtype (for torch) for the parameters.
            device: Device (for torch) for holding the parameters.
        """
        self.parameters: InternalParametersType = {}
        self.model_block_f: List[ModelBlockFunctionType] = []
        self.number_of_parameters: int = 0
        self.dtype: torch.dtype = dtype
        self.device: torch.device = device

    def log_prob(self, parameters: ParametersType, data: DataType) -> torch.Tensor:
        """Calculates the unnormalized log probability in the constrained space.

        This function does not apply any transformations. The parameters are assumed
        to have appropriate values within their constraints.

        Args:
            parameters: Parameters of the model.
            data: Data for the model.

        Returns:
            The unnormalized log probability (without accounting for any transformations).
        """
        wrapped_parameters = SimpleNamespace(**parameters)
        wrapped_data = SimpleNamespace(**data)
        logp = torch.zeros((), dtype=self.dtype, device=self.device)
        for i in range(len(self.model_block_f)):
            self.model_block_f[i](wrapped_data, wrapped_parameters, logp)
        return logp

    def log_prob_packed(
        self, packed_parameters: torch.Tensor, data: DataType
    ) -> torch.Tensor:
        """Calculates the unnormalized log probability in the unconstrained space.

        Accounts for the Jacobian of the transformations from the unconstrained
        space to the constrained values.

        Args:
            packed_parameters: Parameters of the model in unconstrained space.
            data: Data for the model.

        Returns:
            The unnormalized log probability (accounting for transformations).
        """
        parameters, log_j = self.unpack_and_get_log_j(packed_parameters)
        return log_j + self.log_prob(parameters, data)

    def model_block(
        self, block: ModelBlockFunctionType
    ) -> Callable[[DataType, ParametersType, torch.Tensor], Any]:
        """Registers a model block to the model.

        Each registered block will be called in the calculation of the unnormalized
        log probability of the model. The data and parameters arguments of the block
        are assumed to be Dicts and are automatically wrapped in SimpleNamespaces, so
        they should be referred to in the model block function using dot-notation
        (e.g., `data.y`, not `data["y"]`).

        Args:
            block: Function to register as model block. The function should take
                three arguments - data, parameters, logp - and increment logp
                according to the unnormalized log probability of this block. The
                model block function can return anything - the return value is not
                used by the library.

        Returns:
            The model block with automatic wrapping of data Dict and parameters Dict in
            SimpleNamespaces.
        """
        self.model_block_f.append(block)

        # Wrap the original function d and p args in SimpleNamespaces
        # (note: the type of the wrapped function has Dicts instead SimpleNamespaces).
        def wrapped_block(d: DataType, p: ParametersType, logp: torch.Tensor):
            return block(SimpleNamespace(**d), SimpleNamespace(**p), logp)

        return wrapped_block

    def param(
        self,
        name: str,
        size: Tuple[int],
        constraint: Optional[torch.distributions.constraints.Constraint],
    ):
        """Registers a parameter for the model.

        Args:
            name: Name of the parameter.
            size: Size of the parameter.
            constraint: Constraint for the parameter (e.g., positivity).
        """
        ne = int(np.prod(list(size)))
        pack_indices = slice(self.number_of_parameters, self.number_of_parameters + ne)
        self.number_of_parameters += ne
        if constraint is None:
            constraint = constraints.real
        transformation = constraint_registry.biject_to(constraint)
        self.parameters[name] = (size, transformation, pack_indices)

    def unpack(
        self, packed_parameters: torch.Tensor, skip_tf: bool = False
    ) -> ParametersType:
        """Unpacks parameters from unconstrained vector to constrained values.

        Args:
            packed_parameters: Vector containing the flattened unconstrained
                parameters.
            skip_tf: Skip transformation to constrained space.

        Returns:
            Parameter dictionary, with values in the constrained space (if
            skip_tf is False, default) or unconstrained space (if skip_tf is
            True).
        """
        parameters, _ = self.unpack_and_get_log_j(
            packed_parameters, skip_tf, calculate_log_jacobian=False
        )
        return parameters

    def unpack_and_get_log_j(
        self,
        packed_parameters: torch.Tensor,
        skip_tf: bool = False,
        calculate_log_jacobian: bool = True,
    ) -> Tuple[ParametersType, torch.Tensor]:
        """Unpacks parameters, returning also log absolute determinant of the Jacobian.

        Unpacks parameters from unconstrained vector to constrained values and computes the
        log absolute determinant of the Jacobian of the transformations.

        Args:
            packed_parameters: Vector containing the flattened unconstrained
                parameters.
            skip_tf: Skip transformation to constrained space.
            calculate_log_jacobian: Whether to calculate the Jacobian term.

        Returns:
            Tuple containing the parameters Dict and the log absolute determinant of the
            Jacobian of the transformations (or zero if calculate_log_jacobian is False).
        """
        if skip_tf and calculate_log_jacobian:
            raise ValueError(
                "Skipping transformation and returning log Jacobian is not supported"
            )

        parameters = {}
        log_j = torch.zeros((), dtype=self.dtype, device=self.device)

        for key, (size, tf, pack_indices) in self.parameters.items():
            untf_parameter = packed_parameters[pack_indices].view(*size)
            if skip_tf:
                parameters[key] = untf_parameter
            else:
                parameters[key] = tf(untf_parameter)
            if calculate_log_jacobian:
                log_j += tf.log_abs_det_jacobian(untf_parameter, parameters[key]).sum()
        return parameters, log_j

    def pack(self, parameters: ParametersType) -> torch.Tensor:
        """Packs parameters and transforms to unconstrained space.

        Args:
            parameters: Dict of constrained parameters, assumed to be in the same
                order as the parameters registered in the model.

        Returns:
            Vector containing transformed and flattened (packed) parameters.
        """
        packed_parameters = torch.stack(
            [
                tf.inv(val).view(-1)
                for (val, (_, tf, _)) in zip(
                    parameters.values(), self.parameters.values()
                )
            ]
        )
        return packed_parameters

max_stars_repo_name: to-mi/smallx

max_issues_repo_name: to-mi/smallx

max_forks_repo_name: to-mi/smallx


text:
import numpy as np
import scipy as sp
import scipy.ndimage as ndimage
import math
from skimage import exposure,filters
from matplotlib import pyplot as plt
import cv2
import matplotlib.pyplot as plt
from skimage.transform import hough_circle, hough_circle_peaks,hough_ellipse
from skimage.feature import canny
from skimage.draw import circle_perimeter
from skimage.segmentation import active_contour
from skimage.filters import gaussian
import argparse

def resize(img):
	width = 1024
	height = 720
	#####
	return cv2.resize(img,(width,height), interpolation = cv2.INTER_CUBIC)

def rgb2Blue(img):
	b,g,r = cv2.split(img)
	return b

def rgb2Red(img):
	b,g,r = cv2.split(img)
	return r

def rgb2Green(img):
	b,g,r = cv2.split(img)
	return g

def rgb2Gray(img):
	return cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

def rgb2lab(img):
	return cv2.cvtColor(img,cv2.COLOR_BGR2LAB)

#############preprocess###########
##Image is split on B,G,R channel
##Red channel is isolated
##Smoothing over the red channel is applied
##Sharpening and Equalization to te image are applied
##A morph closing is applied to remove artifacts
##################################
def preprocess(img):
	b,g,r = cv2.split(img)
	gray = rgb2Red(img)
	gray_blur = cv2.GaussianBlur(gray, (5,5), 0)
	gray = cv2.addWeighted(gray, 1.5, gray_blur, -0.5, 0, gray)
	kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(31,31))
	gray = ndimage.grey_closing(gray,structure=kernel)
	gray = cv2.equalizeHist(gray)
	#gray = cv2.GaussianBlur(gray, (5,5), 0)
	#gray = cv2.medianBlur(gray,9)

	#gray_la= ndimage.laplace(gray)
	#gray = cv2.GaussianBlur(gray,(3,3),0)
	#gray_eq = cv2.equalizeHist(gray)

	return gray

#############getROI##############
##Image is resized
##We take green channel and smooth it
##Opening is done to remove artifacts, in order to preserve only BRIGHTEST elements
##Now we get the most bright pixel position
##We return that position in a 110x110 window
##It is actually a simple way to detect the optic disc, but it works so..
##################################
def getROI(image):
	image_resized = resize(image)
	b,g,r = cv2.split(image_resized)
	g = cv2.GaussianBlur(g,(15,15),0)
	kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15,15))
	g = ndimage.grey_opening(g,structure=kernel)
	(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(g)

	x0 = int(maxLoc[0])-110
	y0 = int(maxLoc[1])-110
	x1 = int(maxLoc[0])+110
	y1 = int(maxLoc[1])+110

	return image_resized[y0:y1,x0:x1]

def getValue(img):
	shapeRow = img.shape[0]
	shapeCol = img.shape[1]
	x = 0
	y = 0
	acu = 0
	maxloc = []
	for i in range(shapeRow):
		for j in range(shapeCol):
			(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(img[i-15:j-15,i+15:j+15])
			value = maxVal
			if value > acu:
				acu = value
				maxloc = maxLoc
	return maxloc

def kmeans(img):
	## K-Means
	criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
	flags = cv2.KMEANS_RANDOM_CENTERS
	roi = img
	X = roi.reshape((-1, 1))
	X = np.float32(X)
	compactness,labels,centers = cv2.kmeans(X,3,None,criteria,10,flags)

	result = np.choose(labels, centers)
	result.shape = X.shape

	centers = np.uint8(centers)
	res= centers[labels.flatten()]
	res2 = res.reshape((roi.shape))
	return res2

def checkSide(img):
	shapeRow = img.shape[0]
	shapeCol = img.shape[1]
	if cv2.countNonZero(img[:,0:int(shapeCol/2)]) > cv2.countNonZero(img[:,int(shapeCol/2):shapeCol]):
		return True
	else:
		return False

def checkHigh(img):
	shapeRow = img.shape[0]
	shapeCol = img.shape[1]
	if cv2.countNonZero(img[0:int(shapeRow/2),:]) > cv2.countNonZero(img[int(shapeRow/2):shapeRow,:]):
		return True
	else:
		return False

def canny(img,sigma):
	v = np.mean(img)
	sigma = sigma
	lower = int(max(0, (1.0 - sigma) * v))
	upper = int(min(255, (1.0 + sigma) * v))
	edged = cv2.Canny(img, lower, upper)
	return edged

def hough(edged,limm,limM):
	hough_radii = np.arange(limm, limM, 1)
	hough_res = hough_circle(edged, hough_radii)
	return hough_circle_peaks(hough_res, hough_radii,total_num_peaks=1)

parser = argparse.ArgumentParser(description='Optic disc segmentation.')
parser.add_argument("-f", dest='file', action='store', type=str, help='The image to process.')
args = parser.parse_args()

print(args.accumulate(args.integers))

############################
##Here we start the process
############################

image = cv2.imread(args.file)
roi = getROI(image)
preprocessed_roi = preprocess(roi)

#im2, contours, hierarchy = cv2.findContours(segmented, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
##We get our ROI and then we preprocess it.
##After preprocess, we apply a canny border algorithm
##We dilate the borders in order to make it easy to detect with hough

edged = canny(preprocessed_roi,0.22)
kernel = np.ones((3,3),np.uint8)
edged = cv2.dilate(edged,kernel,iterations=3)
accums, cx, cy, radii = hough(edged,55,80)
for center_y, center_x, radius in zip(cy, cx, radii):
	circy, circx = circle_perimeter(center_y, center_x, radius)
	try:
		roi[circy, circx] = (220, 20, 20)
	except :
		continue
#
init = np.array([circx, circy]).T

cv2.imshow('Pre-Processed Region of Interest '+str(i),preprocessed_roi)
cv2.imshow('Region of Interest '+str(i),roi)

############################

while(1):
	k = cv2.waitKey(37)
	if k == 37:
		break

reason: import numpy,import scipy


text:
# Lint as: python3
#     Copyright 2020 Google LLC
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#         https://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.
"""Provides access to the Smart Buidlings dataset for Anomaly Detection."""
from madi.datasets.base_dataset import BaseDataset
import numpy as np
import pandas as pd
import tensorflow as tf

_DATA_FILE = "madi/datasets/data/anomaly_detection_sample_1577622599.csv"
_README_FILE = "madi/datasets/data/anomaly_detection_sample_1577622599_README.md"

class SmartBuildingsDataset(BaseDataset):
  """Smart Buildings data set for Multivariate Anomaly Detection."""

  def __init__(self,
               datafilepath: str = _DATA_FILE,
               readmefilepath: str = _README_FILE):
    self._sample = self._load_data_file(datafilepath)
    self._description = self._load_readme(readmefilepath)

  @property
  def sample(self) -> pd.DataFrame:
    return self._sample

  @property
  def name(self) -> str:
    return "smart_buildings"

  @property
  def description(self) -> str:
    return self._description

  def _load_data_file(self, datafile: str) -> pd.DataFrame:
    sample = None
    if not tf.io.gfile.exists(datafile):
      raise AssertionError("{} does not exist".format(datafile))
    with tf.io.gfile.GFile(datafile) as csv_file:
      sample = pd.read_csv(csv_file, header="infer", index_col=0)

    sample = sample.reindex(np.random.permutation(sample.index))
    return sample

max_stars_repo_name: pganssle-google/madi

max_issues_repo_name: pganssle-google/madi

max_forks_repo_name: pganssle-google/madi