class ProfilingPanel(Panel):
    """
    Panel that displays profiling information.
    """
    title = _('Profiling')

    template = 'debug_toolbar_line_profiler/panels/profiling.html'

    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, '__code__'):
            return
        self.line_profiler.add_function(func)
        for subfunc in getattr(func, 'profile_additional', []):
            self._unwrap_closure_and_profile(subfunc)
        if func.__closure__:
            for cell in func.__closure__:
                target = cell.cell_contents
                if inspect.isclass(target) and View in inspect.getmro(target):
                    for name, value in inspect.getmembers(target):
                        if name[0] != '_' and inspect.ismethod(value):
                            self._unwrap_closure_and_profile(value)
                else:
                    self._unwrap_closure_and_profile(target)

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        self.line_profiler = LineProfiler()
        self._unwrap_closure_and_profile(view_func)
        self.line_profiler.enable_by_count()
        out = self.profiler.runcall(view_func, *args, **view_kwargs)
        self.line_profiler.disable_by_count()
        return out

    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        func_list.append(func)
        func.has_subfuncs = False
        if func.depth < max_depth:
            for subfunc in func.subfuncs():
                if (subfunc.stats[3] >= cum_time or
                        (hasattr(self.stats, 'line_stats') and
                            (subfunc.func in self.stats.line_stats.timings))):
                    func.has_subfuncs = True
                    self.add_node(func_list, subfunc, max_depth, cum_time=cum_time)

    def process_response(self, request, response):
        if not hasattr(self, 'profiler'):
            return None
        # Could be delayed until the panel content is requested (perf. optim.)
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()

        root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)

        func_list = []
        self.add_node(func_list, root, 10, root.stats[3] / 8)

        self.record_stats({'func_list': func_list})
Example #2
0
    df["oversample"] = params.simulator.oversample
    if params.opt_det is not None:
        df["opt_det"] = params.opt_det
    df["stage1_refls"] = stg1_refls
    df["stage1_output_img"] = stg1_img_path

    df.to_pickle(pandas_path)
    return df


if __name__ == '__main__':
    from dials.util import show_mail_on_error

    with show_mail_on_error():
        script = Script()
        RUN = script.run
        lp = None
        if LineProfiler is not None and script.params.profile:
            lp = LineProfiler()
            lp.add_function(hopper_utils.model)
            lp.add_function(hopper_utils.target_func)
            RUN = lp(script.run)
        elif script.params.profile:
            print("Install line_profiler in order to use logging: libtbx.python -m pip install line_profiler")

        RUN()

        if lp is not None:
            stats = lp.get_stats()
            hopper_utils.print_profile(stats, ["model", "target_func"])
class ProfilingDebugPanel(DebugPanel):
    """
    Panel that displays the Django version.
    """
    name = 'Profiling'
    template = 'debug_toolbar/panels/profiling.html'
    has_content = True
    
    def nav_title(self):
        return _('Profiling')
    
    def url(self):
        return ''
    
    def title(self):
        return _('Profiling')
    
    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, 'func_code'):
            return
        self.line_profiler.add_function(func)
        if func.func_closure:
            for cell in func.func_closure:
                if hasattr(cell.cell_contents, 'func_code'):
                    self._unwrap_closure_and_profile(cell.cell_contents)
    
    def process_view(self, request, view_func, view_args, view_kwargs):
        __traceback_hide__ = True
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        if DJ_PROFILE_USE_LINE_PROFILER:
            self.line_profiler = LineProfiler()
            self._unwrap_closure_and_profile(view_func)
            self.line_profiler.enable_by_count()
            out = self.profiler.runcall(view_func, *args, **view_kwargs)
            self.line_profiler.disable_by_count()
        else:
            self.line_profiler = None
            out = self.profiler.runcall(view_func, *args, **view_kwargs)
        return out
    
    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        func_list.append(func)
        func.has_subfuncs = False
        if func.depth < max_depth:
            for subfunc in func.subfuncs():
                if (subfunc.stats[3] >= cum_time or
                   (hasattr(self.stats, 'line_stats') and
                   (subfunc.func in self.stats.line_stats.timings))):
                    func.has_subfuncs = True
                    self.add_node(func_list, subfunc, max_depth, cum_time=cum_time)
    
    def process_response(self, request, response):
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        if DJ_PROFILE_USE_LINE_PROFILER:
            self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()
        
        root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)
        
        func_list = []
        self.add_node(func_list, root, 10, root.stats[3]/8)
        
        self.stats_record({'func_list': func_list})
