text:
'''
Scores an existing model using one or more batches of images from a testing set.

Outputs a confusion matrix, and precision, recall, F-score and support metrics.
'''

from keras.preprocessing import image
from keras.models import load_model
import numpy as np
from datetime import datetime
import os
import itertools
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, precision_recall_fscore_support
import argparse
import logging

logger = logging.getLogger('score_keras')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())

FLAGS = None

def plot_confusion_matrix(cm, classes, outfile,
                          title='Confusion Matrix',
                          cmap=plt.cm.BuPu,
                          size_inches=6):
    """
    Plots the confusion matrix and stores into outfile.
    """
    fig = plt.figure()
    fig.set_size_inches(size_inches, size_inches)
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    class_labels = [ '{} ({})'.format(classes[idx], idx) for idx in range(len(classes))]
    plt.xticks(tick_marks, class_labels, rotation=90)
    plt.yticks(tick_marks, class_labels)

    halfway = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment='center',
                 color='white' if cm[i, j] > halfway else 'black')

    plt.tight_layout()
    plt.ylabel('Actual Label')
    plt.xlabel('Predicted Label')
    os.makedirs(os.path.dirname(outfile), exist_ok=True)
    plt.savefig(outfile)

def load_model_and_images(model_file, image_dir, seed):
    testpath = image_dir

    imagegen = image.ImageDataGenerator()
    test_gen = image.DirectoryIterator(testpath, imagegen, target_size=(224, 224), seed=seed, shuffle=True)

    model = load_model(model_file)
    # Get classes sorted by their value
    classes = [x[0] for x in sorted(test_gen.class_indices.items(), key=lambda x: x[1])]

    return model, test_gen, classes

def evaluate_model(model, image_gen, classes, num_batches, output_metrics, output_image, beta=1.):
    for batch_num in range(num_batches):
        print('Processing batch %d of %d' % (batch_num, num_batches))
        cur_x, cur_y = image_gen.next()
        cur_y_pred = model.predict(cur_x)
        if batch_num == 0:
            y_actual = cur_y
            y_pred = cur_y_pred
        else:
            y_actual = np.concatenate((y_actual, cur_y))
            y_pred = np.concatenate((y_pred, cur_y_pred))
    y_v = np.argmax(y_actual, axis=1)
    y_p_v = np.argmax(y_pred, axis=1)
    metrics = {}
    if output_image:
        print('Writing confusion matrix to %s' % output_image)
        plot_confusion_matrix(confusion_matrix(y_v, y_p_v), classes, output_image)
    if output_metrics:
        print('Writing metrics to %s' % output_metrics)
        precision, recall, fscore, support = precision_recall_fscore_support(y_v, y_p_v, beta=beta)
        metrics = {
            'Precision': precision,
            'Recall': recall,
            'F-Score': fscore,
            'Support': support
        }
        print('Precision: {}, Recall: {}, F-Score: {}, Support: {}'.format(precision, recall, fscore, support))
        os.makedirs(os.path.dirname(output_metrics), exist_ok=True)
        with open(output_metrics, 'w') as fp:
            fp.write('Precision, Recall, F_Score, Support\n')
            fp.write('{}, {}, {}, {}\n'.format(precision, recall, fscore, support))
    return metrics

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        '--model_dir',
        type=str,
        default='./models',
        help='model_dir + output_model + "h5" == full output model file path. Default is ./models.'
    )
    parser.add_argument(
        '--model_name',
        type=str,
        required=True,
        help='Name of model file (model_dir + model_name + ".h5" == full file path).')
    parser.add_argument(
        '--image_dir',
        type=str,
        required=True,
        help='Base path to images, will use "testing" subdirectory underneath.')
    parser.add_argument(
        '--output_img',
        type=str,
        default=None,
        help='Path for output confusion matrix. Defaults to (model_dir + model_name + "_cm.png")')
    parser.add_argument(
        '--output_metrics',
        type=str,
        default=None,
        help='Path for output metrics. Defaults to (model_dir + model_name + "_metrics.csv")')
    parser.add_argument(
        '--num_batches',
        type=int,
        default=10,
        help='Number of batches to iterate for testing. Defaults to 10.')
    parser.add_argument(
        '--seed',
        type=int,
        default=1337,
        help='Set seed for randomization of image iteration')
    FLAGS, _ = parser.parse_known_args()
    model_path = os.path.join(FLAGS.model_dir, FLAGS.model_name + ".h5")
    cm_path = FLAGS.output_img if FLAGS.output_img else os.path.join(FLAGS.model_dir, FLAGS.model_name + "_cm.png")
    metrics_path = FLAGS.output_metrics if FLAGS.output_metrics else os.path.join(FLAGS.model_dir, FLAGS.model_name + "_metrics.csv")
    img_path = FLAGS.image_dir
    if os.path.exists(os.path.join(img_path, "testing")):
        img_path = os.path.join(img_path, "testing")
    m, i, c = load_model_and_images(model_path, img_path, FLAGS.seed)
    logger.info('Loaded model from {}'.format(model_path))
    logger.info('Evaluating model using images from {}'.format(img_path))
    evaluate_model(m, i, c, FLAGS.num_batches, metrics_path, cm_path)

max_stars_repo_name: fujikosu/ImageRecognitionInKeras

max_issues_repo_name: fujikosu/ImageRecognitionInKeras

max_forks_repo_name: fujikosu/ImageRecognitionInKeras


text:
"""Point module.

Provides the point class, reponsible for handling point forces and moments,
reactions and supports. A point is defined by an abstract class with all attributes and
methods associated with point features. Concrete implementations, for instance, for
distinct supports types, are realised by concrete class implementations.

These classes are used internally by SymBeam thus, they shall not be used directly by the
user, in a general scenario. In fact, SymBeam instanciates the points on the beam by
the data coming from the user, accounting for the existance of supports, point forces and
moments and changes of beam properties.

..module:: point
  :synopsis: Point class

..moduleauthor:: A. M. Couto Carneiro <amcc@fe.up.pt>
"""
from abc import ABC, abstractmethod

import matplotlib.patches as patches
import sympy as sym

from sympy.abc import x

# Set numerical tolerance
tol = 1e-6
# ==================================================================================== point
class point(ABC):
    """Abstract definition of a beam point."""

    def __init__(self, x_coord):
        self.x_coord = sym.sympify(x_coord)
        self.reaction_force = sym.sympify(0)
        self.reaction_moment = sym.sympify(0)
        self.external_force = sym.sympify(0)
        self.external_moment = sym.sympify(0)

    # ----------------------------------------------------------------------------- get_name
    @staticmethod
    @abstractmethod
    def get_name():
        """Returns the point type descriptor.

        Returns
        -------
        name : str
          Name of the support
        """

    # ------------------------------------------------------------------- has_reaction_force
    @abstractmethod
    def has_reaction_force(self):
        """Flags if the current point support reactions forces.

        Returns
        -------
        flag : bool
          Flags if the point supports exterior reaction forces
        """

    # ------------------------------------------------------------------ has_reaction_moment
    @abstractmethod
    def has_reaction_moment(self):
        """Flags if the current point support reactions moments.

        Returns
        -------
        flag : bool
          Flags if the point supports exterior reaction moments
        """

    # ---------------------------------------------------- get_deflection_boundary_condition
    @abstractmethod
    def get_deflection_boundary_condition(self, list_deflection):
        """Establishes the deflection boundary condition on the current point.

        Parameters
        ----------
        list_deflection : list of SymPy expressions
          List of the deflection expressions associated with a point as function of x and
          the integration constants

        Returns
        -------
        equations : list of SymPy expressions
          List of equations setting the deflection boundary condition at the point that
          allow the determination of the integration constants
        """

    # ------------------------------------------------------ get_rotation_boundary_condition
    @abstractmethod
    def get_rotation_boundary_condition(self, list_rotation):
        """Establishes the rotation boundary condition on the current point.

        Parameters
        ----------
        list_rotation : list of SymPy expressions
          List of the rotation expressions associated with a point as function of x and
          the integration constants

        Returns
        -------
        equations : list of SymPy expressions
          List of equations setting the rotation boundary condition at the point that
          allow the determination of the integration constants
        """

    # ---------------------------------------------------------- get_fixed_degree_of_freedom
    @abstractmethod
    def get_fixed_degree_of_freedom(self):
        """Returns the number of fixed degrees of freedom in a support.

        Retunrs
        -------
        number_fixed : int
          Number of fixed degrees of freedom at the point
        """

    # --------------------------------------------------------------------------- draw_point
    @abstractmethod
    def draw_point(self, ax):
        """Draws the point in the given axis.

        Parameters
        ----------
        ax : Matplotlib axis object
          Axis where to draw the point
        """

    # ------------------------------------------------------------- has_deflection_condition
    def has_deflection_condition(self):
        """Flags if the current point enforces some boundary condition on the deflection.

        Returns
        -------
        flag : bool
          Flags if the current support sets a deflection boundary condition
        """
        return not (self.is_method_empty(self.get_deflection_boundary_condition))

    # --------------------------------------------------------------- has_rotation_condition
    def has_rotation_condition(self):
        """Flags if the current point enforces some boundary condition on the rotation.

        Returns
        -------
        flag : bool
          Flags if the current support sets a rotation boundary condition
        """
        return not (self.is_method_empty(self.get_rotation_boundary_condition))

    # ---------------------------------------------------- set_geometric_boundary_conditions
    def set_geometric_boundary_conditions(self, list_rotation, list_deflection, equations):
        """Sets the geometric boundary conditions on the global system of equations.

        Parameters
        ----------
        list_rotation : list of SymPy expressions
          List of the rotation expressions associated with a point as function of x and
          the integration constants
        list_deflection : list of SymPy expressions
          List of the deflection expressions associated with a point as function of x and
          the integration constants
        equations : list of SymPy expressions
          List of global geometric boundary condition equations
        """
        if self.has_rotation_condition():
            condition_equation = self.get_rotation_boundary_condition(list_rotation)
            equations.extend(condition_equation)

        if self.has_deflection_condition():
            condition_equation = self.get_deflection_boundary_condition(list_deflection)
            equations.extend(condition_equation)

    # ---------------------------------------------------------------------- is_method_empty
    @staticmethod
    def is_method_empty(func):
        """Detects if a method is empty by comparing the bytecode instructions with an
        empty function with and without docstring documentation.

        Parameters
        ----------
        func : Python function
          Function to check
        """

        def empty_func():
            pass

        def empty_func_with_doc():
            """Empty function with docstring."""
            pass

        return (
            func.__code__.co_code == empty_func.__code__.co_code
            or func.__code__.co_code == empty_func_with_doc.__code__.co_code
        )

    # --------------------------------------------------------------- get_numeric_coordinate
    def get_numeric_coordinate(self, input_substitution={}):
        """Returns the coordinate of the point, by substituting all present symbols byb 1.

        Parameters
        ----------
        input_substitution : dictionary
          User-specified symbols substitution for the symbolic expressions

        Returns
        -------
        x_coord_plot : SymPy float
          Numerical value of point coordinate
        """
        x_coord_plot = self.x_coord
        input_substitution.pop("x", None)
        x_coord_plot = x_coord_plot.subs(input_substitution)
        for ivariable in x_coord_plot.free_symbols:
            x_coord_plot = x_coord_plot.subs({ivariable: 1})

        return x_coord_plot

    # ------------------------------------------------------------------------- draw_support
    def draw_support(self, ax, input_substitution={}):
        """Draws the point in the axis.

        Parameters
        ----------
        ax : Matplotlib axis object
          Axis where to draw the point
        """
        x_coord_plot = self.get_numeric_coordinate(input_substitution=input_substitution)
        self.draw_point(x_coord_plot, ax)

    # --------------------------------------------------------------------------- draw_force
    def draw_force(self, ax, length, input_substitution={}):
        """Draws a point force in the axis.

        Parameters
        ----------
        ax : Matplotlib axis object
          Axis where to draw the point force
        length : float
          Length of the force in the figure
        """
        x_coord_plot = self.get_numeric_coordinate(input_substitution=input_substitution)
        # Set the geometry scale (heuristic).
        scale_x = ax.get_xlim()[1] - ax.get_xlim()[0]
        scale_y = ax.get_ylim()[1] - ax.get_ylim()[0]
        width = 0.002
        head_width = width * 7 * scale_x
        head_length = width * 35 * scale_y
        color = "seagreen"

        # Draw the vector.
        ax.arrow(
            x_coord_plot,
            -length,
            0,
            length,
            clip_on=False,
            color=color,
            width=width,
            head_width=head_width,
            head_length=head_length,
            overhang=0,
            length_includes_head=True,
        )

    # -------------------------------------------------------------------------- draw_moment
    def draw_moment(self, ax, value, input_substitution={}):
        """Draws a point moment in the axis.

        Parameters
        ----------
        ax : Matplotlib axis object
          Axis where to draw the point moment
        value : float
          Relative absolute value the moment, relative to all present moments in the beam
        """
        x_coord_plot = self.get_numeric_coordinate(input_substitution=input_substitution)
        color = "firebrick"
        # Get the limits of the x- and y-axis
        xlim = ax.get_xlim()
        xmin = xlim[0]
        xmax = xlim[1]
        xspan = xmax - xmin

        ylim = ax.get_ylim()
        ymin = ylim[0]
        ymax = ylim[1]
        yspan = ymax - ymin

        # Set the starting and ending angles of the arc.
        diameter = xspan / 25
        if value > 0:
            start_angle = 105
            end_angle = 90
        else:
            start_angle = 90
            end_angle = 75

        # In order to draw an approximately circular arc, get the aspect ratio associated
        # with the data and the figure/axis itself, and scale the diameter.
        bbox = ax.get_window_extent()
        width, height = bbox.width, bbox.height

        data_scale = yspan / xspan
        axis_scale = width / height

        angle = 0
        diameterx = diameter
        diametery = diameter * data_scale * axis_scale

        linewidth = 1 + abs(value) * 1
        markersize = 3 + abs(value) * 4

        # Draw the arc.
        arc = patches.Arc(
            [x_coord_plot, 0],
            diameterx,
            diametery,
            angle=angle,
            theta1=start_angle,
            theta2=end_angle,
            zorder=1000,
            linewidth=linewidth,
            color=color,
            clip_on=False,
        )
        ax.add_patch(arc)

        # Plot the arrow head (marker) and the center of the arc.
        ax.plot(
            x_coord_plot,
            0,
            marker="+",
            clip_on=False,
            markersize=8,
            markerfacecolor=color,
            markeredgecolor=color,
        )
        if value > 0:
            ax.plot(
                x_coord_plot,
                diametery / 2,
                marker="<",
                clip_on=False,
                markersize=markersize,
                markerfacecolor=color,
                markeredgewidth=0,
                markeredgecolor=color,
            )
        else:
            ax.plot(
                x_coord_plot,
                diametery / 2,
                marker=">",
                clip_on=False,
                markersize=markersize,
                markerfacecolor=color,
                markeredgewidth=0,
                markeredgecolor=color,
            )