Example #4
0
def main(program_dir, scale_factor, wta_k, score_type):
    # num_features_list stores the number of features detected in each frame.
    # time stores the time taken in miliseconds to get features in each frame, by the function getKeyPoints
    # num_frames stores the number of frames
    num_features_list = []
    time = []
    num_frames = 0

    # Created the ORB feature matcher object, with the requisite arguments (some default some taken from command line)
    orb = cv2.ORB_create(nfeatures=10000,
                         edgeThreshold=50,
                         patchSize=50,
                         fastThreshold=20,
                         scaleFactor=scale_factor,
                         WTA_K=wta_k,
                         scoreType=score_type)

    # Place your data set in the BENCHMARKS/ORB_feature_detector_algorithm/datasets directory. Make sure it is encoded in FFMPEG
    # format for OpenCV to use it.
    cap = cv2.VideoCapture(program_dir + '/datasets/Raw.mp4')
    if cap.isOpened() == False:
        print("Error opening video file.")
        return

    while cap.isOpened():
        ret, frame = cap.read()
        if np.shape(frame) == ():
            break
        num_frames = num_frames + 1

        # This section computes the time taken in each function call of getKeyPoints. Instead of calling getKeyPoints directly,
        # you call the function through a wrapper which measures the time taken for it's execution.
        profile = LineProfiler()
        profile_wrapper = profile(getKeyPoints)
        kp = profile_wrapper(orb, frame)

        # This part is a bit tricky to understand. Basically, the data variable here is a dictionary. Here is an example of
        # this data variable {('ORB_feature.py', 6, 'getKeyPoints'): [(7, 1, 90299), (8, 1, 27768), (9, 1, 2)]}
        # I also don't know why it is this complicated :((
        # The keys consist of tuples of the type ('path/to/python/program', line_number_of_function_definition, 'function_name').
        # The items consist of a list of tuples, each tuple corresponding to each line in the function definition
        # The tuple is of the type (line_number, number_of_times_the_line_ran, time_taken_in_microseconds)
        # Since I am benchmarking only one function, I used the first and only key in list of keys (data.keys()) as my key,
        # and added the time taken at each line using the last index of tuples corresponding to each line to get the total
        # time of execution of the function. I then stored it in the time list, in ms.
        data = profile.get_stats().timings
        time_taken = data[list(data.keys())[0]][0][2] + data[list(
            data.keys())[0]][1][2] + data[list(data.keys())[0]][2][2]
        time.append(time_taken / 1000.0)
        num_features_list.append(len(kp))

        frame = cv2.drawKeypoints(frame, kp, None, color=(0, 255, 0), flags=0)

    cap.release()

    frame_list = range(1, num_frames + 1)

    # This part prepares plots of the requisite data, and saves it in an SVG file, which is unique to the parameters passed.
    plt.figure(figsize=(25, 10))
    plt.plot(frame_list, num_features_list)
    plt.xlabel('Frame Number')
    plt.ylabel('Detected Features')
    plt.title('Plot of Detected Features in Each Frame')
    plt.tight_layout()
    plt.savefig(program_dir + '/plots/features_number_plot_' +
                str(scale_factor) + '_' + str(wta_k) + '_' +
                ('HARRIS' if score_type == cv2.ORB_HARRIS_SCORE else 'FAST') +
                '.svg')

    plt.clf()

    plt.figure(figsize=(25, 10))
    plt.plot(frame_list, time)
    plt.xlabel('Frame Number')
    plt.ylabel('Time Taken for Identifying Features(ms)')
    plt.title('Plot of Time Taken for Identifying Features in Each Frame')
    plt.tight_layout()
    plt.savefig(program_dir + '/plots/time_plot_' + str(scale_factor) + '_' +
                str(wta_k) + '_' +
                ('HARRIS' if score_type == cv2.ORB_HARRIS_SCORE else 'FAST') +
                '.svg')

    plt.close('all')

    # Returns the average time taken/frame and the average number of features detected/frame
    return sum(time) / len(time), sum(num_features_list) / len(
        num_features_list)