# ====================================================================================== pin
class pin(point):
    """Concrete implementation of a pinned support."""

    @staticmethod
    def get_name():
        return "Pinned Support"

    # --------------------------------------------------------------------------------------
    def has_reaction_force(self):
        return True

    # --------------------------------------------------------------------------------------
    def has_reaction_moment(self):
        return False

    # --------------------------------------------------------------------------------------
    def get_fixed_degree_of_freedom(self):
        return 2

    # --------------------------------------------------------------------------------------
    def get_deflection_boundary_condition(self, list_deflection):
        fixed_equation = list_deflection[0].subs({x: self.x_coord})
        if len(list_deflection) == 2:
            deflection_continuous = list_deflection[0].subs(
                {x: self.x_coord}
            ) - list_deflection[1].subs({x: self.x_coord})
            equations = [fixed_equation, deflection_continuous]
        else:
            equations = [fixed_equation]

        return equations

    # --------------------------------------------------------------------------------------
    def get_rotation_boundary_condition(self, list_rotation):
        if len(list_rotation) == 2:
            rotation_continuous = list_rotation[0].subs({x: self.x_coord}) - list_rotation[
                1
            ].subs({x: self.x_coord})
            equations = [rotation_continuous]
        else:
            equations = []

        return equations

    # --------------------------------------------------------------------------------------
    def draw_point(self, x_coord_plot, ax):
        # Get the limits of the x- and y-axis
        xlim = ax.get_xlim()
        xmin = xlim[0]
        xmax = xlim[1]
        xspan = xmax - xmin

        ylim = ax.get_ylim()
        ymin = ylim[0]
        ymax = ylim[1]
        yspan = ymax - ymin
        ymid = (ymax + ymin) / 2

        # Draw the triangle.
        length_bottom_line = xspan / 20
        ax.plot(
            x_coord_plot,
            ymid - yspan / 11,
            marker="^",
            clip_on=False,
            markersize=20,
            markerfacecolor="silver",
            markeredgewidth=1,
            markeredgecolor="black",
        )

        # Draw the final line.
        ax.plot(
            [x_coord_plot - length_bottom_line / 2, x_coord_plot + length_bottom_line / 2],
            [ymid - yspan / 5, ymid - yspan / 5],
            color="silver",
            linewidth=5,
            clip_on=False,
            solid_capstyle="butt",
        )
        ax.plot(
            [x_coord_plot - length_bottom_line / 2, x_coord_plot + length_bottom_line / 2],
            [ymid - yspan / 5.5, ymid - yspan / 5.5],
            color="black",
            linewidth=1.5,
            clip_on=False,
            solid_capstyle="butt",
        )

# =================================================================================== roller
class roller(point):
    """Concrete implementation of a roller support (same as pin if there are no axial
    loads on the beam).
    """

    @staticmethod
    def get_name():
        return "Roller"

    def has_reaction_force(self):
        return True

    def has_reaction_moment(self):
        return False

    def get_fixed_degree_of_freedom(self):
        return 1

    def get_deflection_boundary_condition(self, list_deflection):
        fixed_equation = list_deflection[0].subs({x: self.x_coord})
        if len(list_deflection) == 2:
            deflection_continuous = list_deflection[0].subs(
                {x: self.x_coord}
            ) - list_deflection[1].subs({x: self.x_coord})
            equations = [fixed_equation, deflection_continuous]
        else:
            equations = [fixed_equation]

        return equations

    def get_rotation_boundary_condition(self, list_rotation):
        if len(list_rotation) == 2:
            rotation_continuous = list_rotation[0].subs({x: self.x_coord}) - list_rotation[
                1
            ].subs({x: self.x_coord})
            equations = [rotation_continuous]
        else:
            equations = []

        return equations

    def draw_point(self, x_coord_plot, ax):
        # Get the limits of the x- and y-axis
        xlim = ax.get_xlim()
        xmin = xlim[0]
        xmax = xlim[1]
        xspan = xmax - xmin

        ylim = ax.get_ylim()
        ymin = ylim[0]
        ymax = ylim[1]
        yspan = ymax - ymin

        # Draw the triangle.
        length_bottom_line = xspan / 20
        ax.plot(
            x_coord_plot,
            -yspan / 15.5,
            marker="^",
            clip_on=False,
            markersize=15,
            markerfacecolor="silver",
            markeredgewidth=1,
            markeredgecolor="black",
        )

        # Draw the circles.
        ax.plot(
            x_coord_plot + xspan / 100,
            -yspan / 6.8,
            marker="o",
            clip_on=False,
            markersize=6,
            markerfacecolor="black",
            markeredgewidth=0,
            markeredgecolor="black",
        )
        ax.plot(
            x_coord_plot - xspan / 100,
            -yspan / 6.8,
            marker="o",
            clip_on=False,
            markersize=6,
            markerfacecolor="black",
            markeredgewidth=0,
            markeredgecolor="black",
        )

        # Draw the final line.
        ax.plot(
            [x_coord_plot - length_bottom_line / 2, x_coord_plot + length_bottom_line / 2],
            [-yspan / 5, -yspan / 5],
            color="silver",
            linewidth=5,
            clip_on=False,
            solid_capstyle="butt",
        )
        ax.plot(
            [x_coord_plot - length_bottom_line / 2, x_coord_plot + length_bottom_line / 2],
            [-yspan / 5.5, -yspan / 5.5],
            color="black",
            linewidth=1.5,
            clip_on=False,
            solid_capstyle="butt",
        )

# =============================================================================== continuity
class continuity(point):
    """Concrete implementation of a continuity point in a beam."""

    @staticmethod
    def get_name():
        return "Continuity point"

    def has_reaction_force(self):
        return False

    def has_reaction_moment(self):
        return False

    def get_fixed_degree_of_freedom(self):
        return 0

    def get_deflection_boundary_condition(self, list_deflection):
        if len(list_deflection) == 2:
            deflection_continuous = list_deflection[0].subs(
                {x: self.x_coord}
            ) - list_deflection[1].subs({x: self.x_coord})
            equations = [deflection_continuous]
        else:
            equations = []

        return equations

    def get_rotation_boundary_condition(self, list_rotation):
        if len(list_rotation) == 2:
            rotation_continuous = list_rotation[0].subs({x: self.x_coord}) - list_rotation[
                1
            ].subs({x: self.x_coord})
            equations = [rotation_continuous]
        else:
            equations = []

        return equations

    def draw_point(self, x_coord_plot, ax):
        pass

# ==================================================================================== fixed
class fixed(point):
    """Concrete implementation of a fixed/clamped support."""

    @staticmethod
    def get_name():
        return "Fixed"

    def has_reaction_force(self):
        return True

    def has_reaction_moment(self):
        return True

    def get_fixed_degree_of_freedom(self):
        return 3

    def get_deflection_boundary_condition(self, list_deflection):
        fixed_equation = list_deflection[0].subs({x: self.x_coord})
        if len(list_deflection) == 2:
            deflection_continuous = list_deflection[0].subs(
                {x: self.x_coord}
            ) - list_deflection[1].subs({x: self.x_coord})
            equations = [fixed_equation, deflection_continuous]
        else:
            equations = [fixed_equation]

        return equations

    def get_rotation_boundary_condition(self, list_rotation):
        fixed_equation = list_rotation[0].subs({x: self.x_coord})
        if len(list_rotation) == 2:
            rotation_continuous = list_rotation[0].subs({x: self.x_coord}) - list_rotation[
                1
            ].subs({x: self.x_coord})
            equations = [fixed_equation, rotation_continuous]
        else:
            equations = [fixed_equation]

        return equations

    def draw_point(self, x_coord_plot, ax):
        # Get the limits of the x- and y-axis
        xlim = ax.get_xlim()
        xmin = xlim[0]
        xmax = xlim[1]
        xspan = xmax - xmin

        ylim = ax.get_ylim()
        ymin = ylim[0]
        ymax = ylim[1]
        yspan = ymax - ymin

        # Draw the triangle.
        if abs(x_coord_plot - xmin) < tol:
            ax.plot(
                [x_coord_plot - xspan / 150, x_coord_plot - xspan / 150],
                [-yspan / 15.5, yspan / 15.5],
                color="silver",
                linewidth=5,
                clip_on=False,
                solid_capstyle="butt",
            )
            ax.plot(
                [x_coord_plot, x_coord_plot],
                [-yspan / 15.5, yspan / 15.5],
                color="black",
                linewidth=1.5,
                clip_on=False,
                solid_capstyle="butt",
            )
        elif abs(x_coord_plot - xmin) > tol:
            ax.plot(
                [x_coord_plot + xspan / 150, x_coord_plot + xspan / 150],
                [-yspan / 15.5, yspan / 15.5],
                color="silver",
                linewidth=5,
                clip_on=False,
                solid_capstyle="butt",
            )
            ax.plot(
                [x_coord_plot, x_coord_plot],
                [-yspan / 15.5, yspan / 15.5],
                color="black",
                linewidth=1.5,
                clip_on=False,
                solid_capstyle="butt",
            )
        else:
            ax.plot(
                [x_coord_plot, x_coord_plot],
                [-yspan / 15.5, yspan / 15.5],
                color="silver",
                linewidth=5,
                clip_on=False,
                solid_capstyle="butt",
            )
            ax.plot(
                [x_coord_plot - xspan / 150, x_coord_plot - xspan / 150],
                [-yspan / 15.5, yspan / 15.5],
                color="black",
                linewidth=1.5,
                clip_on=False,
                solid_capstyle="butt",
            )
            ax.plot(
                [x_coord_plot + xspan / 150, x_coord_plot + xspan / 150],
                [-yspan / 15.5, yspan / 15.5],
                color="black",
                linewidth=1.5,
                clip_on=False,
                solid_capstyle="butt",
            )

# ==================================================================================== hinge
class hinge(point):
    """Concrete implementation of a hinge."""

    @staticmethod
    def get_name():
        return "Hinge"

    def has_reaction_force(self):
        return False

    def has_reaction_moment(self):
        return False

    def get_fixed_degree_of_freedom(self):
        return 0

    def get_deflection_boundary_condition(self, list_deflection):
        if len(list_deflection) == 2:
            deflection_continuous = list_deflection[0].subs(
                {x: self.x_coord}
            ) - list_deflection[1].subs({x: self.x_coord})
            equations = [deflection_continuous]
        else:
            equations = []

        return equations

    def get_rotation_boundary_condition(self, list_rotation):
        pass

    def draw_point(self, x_coord_plot, ax):
        ax.plot(
            x_coord_plot,
            0,
            marker="o",
            clip_on=False,
            markersize=8,
            markerfacecolor="white",
            markeredgewidth=1.5,
            markeredgecolor="black",
        )

# ==========================================================================================

ext: py

max_stars_repo_name: connorivy/symbeam

max_issues_repo_name: connorivy/symbeam

max_forks_repo_name: connorivy/symbeam

reason: import sympy,from sympy


text:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import datetime
import matplotlib.animation as manimation

def load_papers(filename):
    papers = []
    with open(filename) as f:
        for line in f:
            papers.append(Paper(line))
    return papers

class Paper:
    def __init__(self, line):
        words = line.strip().split(",")
        self.title = words[0]
        if words[2] == 'None':
            self.date = None
        else:
            self.date = datetime.datetime.strptime(words[2], "%Y-%m-%d")
        self.coords = []
        for institution, country, lat, long in zip(*[iter(words[3:])]*4):
            try:
                self.coords.append((float(lat), float(long)))
            except ValueError:
                self.coords.append((0, 0))

def draw_globe(satellite_lat, satellite_long, lats, longs, date, writer):
    if date > datetime.date.today():
        date = datetime.date.today()
    satellite_lat %= 360
    satellite_long %= 360
    map = Basemap(projection='ortho',lat_0=satellite_lat,lon_0=satellite_long,resolution='c')
    # draw coastlines, country boundaries, fill continents.
    map.drawcoastlines(linewidth=0.25, zorder=4, color='w')
    map.drawcountries(linewidth=0.25, zorder=2, color='w')
    # draw the edge of the map projection region (the projection limb)
    map.drawmapboundary(fill_color='black', zorder=0)
    # draw lat/lon grid lines every 30 degrees.
    map.drawmeridians(np.arange(0,360,30), color='w')
    map.drawparallels(np.arange(-90,90,30), color='w')
    lats = np.array([lats])
    lons = np.array([longs])
    # compute native map projection coordinates of lat/lon grid.
    x, y = map(lons, lats)
    # contour data over the map.
    cs = map.scatter(x,y, zorder=4, color='r')
    plt.title(str(date))
    writer.grab_frame()
    plt.clf()

if __name__ == "__main__":
    lats = []
    longs = []
    dates = []
    papers = load_papers("Output.csv")
    for paper in papers:
        if paper.date is not None:
            for coord in paper.coords:
                dates.append(paper.date)
                lats.append(coord[0])
                longs.append(coord[1])
    lats = np.array(lats)
    longs = np.array(longs)
    dates = np.array(dates)
    satellite_long = 0
    FFMpegWriter = manimation.writers['ffmpeg']
    metadata = dict(title='CFTR Mutation', artist='MSTP Group 3',
                    comment='')
    writer = FFMpegWriter(fps=25, metadata=metadata, bitrate=4e3)
    fig = plt.figure()

    with writer.saving(fig, "SpinningGlobe.mp4", 100):
        for year in range(1990, 2018, 1):
            for month in range(1, 12, 1):
                for day in range(1, 28, 35):
                    current_date = datetime.datetime(year=year, month=month, day=day)
                    after_longs = longs[dates < current_date]
                    after_lats = lats[dates < current_date]
                    draw_globe(20, satellite_long, after_lats, after_longs, current_date.date(), writer)
                    satellite_long -= 1.5
                    print "%s-%s-%s" % (year, month, day)

max_stars_repo_name: FillanGrady/PublicationMap

max_issues_repo_name: FillanGrady/PublicationMap

max_forks_repo_name: FillanGrady/PublicationMap

reason: import numpy


text:
#!/usr/bin/env python

from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import Qt

import PyQt4.Qwt5 as Qwt
import numpy as np
from datetime import datetime as date
import sys
from az_QwtDial import *
from el_QwtDial import *
import time