class ProfilingPanel(Panel):
    """
    Panel that displays profiling information.
    """
    title = _('Profiling')

    template = 'debug_toolbar_line_profiler/panels/profiling.html'

    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, '__code__'):
            return
        self.line_profiler.add_function(func)
        for subfunc in getattr(func, 'profile_additional', []):
            self._unwrap_closure_and_profile(subfunc)
        if PY2:
            func_closure = func.func_closure
        else:
            func_closure = func.__closure__

        if func_closure:
            for cell in func_closure:
                target = cell.cell_contents
                if hasattr(target, '__code__'):
                    self._unwrap_closure_and_profile(cell.cell_contents)
                if inspect.isclass(target) and View in inspect.getmro(target):
                    for name, value in inspect.getmembers(target):
                        if name[0] != '_' and inspect.ismethod(value):
                            self._unwrap_closure_and_profile(value)

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.view_func = view_func
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        self.line_profiler = LineProfiler()
        self._unwrap_closure_and_profile(view_func)
        signals.profiler_setup.send(sender=self,
                                    profiler=self.line_profiler,
                                    view_func=view_func,
                                    view_args=view_args,
                                    view_kwargs=view_kwargs)
        self.line_profiler.enable_by_count()
        out = self.profiler.runcall(view_func, *args, **view_kwargs)
        self.line_profiler.disable_by_count()
        return out

    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        """
        add_node does a depth first traversal of the call graph, appending a
        FunctionCall object to func_list, so that the Django template only
        has to do a single for loop over func_list that can render a tree
        structure

        Parameters:
            func_list is an array that will have a FunctionCall for each call
                added to it
            func is a FunctionCall object that will have all its callees added
            max_depth is the maximum depth we should recurse
            cum_time is the minimum cum_time a function should have to be
                included in the output
        """
        func_list.append(func)
        func.has_subfuncs = False
        # this function somewhat dangerously relies on FunctionCall to set its
        # subfuncs' depth argument correctly
        if func.depth >= max_depth:
            return

        # func.subfuncs returns FunctionCall objects
        subs = sorted(func.subfuncs(), key=FunctionCall.cumtime, reverse=True)
        for subfunc in subs:
            # a sub function is important if it takes a long time or it has
            # line_stats
            if (subfunc.cumtime() >= cum_time or
                    (hasattr(self.stats, 'line_stats') and
                     subfunc.func in self.stats.line_stats.timings)):
                func.has_subfuncs = True
                self.add_node(
                    func_list=func_list,
                    func=subfunc,
                    max_depth=max_depth,
                    cum_time=subfunc.cumtime()/16)

    def process_response(self, request, response):
        if not hasattr(self, 'profiler'):
            return None
        # Could be delayed until the panel content is requested (perf. optim.)
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()

        func_list = []
        root_func = self.stats.get_root_func(self.view_func)

        if root_func is not None:
            root_node = FunctionCall(statobj=self.stats,
                                     func=root_func,
                                     depth=0)
            self.add_node(
                func_list=func_list,
                func=root_node,
                max_depth=10,
                cum_time=root_node.cumtime() / 8
            )
        # else:
        # what should we do if we didn't detect a root function? It's not
        # clear what causes this, but there are real world examples of it (see
        # https://github.com/dmclain/django-debug-toolbar-line-profiler/issues/11)

        self.record_stats({'func_list': func_list})
Example #6
0
class ProfilingPanel(Panel):
    """
    Panel that displays profiling information.
    """
    title = _('Profiling')

    template = 'debug_toolbar_line_profiler/panels/profiling.html'

    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, '__code__'):
            return
        self.line_profiler.add_function(func)
        for subfunc in getattr(func, 'profile_additional', []):
            self._unwrap_closure_and_profile(subfunc)
        if PY2:
            func_closure = func.func_closure
        else:
            func_closure = func.__closure__

        if func_closure:
            for cell in func_closure:
                target = cell.cell_contents
                if hasattr(target, '__code__'):
                    self._unwrap_closure_and_profile(cell.cell_contents)
                if inspect.isclass(target) and View in inspect.getmro(target):
                    for name, value in inspect.getmembers(target):
                        if name[0] != '_' and inspect.ismethod(value):
                            self._unwrap_closure_and_profile(value)

    def process_view(self, request, view_func, view_args, view_kwargs):
        self.view_func = view_func
        self.profiler = cProfile.Profile()
        args = (request,) + view_args
        self.line_profiler = LineProfiler()
        self._unwrap_closure_and_profile(view_func)
        signals.profiler_setup.send(sender=self,
                                    profiler=self.line_profiler,
                                    view_func=view_func,
                                    view_args=view_args,
                                    view_kwargs=view_kwargs)
        self.line_profiler.enable_by_count()
        out = self.profiler.runcall(view_func, *args, **view_kwargs)
        self.line_profiler.disable_by_count()
        return out

    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        """
        add_node does a depth first traversal of the call graph, appending a
        FunctionCall object to func_list, so that the Django template only
        has to do a single for loop over func_list that can render a tree
        structure

        Parameters:
            func_list is an array that will have a FunctionCall for each call
                added to it
            func is a FunctionCall object that will have all its callees added
            max_depth is the maximum depth we should recurse
            cum_time is the minimum cum_time a function should have to be
                included in the output
        """
        func_list.append(func)
        func.has_subfuncs = False
        # this function somewhat dangerously relies on FunctionCall to set its
        # subfuncs' depth argument correctly
        if func.depth >= max_depth:
            return

        # func.subfuncs returns FunctionCall objects
        subs = sorted(func.subfuncs(), key=FunctionCall.cumtime, reverse=True)
        for subfunc in subs:
            # a sub function is important if it takes a long time or it has
            # line_stats
            if (subfunc.cumtime() >= cum_time or
                    (hasattr(self.stats, 'line_stats') and
                     subfunc.func in self.stats.line_stats.timings)):
                func.has_subfuncs = True
                self.add_node(
                    func_list=func_list,
                    func=subfunc,
                    max_depth=max_depth,
                    cum_time=subfunc.cumtime()/16)

    def process_response(self, request, response):
        if not hasattr(self, 'profiler'):
            return None
        # Could be delayed until the panel content is requested (perf. optim.)
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()

        func_list = []
        root_func = self.stats.get_root_func(self.view_func)

        if root_func is not None:
            root_node = FunctionCall(statobj=self.stats,
                                     func=root_func,
                                     depth=0)
            self.add_node(
                func_list=func_list,
                func=root_node,
                max_depth=10,
                cum_time=root_node.cumtime() / 8
            )
        # else:
        # what should we do if we didn't detect a root function? It's not
        # clear what causes this, but there are real world examples of it (see
        # https://github.com/dmclain/django-debug-toolbar-line-profiler/issues/11)

        self.record_stats({'func_list': func_list})