class MainWindow(QtGui.QWidget):
    def __init__(self, ip, port):
        QtGui.QMainWindow.__init__(self)

        self.resize(700, 700)
        self.setFixedWidth(700)
        self.setFixedHeight(500)
        self.setWindowTitle('MD01 Controller v1.0')
        self.setContentsMargins(0,0,0,0)

        self.ip = ip
        self.port = port

        self.cur_az = 0
        self.tar_az = 0
        self.cur_el = 0
        self.tar_el = 0
        self.home_az = 0.0
        self.home_el = 0.0

        self.callback    = None   #Callback accessor for tracking control
        self.connected   = False  #Status of TCP/IP connection to MD-01
        self.update_rate = 250    #Feedback Query Auto Update Interval in milliseconds

        self.initUI()
        self.darken()
        self.setFocus()

    def initUI(self):
        self.initFrames()
        self.initAzimuth()
        self.initElevation()
        self.initControls()
        self.initNet()
        self.connectSignals()

    def setCallback(self, callback):
        self.callback = callback

    def connectSignals(self):
        self.azPlusPtOneButton.clicked.connect(self.azPlusPtOneButtonClicked)
        self.azPlusOneButton.clicked.connect(self.azPlusOneButtonClicked)
        self.azPlusTenButton.clicked.connect(self.azPlusTenButtonClicked)
        self.azMinusPtOneButton.clicked.connect(self.azMinusPtOneButtonClicked)
        self.azMinusOneButton.clicked.connect(self.azMinusOneButtonClicked)
        self.azMinusTenButton.clicked.connect(self.azMinusTenButtonClicked)
        self.azTextBox.returnPressed.connect(self.azTextBoxReturnPressed)

        self.elPlusPtOneButton.clicked.connect(self.elPlusPtOneButtonClicked)
        self.elPlusOneButton.clicked.connect(self.elPlusOneButtonClicked)
        self.elPlusTenButton.clicked.connect(self.elPlusTenButtonClicked)
        self.elMinusPtOneButton.clicked.connect(self.elMinusPtOneButtonClicked)
        self.elMinusOneButton.clicked.connect(self.elMinusOneButtonClicked)
        self.elMinusTenButton.clicked.connect(self.elMinusTenButtonClicked)
        self.elTextBox.returnPressed.connect(self.elTextBoxReturnPressed)

        self.connectButton.clicked.connect(self.connectButtonEvent)
        self.queryButton.clicked.connect(self.queryButtonEvent)
        self.stopButton.clicked.connect(self.stopButtonEvent)
        self.homeButton.clicked.connect(self.homeButtonEvent)
        self.updateButton.clicked.connect(self.updateButtonEvent)
        self.autoQuery_cb.stateChanged.connect(self.catchAutoQueryEvent)

        QtCore.QObject.connect(self.updateTimer, QtCore.SIGNAL('timeout()'), self.queryButtonEvent)
        QtCore.QObject.connect(self.update_rate_le, QtCore.SIGNAL('editingFinished()'), self.updateRate)
        QtCore.QObject.connect(self.ipAddrTextBox, QtCore.SIGNAL('editingFinished()'), self.updateIPAddress)
        QtCore.QObject.connect(self.portTextBox, QtCore.SIGNAL('editingFinished()'), self.updatePort)

    def updateButtonEvent(self):
        self.updateAzimuth()
        self.updateElevation()

    def homeButtonEvent(self):
        self.tar_az = self.home_az
        self.tar_el = self.home_el
        self.updateAzimuth()
        self.updateElevation()

    def stopButtonEvent(self):
        status, self.cur_az, self.cur_el = self.callback.set_stop()
        if status != -1:
            self.az_compass.set_cur_az(self.cur_az)
            self.el_compass.set_cur_el(self.cur_el)

    def queryButtonEvent(self):
        status, self.cur_az, self.cur_el = self.callback.get_status()
        if status != -1:
            self.az_compass.set_cur_az(self.cur_az)
            self.el_compass.set_cur_el(self.cur_el)
        else:
            self.autoQuery_cb.setCheckState(QtCore.Qt.Unchecked)

    def connectButtonEvent(self):
        if (not self.connected):  #Not connected, attempt to connect
            self.connected = self.callback.connect()
            if (self.connected):
                self.connectButton.setText('Disconnect')
                self.net_label.setText("Connected")
                self.net_label.setStyleSheet("QLabel {  font-weight:bold; color:rgb(0,255,0) ; }")
                self.ipAddrTextBox.setStyleSheet("QLineEdit {background-color:rgb(225,225,225); color:rgb(0,0,0);}")
                self.portTextBox.setStyleSheet("QLineEdit {background-color:rgb(225,225,225); color:rgb(0,0,0);}")
                self.ipAddrTextBox.setEnabled(False)
                self.portTextBox.setEnabled(False)
        else:
            self.connected = self.callback.disconnect()
            if (not self.connected):
                self.connectButton.setText('Connect')
                self.net_label.setText("Disconnected")
                self.net_label.setStyleSheet("QLabel {  font-weight:bold; color:rgb(255,0,0) ; }")
                self.ipAddrTextBox.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
                self.portTextBox.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
                self.ipAddrTextBox.setEnabled(True)
                self.portTextBox.setEnabled(True)

    def catchAutoQueryEvent(self, state):
        CheckState = (state == QtCore.Qt.Checked)
        if CheckState == True:
            self.updateTimer.start()
            print self.getTimeStampGMT() + "GUI|  Started Auto Update, Interval: " + str(self.update_rate) + " [ms]"
        else:
            self.updateTimer.stop()
            print self.getTimeStampGMT() + "GUI|  Stopped Auto Update"

    def updateRate(self):
        self.update_rate = float(self.update_rate_le.text()) * 1000.0
        self.updateTimer.setInterval(self.update_rate)
        print self.getTimeStampGMT() + "GUI|  Updated Rate Interval to " + str(self.update_rate) + " [ms]"

    def updateIPAddress(self):
        self.ip = self.ipAddrTextBox.text()

    def updatePort(self):
        self.port = self.portTextBox.text()

    def azTextBoxReturnPressed(self):
        self.tar_az = float(self.azTextBox.text())
        self.updateAzimuth()

    def azPlusPtOneButtonClicked(self):
        self.tar_az = self.tar_az + 0.1
        self.updateAzimuth()

    def azPlusOneButtonClicked(self):
        self.tar_az = self.tar_az + 1
        self.updateAzimuth()

    def azPlusTenButtonClicked(self):
        self.tar_az = self.tar_az + 10
        self.updateAzimuth()

    def azMinusPtOneButtonClicked(self):
        self.tar_az = self.tar_az - 0.1
        self.updateAzimuth()

    def azMinusOneButtonClicked(self):
        self.tar_az = self.tar_az - 1
        self.updateAzimuth()

    def azMinusTenButtonClicked(self):
        self.tar_az = self.tar_az - 10
        self.updateAzimuth()

    def updateAzimuth(self):
        if self.tar_az < -180:
            self.tar_az = -180
            self.azTextBox.setText(str(self.tar_az))
        if self.tar_az > 540:
            self.tar_az = 540
            self.azTextBox.setText(str(self.tar_az))
        self.az_compass.set_tar_az(self.tar_az)
        self.callback.set_position(self.tar_az, self.tar_el)

    def elTextBoxReturnPressed(self):
        self.tar_el = float(self.elTextBox.text())
        self.updateElevation()

    def elPlusPtOneButtonClicked(self):
        self.tar_el = self.tar_el + 0.1
        self.updateElevation()

    def elPlusOneButtonClicked(self):
        self.tar_el = self.tar_el + 1
        self.updateElevation()

    def elPlusTenButtonClicked(self):
        self.tar_el = self.tar_el + 10
        self.updateElevation()

    def elMinusPtOneButtonClicked(self):
        self.tar_el = self.tar_el - 0.1
        self.updateElevation()

    def elMinusOneButtonClicked(self):
        self.tar_el = self.tar_el - 1
        self.updateElevation()

    def elMinusTenButtonClicked(self):
        self.tar_el = self.tar_el - 10
        self.updateElevation()

    def updateElevation(self):
        if self.tar_el < 0:
            self.tar_el = 0
            self.elTextBox.setText(str(self.tar_el))
        if self.tar_el > 180:
            self.tar_el = 180
            self.elTextBox.setText(str(self.tar_el))
        self.el_compass.set_tar_el(self.tar_el)
        self.callback.set_position(self.tar_az, self.tar_el)

    def initNet(self):
        self.ipAddrTextBox = QtGui.QLineEdit()
        self.ipAddrTextBox.setText(self.ip)
        self.ipAddrTextBox.setInputMask("000.000.000.000;")
        self.ipAddrTextBox.setEchoMode(QtGui.QLineEdit.Normal)
        self.ipAddrTextBox.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
        self.ipAddrTextBox.setMaxLength(15)

        self.portTextBox = QtGui.QLineEdit()
        self.portTextBox.setText(str(self.port))
        self.port_validator = QtGui.QIntValidator()
        self.port_validator.setRange(0,65535)
        self.portTextBox.setValidator(self.port_validator)
        self.portTextBox.setEchoMode(QtGui.QLineEdit.Normal)
        self.portTextBox.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
        self.portTextBox.setMaxLength(5)
        self.portTextBox.setFixedWidth(50)

        label = QtGui.QLabel('Status:')
        label.setAlignment(QtCore.Qt.AlignRight)
        self.net_label = QtGui.QLabel('Disconnected')
        self.net_label.setAlignment(QtCore.Qt.AlignLeft)
        self.net_label.setFixedWidth(150)

        self.connectButton = QtGui.QPushButton("Connect")
        self.net_label.setStyleSheet("QLabel {font-weight:bold; color:rgb(255,0,0);}")

        hbox1 = QtGui.QHBoxLayout()
        hbox1.addWidget(self.ipAddrTextBox)
        hbox1.addWidget(self.portTextBox)

        hbox2 = QtGui.QHBoxLayout()
        hbox2.addWidget(label)
        hbox2.addWidget(self.net_label)

        vbox = QtGui.QVBoxLayout()
        vbox.addLayout(hbox1)
        vbox.addWidget(self.connectButton)
        vbox.addLayout(hbox2)

        self.net_fr.setLayout(vbox)

    def initControls(self):
        self.updateButton = QtGui.QPushButton("Update")
        self.queryButton  = QtGui.QPushButton("Query")
        self.homeButton   = QtGui.QPushButton("Home")
        self.stopButton   = QtGui.QPushButton("STOP!")

        self.autoQuery_cb = QtGui.QCheckBox("Auto Query", self)  #Automatically update ADC voltages checkbox option
        self.autoQuery_cb.setStyleSheet("QCheckBox { font-size: 12px; \
                                                    background-color:rgb(0,0,0); \
                                                    color:rgb(255,255,255); }")

        self.update_rate_le = QtGui.QLineEdit()
        self.update_rate_le.setText("0.25")
        self.update_val = QtGui.QDoubleValidator()
        self.update_rate_le.setValidator(self.update_val)
        self.update_rate_le.setEchoMode(QtGui.QLineEdit.Normal)
        self.update_rate_le.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
        self.update_rate_le.setMaxLength(4)
        self.update_rate_le.setFixedWidth(50)

        hbox1 = QtGui.QHBoxLayout()
        hbox1.addWidget(self.autoQuery_cb)
        hbox1.addWidget(self.update_rate_le)

        hbox2 = QtGui.QHBoxLayout()
        hbox2.addWidget(self.updateButton)
        hbox2.addWidget(self.queryButton)
        hbox2.addWidget(self.homeButton)

        hbox3 = QtGui.QHBoxLayout()
        hbox3.addWidget(self.stopButton)

        vbox = QtGui.QVBoxLayout()
        vbox.addLayout(hbox1)
        vbox.addLayout(hbox2)
        vbox.addLayout(hbox3)

        self.button_fr.setLayout(vbox)

        self.updateTimer = QtCore.QTimer(self)
        self.updateTimer.setInterval(self.update_rate)

    def initElevation(self):
        self.el_compass = el_QwtDial(self.el_fr)
        x = 172; y = 350; w = 50; s = 4; h = 25
        self.elPlusPtOneButton = QtGui.QPushButton(self.el_fr)
        self.elPlusPtOneButton.setText("+0.1")
        self.elPlusPtOneButton.setGeometry(x,y,w,h)

        self.elPlusOneButton = QtGui.QPushButton(self.el_fr)
        self.elPlusOneButton.setText("+1.0")
        self.elPlusOneButton.setGeometry(x+s+w,y,w,h)

        self.elPlusTenButton = QtGui.QPushButton(self.el_fr)
        self.elPlusTenButton.setText("+10.0")
        self.elPlusTenButton.setGeometry(x+(2*s)+(2*w),y,w,h)

        self.elMinusPtOneButton = QtGui.QPushButton(self.el_fr)
        self.elMinusPtOneButton.setText("-0.1")
        self.elMinusPtOneButton.setGeometry(x-s-w,y,w,h)

        self.elMinusOneButton = QtGui.QPushButton(self.el_fr)
        self.elMinusOneButton.setText("-1.0")
        self.elMinusOneButton.setGeometry(x-(2*s)-(2*w),y,w,h)

        self.elMinusTenButton = QtGui.QPushButton(self.el_fr)
        self.elMinusTenButton.setText("-10.0")
        self.elMinusTenButton.setGeometry(x-(3*s)-(3*w),y,w,h)

        self.elTextBox = QtGui.QLineEdit(self.el_fr)
        self.elTextBox.setText("000.0")
        self.elTextBox.setInputMask("000.0;")
        self.elTextBox.setEchoMode(QtGui.QLineEdit.Normal)
        self.elTextBox.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
        self.elTextBox.setMaxLength(6)
        self.elTextBox.setGeometry(145,320,60,25)

    def initAzimuth(self):
        #geo = self.az_fr.frameRect()
        #print geo.x, geo.y, geo.width, geo.height
        self.az_compass = az_QwtDial(self.az_fr)
        x = 172; y = 350; w = 50; s = 4; h = 25
        self.azPlusPtOneButton = QtGui.QPushButton(self.az_fr)
        self.azPlusPtOneButton.setText("+0.1")
        self.azPlusPtOneButton.setGeometry(x,y,w,h)

        self.azPlusOneButton = QtGui.QPushButton(self.az_fr)
        self.azPlusOneButton.setText("+1.0")
        self.azPlusOneButton.setGeometry(x+s+w,y,w,h)

        self.azPlusTenButton = QtGui.QPushButton(self.az_fr)
        self.azPlusTenButton.setText("+10.0")
        self.azPlusTenButton.setGeometry(x+(2*s)+(2*w),y,w,h)

        self.azMinusPtOneButton = QtGui.QPushButton(self.az_fr)
        self.azMinusPtOneButton.setText("-0.1")
        self.azMinusPtOneButton.setGeometry(x-s-w,y,w,h)

        self.azMinusOneButton = QtGui.QPushButton(self.az_fr)
        self.azMinusOneButton.setText("-1.0")
        self.azMinusOneButton.setGeometry(x-(2*s)-(2*w),y,w,h)

        self.azMinusTenButton = QtGui.QPushButton(self.az_fr)
        self.azMinusTenButton.setText("-10.0")
        self.azMinusTenButton.setGeometry(x-(3*s)-(3*w),y,w,h)

        self.azTextBox = QtGui.QLineEdit(self.az_fr)
        self.azTextBox.setText("180.0")
        self.azTextBox.setInputMask("#000.0;")
        self.azTextBox.setEchoMode(QtGui.QLineEdit.Normal)
        self.azTextBox.setStyleSheet("QLineEdit {background-color:rgb(255,255,255); color:rgb(0,0,0);}")
        self.azTextBox.setMaxLength(6)
        self.azTextBox.setGeometry(145,320,60,25)

    def initFrames(self):
        self.az_fr = QtGui.QFrame(self)
        self.az_fr.setFrameShape(QtGui.QFrame.StyledPanel)
        self.az_fr.setFixedWidth(340)
        self.az_fr.setFixedHeight(380)

        self.el_fr = QtGui.QFrame(self)
        self.el_fr.setFrameShape(QtGui.QFrame.StyledPanel)
        self.el_fr.setFixedWidth(340)
        self.el_fr.setFixedHeight(380)

        self.button_fr = QtGui.QFrame(self)
        self.button_fr.setFrameShape(QtGui.QFrame.StyledPanel)
        self.button_fr.setFixedWidth(340)
        self.button_fr.setFixedHeight(100)

        self.net_fr = QtGui.QFrame(self)
        self.net_fr.setFrameShape(QtGui.QFrame.StyledPanel)
        self.net_fr.setFixedWidth(340)
        self.net_fr.setFixedHeight(100)

        vbox = QtGui.QVBoxLayout()
        hbox1 = QtGui.QHBoxLayout()
        hbox2 = QtGui.QHBoxLayout()

        hbox1.addWidget(self.az_fr)
        hbox1.addWidget(self.el_fr)

        hbox2.addWidget(self.net_fr)
        hbox2.addWidget(self.button_fr)

        vbox.addLayout(hbox1)
        vbox.addLayout(hbox2)

        self.setLayout(vbox)

    def darken(self):
        palette = QtGui.QPalette()
        palette.setColor(QtGui.QPalette.Background,QtCore.Qt.black)
        palette.setColor(QtGui.QPalette.WindowText,QtCore.Qt.white)
        palette.setColor(QtGui.QPalette.Text,QtCore.Qt.white)
        self.setPalette(palette)

    def getTimeStampGMT(self):
        return str(date.utcnow()) + " GMT | "

max_stars_repo_name: vt-gs/tracking

max_issues_repo_name: vt-gs/tracking

max_forks_repo_name: vt-gs/tracking


text:
# Copyright 2021 IBM Corporation
#
# 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
#
# http://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.

from __future__ import print_function
import random
import logging
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold, StratifiedKFold

class DataLoader:
    def __init__(self, config, logger):
        # Initialize Dataloader Configuration
        logging.info('[DATALOADER]: Initializing Scene Dataloader')
        self.config = config
        self.dl_cfg = self.config['dataloader']

        # Initialize PRNG Seed Values
        if self.dl_cfg.enable_seed:
            random.seed(self.dl_cfg.seed)
            np.random.seed(self.dl_cfg.seed)

        # Load Dataset
        logging.info('[DATALOADER]: Loading Dataset Files')
        logging.info('[DATALOADER]: > Loading File: ' + self.dl_cfg.data_path)
        self.data = pd.read_csv(self.dl_cfg.data_path)
        logging.info('[DATALOADER]: > Loaded: ' + self.dl_cfg.data_path + '\t' + \
                     'Data Shape: ' + str(self.data.shape))

    def get_data(self):
        # Initialize Crossfold Validation
        if self.dl_cfg.crossval.stratified:
            kf = StratifiedKFold(n_splits=self.dl_cfg.crossval.folds,
                                 shuffle=self.dl_cfg.crossval.shuffle,
                                 random_state=self.dl_cfg.crossval.random_state)
        else:
            kf = KFold(n_splits=self.dl_cfg.crossval.folds,
                       shuffle=self.dl_cfg.crossval.shuffle,
                       random_state=self.dl_cfg.crossval.random_state)

        for fold, (train_index, test_index) in enumerate(kf.split(self.data)):
            # Initialize Data Features and Labels
            X_train = self.data.to_numpy()[train_index, :-1]
            y_train = self.data.to_numpy()[train_index, -1].astype(int)
            X_test = self.data.to_numpy()[test_index, :-1]
            y_test = self.data.to_numpy()[test_index, -1].astype(int)

            # Set Dataloader Attributes
            self.num_class = len(np.unique(y_train))

            yield fold, X_train, y_train, X_test, y_test


text:
import numpy as np
import theano
import theano.tensor as T
import treeano.nodes as tn
fX = theano.config.floatX

# TODO change me
# n = tn.SpatialRepeatNDNode
n = tn.SpatialSparseUpsampleNode

network = tn.SequentialNode(
    "s",
    [tn.InputNode("i", shape=(32, 32, 32, 32, 32)),
     n("us", upsample_factor=(2, 2, 2))]
).network()
fn = network.function(["i"], ["s"])
x = np.random.randn(32, 32, 32, 32, 32).astype(fX)

"""
20150926 results:

%timeit fn(x)

SpatialRepeatNDNode: 663 ms
SpatialSparseUpsampleNode: 424 ms
"""

reason: import numpy,import theano


text:
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np