Example #7
0
class ProfilingDebugPanel(DebugPanel):
    """
    Panel that displays the Django version.
    """
    name = 'Profiling'
    template = 'debug_toolbar/panels/profiling.html'
    has_content = True

    def nav_title(self):
        return _('Profiling')

    def url(self):
        return ''

    def title(self):
        return _('Profiling')

    def _unwrap_closure_and_profile(self, func):
        if not hasattr(func, 'func_code'):
            return
        self.line_profiler.add_function(func)
        if func.func_closure:
            for cell in func.func_closure:
                if hasattr(cell.cell_contents, 'func_code'):
                    self._unwrap_closure_and_profile(cell.cell_contents)

    def process_view(self, request, view_func, view_args, view_kwargs):
        __traceback_hide__ = True
        self.profiler = cProfile.Profile()
        args = (request, ) + view_args
        if DJ_PROFILE_USE_LINE_PROFILER:
            self.line_profiler = LineProfiler()
            self._unwrap_closure_and_profile(view_func)
            self.line_profiler.enable_by_count()
            out = self.profiler.runcall(view_func, *args, **view_kwargs)
            self.line_profiler.disable_by_count()
        else:
            self.line_profiler = None
            out = self.profiler.runcall(view_func, *args, **view_kwargs)
        return out

    def add_node(self, func_list, func, max_depth, cum_time=0.1):
        func_list.append(func)
        func.has_subfuncs = False
        if func.depth < max_depth:
            for subfunc in func.subfuncs():
                if (subfunc.stats[3] >= cum_time
                        or (hasattr(self.stats, 'line_stats') and
                            (subfunc.func in self.stats.line_stats.timings))):
                    func.has_subfuncs = True
                    self.add_node(func_list,
                                  subfunc,
                                  max_depth,
                                  cum_time=cum_time)

    def process_response(self, request, response):
        __traceback_hide__ = True
        if not hasattr(self, 'profiler'):
            return None
        self.profiler.create_stats()
        self.stats = DjangoDebugToolbarStats(self.profiler)
        if DJ_PROFILE_USE_LINE_PROFILER:
            self.stats.line_stats = self.line_profiler.get_stats()
        self.stats.calc_callees()

        root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)

        func_list = []
        self.add_node(func_list, root, 10, root.stats[3] / 8)

        self.record_stats({'func_list': func_list})
        "C",
        "B",
    ],
    "B": ["K"],
    "C": ["F", "D"],
    "D": ["G", "H", "E"],
    "E": [],
    "F": ["J", "G"],
    "G": [],
    "H": [],
    "J": ["H"],
    "K": []
}

# profile the depth_traversal

lp_wrapper = lp(depth_traversal)
lp_wrapper(graph_1, "A")
stats1 = lp.get_stats()

# profile the breadth_traversal

lp_wrapper = lp(breadth_traversal)
lp_wrapper(graph_1, "A")
stats2 = lp.get_stats()

create_chart(stats1, 1)
create_chart(stats2, 2)

plt.show()