def autolabel(ax, rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

def single_table(labels, data_list, rect_cnt, rect_label, width, ylabel, title, scale = 1.0, numbering = False):
    plt.rcParams.update({'font.size': int(14 * scale), 'text.latex.preamble': [r'\usepackage{amsmath}'], 'text.usetex': True})
    # plt.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']

    hatches = ['', '//', '\\\\', 'xx', '...', '//', '*', '\\\\', '---', '\\\\', '//', '', '...', '////', '\\\\\\', 'xxxx', '....', '//', '*', '\\\\', '---', '\\\\', '//', '', '...']
    x = np.arange(len(labels))
    #plt.xlabel("Coreset Size", fontsize=10)
    fig, ax = plt.subplots()
    plt.xlabel("Coreset Size", fontsize=16)
    fig.canvas.set_window_title(title)
    rects = [ ax.bar(x = x - width / 2 + width / rect_cnt / 2 + i * width / rect_cnt,
    height=data_list[i], edgecolor='black',
    width=width / rect_cnt, label = rect_label[i], hatch = hatches[i]) for i in range(rect_cnt) ]
    #if data_list_upper != None:
    #    rects.append([ ax.bar(x = x - width / 2 + width / rect_cnt / 2 + i * width / rect_cnt,
    #    height=data_list_upper[i], edgecolor='black',
    #    width=width / rect_cnt, label = rect_label_upper[i], hatch = hatches[rect_cnt + i],
    #    bottom=data_list[i]) for i in range(rect_cnt)])

    ax.set_ylabel(ylabel, fontsize=16)
    #ax.set_title(title)
    ax.set_xticks(x)
    ax.set_xticklabels(labels, fontsize=10)
    ax.legend()

    if numbering:
        for rect in rects:
            autolabel(ax, rect)

    fig.tight_layout()

def parse_runtime(path):
    coreset_sizes = []
    dijkstra_times = []
    vxs_times = []
    vxs_costs = []
    sxs_costs = []
    sxs_times = []
    for line in filter(lambda x: not x.startswith("#"), open(path)):
        toks = line.strip().split()
        coreset_sizes.append(int(toks[0]))
        dijkstra_times.append(int(toks[2]))
        vxs_times.append(int(toks[3]))
        vxs_costs.append(float(toks[4]))
        sxs_times.append(int(toks[5]))
        sxs_costs.append(float(toks[6]))
    return list(map(np.array, (coreset_sizes, dijkstra_times, vxs_times, vxs_costs, sxs_times, sxs_costs)))

BENCHDATA_PATH="brooklyn.coreset.25.runtime"

full_opt_cost = 177247664
thorup_opt_cost = 179024784

thorup_sampling_time = 17083
distmatgen = 48983 + 2977
lsearch_thorup = 443716
cost_time = 201
sampler_gen = 60
total_cs_construction_time = sum((thorup_sampling_time, distmatgen, lsearch_thorup, cost_time, sampler_gen))
full_distmat_time = 551612
full_lsearch = 41321203
total_full_time = full_lsearch + full_distmat_time

coreset_sizes, dijkstra_times, vxs_times, vxs_costs, sxs_times, sxs_costs = parse_runtime(BENCHDATA_PATH)
#vxs_times = np.log2(vxs_times)
#sxs_times = np.log2(sxs_times)

plt.rcParams["mathtext.fontset"] = "cm"
plt.style.use('tableau-colorblind10')

rect_label = [r'$T_{X \times V}$', r'$T_{cs}$', r'$T_{D \times V}$', r'$T_{D\times D}$']
labels = np.array(list(map(str, coreset_sizes)))
construction_times = np.array([total_cs_construction_time] * len(labels))
full_times = np.array([total_full_time] * len(labels))
Y = 'Runtime (ms)'
selection = np.array([3,5,7,9,11], dtype=np.uint32).astype(int)
if __name__ == "__main__":
    slab = labels[selection]
    single_table(labels[selection],
                 [full_times[selection], construction_times[selection], vxs_times[selection], sxs_times[selection]], 4, rect_label, 0.6, Y, "runtime")
    plt.savefig("fig4.png", dpi=600)
    plt.savefig("fig4.svg")
    plt.clf()
Y = 'Base-2 Logarithmic Runtime (ms)'
full_times = np.log2(full_times)
construction_times = np.log2(construction_times)
vxs_times = np.log2(vxs_times)
sxs_times = np.log2(sxs_times)
if __name__ == "__main__":
    single_table(labels[selection],
                 [full_times[selection], construction_times[selection], vxs_times[selection], sxs_times[selection]], 4, rect_label, 0.6, Y, "runtime")
    #single_table(labels, [full_times, construction_times, vxs_times, sxs_times], 4, rect_label, 0.6, Y, "runtime")
    plt.savefig("fig4.log2.png", dpi=600)
    plt.savefig("fig4.log2.svg")
    plt.clf()

# newlabels = ["Full", "Thorup"] + labels
Y = r"Objective Value ($10^8$ m)"

if __name__ == "__main__":
    sxs_costs *= 1e-8
    vxs_costs *= 1e-8
    #thorup_opt_cost *= 1e-8
    full_opt_cost *= 1e-8
    plt.plot(labels, sxs_costs.T, linewidth=1.5, linestyle='--')
    plt.plot(labels, vxs_costs.T, linewidth=1.5, linestyle='-.')
    plt.axhline(y=full_opt_cost, color='r',
                #linestyle='',
                linewidth=2)
    #plt.axhline(y=thorup_opt_cost, color='k', linestyle='--')

    #plt.legend((r"$D\times D$", r"$D\times V$", r"$V\times V$", r"$T_{\text{cs}}$"))
    plt.legend((r"$D\times D$", r"$D\times V$", r"$X\times V$"))
    plt.xlabel("Coreset Size", fontsize=18)
    plt.xticks(fontsize=10)
    plt.ylabel(Y, fontsize=18)
    plt.savefig("fig4b.png", dpi=600)
    plt.savefig("fig4b.svg")
    plt.tight_layout()

max_forks_repo_name: dnbaker/minicore


text:
# Copyright 2020 Google Research. 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.
# You may obtain a copy of the License at
#
#     http://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.
# ==============================================================================
r"""Inference related utilities."""

from __future__ import absolute_import
from __future__ import division
# gtype import
from __future__ import print_function

import copy
import functools
import os
import time
from typing import Text, Dict, Any, List, Tuple, Union

from absl import logging
import numpy as np
from PIL import Image
import tensorflow.compat.v1 as tf
import yaml

import anchors
import dataloader
import det_model_fn
import hparams_config
import utils
from visualize import vis_utils
from tensorflow.python.client import timeline  # pylint: disable=g-direct-tensorflow-import

coco_id_mapping = {
    1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane',
    6: 'bus', 7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light',
    11: 'fire hydrant', 13: 'stop sign', 14: 'parking meter', 15: 'bench',
    16: 'bird', 17: 'cat', 18: 'dog', 19: 'horse', 20: 'sheep', 21: 'cow',
    22: 'elephant', 23: 'bear', 24: 'zebra', 25: 'giraffe', 27: 'backpack',
    28: 'umbrella', 31: 'handbag', 32: 'tie', 33: 'suitcase', 34: 'frisbee',
    35: 'skis', 36: 'snowboard', 37: 'sports ball', 38: 'kite',
    39: 'baseball bat', 40: 'baseball glove', 41: 'skateboard', 42: 'surfboard',
    43: 'tennis racket', 44: 'bottle', 46: 'wine glass', 47: 'cup', 48: 'fork',
    49: 'knife', 50: 'spoon', 51: 'bowl', 52: 'banana', 53: 'apple',
    54: 'sandwich', 55: 'orange', 56: 'broccoli', 57: 'carrot', 58: 'hot dog',
    59: 'pizza', 60: 'donut', 61: 'cake', 62: 'chair', 63: 'couch',
    64: 'potted plant', 65: 'bed', 67: 'dining table', 70: 'toilet', 72: 'tv',
    73: 'laptop', 74: 'mouse', 75: 'remote', 76: 'keyboard', 77: 'cell phone',
    78: 'microwave', 79: 'oven', 80: 'toaster', 81: 'sink', 82: 'refrigerator',
    84: 'book', 85: 'clock', 86: 'vase', 87: 'scissors', 88: 'teddy bear',
    89: 'hair drier', 90: 'toothbrush',
}  # pyformat: disable

def image_preprocess(image, image_size: Union[int, Tuple[int, int]]):
  """Preprocess image for inference.

  Args:
    image: input image, can be a tensor or a numpy arary.
    image_size: single integer of image size for square image or tuple of two
      integers, in the format of (image_height, image_width).

  Returns:
    (image, scale): a tuple of processed image and its scale.
  """
  input_processor = dataloader.DetectionInputProcessor(image, image_size)
  input_processor.normalize_image()
  input_processor.set_scale_factors_to_output_size()
  image = input_processor.resize_and_crop_image()
  image_scale = input_processor.image_scale_to_original
  return image, image_scale

@tf.autograph.to_graph
def batch_image_files_decode(image_files):
  raw_images = tf.TensorArray(tf.uint8, size=0, dynamic_size=True)
  for i in tf.range(tf.shape(image_files)[0]):
    image = tf.io.decode_image(image_files[i])
    image.set_shape([None, None, None])
    raw_images = raw_images.write(i, image)
  return raw_images.stack()

def batch_image_preprocess(raw_images,
                           image_size: Union[int, Tuple[int, int]],
                           batch_size: int = None):
  """Preprocess batched images for inference.

  Args:
    raw_images: a list of images, each image can be a tensor or a numpy arary.
    image_size: single integer of image size for square image or tuple of two
      integers, in the format of (image_height, image_width).
    batch_size: if None, use map_fn to deal with dynamic batch size.

  Returns:
    (image, scale): a tuple of processed images and scales.
  """
  if not batch_size:
    # map_fn is a little bit slower due to some extra overhead.
    map_fn = functools.partial(image_preprocess, image_size=image_size)
    images, scales = tf.map_fn(
        map_fn, raw_images, dtype=(tf.float32, tf.float32), back_prop=False)
    return (images, scales)

  # If batch size is known, use a simple loop.
  scales, images = [], []
  for i in range(batch_size):
    image, scale = image_preprocess(raw_images[i], image_size)
    scales.append(scale)
    images.append(image)
  images = tf.stack(images)
  scales = tf.stack(scales)
  return (images, scales)

def build_inputs(image_path_pattern: Text, image_size: Union[int, Tuple[int,
                                                                        int]]):
  """Read and preprocess input images.

  Args:
    image_path_pattern: a path to indicate a single or multiple files.
    image_size: single integer of image size for square image or tuple of two
      integers, in the format of (image_height, image_width).

  Returns:
    (raw_images, images, scales): raw images, processed images, and scales.

  Raises:
    ValueError if image_path_pattern doesn't match any file.
  """
  raw_images, images, scales = [], [], []
  for f in tf.io.gfile.glob(image_path_pattern):
    image = Image.open(f)
    raw_images.append(image)
    image, scale = image_preprocess(image, image_size)
    images.append(image)
    scales.append(scale)
  if not images:
    raise ValueError(
        'Cannot find any images for pattern {}'.format(image_path_pattern))
  return raw_images, tf.stack(images), tf.stack(scales)

def build_model(model_name: Text, inputs: tf.Tensor, **kwargs):
  """Build model for a given model name.

  Args:
    model_name: the name of the model.
    inputs: an image tensor or a numpy array.
    **kwargs: extra parameters for model builder.

  Returns:
    (cls_outputs, box_outputs): the outputs for class and box predictions.
    Each is a dictionary with key as feature level and value as predictions.
  """
  model_arch = det_model_fn.get_model_arch(model_name)
  mixed_precision = kwargs.get('mixed_precision', None)
  precision = utils.get_precision(kwargs.get('strategy', None), mixed_precision)
  cls_outputs, box_outputs = utils.build_model_with_precision(
      precision, model_arch, inputs, False, model_name, **kwargs)
  if mixed_precision:
    # Post-processing has multiple places with hard-coded float32.
    # TODO(tanmingxing): Remove them once post-process can adpat to dtypes.
    cls_outputs = {k: tf.cast(v, tf.float32) for k, v in cls_outputs.items()}
    box_outputs = {k: tf.cast(v, tf.float32) for k, v in box_outputs.items()}
  return cls_outputs, box_outputs

def restore_ckpt(sess, ckpt_path, ema_decay=0.9998, export_ckpt=None):
  """Restore variables from a given checkpoint.

  Args:
    sess: a tf session for restoring or exporting models.
    ckpt_path: the path of the checkpoint. Can be a file path or a folder path.
    ema_decay: ema decay rate. If None or zero or negative value, disable ema.
    export_ckpt: whether to export the restored model.
  """
  sess.run(tf.global_variables_initializer())
  if tf.io.gfile.isdir(ckpt_path):
    ckpt_path = tf.train.latest_checkpoint(ckpt_path)
  if ema_decay > 0:
    ema = tf.train.ExponentialMovingAverage(decay=0.0)
    ema_vars = utils.get_ema_vars()
    var_dict = ema.variables_to_restore(ema_vars)
    ema_assign_op = ema.apply(ema_vars)
  else:
    var_dict = utils.get_ema_vars()
    ema_assign_op = None

  tf.train.get_or_create_global_step()
  sess.run(tf.global_variables_initializer())
  saver = tf.train.Saver(var_dict, max_to_keep=1)
  if ckpt_path == '_':
    logging.info('Running test: do not load any ckpt.')
    return

  # Restore all variables from ckpt.
  saver.restore(sess, ckpt_path)

  if export_ckpt:
    print('export model to {}'.format(export_ckpt))
    if ema_assign_op is not None:
      sess.run(ema_assign_op)
    saver = tf.train.Saver(max_to_keep=1, save_relative_paths=True)
    saver.save(sess, export_ckpt)

def det_post_process_combined(params, cls_outputs, box_outputs, scales,
                              min_score_thresh, max_boxes_to_draw):
  """A combined version of det_post_process with dynamic batch size support."""
  batch_size = tf.shape(list(cls_outputs.values())[0])[0]
  cls_outputs_all = []
  box_outputs_all = []
  # Concatenates class and box of all levels into one tensor.
  for level in range(params['min_level'], params['max_level'] + 1):
    if params['data_format'] == 'channels_first':
      cls_outputs[level] = tf.transpose(cls_outputs[level], [0, 2, 3, 1])
      box_outputs[level] = tf.transpose(box_outputs[level], [0, 2, 3, 1])

    cls_outputs_all.append(
        tf.reshape(cls_outputs[level], [batch_size, -1, params['num_classes']]))
    box_outputs_all.append(tf.reshape(box_outputs[level], [batch_size, -1, 4]))
  cls_outputs_all = tf.concat(cls_outputs_all, 1)
  box_outputs_all = tf.concat(box_outputs_all, 1)

  # Create anchor_label for picking top-k predictions.
  eval_anchors = anchors.Anchors(params['min_level'], params['max_level'],
                                 params['num_scales'], params['aspect_ratios'],
                                 params['anchor_scale'], params['image_size'])
  anchor_boxes = eval_anchors.boxes
  scores = tf.math.sigmoid(cls_outputs_all)
  # apply bounding box regression to anchors
  boxes = anchors.decode_box_outputs_tf(box_outputs_all, anchor_boxes)
  boxes = tf.expand_dims(boxes, axis=2)
  scales = tf.expand_dims(scales, axis=-1)
  nmsed_boxes, nmsed_scores, nmsed_classes, valid_detections = (
      tf.image.combined_non_max_suppression(
          boxes,
          scores,
          max_boxes_to_draw,
          max_boxes_to_draw,
          score_threshold=min_score_thresh,
          clip_boxes=False))
  del valid_detections  # to be used in futue.

  image_ids = tf.cast(
      tf.tile(
          tf.expand_dims(tf.range(batch_size), axis=1), [1, max_boxes_to_draw]),
      dtype=scales.dtype)
  image_size = utils.parse_image_size(params['image_size'])
  ymin = tf.clip_by_value(nmsed_boxes[..., 0], 0, image_size[0]) * scales
  xmin = tf.clip_by_value(nmsed_boxes[..., 1], 0, image_size[1]) * scales
  ymax = tf.clip_by_value(nmsed_boxes[..., 2], 0, image_size[0]) * scales
  xmax = tf.clip_by_value(nmsed_boxes[..., 3], 0, image_size[1]) * scales

  classes = tf.cast(nmsed_classes + 1, scales.dtype)
  detection_list = [image_ids, ymin, xmin, ymax, xmax, nmsed_scores, classes]
  detections = tf.stack(detection_list, axis=2, name='detections')
  return detections

def det_post_process(params: Dict[Any, Any], cls_outputs: Dict[int, tf.Tensor],
                     box_outputs: Dict[int, tf.Tensor], scales: List[float],
                     min_score_thresh, max_boxes_to_draw):
  """Post preprocessing the box/class predictions.

  Args:
    params: a parameter dictionary that includes `min_level`, `max_level`,
      `batch_size`, and `num_classes`.
    cls_outputs: an OrderDict with keys representing levels and values
      representing logits in [batch_size, height, width, num_anchors].
    box_outputs: an OrderDict with keys representing levels and values
      representing box regression targets in [batch_size, height, width,
      num_anchors * 4].
    scales: a list of float values indicating image scale.
    min_score_thresh: A float representing the threshold for deciding when to
      remove boxes based on score.
    max_boxes_to_draw: Max number of boxes to draw.

  Returns:
    detections_batch: a batch of detection results. Each detection is a tensor
      with each row as [image_id, ymin, xmin, ymax, xmax, score, class].
  """
  if not params['batch_size']:
    # Use combined version for dynamic batch size.
    return det_post_process_combined(params, cls_outputs, box_outputs, scales,
                                     min_score_thresh, max_boxes_to_draw)

  # TODO(tanmingxing): refactor the code to make it more explicity.
  outputs = {
      'cls_outputs_all': [None],
      'box_outputs_all': [None],
      'indices_all': [None],
      'classes_all': [None]
  }
  det_model_fn.add_metric_fn_inputs(params, cls_outputs, box_outputs, outputs,
                                    -1)

  # Create anchor_label for picking top-k predictions.
  eval_anchors = anchors.Anchors(params['min_level'], params['max_level'],
                                 params['num_scales'], params['aspect_ratios'],
                                 params['anchor_scale'], params['image_size'])
  anchor_labeler = anchors.AnchorLabeler(eval_anchors, params['num_classes'])

  # Add all detections for each input image.
  detections_batch = []
  for index in range(params['batch_size']):
    cls_outputs_per_sample = outputs['cls_outputs_all'][index]
    box_outputs_per_sample = outputs['box_outputs_all'][index]
    indices_per_sample = outputs['indices_all'][index]
    classes_per_sample = outputs['classes_all'][index]
    detections = anchor_labeler.generate_detections(
        cls_outputs_per_sample,
        box_outputs_per_sample,
        indices_per_sample,
        classes_per_sample,
        image_id=[index],
        image_scale=[scales[index]],
        image_size=params['image_size'],
        min_score_thresh=min_score_thresh,
        max_boxes_to_draw=max_boxes_to_draw,
        disable_pyfun=params.get('disable_pyfun'))
    if params['batch_size'] > 1:
      # pad to fixed length if batch size > 1.
      padding_size = max_boxes_to_draw - tf.shape(detections)[0]
      detections = tf.pad(detections, [[0, padding_size], [0, 0]])
    detections_batch.append(detections)
  return tf.stack(detections_batch, name='detections')

def visualize_image(image,
                    boxes,
                    classes,
                    scores,
                    id_mapping,
                    min_score_thresh=anchors.MIN_SCORE_THRESH,
                    max_boxes_to_draw=anchors.MAX_DETECTIONS_PER_IMAGE,
                    line_thickness=2,
                    **kwargs):
  """Visualizes a given image.

  Args:
    image: a image with shape [H, W, C].
    boxes: a box prediction with shape [N, 4] ordered [ymin, xmin, ymax, xmax].
    classes: a class prediction with shape [N].
    scores: A list of float value with shape [N].
    id_mapping: a dictionary from class id to name.
    min_score_thresh: minimal score for showing. If claass probability is below
      this threshold, then the object will not show up.
    max_boxes_to_draw: maximum bounding box to draw.
    line_thickness: how thick is the bounding box line.
    **kwargs: extra parameters.

  Returns:
    output_image: an output image with annotated boxes and classes.
  """
  category_index = {k: {'id': k, 'name': id_mapping[k]} for k in id_mapping}
  img = np.array(image)
  vis_utils.visualize_boxes_and_labels_on_image_array(
      img,
      boxes,
      classes,
      scores,
      category_index,
      min_score_thresh=min_score_thresh,
      max_boxes_to_draw=max_boxes_to_draw,
      line_thickness=line_thickness,
      **kwargs)
  return img

def parse_label_id_mapping(
    label_id_mapping: Union[Text, Dict[int, Text]] = None) -> Dict[int, Text]:
  """Parse label id mapping from a string or a yaml file.

  The label_id_mapping is a dict that maps class id to its name, such as:

    {
      1: "person",
      2: "dog"
    }

  Args:
    label_id_mapping:

  Returns:
    A dictionary with key as integer id and value as a string of name.
  """
  if label_id_mapping is None:
    return coco_id_mapping

  if isinstance(label_id_mapping, dict):
    label_id_dict = label_id_mapping
  elif isinstance(label_id_mapping, str):
    with tf.io.gfile.GFile(label_id_mapping) as f:
      label_id_dict = yaml.load(f, Loader=yaml.FullLoader)
  else:
    raise TypeError('label_id_mapping must be a dict or a yaml filename, '
                    'containing a mapping from class ids to class names.')

  return label_id_dict

def visualize_image_prediction(image,
                               prediction,
                               disable_pyfun=True,
                               label_id_mapping=None,
                               **kwargs):
  """Viusalize detections on a given image.

  Args:
    image: Image content in shape of [height, width, 3].
    prediction: a list of vector, with each vector has the format of [image_id,
      ymin, xmin, ymax, xmax, score, class].
    disable_pyfun: disable pyfunc for faster post processing.
    label_id_mapping: a map from label id to name.
    **kwargs: extra parameters for vistualization, such as min_score_thresh,
      max_boxes_to_draw, and line_thickness.

  Returns:
    a list of annotated images.
  """
  boxes = prediction[:, 1:5]
  classes = prediction[:, 6].astype(int)
  scores = prediction[:, 5]

  if not disable_pyfun:
    # convert [x, y, width, height] to [y, x, height, width]
    boxes[:, [0, 1, 2, 3]] = boxes[:, [1, 0, 3, 2]]

  label_id_mapping = label_id_mapping or coco_id_mapping

  return visualize_image(image, boxes, classes, scores, label_id_mapping,
                         **kwargs)

class ServingDriver(object):
  """A driver for serving single or batch images.

  This driver supports serving with image files or arrays, with configurable
  batch size.

  Example 1. Serving streaming image contents:

    driver = inference.ServingDriver(
      'efficientdet-d0', '/tmp/efficientdet-d0', batch_size=1)
    driver.build()
    for m in image_iterator():
      predictions = driver.serve_files([m])
      driver.visualize(m, predictions[0])
      # m is the new image with annotated boxes.

  Example 2. Serving batch image contents:

    imgs = []
    for f in ['/tmp/1.jpg', '/tmp/2.jpg']:
      imgs.append(np.array(Image.open(f)))

    driver = inference.ServingDriver(
      'efficientdet-d0', '/tmp/efficientdet-d0', batch_size=len(imgs))
    driver.build()
    predictions = driver.serve_images(imgs)
    for i in range(len(imgs)):
      driver.visualize(imgs[i], predictions[i])

  Example 3: another way is to use SavedModel:

    # step1: export a model.
    driver = inference.ServingDriver('efficientdet-d0', '/tmp/efficientdet-d0')
    driver.build()
    driver.export('/tmp/saved_model_path')

    # step2: Serve a model.
    with tf.Session() as sess:
      tf.saved_model.load(sess, ['serve'], self.saved_model_dir)
      raw_images = []
      for f in tf.io.gfile.glob('/tmp/images/*.jpg'):
        raw_images.append(np.array(PIL.Image.open(f)))
      detections = sess.run('detections:0', {'image_arrays:0': raw_images})
      driver = inference.ServingDriver(
        'efficientdet-d0', '/tmp/efficientdet-d0')
      driver.visualize(raw_images[0], detections[0])
      PIL.Image.fromarray(raw_images[0]).save(output_image_path)
  """

  def __init__(self,
               model_name: Text,
               ckpt_path: Text,
               batch_size: int = 1,
               use_xla: bool = False,
               min_score_thresh: float = None,
               max_boxes_to_draw: float = None,
               line_thickness: int = None,
               model_params: Dict[Text, Any] = None):
    """Initialize the inference driver.

    Args:
      model_name: target model name, such as efficientdet-d0.
      ckpt_path: checkpoint path, such as /tmp/efficientdet-d0/.
      batch_size: batch size for inference.
      use_xla: Whether run with xla optimization.
      min_score_thresh: minimal score threshold for filtering predictions.
      max_boxes_to_draw: the maximum number of boxes per image.
      line_thickness: the line thickness for drawing boxes.
      model_params: model parameters for overriding the config.
    """
    self.model_name = model_name
    self.ckpt_path = ckpt_path
    self.batch_size = batch_size

    self.params = hparams_config.get_detection_config(model_name).as_dict()

    if model_params:
      self.params.update(model_params)
    self.params.update(dict(is_training_bn=False))
    self.label_id_mapping = parse_label_id_mapping(
        self.params.get('label_id_mapping', None))

    self.signitures = None
    self.sess = None
    self.disable_pyfun = True
    self.use_xla = use_xla

    self.min_score_thresh = (min_score_thresh if min_score_thresh is not None
                             else anchors.MIN_SCORE_THRESH)
    self.max_boxes_to_draw = (
        max_boxes_to_draw or anchors.MAX_DETECTIONS_PER_IMAGE)
    self.line_thickness = line_thickness

  def __del__(self):
    if self.sess:
      self.sess.close()

  def _build_session(self):
    sess_config = tf.ConfigProto()
    if self.use_xla:
      sess_config.graph_options.optimizer_options.global_jit_level = (
          tf.OptimizerOptions.ON_2)
    return tf.Session(config=sess_config)

  def build(self, params_override=None):
    """Build model and restore checkpoints."""
    params = copy.deepcopy(self.params)
    if params_override:
      params.update(params_override)

    if not self.sess:
      self.sess = self._build_session()
    with self.sess.graph.as_default():
      image_files = tf.placeholder(tf.string, name='image_files', shape=[None])
      raw_images = batch_image_files_decode(image_files)
      raw_images = tf.identity(raw_images, name='image_arrays')
      images, scales = batch_image_preprocess(raw_images, params['image_size'],
                                              self.batch_size)
      if params['data_format'] == 'channels_first':
        images = tf.transpose(images, [0, 3, 1, 2])
      class_outputs, box_outputs = build_model(self.model_name, images,
                                               **params)
      params.update(
          dict(batch_size=self.batch_size, disable_pyfun=self.disable_pyfun))
      detections = det_post_process(params, class_outputs, box_outputs, scales,
                                    self.min_score_thresh,
                                    self.max_boxes_to_draw)

      restore_ckpt(
          self.sess,
          self.ckpt_path,
          ema_decay=self.params['moving_average_decay'],
          export_ckpt=None)

    self.signitures = {
        'image_files': image_files,
        'image_arrays': raw_images,
        'prediction': detections,
    }
    return self.signitures

  def visualize(self, image, prediction, **kwargs):
    """Visualize prediction on image."""
    return visualize_image_prediction(
        image,
        prediction,
        disable_pyfun=self.disable_pyfun,
        label_id_mapping=self.label_id_mapping,
        **kwargs)

  def serve_files(self, image_files: List[Text]):
    """Serve a list of input image files.

    Args:
      image_files: a list of image files with shape [1] and type string.

    Returns:
      A list of detections.
    """
    if not self.sess:
      self.build()
    predictions = self.sess.run(
        self.signitures['prediction'],
        feed_dict={self.signitures['image_files']: image_files})
    return predictions

  def benchmark(self, image_arrays, trace_filename=None):
    """Benchmark inference latency/throughput.

    Args:
      image_arrays: a list of images in numpy array format.
      trace_filename: If None, specify the filename for saving trace.
    """
    if not self.sess:
      self.build()

    # init session
    self.sess.run(
        self.signitures['prediction'],
        feed_dict={self.signitures['image_arrays']: image_arrays})

    start = time.perf_counter()
    for _ in range(10):
      self.sess.run(
          self.signitures['prediction'],
          feed_dict={self.signitures['image_arrays']: image_arrays})
    end = time.perf_counter()
    inference_time = (end - start) / 10

    print('Per batch inference time: ', inference_time)
    print('FPS: ', self.batch_size / inference_time)

    if trace_filename:
      run_options = tf.RunOptions()
      run_options.trace_level = tf.RunOptions.FULL_TRACE
      run_metadata = tf.RunMetadata()
      self.sess.run(
          self.signitures['prediction'],
          feed_dict={self.signitures['image_arrays']: image_arrays},
          options=run_options,
          run_metadata=run_metadata)
      with tf.io.gfile.GFile(trace_filename, 'w') as trace_file:
        trace = timeline.Timeline(step_stats=run_metadata.step_stats)
        trace_file.write(trace.generate_chrome_trace_format(show_memory=True))

  def serve_images(self, image_arrays):
    """Serve a list of image arrays.

    Args:
      image_arrays: A list of image content with each image has shape [height,
        width, 3] and uint8 type.

    Returns:
      A list of detections.
    """
    if not self.sess:
      self.build()
    predictions = self.sess.run(
        self.signitures['prediction'],
        feed_dict={self.signitures['image_arrays']: image_arrays})
    return predictions

  def load(self, saved_model_dir_or_frozen_graph: Text):
    """Load the model using saved model or a frozen graph."""
    if not self.sess:
      self.sess = self._build_session()
    self.signitures = {
        'image_files': 'image_files:0',
        'image_arrays': 'image_arrays:0',
        'prediction': 'detections:0',
    }

    # Load saved model if it is a folder.
    if tf.io.gfile.isdir(saved_model_dir_or_frozen_graph):
      return tf.saved_model.load(self.sess, ['serve'],
                                 saved_model_dir_or_frozen_graph)

    # Load a frozen graph.
    graph_def = tf.GraphDef()
    with tf.gfile.GFile(saved_model_dir_or_frozen_graph, 'rb') as f:
      graph_def.ParseFromString(f.read())
    return tf.import_graph_def(graph_def, name='')

  def freeze(self):
    """Freeze the graph."""
    output_names = [self.signitures['prediction'].op.name]
    graphdef = tf.graph_util.convert_variables_to_constants(
        self.sess, self.sess.graph_def, output_names)
    return graphdef

  def export(self,
             output_dir: Text,
             tflite_path: Text = None,
             tensorrt: Text = None):
    """Export a saved model, frozen graph, and potential tflite/tensorrt model.

    Args:
      output_dir: the output folder for saved model.
      tflite_path: the path for saved tflite file.
      tensorrt: If not None, must be {'FP32', 'FP16', 'INT8'}.
    """
    signitures = self.signitures
    signature_def_map = {
        'serving_default':
            tf.saved_model.predict_signature_def(
                {signitures['image_arrays'].name: signitures['image_arrays']},
                {signitures['prediction'].name: signitures['prediction']}),
    }
    b = tf.saved_model.Builder(output_dir)
    b.add_meta_graph_and_variables(
        self.sess,
        tags=['serve'],
        signature_def_map=signature_def_map,
        assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS),
        clear_devices=True)
    b.save()
    logging.info('Model saved at %s', output_dir)

    # also save freeze pb file.
    graphdef = self.freeze()
    pb_path = os.path.join(output_dir, self.model_name + '_frozen.pb')
    tf.io.gfile.GFile(pb_path, 'wb').write(graphdef.SerializeToString())
    logging.info('Frozen graph saved at %s', pb_path)

    if tflite_path:
      height, width = utils.parse_image_size(self.params['image_size'])
      input_name = signitures['image_arrays'].op.name
      input_shapes = {input_name: [None, height, width, 3]}
      converter = tf.lite.TFLiteConverter.from_saved_model(
          output_dir,
          input_arrays=[input_name],
          input_shapes=input_shapes,
          output_arrays=[signitures['prediction'].op.name])
      converter.experimental_new_converter = True
      converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]
      tflite_model = converter.convert()

      tf.io.gfile.GFile(tflite_path, 'wb').write(tflite_model)
      logging.info('TFLite is saved at %s', tflite_path)

    if tensorrt:
      from tensorflow.python.compiler.tensorrt import trt  # pylint: disable=g-direct-tensorflow-import,g-import-not-at-top
      sess_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
      trt_path = os.path.join(output_dir, 'tensorrt_' + tensorrt.lower())
      trt.create_inference_graph(
          None,
          None,
          precision_mode=tensorrt,
          input_saved_model_dir=output_dir,
          output_saved_model_dir=trt_path,
          session_config=sess_config)
      logging.info('TensorRT model is saved at %s', trt_path)

class InferenceDriver(object):
  """A driver for doing batch inference.

  Example usage:

   driver = inference.InferenceDriver('efficientdet-d0', '/tmp/efficientdet-d0')
   driver.inference('/tmp/*.jpg', '/tmp/outputdir')

  """

  def __init__(self,
               model_name: Text,
               ckpt_path: Text,
               model_params: Dict[Text, Any] = None):
    """Initialize the inference driver.

    Args:
      model_name: target model name, such as efficientdet-d0.
      ckpt_path: checkpoint path, such as /tmp/efficientdet-d0/.
      model_params: model parameters for overriding the config.
    """
    self.model_name = model_name
    self.ckpt_path = ckpt_path
    self.params = hparams_config.get_detection_config(model_name).as_dict()
    if model_params:
      self.params.update(model_params)
    self.params.update(dict(is_training_bn=False))
    self.label_id_mapping = parse_label_id_mapping(
        self.params.get('label_id_mapping', None))

    self.disable_pyfun = True

  def inference(self, image_path_pattern: Text, output_dir: Text, **kwargs):
    """Read and preprocess input images.

    Args:
      image_path_pattern: Image file pattern such as /tmp/img*.jpg
      output_dir: the directory for output images. Output images will be named
        as 0.jpg, 1.jpg, ....
      **kwargs: extra parameters for for vistualization, such as
        min_score_thresh, max_boxes_to_draw, and line_thickness.

    Returns:
      Annotated image.
    """
    params = copy.deepcopy(self.params)
    with tf.Session() as sess:
      # Buid inputs and preprocessing.
      raw_images, images, scales = build_inputs(image_path_pattern,
                                                params['image_size'])
      if params['data_format'] == 'channels_first':
        images = tf.transpose(images, [0, 3, 1, 2])
      # Build model.
      class_outputs, box_outputs = build_model(self.model_name, images,
                                               **self.params)
      restore_ckpt(
          sess,
          self.ckpt_path,
          ema_decay=self.params['moving_average_decay'],
          export_ckpt=None)

      # for postprocessing.
      params.update(
          dict(batch_size=len(raw_images), disable_pyfun=self.disable_pyfun))

      # Build postprocessing.
      detections_batch = det_post_process(
          params,
          class_outputs,
          box_outputs,
          scales,
          min_score_thresh=kwargs.get('min_score_thresh',
                                      anchors.MIN_SCORE_THRESH),
          max_boxes_to_draw=kwargs.get('max_boxes_to_draw',
                                       anchors.MAX_DETECTIONS_PER_IMAGE))
      predictions = sess.run(detections_batch)
      # Visualize results.
      for i, prediction in enumerate(predictions):
        img = visualize_image_prediction(
            raw_images[i],
            prediction,
            disable_pyfun=self.disable_pyfun,
            label_id_mapping=self.label_id_mapping,
            **kwargs)
        output_image_path = os.path.join(output_dir, str(i) + '.jpg')
        Image.fromarray(img).save(output_image_path)
        logging.info('writing file to %s', output_image_path)

      return predictions

max_stars_repo_name: bessszilard/automl

max_issues_repo_name: IspML/automl

max_forks_repo_name: IspML/automl


text:
#!/usr/bin/env python
# coding: utf-8

# In[1]:

# environment set up
import tensorflow as tf
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Reshape, Conv1D, MaxPooling1D, Flatten, Dense, Dropout
from tensorflow.keras.utils import to_categorical
import numpy as np
import matplotlib.pyplot as plt
import os
import random
import pandas as pd

# working folder
directory = os.environ['DATA_DIR']
os.chdir(directory)

# In[ ]:

max_stars_repo_path: lab-notebook/smunukutla/2019-11-25-SAM - Fully Connected Raw Data.py

max_stars_repo_name: velexi-corporation/spectra-ml

max_issues_repo_name: velexi-corporation/spectra-ml

max_forks_repo_name: velexi-corporation/spectra-ml


text:
# coding=utf-8
# Copyright 2022 The TensorFlow Datasets Authors.
#
# 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
#
#     http://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.

"""Radon dataset."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections

import numpy as np
import six.moves.urllib as urllib
import tensorflow as tf
import tensorflow_datasets.public_api as tfds

BASE_URL = 'http://www.stat.columbia.edu/~gelman/arm/examples/radon/'

_CITATION = """\
@book{GelmanHill:2007,
  author = {Gelman, Andrew and Hill, Jennifer},
  title = {Data Analysis Using Regression and Multilevel/Hierarchical Models},
  publisher = {Cambridge University Press},
  series = {Analytical methods for social research},
  year = 2007
}
"""

_DESCRIPTION = """Radon is a radioactive gas that enters homes through contact
points with the ground. It is a carcinogen that is the primary cause of lung
cancer in non-smokers. Radon levels vary greatly from household to household.
This dataset contains measured radon levels in U.S homes by county and state.
The 'activity' label is the measured radon concentration in pCi/L. Important
predictors are 'floor' (the floor of the house in which the measurement was
taken), 'county' (the U.S. county in which the house is located), and 'Uppm' (a
measurement of uranium level of the soil by county)."""

def convert_to_int(d):
  return np.int32(d)

def convert_to_float(d):
  return np.float32(d)

def return_same(d):
  return d

FEATURES = collections.OrderedDict([
    ('idnum', (tf.int32, convert_to_int)),
    ('state', (tf.string, return_same)),
    ('state2', (tf.string, return_same)),
    ('stfips', (tf.int32, convert_to_int)),
    ('zip', (tf.int32, convert_to_int)),
    ('region', (tf.int32, convert_to_int)),
    ('typebldg', (tf.int32, convert_to_int)),
    ('floor', (tf.int32, convert_to_int)),
    ('room', (tf.int32, convert_to_int)),
    ('basement', (tf.string, return_same)),
    ('windoor', (tf.string, return_same)),
    ('rep', (tf.int32, convert_to_int)),
    ('stratum', (tf.int32, convert_to_int)),
    ('wave', (tf.int32, convert_to_int)),
    ('starttm', (tf.int32, convert_to_int)),
    ('stoptm', (tf.int32, convert_to_int)),
    ('startdt', (tf.int32, convert_to_int)),
    ('stopdt', (tf.int32, convert_to_int)),
    ('pcterr', (tf.float32, convert_to_float)),
    ('adjwt', (tf.float32, convert_to_float)),
    ('dupflag', (tf.int32, convert_to_int)),
    ('zipflag', (tf.int32, convert_to_int)),
    ('cntyfips', (tf.int32, convert_to_int)),
    ('county', (tf.string, return_same)),
    ('Uppm', (tf.float32, convert_to_float)),
    ('lon', (tf.float32, convert_to_float)),
    ('lat', (tf.float32, convert_to_float)),
])

class Radon(tfds.core.GeneratorBasedBuilder):
  """Radon dataset."""

  VERSION = tfds.core.Version('1.0.0')

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=_DESCRIPTION,
        features=tfds.features.FeaturesDict({
            'activity': tf.float32,
            'features': {name: dtype for name, (dtype, _) in FEATURES.items()}
        }),
        supervised_keys=('features', 'activity'),
        homepage='http://www.stat.columbia.edu/~gelman/arm/examples/radon/',
        citation=_CITATION,
    )

  def _split_generators(self, dl_manager):
    """Returns SplitGenerators."""
    paths = dl_manager.download({
        'file_path_srrs2': urllib.parse.urljoin(BASE_URL, 'srrs2.dat'),
        'file_path_cty': urllib.parse.urljoin(BASE_URL, 'cty.dat')
    })
    return [
        tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN,
            gen_kwargs=paths,
        ),
    ]

  def _generate_examples(self, file_path_srrs2, file_path_cty):
    """Yields examples."""
    pd = tfds.core.lazy_imports.pandas

    with tf.io.gfile.GFile(file_path_srrs2) as f:
      df_srrs2 = pd.read_csv(f)
    with tf.io.gfile.GFile(file_path_cty) as f:
      df_cty = pd.read_csv(f)

    df_srrs2.rename(columns=str.strip, inplace=True)
    df_cty.rename(columns=str.strip, inplace=True)

    # We will now join datasets on Federal Information Processing Standards
    # (FIPS) id, ie, codes that link geographic units, counties and county
    # equivalents. http://jeffgill.org/Teaching/rpqm_9.pdf
    df_srrs2['fips'] = 1000 * df_srrs2.stfips + df_srrs2.cntyfips
    df_cty['fips'] = 1000 * df_cty.stfips + df_cty.ctfips

    df = df_srrs2.merge(df_cty[['fips', 'Uppm', 'lon', 'lat']], on='fips')
    df = df.drop_duplicates(subset='idnum')
    df.drop('fips', axis=1, inplace=True)

    df['wave'].replace({'  .': '-1'}, inplace=True)
    df['rep'].replace({' .': '-1'}, inplace=True)
    df['zip'].replace({'     ': '-1'}, inplace=True)

    for i, (_, row) in enumerate(df.iterrows()):
      radon_val = row.pop('activity')
      yield i, {
          'activity': float(radon_val),
          'features': {
              name: FEATURES[name][1](value) for name, value in row.items()
          }
      }


text:
import time
import numpy as np
start_time = None

def new_explosion():
    global start_time
    N = PINS.N.get()
    if PINS.uniforms.defined:
        uniforms = PINS.uniforms.get()
    else:
        uniforms = {}

    # New centerpos
    centerpos = np.random.uniform(-0.5, 0.5, (3,))
    uniforms['u_centerPosition'] = tuple(centerpos)

    alpha = 1.0 / N ** 0.08
    color = np.random.uniform(0.1, 0.9, (3,))
    uniforms['u_color'] = tuple(color) + (alpha,)

    gravity = PINS.gravity.get()
    uniforms['u_gravity'] = gravity

    shrink_with_age = PINS.shrink_with_age.get()
    uniforms['u_shrink_with_age'] = shrink_with_age

    start_time = time.time()
    uniforms['u_time'] = 0
    PINS.uniforms.set(uniforms)

def update():
    if start_time is None:
        return
    uniforms = PINS.uniforms.get()
    curr_time = time.time() - start_time
    uniforms['u_time'] = curr_time
    pointsize = PINS.pointsize.get()
    uniforms['u_pointsize'] = pointsize
    PINS.uniforms.set(uniforms)
    PINS.updated.set()