Instruction:
Create a Python 3 function to
initialize the correct environment variables for spark

Response:
def _set_environment_variables(self):
        """Initializes the correct environment variables for spark"""
        cmd = []

        # special case for driver JVM properties.
        self._set_launcher_property("driver-memory", "spark.driver.memory")
        self._set_launcher_property("driver-library-path", "spark.driver.extraLibraryPath")
        self._set_launcher_property("driver-class-path", "spark.driver.extraClassPath")
        self._set_launcher_property("driver-java-options", "spark.driver.extraJavaOptions")
        self._set_launcher_property("executor-memory", "spark.executor.memory")
        self._set_launcher_property("executor-cores", "spark.executor.cores")

        for key, val in self._spark_launcher_args.items():
            if val is None:
                continue
            val = list(as_iterable(val))
            if len(val):
                if key in self._boolean_args:
                    cmd.append("--{key}".format(key=key))
                else:
                    sep = self._spark_launcher_arg_sep.get(key, ',')
                    cmd.append('--{key} {val}'.format(key=key, val=sep.join(str(x) for x in val)))

        cmd += ['pyspark-shell']
        cmd_line = ' '.join(x for x in cmd if x)
        os.environ["PYSPARK_SUBMIT_ARGS"] = cmd_line
        log.info("spark-submit arguments: %s", cmd_line)


Instruction:
Write a Python 3 script for
initializing spark so that pyspark is importable.

Response:
def _init_spark(self):
        """Initializes spark so that pyspark is importable.  This also sets up the required environment variables
        """
        global _SPARK_INITIALIZED
        spark_home = self.spark_home
        python_path = self._python_path

        if use_findspark:
            if _SPARK_INITIALIZED:
                if spark_home == os.environ["SPARK_HOME"]:
                    # matches with already initialized
                    pass
                else:
                    # findspark adds two path to the search path.
                    sys.path.pop(0)
                    sys.path.pop(0)
                    findspark.init(spark_home=spark_home, edit_rc=False, edit_profile=False, python_path=python_path)
            else:
                findspark.init(spark_home=spark_home, edit_rc=False, edit_profile=False, python_path=python_path)

        _SPARK_INITIALIZED = True
        self._set_environment_variables()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def spark_context(self, application_name):

        # initialize the spark configuration
        self._init_spark()
        import pyspark
        import pyspark.sql

        # initialize conf
        spark_conf = pyspark.SparkConf()
        for k, v in self._spark_conf_helper._conf_dict.items():
            spark_conf.set(k, v)

        log.info("Starting SparkContext")
        return pyspark.SparkContext(appName=application_name, conf=spark_conf)

Response: Create a SparkContext given the parameters configured in this class.


Instruction:
Create a Python 3 function for
creating a SparkContext object given the parameters configured in this class and application_name.

Response:
def sql_context(self, application_name):
        """Create a spark context given the parameters configured in this class.

        The caller is responsible for calling ``.close`` on the resulting spark context

        Parameters
        ----------
        application_name : string

        Returns
        -------
        sc : SparkContext
        """
        sc = self.spark_context(application_name)
        import pyspark
        sqlContext = pyspark.SQLContext(sc)
        return (sc, sqlContext)


Instruction:
Given the following Python 3 function, write the documentation
def call(self, uri, params=None, token=None, risk_token=None, client_ip=None):

        headers = {'Content-Type': 'application/json',
                   'User-Agent': 'WePay Python SDK'}
        url = self.api_endpoint + uri

        if self.access_token or token:
            headers['Authorization'] = 'Bearer ' + \
                (token if token else self.access_token)

        if self.api_version:
            headers['Api-Version'] = self.api_version

        if risk_token:
            headers['WePay-Risk-Token'] = risk_token

        if client_ip:
            headers['Client-IP'] = client_ip

        if params:
            params = json.dumps(params)

        try:
            response = self.requests.post(
                url, data=params, headers=headers,
                timeout=self.request_timeout)
            return response.json()
        except:
            if 400 <= response.status_code <= 599:
                raise Exception('Unknown error. Please contact support@wepay.com')

Response: Calls wepay. com and returns the JSON response.


Instruction:
Write a Python 3 script to
return the URL to send the user to in order to get authorization.

Response:
def get_authorization_url(self, redirect_uri, client_id, options=None,
                              scope=None):
        """
        Returns a URL to send the user to in order to get authorization.
        After getting authorization the user will return to redirect_uri.
        Optionally, scope can be set to limit permissions, and the options
        dict can be loaded with any combination of state, user_name
        or user_email.

        :param str redirect_uri: The URI to redirect to after a authorization.
        :param str client_id: The client ID issued by WePay to your app.
        :keyword dict options: Allows for passing additional values to the
            authorize call, aside from scope, redirect_uri, and etc.
        :keyword str scope: A comma-separated string of permissions.
        """
        if not options:
            options = {}
        if not scope:
            scope = "manage_accounts,collect_payments," \
                    "view_user,preapprove_payments," \
                    "manage_subscriptions,send_money"

        options['scope'] = scope
        options['redirect_uri'] = redirect_uri
        options['client_id'] = client_id

        return self.browser_endpoint + '/oauth2/authorize?' + \
            urllib.urlencode(options)


Instruction:
Make a summary of the following Python 3 code
def get_token(
            self, redirect_uri, client_id, client_secret,
            code, callback_uri=None):
        """
        Calls wepay.com/v2/oauth2/token to get an access token. Sets the
        access_token for the WePay instance and returns the entire response
        as a dict. Should only be called after the user returns from being
        sent to get_authorization_url.

        :param str redirect_uri: The same URI specified in the
            :py:meth:`get_authorization_url` call that preceeded this.
        :param str client_id: The client ID issued by WePay to your app.
        :param str client_secret: The client secret issued by WePay
            to your app.
        :param str code: The code returned by :py:meth:`get_authorization_url`.
        :param str callback_uri: The callback_uri you want to receive IPNs for
            this user on.
        """
        params = {
            'redirect_uri': redirect_uri,
            'client_id': client_id,
            'client_secret': client_secret,
            'code': code,
        }
        if callback_uri:
            params.update({'callback_uri': callback_uri})
        response = self.call('/oauth2/token', params)

        # The call to /oauth2/token should return an access_token
        # if the access_token was not returned, then an error occured
        # we need to raise this error,
        # otherwise this will die when trying to use the 'access_token' field
        if 'access_token' not in response:
            raise WePayError(response['error'], response['error_code'], response['error_description'])

        self.access_token = response['access_token']
        return response

Response: Get an access token for the WePay instance.


Instruction:
Can you implement a function in Python 3 that
creates a blank figure.

Response:
def figure(bgcolor=(1,1,1), size=(1000,1000)):
        """Create a blank figure.

        Parameters
        ----------
        bgcolor : (3,) float
           Color of the background with values in [0,1].
        size : (2,) int
           Width and height of the figure in pixels.
        """
        Visualizer3D._scene = Scene(background_color=np.array(bgcolor))
        Visualizer3D._scene.ambient_light = AmbientLight(color=[1.0, 1.0, 1.0], strength=1.0)
        Visualizer3D._init_size = np.array(size)


Instruction:
Can you create a Python 3 function that
displays the current figure and enable interaction.

Response:
def show(animate=False, axis=np.array([0.,0.,1.]), clf=True, **kwargs):
        """Display the current figure and enable interaction.

        Parameters
        ----------
        animate : bool
            Whether or not to animate the scene.
        axis : (3,) float or None
            If present, the animation will rotate about the given axis in world coordinates.
            Otherwise, the animation will rotate in azimuth.
        clf : bool
            If true, the Visualizer is cleared after showing the figure.
        kwargs : dict
            Other keyword arguments for the SceneViewer instance.
        """
        x = SceneViewer(Visualizer3D._scene,
                        size=Visualizer3D._init_size,
                        animate=animate,
                        animate_axis=axis,
                        save_directory=Visualizer3D._save_directory,
                        **kwargs)
        if x.save_directory:
            Visualizer3D._save_directory = x.save_directory
        if clf:
            Visualizer3D.clf()


Instruction:
Explain what the following Python 3 code does
def render(n_frames=1, axis=np.array([0.,0.,1.]), clf=True, **kwargs):
        v = SceneViewer(Visualizer3D._scene,
                        size=Visualizer3D._init_size,
                        animate=(n_frames > 1),
                        animate_axis=axis,
                        max_frames=n_frames,
                        **kwargs)

        if clf:
            Visualizer3D.clf()

        return v.saved_frames

Response: Render frames from the viewer.


Instruction:
Create a Python 3 function to
save frames from the viewer out to a file.

Response:
def save(filename, n_frames=1, axis=np.array([0.,0.,1.]), clf=True, **kwargs):
        """Save frames from the viewer out to a file.

        Parameters
        ----------
        filename : str
            The filename in which to save the output image. If more than one frame,
            should have extension .gif.
        n_frames : int
            Number of frames to render. If more than one, the scene will animate.
        axis : (3,) float or None
            If present, the animation will rotate about the given axis in world coordinates.
            Otherwise, the animation will rotate in azimuth.
        clf : bool
            If true, the Visualizer is cleared after rendering the figure.
        kwargs : dict
            Other keyword arguments for the SceneViewer instance.
        """
        if n_frames >1 and os.path.splitext(filename)[1] != '.gif':
            raise ValueError('Expected .gif file for multiple-frame save.')
        v = SceneViewer(Visualizer3D._scene,
                        size=Visualizer3D._init_size,
                        animate=(n_frames > 1),
                        animate_axis=axis,
                        max_frames=n_frames,
                        **kwargs)
        data = [m.data for m in v.saved_frames]
        if len(data) > 1:
            imageio.mimwrite(filename, data, fps=v._animate_rate, palettesize=128, subrectangles=True)
        else:
            imageio.imwrite(filename, data[0])

        if clf:
            Visualizer3D.clf()


Instruction:
Can you tell what is the following Python 3 function doing
def save_loop(filename, framerate=30, time=3.0, axis=np.array([0.,0.,1.]), clf=True, **kwargs):
        n_frames = framerate * time
        az = 2.0 * np.pi / n_frames
        Visualizer3D.save(filename, n_frames=n_frames, axis=axis, clf=clf,
                          animate_rate=framerate, animate_az=az)
        if clf:
            Visualizer3D.clf()

Response: Off - screen save a loop of 3D.


Instruction:
Explain what the following Python 3 code does
def clf():
        Visualizer3D._scene = Scene(background_color=Visualizer3D._scene.background_color)
        Visualizer3D._scene.ambient_light = AmbientLight(color=[1.0, 1.0, 1.0], strength=1.0)

Response: Clear the current figure figure


Instruction:
Can you generate a brief explanation for the following Python 3 code
def points(points, T_points_world=None, color=np.array([0,1,0]), scale=0.01, n_cuts=20, subsample=None, random=False, name=None):
        if isinstance(points, BagOfPoints):
            if points.dim != 3:
                raise ValueError('BagOfPoints must have dimension 3xN!')
        else:
            if type(points) is not np.ndarray:
                raise ValueError('Points visualizer expects BagOfPoints or numpy array!')
            if len(points.shape) == 1:
                points = points[:,np.newaxis].T
            if len(points.shape) != 2 or points.shape[1] != 3:
                raise ValueError('Numpy array of points must have dimension (N,3)')
            frame = 'points'
            if T_points_world:
                frame = T_points_world.from_frame
            points = PointCloud(points.T, frame=frame)

        color = np.array(color)
        if subsample is not None:
            num_points = points.num_points
            points, inds = points.subsample(subsample, random=random)
            if color.shape[0] == num_points and color.shape[1] == 3:
                color = color[inds,:]

        # transform into world frame
        if points.frame != 'world':
            if T_points_world is None:
                T_points_world = RigidTransform(from_frame=points.frame, to_frame='world')
            points_world = T_points_world * points
        else:
            points_world = points

        point_data = points_world.data
        if len(point_data.shape) == 1:
            point_data = point_data[:,np.newaxis]
        point_data = point_data.T

        mpcolor = color
        if len(color.shape) > 1:
            mpcolor = color[0]
        mp = MaterialProperties(
            color = np.array(mpcolor),
            k_a = 0.5,
            k_d = 0.3,
            k_s = 0.0,
            alpha = 10.0,
            smooth=True
        )

        # For each point, create a sphere of the specified color and size.
        sphere = trimesh.creation.uv_sphere(scale, [n_cuts, n_cuts])
        raw_pose_data = np.tile(np.eye(4), (points.num_points, 1))
        raw_pose_data[3::4, :3] = point_data

        instcolor = None
        if color.ndim == 2 and color.shape[0] == points.num_points and color.shape[1] == 3:
            instcolor = color
        obj = InstancedSceneObject(sphere, raw_pose_data=raw_pose_data, colors=instcolor, material=mp)
        if name is None:
            name = str(uuid.uuid4())
        Visualizer3D._scene.add_object(name, obj)

Response: Scatter a point cloud in pose T_points_world.


Instruction:
Explain what the following Python 3 code does
def mesh(mesh, T_mesh_world=RigidTransform(from_frame='obj', to_frame='world'),
             style='surface', smooth=False, color=(0.5,0.5,0.5), name=None):
        """Visualize a 3D triangular mesh.

        Parameters
        ----------
        mesh : trimesh.Trimesh
            The mesh to visualize.
        T_mesh_world : autolab_core.RigidTransform
            The pose of the mesh, specified as a transformation from mesh frame to world frame.
        style : str
            Triangular mesh style, either 'surface' or 'wireframe'.
        smooth : bool
            If true, the mesh is smoothed before rendering.
        color : 3-tuple
            Color tuple.
        name : str
            A name for the object to be added.
        """
        if not isinstance(mesh, trimesh.Trimesh):
            raise ValueError('Must provide a trimesh.Trimesh object')

        mp = MaterialProperties(
            color = np.array(color),
            k_a = 0.5,
            k_d = 0.3,
            k_s = 0.1,
            alpha = 10.0,
            smooth=smooth,
            wireframe=(style == 'wireframe')
        )

        obj = SceneObject(mesh, T_mesh_world, mp)
        if name is None:
            name = str(uuid.uuid4())
        Visualizer3D._scene.add_object(name, obj)

Response: Visualize a 3D triangular mesh.


Instruction:
Write a Python 3 script to
visualize a mesh in a stable pose.

Response:
def mesh_stable_pose(mesh, T_obj_table,
                         T_table_world=RigidTransform(from_frame='table', to_frame='world'),
                         style='wireframe', smooth=False, color=(0.5,0.5,0.5),
                         dim=0.15, plot_table=True, plot_com=False, name=None):
        """Visualize a mesh in a stable pose.

        Parameters
        ----------
        mesh : trimesh.Trimesh
            The mesh to visualize.
        T_obj_table : autolab_core.RigidTransform
            Pose of object relative to table.
        T_table_world : autolab_core.RigidTransform
            Pose of table relative to world.
        style : str
            Triangular mesh style, either 'surface' or 'wireframe'.
        smooth : bool
            If true, the mesh is smoothed before rendering.
        color : 3-tuple
            Color tuple.
        dim : float
            The side-length for the table.
        plot_table : bool
            If true, a table is visualized as well.
        plot_com : bool
            If true, a ball is visualized at the object's center of mass.
        name : str
            A name for the object to be added.

        Returns
        -------
        autolab_core.RigidTransform
            The pose of the mesh in world frame.
        """
        T_obj_table = T_obj_table.as_frames('obj', 'table')
        T_obj_world = T_table_world * T_obj_table

        Visualizer3D.mesh(mesh, T_obj_world, style=style, smooth=smooth, color=color, name=name)
        if plot_table:
            Visualizer3D.table(T_table_world, dim=dim)
        if plot_com:
            Visualizer3D.points(Point(np.array(mesh.center_mass), 'obj'), T_obj_world, scale=0.01)
        return T_obj_world


Instruction:
Implement a Python 3 function for
plotting a 3D pose of the current species.

Response:
def pose(T_frame_world, alpha=0.1, tube_radius=0.005, center_scale=0.01):
        """Plot a 3D pose as a set of axes (x red, y green, z blue).

        Parameters
        ----------
        T_frame_world : autolab_core.RigidTransform
            The pose relative to world coordinates.
        alpha : float
            Length of plotted x,y,z axes.
        tube_radius : float
            Radius of plotted x,y,z axes.
        center_scale : float
            Radius of the pose's origin ball.
        """
        R = T_frame_world.rotation
        t = T_frame_world.translation

        x_axis_tf = np.array([t, t + alpha * R[:,0]])
        y_axis_tf = np.array([t, t + alpha * R[:,1]])
        z_axis_tf = np.array([t, t + alpha * R[:,2]])

        Visualizer3D.points(t, color=(1,1,1), scale=center_scale)
        Visualizer3D.plot3d(x_axis_tf, color=(1,0,0), tube_radius=tube_radius)
        Visualizer3D.plot3d(y_axis_tf, color=(0,1,0), tube_radius=tube_radius)
        Visualizer3D.plot3d(z_axis_tf, color=(0,0,1), tube_radius=tube_radius)


Instruction:
Make a summary of the following Python 3 code
def table(T_table_world=RigidTransform(from_frame='table', to_frame='world'), dim=0.16, color=(0,0,0)):

        table_vertices = np.array([[ dim,  dim, 0],
                                   [ dim, -dim, 0],
                                   [-dim,  dim, 0],
                                   [-dim, -dim, 0]]).astype('float')
        table_tris = np.array([[0, 1, 2], [1, 2, 3]])
        table_mesh = trimesh.Trimesh(table_vertices, table_tris)
        table_mesh.apply_transform(T_table_world.matrix)
        Visualizer3D.mesh(table_mesh, style='surface', smooth=True, color=color)

Response: Plot a 3D table mesh in 3D.


Instruction:
Implement a function in Python 3 to
plot a 3D curve through a set of 3D points using tubes.

Response:
def plot3d(points, color=(0.5, 0.5, 0.5), tube_radius=0.005, n_components=30, name=None):
        """Plot a 3d curve through a set of points using tubes.

        Parameters
        ----------
        points : (n,3) float
            A series of 3D points that define a curve in space.
        color : (3,) float
            The color of the tube.
        tube_radius : float
            Radius of tube representing curve.
        n_components : int
            The number of edges in each polygon representing the tube.
        name : str
            A name for the object to be added.
        """
        points = np.asanyarray(points)
        mp = MaterialProperties(
            color = np.array(color),
            k_a = 0.5,
            k_d = 0.3,
            k_s = 0.0,
            alpha = 10.0,
            smooth=True
        )

        # Generate circular polygon
        vec = np.array([0,1]) * tube_radius
        angle = np.pi * 2.0 / n_components
        rotmat = np.array([
            [np.cos(angle), -np.sin(angle)],
            [np.sin(angle), np.cos(angle)]
        ])
        perim = []
        for i in range(n_components):
            perim.append(vec)
            vec = np.dot(rotmat, vec)
        poly = Polygon(perim)

        # Sweep it out along the path
        mesh = trimesh.creation.sweep_polygon(poly, points)
        obj = SceneObject(mesh, material=mp)
        if name is None:
            name = str(uuid.uuid4())
        Visualizer3D._scene.add_object(name, obj)


Instruction:
Can you tell what is the following Python 3 function doing
def figure(size=(8,8), *args, **kwargs):
        return plt.figure(figsize=size, *args, **kwargs)

Response: Creates a figure with the current version of the current version of the current version of the mayavi figure


Instruction:
Write a Python 3 script to
show the current figure.

Response:
def show(filename=None, *args, **kwargs):
        """ Show the current figure.

        Parameters
        ----------
        filename : :obj:`str`
            filename to save the image to, for auto-saving
        """
        if filename is None:
            plt.show(*args, **kwargs)
        else:
            plt.savefig(filename, *args, **kwargs)


Instruction:
Can you write a function in Python 3 where it
displays an image. Parameters ---------- image : :obj:`perception.Image` image to display auto_subplot : bool whether or not to automatically subplot for multi-channel images e.g. rgbd

Response:
def imshow(image, auto_subplot=False, **kwargs):
        """ Displays an image.
        Parameters
        ----------
        image : :obj:`perception.Image`
            image to display
        auto_subplot : bool
            whether or not to automatically subplot for multi-channel images e.g. rgbd
        """
        if isinstance(image, BinaryImage) or isinstance(image, GrayscaleImage):
            plt.imshow(image.data, cmap=plt.cm.gray, **kwargs)
        elif isinstance(image, ColorImage) or isinstance(image, SegmentationImage):
            plt.imshow(image.data, **kwargs)
        elif isinstance(image, DepthImage):
            plt.imshow(image.data, cmap=plt.cm.gray_r, **kwargs)
        elif isinstance(image, RgbdImage):
            if auto_subplot:
                plt.subplot(1,2,1)
                plt.imshow(image.color.data, **kwargs)
                plt.axis('off')
                plt.subplot(1,2,2)
                plt.imshow(image.depth.data, cmap=plt.cm.gray_r, **kwargs)
            else:
                plt.imshow(image.color.data, **kwargs)
        elif isinstance(image, GdImage):
            if auto_subplot:
                plt.subplot(1,2,1)
                plt.imshow(image.gray.data, cmap=plt.cm.gray, **kwargs)
                plt.axis('off')
                plt.subplot(1,2,2)
                plt.imshow(image.depth.data, cmap=plt.cm.gray_r, **kwargs)
            else:
                plt.imshow(image.gray.data, cmap=plt.cm.gray, **kwargs)
        plt.axis('off')


Instruction:
Implement a function in Python 3 to
draw a box on the current plot.

Response:
def box(b, line_width=2, color='g', style='-'):
        """ Draws a box on the current plot.

        Parameters
        ----------
        b : :obj:`autolab_core.Box`
            box to draw
        line_width : int
            width of lines on side of box
        color : :obj:`str`
            color of box
        style : :obj:`str`
            style of lines to draw
        """
        if not isinstance(b, Box):
            raise ValueError('Input must be of type Box')

        # get min pixels
        min_i = b.min_pt[1]
        min_j = b.min_pt[0]
        max_i = b.max_pt[1]
        max_j = b.max_pt[0]
        top_left = np.array([min_i, min_j])
        top_right = np.array([max_i, min_j])
        bottom_left = np.array([min_i, max_j])
        bottom_right = np.array([max_i, max_j])

        # create lines
        left = np.c_[top_left, bottom_left].T
        right = np.c_[top_right, bottom_right].T
        top = np.c_[top_left, top_right].T
        bottom = np.c_[bottom_left, bottom_right].T

        # plot lines
        plt.plot(left[:,0], left[:,1], linewidth=line_width, color=color, linestyle=style)
        plt.plot(right[:,0], right[:,1], linewidth=line_width, color=color, linestyle=style)
        plt.plot(top[:,0], top[:,1], linewidth=line_width, color=color, linestyle=style)
        plt.plot(bottom[:,0], bottom[:,1], linewidth=line_width, color=color, linestyle=style)


Instruction:
Can you generate the documentation for the following Python 3 function
def contour(c, subsample=1, size=10, color='g'):
        if not isinstance(c, Contour):
            raise ValueError('Input must be of type Contour')

        for i in range(c.num_pixels)[0::subsample]:
            plt.scatter(c.boundary_pixels[i,1], c.boundary_pixels[i,0], s=size, c=color)

Response: Draws a contour on the current plot by scattering points.


Instruction:
Write a Python 3 function for
plotting a 2D grasp with arrow and jaw style using matplotlib.

Response:
def grasp(grasp, width=None, color='r', arrow_len=4, arrow_head_len = 2, arrow_head_width = 3,
              arrow_width = 1, jaw_len=3, jaw_width = 1.0,
              grasp_center_size=1, grasp_center_thickness=2.5,
              grasp_center_style='+', grasp_axis_width=1,
              grasp_axis_style='--', line_width=1.0, alpha=50, show_center=True, show_axis=False, scale=1.0):
        """
        Plots a 2D grasp with arrow and jaw style using matplotlib

        Parameters
        ----------
        grasp : :obj:`Grasp2D`
            2D grasp to plot
        width : float
            width, in pixels, of the grasp (overrides Grasp2D.width_px)
        color : :obj:`str`
            color of plotted grasp
        arrow_len : float
            length of arrow body
        arrow_head_len : float
            length of arrow head
        arrow_head_width : float
            width of arrow head
        arrow_width : float
            width of arrow body
        jaw_len : float
            length of jaw line
        jaw_width : float
            line width of jaw line
        grasp_center_thickness : float
            thickness of grasp center
        grasp_center_style : :obj:`str`
            style of center of grasp
        grasp_axis_width : float
            line width of grasp axis
        grasp_axis_style : :obj:`str`
            style of grasp axis line
        show_center : bool
            whether or not to plot the grasp center
        show_axis : bool
            whether or not to plot the grasp axis
        """
        # set vars for suction
        skip_jaws = False
        if not hasattr(grasp, 'width'):
            grasp_center_style = '.'
            grasp_center_size = 50
            plt.scatter(grasp.center.x, grasp.center.y, c=color, marker=grasp_center_style, s=scale*grasp_center_size)

            if hasattr(grasp, 'orientation'):
                axis = np.array([np.cos(grasp.angle), np.sin(grasp.angle)])
                p = grasp.center.data + alpha * axis
                line = np.c_[grasp.center.data, p]
                plt.plot(line[0,:], line[1,:], color=color, linewidth=scale*grasp_axis_width)
                plt.scatter(p[0], p[1], c=color, marker=grasp_center_style, s=scale*grasp_center_size)
            return

        # plot grasp center
        if show_center:
            plt.plot(grasp.center.x, grasp.center.y, c=color, marker=grasp_center_style, mew=scale*grasp_center_thickness, ms=scale*grasp_center_size)
        if skip_jaws:
            return

        # compute axis and jaw locations
        axis = grasp.axis
        width_px = width
        if width_px is None and hasattr(grasp, 'width_px'):
            width_px = grasp.width_px
        g1 = grasp.center.data - (float(width_px) / 2) * axis
        g2 = grasp.center.data + (float(width_px) / 2) * axis
        g1p = g1 - scale * arrow_len * axis # start location of grasp jaw 1
        g2p = g2 + scale * arrow_len * axis # start location of grasp jaw 2

        # plot grasp axis
        if show_axis:
            plt.plot([g1[0], g2[0]], [g1[1], g2[1]], color=color, linewidth=scale*grasp_axis_width, linestyle=grasp_axis_style)

        # direction of jaw line
        jaw_dir = scale * jaw_len * np.array([axis[1], -axis[0]])

        # length of arrow
        alpha = scale*(arrow_len - arrow_head_len)

        # plot first jaw
        g1_line = np.c_[g1p, g1 - scale*arrow_head_len*axis].T
        plt.arrow(g1p[0], g1p[1], alpha*axis[0], alpha*axis[1], width=scale*arrow_width, head_width=scale*arrow_head_width, head_length=scale*arrow_head_len, fc=color, ec=color)
        jaw_line1 = np.c_[g1 + jaw_dir, g1 - jaw_dir].T

        plt.plot(jaw_line1[:,0], jaw_line1[:,1], linewidth=scale*jaw_width, c=color)

        # plot second jaw
        g2_line = np.c_[g2p, g2 + scale*arrow_head_len*axis].T
        plt.arrow(g2p[0], g2p[1], -alpha*axis[0], -alpha*axis[1], width=scale*arrow_width, head_width=scale*arrow_head_width, head_length=scale*arrow_head_len, fc=color, ec=color)
        jaw_line2 = np.c_[g2 + jaw_dir, g2 - jaw_dir].T
        plt.plot(jaw_line2[:,0], jaw_line2[:,1], linewidth=scale*jaw_width, c=color)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def all_subs(bounds):
    idx_list = []
    for i in range(len(bounds)):
        this_dim = bounds[i]
        lo,hi = this_dim[0],this_dim[1]   # bounds for this dimension
        this_dim_idxs = range(lo,hi+1)    # indexes for this dimension
        idx_list.append(this_dim_idxs)

    return idx2subs(idx_list)

Response:
given a list of tuples specifying the bounds of an array all_subs()
       returns a list of all the tuples of subscripts for that array


Instruction:
Make a summary of the following Python 3 code
def idx2subs(idx_list):
    if not idx_list:
        return [()]
    return [items + (item,)
            for items in idx2subs(idx_list[:-1]) for item in idx_list[-1]]

Response: Given a list of index values for each dimension of an array returns a list of tuples of subscripts for all of the array elements specified by those index values.


Instruction:
Write a Python 3 function for
giving an expression expr denoting a list of values returns a list of values for that expression.

Response:
def array_values(expr):
    """Given an expression expr denoting a list of values, array_values(expr)
       returns a list of values for that expression."""
    if isinstance(expr, Array):
        return expr.get_elems(all_subs(expr._bounds))
    elif isinstance(expr, list):
        vals = [array_values(x) for x in expr]
        return flatten(vals)
    else:
        return [expr]


Instruction:
Write a Python 3 script to
give an expression that denotes the set of elements that are to be accessed by the array_subscripts() returns a list of the elements denoted by expr.

Response:
def array_subscripts(expr):
    """Given a subscript expression expr (i.e., an expression that denotes the
       set of elements of some array that are to be accessed),
       array_subscripts() returns a list of the elements denoted by expr."""
    if isinstance(expr, Array):
        return all_subs(expr._bounds)
    elif isinstance(expr, list):
        subs = [subscripts(x) for x in expr]
        return flatten(subs)
    else:
        return [expr]


Instruction:
Write a Python 3 function for
giving a list of values in_list returns the list obtained by flattening the top - level elements of in_list

Response:
def flatten(in_list):
    """given a list of values in_list, flatten returns the list obtained by
       flattening the top-level elements of in_list."""
    out_list = []
    for val in in_list:
        if isinstance(val, list):
            out_list.extend(val)
        else:
            out_list.append(val)

    return out_list


Instruction:
Make a summary of the following Python 3 code
def implied_loop_expr(expr, start, end, delta):
    if delta > 0:
        stop = end+1
    else:
        stop = end-1

    result_list = [expr(x) for x in range(start,stop,delta)]

    # return the flattened list of results
    return list(itertools.chain(result_list))

Response:
given the parameters of an implied loop expr returns a list of values of the lambda expression expr applied to
       successive values of the implied loop


Instruction:
Explain what the following Python 3 code does
def _mk_uninit_array(self, bounds):
        if len(bounds) == 0:
            raise For2PyError("Zero-length arrays current not handled!.")

        this_dim = bounds[0]
        lo,hi = this_dim[0],this_dim[1]
        sz = hi-lo+1

        if len(bounds) == 1:
            return [None] * sz

        sub_array = self._mk_uninit_array(bounds[1:])
        this_array = [copy.deepcopy(sub_array) for i in range(sz)]

        return this_array

Response:
creates and returns an N - dimensional array of
            the size specified by the bounds with each element set to the value
            None.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _posn(self, bounds, idx):
        lo,hi = bounds[0],bounds[1]
        assert idx >= lo and idx <= hi, \
               f"Array index {idx} out of bounds: {bounds}\n"

        return idx-lo

Response: returns the position in a 0 - based array corresponding to idx


Instruction:
How would you explain what the following Python 3 function does
def _access(self, subs, acc_type, val):
        if isinstance(subs, int):
        # if subs is just an integer, take it to be an index value.
            subs = (subs,)

        if len(subs) == 0:
            raise For2PyError("Zero-length arrays currently not handled.")

        bounds = self._bounds
        sub_arr = self._values
        ndims = len(subs)
        for i in range(ndims):
            this_pos = self._posn(bounds[i], subs[i])

            if i == ndims-1:
                if acc_type == _GET_:
                    return sub_arr[this_pos]
                else:
                    sub_arr[this_pos] = val
            else:
                sub_arr = sub_arr[this_pos]

Response: _access returns the value of the element specified by the subscript values subs.


Instruction:
How would you implement a function in Python 3 that
performs string replacement from dict in a single pass.

Response:
def multiple_replace(d: Dict[str, str], text: str) -> str:
    """ Performs string replacement from dict in a single pass. Taken from
    https://www.oreilly.com/library/view/python-cookbook/0596001673/ch03s15.html
    """
  # Create a regular expression from all of the dictionary keys
    regex = re.compile("|".join(map(re.escape, d.keys())))

  # For each match, look up the corresponding value in the dictionary
    return regex.sub(lambda match: d[match.group(0)], text)


Instruction:
Make a summary of the following Python 3 code
def create_parameterized_CAG(input, output, filename="CAG_with_indicators_and_values.pdf"):
    with open(input, "rb") as f:
        G = pickle.load(f)
    G.parameterize(year=2017, month=4)
    G.get_timeseries_values_for_indicators()
    with open(output, "wb") as f:
        pickle.dump(G, f)

Response: Create a CAG with mapped and parameterized indicators.


Instruction:
Implement a Python 3 function for
getting a set of all unique concepts in the INDRA statements.

Response:
def get_concepts(sts: List[Influence]) -> Set[str]:
    """ Get a set of all unique concepts in the list of INDRA statements. """
    return set(flatMap(nameTuple, sts))


Instruction:
Explain what the following Python 3 code does
def get_valid_statements_for_modeling(sts: List[Influence]) -> List[Influence]:

    return [
        s
        for s in sts
        if is_grounded_statement(s)
        and (s.subj_delta["polarity"] is not None)
        and (s.obj_delta["polarity"] is not None)
    ]

Response: Select INDRA statements that can be used to construct a Delphi model from a given list of INDRA statements.


Instruction:
Implement a Python 3 function for
checking if an Influence statement is grounded.

Response:
def _(s: Influence) -> bool:
    """ Check if an Influence statement is grounded """
    return is_grounded(s.subj) and is_grounded(s.obj)


Instruction:
Write a Python 3 function that can
check if an Influence statement is grounded.

Response:
def is_grounded_statement(s: Influence) -> bool:
    """ Check if an Influence statement is grounded """
    return is_grounded_concept(s.subj) and is_grounded_concept(s.obj)


Instruction:
Write a Python 3 function that can
check if a concept has a high grounding score.

Response:
def _(c: Concept, cutoff: float = 0.7) -> bool:
    """Check if a concept has a high grounding score. """

    return is_grounded(c) and (top_grounding_score(c) >= cutoff)


Instruction:
Write a Python 3 function for
returning true if subj and obj are grounded to the UN ontology.

Response:
def _(s: Influence, cutoff: float = 0.7) -> bool:
    """ Returns true if both subj and obj are grounded to the UN ontology. """

    return all(map(lambda c: is_well_grounded(c, cutoff), s.agent_list()))


Instruction:
How would you code a function in Python 3 to
check if a concept is grounded and has a high grounding score.

Response:
def is_well_grounded_concept(c: Concept, cutoff: float = 0.7) -> bool:
    """Check if a concept has a high grounding score. """

    return is_grounded(c) and (top_grounding_score(c) >= cutoff)


Instruction:
Implement a function in Python 3 to
return true if subj and obj are grounded to the UN ontology.

Response:
def is_well_grounded_statement(s: Influence, cutoff: float = 0.7) -> bool:
    """ Returns true if both subj and obj are grounded to the UN ontology. """

    return all(
        map(lambda c: is_well_grounded_concept(c, cutoff), s.agent_list())
    )


Instruction:
Can you generate the documentation for the following Python 3 function
def is_grounded_to_name(c: Concept, name: str, cutoff=0.7) -> bool:
    return (top_grounding(c) == name) if is_well_grounded(c, cutoff) else False

Response: Check if a concept is grounded to a given name.


Instruction:
How would you explain what the following Python 3 function does
def contains_relevant_concept(
    s: Influence, relevant_concepts: List[str], cutoff=0.7
) -> bool:
    """ Returns true if a given Influence statement has a relevant concept, and
    false otherwise. """

    return any(
        map(lambda c: contains_concept(s, c, cutoff=cutoff), relevant_concepts)
    )

Response: Returns true if a given Influence statement has a relevant concept and False otherwise.


Instruction:
Given the following Python 3 function, write the documentation
def top_grounding(c: Concept) -> str:
    return c.db_refs["UN"][0][0] if "UN" in c.db_refs else c.name

Response: Return the top - scoring grounding from the UN ontology.


Instruction:
Implement a function in Python 3 to
return a 2 - tuple consisting of the top groundings of the subj and obj of an Influence statement.

Response:
def nameTuple(s: Influence) -> Tuple[str, str]:
    """ Returns a 2-tuple consisting of the top groundings of the subj and obj
    of an Influence statement. """
    return top_grounding(s.subj), top_grounding(s.obj)


Instruction:
Make a summary of the following Python 3 code
def createNewICM():
    data = json.loads(request.data)
    G = AnalysisGraph.from_uncharted_json_serialized_dict(data)
    G.assemble_transition_model_from_gradable_adjectives()
    G.sample_from_prior()
    G.to_sql(app=current_app)
    _metadata = ICMMetadata.query.filter_by(id=G.id).first().deserialize()
    del _metadata["model_id"]
    return jsonify(_metadata)

Response: Create a new ICM


Instruction:
Can you generate the documentation for the following Python 3 function
def getICMByUUID(uuid: str):
    _metadata = ICMMetadata.query.filter_by(id=uuid).first().deserialize()
    del _metadata["model_id"]
    return jsonify(_metadata)

Response: Fetch an ICM by UUID


Instruction:
Implement a function in Python 3 to
return all ICM primitives

Response:
def getICMPrimitives(uuid: str):
    """ returns all ICM primitives (TODO - needs filter support)"""
    primitives = [
        p.deserialize()
        for p in CausalPrimitive.query.filter_by(model_id=uuid).all()
    ]
    for p in primitives:
        del p["model_id"]
    return jsonify(primitives)


Instruction:
How would you code a function in Python 3 to
return a list of Evidence objects for a causal primitive

Response:
def getEvidenceForID(uuid: str, prim_id: str):
    """ returns evidence for a causal primitive (needs pagination support)"""
    evidences = [
        evidence.deserialize()
        for evidence in Evidence.query.filter_by(
            causalrelationship_id=prim_id
        ).all()
    ]
    for evidence in evidences:
        del evidence["causalrelationship_id"]

    return jsonify(evidences)


Instruction:
How would you code a function in Python 3 to
execute an experiment over the model

Response:
def createExperiment(uuid: str):
    """ Execute an experiment over the model"""
    data = request.get_json()
    G = DelphiModel.query.filter_by(id=uuid).first().model
    if os.environ.get("TRAVIS") is not None:
        config_file="bmi_config.txt"
    else:
        if not os.path.exists("/tmp/delphi"):
            os.makedirs("/tmp/delphi", exist_ok=True)
        config_file="/tmp/delphi/bmi_config.txt"

    G.initialize(initialize_indicators = False, config_file=config_file)
    for n in G.nodes(data=True):
        rv = n[1]["rv"]
        rv.partial_t = 0.0
        for variable in data["interventions"]:
            if n[1]["id"] == variable["id"]:
                # TODO : Right now, we are only taking the first value in the
                # "values" list. Need to generalize this so that you can have
                # multiple interventions at different times.

                # TODO : The subtraction of 1 is a TEMPORARY PATCH to address
                # the mismatch in semantics between the ICM API and the Delphi
                # model. MUST FIX ASAP.
                rv.partial_t = variable["values"]["value"]["value"] - 1
                for s0 in G.s0:
                    s0[f"∂({n[0]})/∂t"] = rv.partial_t
                break

    id = str(uuid4())
    experiment = ForwardProjection(baseType="ForwardProjection", id=id)
    db.session.add(experiment)
    db.session.commit()

    result = ForwardProjectionResult(id=id, baseType="ForwardProjectionResult")
    db.session.add(result)
    db.session.commit()

    d = dateutil.parser.parse(data["projection"]["startTime"])

    n_timesteps = data["projection"]["numSteps"]

    for i in range(n_timesteps):
        if data["projection"]["stepSize"] == "MONTH":
            d = d + relativedelta(months=1)
        elif data["projection"]["stepSize"] == "YEAR":
            d = d + relativedelta(years=1)

        for n in G.nodes(data=True):
            CausalVariable.query.filter_by(
                id=n[1]["id"]
            ).first().lastUpdated = d.isoformat()
            result.results.append(
                {
                    "id": n[1]["id"],
                    "baseline": {
                        "active": "ACTIVE",
                        "time": d.isoformat(),
                        "value": {"baseType": "FloatValue", "value": 1.0},
                    },
                    "intervened": {
                        "active": "ACTIVE",
                        "time": d.isoformat(),
                        "value": {
                            "baseType": "FloatValue",
                            "value": np.median([s[n[0]] for s in G.s0]),
                        },
                    },
                }
            )

        G.update(update_indicators=False)

        # Hack for 12-month evaluation - have the partial derivative decay over
        # time to restore equilibrium

        tau = 1.0 # Time constant to control the rate of the decay
        for n in G.nodes(data=True):
            for variable in data["interventions"]:
                if n[1]["id"] == variable["id"]:
                    rv = n[1]["rv"]
                    for s0 in G.s0:
                        s0[f"∂({n[0]})/∂t"] = rv.partial_t * exp(-tau*i)

    db.session.add(result)
    db.session.commit()

    return jsonify(
        {
            "id": experiment.id,
            "message": "Forward projection sent successfully",
        }
    )


Instruction:
Can you write a function in Python 3 where it
lists active (running or completed) experiments

Response:
def getExperiments(uuid: str):
    """ list active (running or completed) experiments"""
    return jsonify([x.deserialize() for x in Experiment.query.all()])


Instruction:
Create a Python 3 function to
create an HTML representation of INDRA statements to be manually inspected for validity.

Response:
def create_statement_inspection_table(sts: List[Influence]):
    """ Display an HTML representation of a table with INDRA statements to
    manually inspect for validity.

    Args:
        sts: A list of INDRA statements to be manually inspected for validity.
    """

    columns = [
        "un_groundings",
        "subj_polarity",
        "obj_polarity",
        "Sentence",
        "Source API",
    ]

    polarity_to_str = lambda x: "+" if x == 1 else "-" if x == -1 else "None"
    l = []
    for s in sts:
        subj_un_grounding = s.subj.db_refs["UN"][0][0].split("/")[-1]
        obj_un_grounding = s.obj.db_refs["UN"][0][0].split("/")[-1]
        subj_polarity = s.subj_delta["polarity"]
        obj_polarity = s.obj_delta["polarity"]
        subj_adjectives = s.subj_delta["adjectives"]
        for e in s.evidence:
            l.append(
                (
                    (subj_un_grounding, obj_un_grounding),
                    subj_polarity,
                    obj_polarity,
                    e.text,
                    e.source_api,
                )
            )

    df = pd.DataFrame(l, columns=columns)
    df = df.pivot_table(index=["un_groundings", "Source API", "Sentence"])

    def hover(hover_color="#ffff99"):
        return dict(
            selector="tr:hover",
            props=[("background-color", "%s" % hover_color)],
        )

    styles = [
        hover(),
        dict(props=[("font-size", "100%"), ("font-family", "Gill Sans")]),
    ]

    return df.style.set_table_styles(styles)


Instruction:
Explain what the following Python 3 code does
def get_python_shell():

    env = os.environ
    shell = "shell"
    program = os.path.basename(env["_"])

    if "jupyter-notebook" in program:
        shell = "jupyter-notebook"
    elif "JPY_PARENT_PID" in env or "ipython" in program:
        shell = "ipython"
        if "JPY_PARENT_PID" in env:
            shell = "ipython-notebook"

    return shell

Response:
Determine python shell

    get_python_shell() returns

    'shell' (started python on command line using "python")
    'ipython' (started ipython on command line using "ipython")
    'ipython-notebook' (e.g., running in Spyder or started with "ipython qtconsole")
    'jupyter-notebook' (running in a Jupyter notebook)

    See also https://stackoverflow.com/a/37661854


Instruction:
Create a Python 3 function to
create a CAG that examines the downstream effects of changes in precipitation.

Response:
def create_precipitation_centered_CAG(input, output):
    """ Get a CAG that examines the downstream effects of changes in precipitation. """

    with open(input, "rb") as f:
        G = pickle.load(f)
    G = G.get_subgraph_for_concept(
        "UN/events/weather/precipitation", depth=2, reverse=False
    )
    G.prune(cutoff=2)

    # Manually correcting a bad CWMS extraction
    G.edges[
        "UN/events/weather/precipitation",
        "UN/entities/human/infrastructure/transportation/road",
    ]["InfluenceStatements"][0].obj_delta["polarity"] = -1
    with open(output, "wb") as f:
        pickle.dump(G, f)


Instruction:
Here you have a function in Python 3, explain what it does
def index_modules(root) -> Dict:

    module_index_dict = {
        node["name"]: (node.get("tag"), index)
        for index, node in enumerate(root)
        if node.get("tag") in ("module", "program", "subroutine")
    }

    return module_index_dict

Response:
Counts the number of modules in the Fortran file including the program
    file.


Instruction:
Write a Python 3 script to
print out the array declaration in a format of Array class object declaration.

Response:
def printArray(self, node, printState: PrintState):
        """ Prints out the array declaration in a format of Array class
            object declaration. 'arrayName = Array(Type, [bounds])'
        """
        if (
            self.nameMapper[node["name"]] not in printState.definedVars
            and self.nameMapper[node["name"]] not in printState.globalVars
        ):
            printState.definedVars += [self.nameMapper[node["name"]]]
            assert int(node["count"]) > 0
            printState.definedVars += [node["name"]]

            varType = ""
            if node["type"].upper() == "INTEGER":
                varType = "int"
            elif node["type"].upper() in ("DOUBLE", "REAL"):
                varType = "float"
            elif node["type"].upper() == "CHARACTER":
                varType = "str"
            elif node["isDevTypeVar"]:
                varType = node["type"].lower() + "()"

            assert varType != ""

            self.pyStrings.append(f"{node['name']} = Array({varType}, [")
            for i in range(0, int(node["count"])):
                loBound = node["low" + str(i + 1)]
                upBound = node["up" + str(i + 1)]
                dimensions = f"({loBound}, {upBound})"
                if i < int(node["count"]) - 1:
                    self.pyStrings.append(f"{dimensions}, ")
                else:
                    self.pyStrings.append(f"{dimensions}")
            self.pyStrings.append("])")

            if node["isDevTypeVar"]:
                self.pyStrings.append(printState.sep)
                # This may require updating later when we have to deal with the
                # multi-dimensional derived type arrays
                upBound = node["up1"]
                self.pyStrings.append(
                    f"for z in range(1, {upBound}+1):" + printState.sep
                )
                self.pyStrings.append(
                    f"    obj = {node['type']}()" + printState.sep
                )
                self.pyStrings.append(
                    f"    {node['name']}.set_(z, obj)" + printState.sep
                )


Instruction:
Here you have a function in Python 3, explain what it does
def draw_graph(G: nx.DiGraph, filename: str):
    A = to_agraph(G)
    A.graph_attr["rankdir"] = "LR"
    A.draw(filename, prog="dot")

Response: Draw a networkx graph with Pygraphviz.


Instruction:
Write a Python 3 function for
getting all input nodes from a network.

Response:
def get_input_nodes(G: nx.DiGraph) -> List[str]:
    """ Get all input nodes from a network. """
    return [n for n, d in G.in_degree() if d == 0]


Instruction:
Explain what the following Python 3 code does
def get_output_nodes(G: nx.DiGraph) -> List[str]:
    return [n for n, d in G.out_degree() if d == 0]

Response: Get all output nodes from a network.


Instruction:
Can you implement a function in Python 3 that
gets a networkx graph from a DOT file.

Response:
def nx_graph_from_dotfile(filename: str) -> nx.DiGraph:
    """ Get a networkx graph from a DOT file, and reverse the edges. """
    return nx.DiGraph(read_dot(filename).reverse())


Instruction:
Write a Python 3 script for
outputting a networkx graph to a DOT file.

Response:
def to_dotfile(G: nx.DiGraph, filename: str):
    """ Output a networkx graph to a DOT file. """
    A = to_agraph(G)
    A.write(filename)


Instruction:
Make a summary of the following Python 3 code
def get_shared_nodes(G1: nx.DiGraph, G2: nx.DiGraph) -> List[str]:
    return list(set(G1.nodes()).intersection(set(G2.nodes())))

Response: Get all the nodes that are common to both networks.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def line_is_comment(line: str) -> bool:

    if line[0] in "cCdD*!":
        return True

    llstr = line.strip()
    if len(llstr) == 0 or llstr[0] == "!":
        return True

    return False

Response: Check if a line is a comment line.


Instruction:
Here you have a function in Python 3, explain what it does
def line_starts_subpgm(line: str) -> Tuple[bool, Optional[str]]:

    match = RE_SUB_START.match(line)
    if match != None:
        f_name = match.group(1)
        return (True, f_name)

    match = RE_FN_START.match(line)
    if match != None:
        f_name = match.group(1)
        return (True, f_name)

    return (False, None)

Response: Determines if a line in the program starts a subprogram definition.


Instruction:
How would you explain what the following Python 3 function does
def program_unit_name(line:str) -> str:
   match = RE_PGM_UNIT_START.match(line)
   assert match != None
   return match.group(2)

Response:
Given a line that starts a program unit this function returns the name associated
      with that program unit.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def line_is_continuation(line: str) -> bool:

    llstr = line.lstrip()
    return len(llstr) > 0 and llstr[0] == "&"

Response: Returns True iff line is a continuation line.


Instruction:
Can you generate the documentation for the following Python 3 function
def line_is_executable(line: str) -> bool:

    if line_is_comment(line):
        return False

    if re.match(RE_TYPE_NAMES, line):
        return False

    for exp in EXECUTABLE_CODE_START:
        if re.match(exp, line) != None:
            return True

    return False

Response: line_is_executable() returns True iff the line can start an anova facility executable statement in a program.


Instruction:
Write a Python 3 script for
returning an iterator that yields x followed by xs.

Response:
def prepend(x: T, xs: Iterable[T]) -> Iterator[T]:
    """ Prepend a value to an iterable.

    Parameters
    ----------
    x
        An element of type T.
    xs
        An iterable of elements of type T.

    Returns
    -------
    Iterator
        An iterator that yields *x* followed by elements of *xs*.

    Examples
    --------

    >>> from delphi.utils.fp import prepend
    >>> list(prepend(1, [2, 3]))
    [1, 2, 3]

    """
    return chain([x], xs)


Instruction:
Write a Python 3 function that can
append a value to an iterable.

Response:
def append(x: T, xs: Iterable[T]) -> Iterator[T]:
    """ Append a value to an iterable.

    Parameters
    ----------
    x
        An element of type T.
    xs
        An iterable of elements of type T.

    Returns
    -------
    Iterator
        An iterator that yields elements of *xs*, then yields *x*.

    Examples
    --------
    >>> from delphi.utils.fp import append
    >>> list(append(1, [2, 3]))
    [2, 3, 1]

    """

    return chain(xs, [x])


Instruction:
Write a Python 3 script to
make an iterator that returns accumulated results of a binary function f applied to elements of an iterable.

Response:
def scanl(f: Callable[[T, U], T], x: T, xs: Iterable[U]) -> Iterator[T]:
    """ Make an iterator that returns accumulated results of a binary function
    applied to elements of an iterable.

    .. math::
        scanl(f, x_0, [x_1, x_2, ...]) = [x_0, f(x_0, x_1), f(f(x_0, x_1), x_2), ...]

    Parameters
    ----------
    f
        A binary function of two arguments of type T.
    x
        An initializer element of type T.
    xs
        An iterable of elements of type T.

    Returns
    -------
    Iterator
        The iterator of accumulated results.

    Examples
    --------
    >>> from delphi.utils.fp import scanl
    >>> list(scanl(lambda x, y: x + y, 10, range(5)))
    [10, 10, 11, 13, 16, 20]

    """

    return accumulate(prepend(x, xs), f)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def scanl1(f: Callable[[T, T], T], xs: Iterable[T]) -> Iterator[T]:
    return accumulate(xs, f)

Response: Make an iterator that returns accumulated results of a binary function f applied to elements of an iterable xs.


Instruction:
Can you implement a function in Python 3 that
returns the accumulated result of a binary function applied to elements of an iterable.

Response:
def foldl(f: Callable[[T, U], T], x: T, xs: Iterable[U]) -> T:
    """ Returns the accumulated result of a binary function applied to elements
    of an iterable.

    .. math::
        foldl(f, x_0, [x_1, x_2, x_3]) = f(f(f(f(x_0, x_1), x_2), x_3)

    Examples
    --------
    >>> from delphi.utils.fp import foldl
    >>> foldl(lambda x, y: x + y, 10, range(5))
    20

    """
    return reduce(f, xs, x)


Instruction:
Can you write a function in Python 3 where it
returns the accumulated result of a binary function applied to elements of an iterable.

Response:
def foldl1(f: Callable[[T, T], T], xs: Iterable[T]) -> T:
    """ Returns the accumulated result of a binary function applied to elements
    of an iterable.

    .. math::
        foldl1(f, [x_0, x_1, x_2, x_3]) = f(f(f(f(x_0, x_1), x_2), x_3)

    Examples
    --------
    >>> from delphi.utils.fp import foldl1
    >>> foldl1(lambda x, y: x + y, range(5))
    10
    """

    return reduce(f, xs)


Instruction:
Here you have a function in Python 3, explain what it does
def flatten(xs: Union[List, Tuple]) -> List:
    return (
        sum(map(flatten, xs), [])
        if (isinstance(xs, list) or isinstance(xs, tuple))
        else [xs]
    )

Response: Flatten a nested list or tuple.


Instruction:
Can you tell what is the following Python 3 function doing
def iterate(f: Callable[[T], T], x: T) -> Iterator[T]:
    return scanl(lambda x, _: f(x), x, repeat(None))

Response:
Takes a function and returns the result of successive
    applications of a function to an element.


Instruction:
Make a summary of the following Python 3 code
def ptake(n: int, xs: Iterable[T]) -> Iterable[T]:
    return tqdm(take(n, xs), total=n)

Response: Take with a tqdm progress bar.


Instruction:
Can you tell what is the following Python 3 function doing
def ltake(n: int, xs: Iterable[T]) -> List[T]:
    return list(take(n, xs))

Response: A lazy version of take.


Instruction:
Given the following Python 3 function, write the documentation
def compose(*fs: Any) -> Callable:
    return foldl1(lambda f, g: lambda *x: f(g(*x)), fs)

Response: Compose functions from left to right.


Instruction:
Explain what the following Python 3 code does
def rcompose(*fs: Any) -> Callable:
    return foldl1(lambda f, g: lambda *x: g(f(*x)), fs)

Response: Compose functions from right to left.


Instruction:
Can you tell what is the following Python 3 function doing
def flatMap(f: Callable, xs: Iterable) -> List:
    return flatten(lmap(f, xs))

Response: Map a function onto an iterable and flatten the result.


Instruction:
Can you implement a function in Python 3 that
collects data into fixed - length chunks or blocks.

Response:
def grouper(xs: Iterable, n: int, fillvalue=None):
    """Collect data into fixed-length chunks or blocks.
    >>> from delphi.utils.fp import grouper
    >>> list(grouper('ABCDEFG', 3, 'x'))
    [('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]
    """
    args = [iter(xs)] * n
    return zip_longest(*args, fillvalue=fillvalue)


Instruction:
Create a Python 3 function to
process CliMIS crop production data.

Response:
def process_climis_crop_production_data(data_dir: str):
    """ Process CliMIS crop production data """

    climis_crop_production_csvs = glob(
        "{data_dir}/Climis South Sudan Crop Production Data/"
        "Crops_EstimatedProductionConsumptionBalance*.csv"
    )
    state_county_df = pd.read_csv(
        f"{data_dir}/ipc_data.csv", skipinitialspace=True
    )
    combined_records = []

    for f in climis_crop_production_csvs:
        year = int(f.split("/")[-1].split("_")[2].split(".")[0])
        df = pd.read_csv(f).dropna()
        for i, r in df.iterrows():
            record = {
                "Year": year,
                "Month": None,
                "Source": "CliMIS",
                "Country": "South Sudan",
            }
            region = r["State/County"].strip()

            if region.lower() in state_county_df["State"].str.lower().values:
                record["State"] = region
                record["County"] = None
            else:
                potential_states = state_county_df.loc[
                    state_county_df["County"] == region
                ]["State"]
                record["State"] = (
                    potential_states.iloc[0]
                    if len(potential_states) != 0
                    else None
                )
                record["County"] = region

            for field in r.index:
                if field != "State/County":
                    if "Net Cereal production" in field:
                        record["Variable"] = "Net Cereal Production"
                        record["Value"] = r[field]
                    if field.split()[-1].startswith("("):
                        record["Unit"] = field.split()[-1][1:-1].lower()
                    else:
                        record["Unit"] = None

            combined_records.append(record)

    df = pd.DataFrame(combined_records)
    return df


Instruction:
Make a summary of the following Python 3 code
def process_climis_livestock_data(data_dir: str):

    records = []

    livestock_data_dir = f"{data_dir}/Climis South Sudan Livestock Data"

    for filename in glob(
        f"{livestock_data_dir}/Livestock Body Condition/*2017.csv"
    ):
        records += process_file_with_single_table(
            filename,
            lambda ind: f"Percentage of {filename.split('_')[-3].lower()} with body condition {ind.lower()}",
            lambda f: f.split("_")[-2],
        )

    for filename in glob(
        f"{livestock_data_dir}/Livestock Production/*2017.csv"
    ):
        records += process_file_with_single_table(
            filename,
            lambda ind: "Percentage of householding at least milking one of their livestocks",
            lambda f: f.split("_")[1],
        )

    disease_acronym_dict = {
        "FMD": "Foot and Mouth Disease (FMD)",
        "LSD": "Lumpy Skin Disease (LSD)",
        "CBPP": "Contagious Bovine Pleuropneumonia (CBPP)",
        "CCPP": "Contagious Caprine Pleuropneumonia (CCPP)",
        "NC": "NC",
        "PPR": "Peste des Petits Ruminants (PPR)",
        "Others": "Other diseases",
    }

    func = (
        lambda k, i: f"Percentage of livestock with {disease_acronym_dict[k]} that are {i.lower().strip()}"
    )
    livestock_disease_header_dict = {
        k: partial(func, k) for k in disease_acronym_dict
    }

    livestock_migration_header_dict = {
        "Livestock migration": lambda i: f"Percentage of livestock migrating {i.split()[-1].lower()}",
        "Distance covered": lambda i: "Distance covered by migrating livestock",
        "Proportion of livestock that migrated": lambda i: "Percentage of livestock that migrated",
        "Migration normal at this time of the year": lambda i: f"Migration normal at this time of year, {i}",
        "Duration in months when the migrated animals are expected to be back after": lambda i: "Duration in months when the migrated animals are expected to be back after",
        "Reasons for livestock migration": lambda i: f"Percentage of livestock migrating due to {i.lower()}",
    }

    def process_directory(dirname, header_dict):
        return pd.concat(
            [
                df
                for df in [
                    process_file_with_multiple_tables(f, header_dict)
                    for f in glob(f"{livestock_data_dir}/{dirname}/*2017.csv")
                ]
                if df is not None
            ]
        )

    func2 = (
        lambda k, i: f"{k.replace('animals', i.lower()).replace('stock', 'stock of '+i.lower()).replace('animal', i.lower())}"
    )
    livestock_ownership_headers = [
        "Average current stock per household",
        "Average number of animals born per household during last 4 weeks",
        "Average number of animals acquired per household during last 4 weeks (dowry, purchase, gift)",
        "Average number of animals given out as bride price/gift per household during last 4 weeks per household",
        "Average number of animals sold per household during last 4 weeks household",
        "Average price of animal sold (SSP)",
        "Average number of animals exchanged for grain per household during last 4 weeks",
        "Average number of animals died/slaughtered/lost per  household during last 4 weeks",
    ]

    livestock_ownership_header_dict = {
        k: partial(func2, k) for k in livestock_ownership_headers
    }
    ownership_df = process_directory(
        "Livestock Ownership", livestock_ownership_header_dict
    )

    disease_df = process_directory(
        "Livestock Diseases", livestock_disease_header_dict
    )

    livestock_migration_df = process_directory(
        "Livestock Migration", livestock_migration_header_dict
    )

    livestock_pasture_header_dict = {
        "Pasture condtion": lambda i: f"Percentage of livestock pasture in {i.lower()} condition",
        "Pasture condition compared to similar time in a normal year": lambda i: f"Percentage of livestock pasture in {i.lower()} condition compared to a similar time in a normal year",
        "Browse condition": lambda i: f"Percentage of livestock pasture in {i.lower()} browse condition",
        "Browse condition compared to similar time in a normal year": lambda i: f"Percentage of livestock pasture in {i.lower()} browse condition compared to a similar time in a normal year",
        "Presence of constraints in accessing forage": lambda i: f"Percentage reporting the {('presence' if i=='Yes' else 'absence')} of constraints in accessing forage",
        "Main forage constraints": lambda i: f"Percentage reporting {i.lower()} as the main forage constraint",
    }
    livestock_pasture_df = process_directory(
        "Livestock Pasture", livestock_pasture_header_dict
    )
    livestock_water_sources_header_dict = {
        "Main water sources": lambda i: f"Percentage of livestock whose main water source is {i.lower()}",
        "Number of days livestock have been watered in the last 7 days": lambda i: f"Number of days {i.lower()} have been watered in the last 7 days",
    }

    livestock_water_sources_df = process_directory(
        "Livestock Water Sources", livestock_water_sources_header_dict
    )

    for filename in glob(f"{livestock_data_dir}/Livestock Loss/*2017.csv"):
        records += process_file_with_single_table(
            filename,
            lambda ind: f"Percentage of {filename.split('_')[-3].lower()} loss accounted for by {ind.lower()}",
            lambda f: f.split("_")[-2],
        )

    for record in records:
        if isinstance(record["Value"], str):
            record["Value"] = record["Value"].replace("%", "")

    livestock_prices_df = pd.concat(
        [
            make_livestock_prices_table(f)
            for f in glob(
                f"{livestock_data_dir}/Livestock Market Prices/*2017.csv"
            )
        ]
    )

    climis_livestock_data_df = pd.concat(
        [
            pd.DataFrame(records),
            disease_df,
            ownership_df,
            livestock_prices_df,
            livestock_migration_df,
            livestock_pasture_df,
            livestock_water_sources_df,
        ]
    )
    return climis_livestock_data_df

Response: Processes all files in CliMIS livestock data directory and returns a list of dictionaries containing all the relevant data.


Instruction:
Explain what the following Python 3 code does
def separate_trailing_comments(lines: List[str]) -> List[Tuple[int, str]]:

    enumerated_lines = list(enumerate(lines, 1))
    i = 0
    while i < len(enumerated_lines):
        (n, code_line) = enumerated_lines[i]
        if not line_is_comment(code_line):
            (code_part, comment_part) = split_trailing_comment(code_line)
            if comment_part is not None:
                enumerated_lines[i] = (n, comment_part)
                enumerated_lines.insert(i + 1, (n, code_part))
        i += 1

    return enumerated_lines

Response: Separates trailing comments from the source code lines.


Instruction:
Implement a Python 3 function for
giving a list of lines that are continued by a single line merge the lines that are continued by a single line.

Response:
def merge_continued_lines(lines):
    """Given a list of numered Fortran source code lines, i.e., pairs of the
       form (n, code_line) where n is a line number and code_line is a line
       of code, merge_continued_lines() merges sequences of lines that are
       indicated to be continuation lines.
    """

    # Before a continuation line L1 is merged with the line L0 before it (and
    # presumably the one L1 is continuing), ensure that L0 is not a comment.
    # If L0 is a comment, swap L0 and L1.
    chg = True
    while chg:
        chg = False
        i = 0
        while i < len(lines) - 1:
            ln0, ln1 = lines[i], lines[i + 1]
            if line_is_comment(ln0[1]) and line_is_continuation(ln1[1]):
                # swap the code portions of lines[i] and lines[i+1]
                lines[i], lines[i + 1] = (ln0[0], ln1[1]), (ln1[0], ln0[1])
                chg = True
            i += 1

    # Merge continuation lines
    chg = True
    while chg:
        chg = False
        i = 0
        while i < len(lines):
            line = lines[i]
            if line_is_continuation(line[1]):
                assert i > 0
                (prev_linenum, prev_line_code) = lines[i - 1]
                curr_line_code = line[1].lstrip()[
                    1:
                ]  # remove continuation  char
                merged_code = prev_line_code.rstrip() + curr_line_code.lstrip()

                lines[i - 1] = (prev_linenum, merged_code)
                lines.pop(i)
                chg = True
            i += 1

    return lines


Instruction:
Can you create a Python 3 function that
gives a line of code returns a string indicating what kind of code it is.

Response:
def type_of_line(line):
    """Given a line of code, type_of_line() returns a string indicating
       what kind of code it is."""

    if line_is_comment(line):
        return "comment"
    elif line_is_executable(line):
        return "exec_stmt"
    elif line_is_pgm_unit_end(line):
        return "pgm_unit_end"
    else:
        if line_is_pgm_unit_start(line):
            return "pgm_unit_start"
        else:
            return "other"


Instruction:
Write a Python 3 script to
give a list of numbered lines from a Fortran file where comments internal to subprogram bodies have been moved out into their own lines, extract_comments() extracts comments into a dictionary and replaces each comment internal to subprogram bodies with a marker statement. It returns a pair (code, comments) where code is a list of numbered lines with comments removed and marker statements (plus corresponding variable declarations) added; and comments is a dictionary mapping marker statement variables to the corresponding comments.

Response:
def extract_comments(
    lines: List[Tuple[int, str]]
) -> Tuple[List[Tuple[int, str]], Dict[str, List[str]]]:
    """Given a list of numbered lines from a Fortran file where comments
       internal to subprogram bodies have been moved out into their own lines,
       extract_comments() extracts comments into a dictionary and replaces
       each comment internal to subprogram bodies with a marker statement.
       It returns a pair (code, comments) where code is a list of numbered
       lines with comments removed and marker statements (plus corresponding
       variable declarations) added; and comments is a dictionary mapping
       marker statement variables to the corresponding comments."""

    curr_comment = []
    curr_fn, prev_fn, curr_marker = None, None, None
    comments = OrderedDict()

    # curr_state refers to the state of the finite-state machine (see above)
    curr_state = "outside"

    comments["$file_head"] = []
    comments["$file_foot"] = []

    for i in range(len(lines)):
        (linenum, line) = lines[i]

        # determine what kind of line this is
        line_type = type_of_line(line)

        # process the line appropriately
        if curr_state == "outside":
            assert line_type in ("comment", "pgm_unit_start"), (
                line_type,
                line,
            )
            if line_type == "comment":
                curr_comment.append(line)
                lines[i] = (linenum, None)
            else:
                # line_type == "pgm_unit_start"
                pgm_unit_name = program_unit_name(line)
                comments["$file_head"] = curr_comment

                if prev_fn is not None:
                    comments[prev_fn]["foot"] = curr_comment

                prev_fn = curr_fn
                curr_fn = pgm_unit_name

                internal_comments = OrderedDict()

                comments[curr_fn] = init_comment_map(
                    curr_comment, [], [], internal_comments
                )
                curr_comment = []

        elif curr_state == "in_neck":
            assert line_type in ("comment", "exec_stmt", "other")
            if line_type == "comment":
                curr_comment.append(line)
                lines[i] = (linenum, None)
            elif line_type == "exec_stmt":
                comments[curr_fn]["neck"] = curr_comment
                curr_comment = []
            else:
                pass  # nothing to do -- continue

        elif curr_state == "in_body":
            assert line_type in (
                "comment",
                "exec_stmt",
                "pgm_unit_end",
            ), f"[Line {linenum}]: {line}"

            if line_type == "comment":
                # Ignore empty lines, which are technically comments but which
                # don't contribute any semantic content.
                if line != "\n":
                    marker_var = f"{INTERNAL_COMMENT_PREFIX}_{linenum}"
                    marker_stmt = f"        {marker_var} = .True.\n"
                    internal_comments[marker_var] = line
                    lines[i] = (linenum, marker_stmt)
            else:
                pass  # nothing to do -- continue

        # update the current state
        curr_state = TRANSITIONS[curr_state][line_type]

    # if there's a comment at the very end of the file, make it the foot
    # comment of curr_fn
    if curr_comment != [] and comments.get(curr_fn):
        comments[curr_fn]["foot"] = curr_comment
        comments["$file_foot"] = curr_comment

    return (lines, comments)


Instruction:
Write a Python 3 script for
taking a line and splits it into two parts code_part comment_part and code_part where comment_part is the trailing comment character.

Response:
def split_trailing_comment(line: str) -> str:
    """Takes a line and splits it into two parts (code_part, comment_part)
    where code_part is the line up to but not including any trailing
    comment (the '!' comment character and subsequent characters
    to the end of the line), while comment_part is the trailing comment.

    Args:
        line: A line of Fortran source code.

    Returns:
        A pair (code_part, comment_part) where comment_part is the trailing
        comment.  If the line does not contain any trailing comment, then
        comment_part is None.
    """

    if line.find("!") == -1:
        return (line, None)

    i = 0
    while i < len(line):
        if line[i] == "'":
            j = line.find("'", i + 1)
            if j == -1:
                sys.stderr.write("WEIRD: unbalanced quote ': line = " + line)
                return line
            else:
                i = j + 1
        elif line[i] == '"':
            j = line.find('"', i + 1)
            if j == -1:
                sys.stderr.write('WEIRD: unbalanced quote ": line = ' + line)
                return line
            else:
                i = j + 1
        elif line[i] == "!":  # partial-line comment
            comment_part = line[i:]
            code_part = line[:i].rstrip() + "\n"
            return (code_part, comment_part)
        else:
            i += 1

    return (line, None)


Instruction:
Implement a function in Python 3 to
process is the interface used by an earlier version of this preprocessor.

Response:
def process(inputLines: List[str]) -> str:
    """process() provides the interface used by an earlier version of this
       preprocessor."""
    lines = separate_trailing_comments(inputLines)
    merge_continued_lines(lines)
    (lines, comments) = extract_comments(lines)
    actual_lines = [
        line[1]
        for line in lines
        if line[1] is not None and "i_g_n_o_r_e___m_e_" not in line[1]
    ]
    return "".join(actual_lines)


Instruction:
Create a Python 3 function to
assign uuids to nodes and edges.

Response:
def assign_uuids_to_nodes_and_edges(self):
        """ Assign uuids to nodes and edges. """
        for node in self.nodes(data=True):
            node[1]["id"] = str(uuid4())

        for edge in self.edges(data=True):
            edge[2]["id"] = str(uuid4())


Instruction:
Write a Python 3 function that can
construct an AnalysisGraph object from a pickle file containing a list of INDRA statements.

Response:
def from_statements_file(cls, file: str):
        """ Construct an AnalysisGraph object from a pickle file containing a
        list of INDRA statements. """

        with open(file, "rb") as f:
            sts = pickle.load(f)

        return cls.from_statements(sts)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_statements(
        cls, sts: List[Influence], assign_default_polarities: bool = True
    ):
        """ Construct an AnalysisGraph object from a list of INDRA statements.
        Unknown polarities are set to positive by default.

        Args:
            sts: A list of INDRA Statements

        Returns:
            An AnalysisGraph instance constructed from a list of INDRA
            statements.
        """

        _dict = {}
        for s in sts:
            if assign_default_polarities:
                for delta in deltas(s):
                    if delta["polarity"] is None:
                        delta["polarity"] = 1
            concepts = nameTuple(s)

            # Excluding self-loops for now:
            if concepts[0] != concepts[1]:
                if all(
                    map(exists, (delta["polarity"] for delta in deltas(s)))
                ):
                    if concepts in _dict:
                        _dict[concepts].append(s)
                    else:
                        _dict[concepts] = [s]

        edges = [
            (*concepts, {"InfluenceStatements": statements})
            for concepts, statements in _dict.items()
        ]
        return cls(edges)

Response: Construct an AnalysisGraph object from a list of INDRA Statements.


Instruction:
Can you tell what is the following Python 3 function doing
def from_text(cls, text: str):

        eidosProcessor = process_text(text)
        return cls.from_statements(eidosProcessor.statements)

Response: Construct an AnalysisGraph object from a text string.


Instruction:
Implement a Python 3 function for
constructing an AnalysisGraph object from a file containing INDRA statements serialized by Uncharted s CauseMos webapp.

Response:
def from_uncharted_json_file(cls, file):
        """ Construct an AnalysisGraph object from a file containing INDRA
        statements serialized exported by Uncharted's CauseMos webapp.
        """
        with open(file, "r") as f:
            _dict = json.load(f)
        return cls.from_uncharted_json_serialized_dict(_dict)


Instruction:
Given the following Python 3 function, write the documentation
def from_uncharted_json_serialized_dict(
        cls, _dict, minimum_evidence_pieces_required: int = 1
    ):
        """ Construct an AnalysisGraph object from a dict of INDRA statements
        exported by Uncharted's CauseMos webapp. """
        sts = _dict["statements"]
        G = nx.DiGraph()
        for s in sts:
            if len(s["evidence"]) >= minimum_evidence_pieces_required:
                subj, obj = s["subj"], s["obj"]
                if (
                    subj["db_refs"]["concept"] is not None
                    and obj["db_refs"]["concept"] is not None
                ):
                    subj_name, obj_name = [
                        "/".join(s[x]["db_refs"]["concept"].split("/")[:])
                        for x in ["subj", "obj"]
                    ]
                    G.add_edge(subj_name, obj_name)
                    subj_delta = s["subj_delta"]
                    obj_delta = s["obj_delta"]

                    for delta in (subj_delta, obj_delta):
                        # TODO : Ensure that all the statements provided by
                        # Uncharted have unambiguous polarities.
                        if delta["polarity"] is None:
                            delta["polarity"] = 1

                    influence_stmt = Influence(
                        Concept(subj_name, db_refs=subj["db_refs"]),
                        Concept(obj_name, db_refs=obj["db_refs"]),
                        subj_delta=s["subj_delta"],
                        obj_delta=s["obj_delta"],
                        evidence=[
                            INDRAEvidence(
                                source_api=ev["source_api"],
                                annotations=ev["annotations"],
                                text=ev["text"],
                                epistemics=ev.get("epistemics"),
                            )
                            for ev in s["evidence"]
                        ],
                    )
                    influence_sts = G.edges[subj_name, obj_name].get(
                        "InfluenceStatements", []
                    )
                    influence_sts.append(influence_stmt)
                    G.edges[subj_name, obj_name][
                        "InfluenceStatements"
                    ] = influence_sts

        for concept, indicator in _dict[
            "concept_to_indicator_mapping"
        ].items():
            if indicator is not None:
                indicator_source, indicator_name = (
                    indicator["name"].split("/")[0],
                    "/".join(indicator["name"].split("/")[1:]),
                )
                if concept in G:
                    if G.nodes[concept].get("indicators") is None:
                        G.nodes[concept]["indicators"] = {}
                    G.nodes[concept]["indicators"][indicator_name] = Indicator(
                        indicator_name, indicator_source
                    )

        self = cls(G)
        self.assign_uuids_to_nodes_and_edges()
        return self

Response: Construct an AnalysisGraph object from a dictionary of INDRA statements.


Instruction:
Can you create a Python 3 function that
assembles transition model from gradient adjectives.

Response:
def assemble_transition_model_from_gradable_adjectives(self):
        """ Add probability distribution functions constructed from gradable
        adjective data to the edges of the analysis graph data structure.

        Args:
            adjective_data
            res
        """

        df = pd.read_sql_table("gradableAdjectiveData", con=engine)
        gb = df.groupby("adjective")

        rs = gaussian_kde(
            flatMap(
                lambda g: gaussian_kde(get_respdevs(g[1]))
                .resample(self.res)[0]
                .tolist(),
                gb,
            )
        ).resample(self.res)[0]

        for edge in self.edges(data=True):
            edge[2]["ConditionalProbability"] = constructConditionalPDF(
                gb, rs, edge
            )
            edge[2]["βs"] = np.tan(
                edge[2]["ConditionalProbability"].resample(self.res)[0]
            )


Instruction:
Can you implement a function in Python 3 that
samples elements of the stochastic transition matrix from the prior state.

Response:
def sample_from_prior(self):
        """ Sample elements of the stochastic transition matrix from the prior
        distribution, based on gradable adjectives. """

        # simple_path_dict caches the results of the graph traversal that finds
        # simple paths between pairs of nodes, so that it doesn't have to be
        # executed for every sampled transition matrix.

        node_pairs = list(permutations(self.nodes(), 2))
        simple_path_dict = {
            node_pair: [
                list(pairwise(path))
                for path in nx.all_simple_paths(self, *node_pair)
            ]
            for node_pair in node_pairs
        }

        self.transition_matrix_collection = []

        elements = self.get_latent_state_components()

        for i in range(self.res):
            A = pd.DataFrame(
                np.identity(2 * len(self)), index=elements, columns=elements
            )

            for node in self.nodes:
                A[f"∂({node})/∂t"][node] = self.Δt

            for node_pair in node_pairs:
                A[f"∂({node_pair[0]})/∂t"][node_pair[1]] = sum(
                    np.prod(
                        [
                            self.edges[edge[0], edge[1]]["βs"][i]
                            for edge in simple_path_edge_list
                        ]
                    )
                    * self.Δt
                    for simple_path_edge_list in simple_path_dict[node_pair]
                )
            self.transition_matrix_collection.append(A)


Instruction:
Create a Python 3 function to
sample observed state vector. This is the implementation of the Observed state vector function. This is the implementation of the Observed state vector function.

Response:
def sample_observed_state(self, s: pd.Series) -> Dict:
        """ Sample observed state vector. This is the implementation of the
        emission function.

        Args:
            s: Latent state vector.

        Returns:
            Observed state vector.
        """

        return {
            n[0]: {
                i.name: np.random.normal(s[n[0]] * i.mean, i.stdev)
                for i in n[1]["indicators"].values()
            }
            for n in self.nodes(data=True)
        }


Instruction:
Write a Python 3 script to
sample observed state sequences from the likelihood model given a collection of transition matrices.

Response:
def sample_from_likelihood(self, n_timesteps=10):
        """ Sample a collection of observed state sequences from the likelihood
        model given a collection of transition matrices.

        Args:
            n_timesteps: The number of timesteps for the sequences.
        """

        self.latent_state_sequences = lmap(
            lambda A: ltake(
                n_timesteps,
                iterate(
                    lambda s: pd.Series(A @ s.values, index=s.index), self.s0
                ),
            ),
            self.transition_matrix_collection,
        )

        self.observed_state_sequences = [
            [self.sample_observed_state(s) for s in latent_state_sequence]
            for latent_state_sequence in self.latent_state_sequences
        ]


Instruction:
Create a Python 3 function for
sampling a new transition matrix from the proposal distribution A.

Response:
def sample_from_proposal(self, A: pd.DataFrame) -> None:
        """ Sample a new transition matrix from the proposal distribution,
        given a current candidate transition matrix. In practice, this amounts
        to the in-place perturbation of an element of the transition matrix
        currently being used by the sampler.

        Args
        """

        # Choose the element of A to perturb
        self.source, self.target, self.edge_dict = random.choice(
            list(self.edges(data=True))
        )
        self.original_value = A[f"∂({self.source})/∂t"][self.target]
        A[f"∂({self.source})/∂t"][self.target] += np.random.normal(scale=0.001)


Instruction:
Given the following Python 3 function, write the documentation
def get_timeseries_values_for_indicators(
        self, resolution: str = "month", months: Iterable[int] = range(6, 9)
    ):
        """ Attach timeseries to indicators, for performing Bayesian inference.
         """
        if resolution == "month":
            funcs = [
                partial(get_indicator_value, month=month) for month in months
            ]
        else:
            raise NotImplementedError(
                "Currently, only the 'month' resolution is supported."
            )

        for n in self.nodes(data=True):
            for indicator in n[1]["indicators"].values():
                indicator.timeseries = [
                    func(indicator, year="2017")[0] for func in funcs
                ]
                if len(set(indicator.timeseries)) == 1:
                    indicator.timeseries = None

Response: Attach timeseries to indicators for performing Bayesian inference.


Instruction:
Write a Python 3 function that can
run Bayesian inference - sample from the posterior distribution.

Response:
def sample_from_posterior(self, A: pd.DataFrame) -> None:
        """ Run Bayesian inference - sample from the posterior distribution."""
        self.sample_from_proposal(A)
        self.set_latent_state_sequence(A)
        self.update_log_prior(A)
        self.update_log_likelihood()

        candidate_log_joint_probability = self.log_prior + self.log_likelihood

        delta_log_joint_probability = (
            candidate_log_joint_probability - self.log_joint_probability
        )

        acceptance_probability = min(1, np.exp(delta_log_joint_probability))
        if acceptance_probability > np.random.rand():
            self.update_log_joint_probability()
        else:
            A[f"∂({self.source})/∂t"][self.target] = self.original_value
            self.set_latent_state_sequence(A)
            self.update_log_likelihood()
            self.update_log_prior(A)
            self.update_log_joint_probability()


Instruction:
Can you implement a function in Python 3 that
infers the transition matrix coefficient from data.

Response:
def infer_transition_matrix_coefficient_from_data(
        self,
        source: str,
        target: str,
        state: Optional[str] = None,
        crop: Optional[str] = None,
    ):
        """ Infer the distribution of a particular transition matrix
        coefficient from data.

        Args:
            source: The source of the edge corresponding to the matrix element
                to infer.
            target: The target of the edge corresponding to the matrix element
                to infer.
            state:
                The state in South Sudan for which the transition matrix
                coefficient should be calculated.
            crop:
                The crop for which the transition matrix coefficient should be
                calculated.
        """
        rows = engine.execute(
            f"select * from dssat where `Crop` like '{crop}'"
            f" and `State` like '{state}'"
        )
        xs, ys = lzip(*[(r["Rainfall"], r["Production"]) for r in rows])
        xs_scaled, ys_scaled = xs / np.mean(xs), ys / np.mean(ys)
        p, V = np.polyfit(xs_scaled, ys_scaled, 1, cov=True)
        self.edges[source, target]["βs"] = np.random.normal(
            p[0], np.sqrt(V[0][0]), self.res
        )
        self.sample_from_prior()


Instruction:
Given the following Python 3 function, write the documentation
def create_bmi_config_file(self, filename: str = "bmi_config.txt") -> None:
        s0 = self.construct_default_initial_state()
        s0.to_csv(filename, index_label="variable")

Response: Create a BMI config file to initialize the model.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def default_update_function(self, n: Tuple[str, dict]) -> List[float]:
        return [
            self.transition_matrix_collection[i].loc[n[0]].values
            @ self.s0[i].values
            for i in range(self.res)
        ]

Response: The default update function for a CAG node.


Instruction:
Write a Python 3 function that can
initialize the AnalysisGraph with a config file.

Response:
def initialize(
        self, config_file: str = "bmi_config.txt", initialize_indicators=True
    ):
        """ Initialize the executable AnalysisGraph with a config file.

        Args:
            config_file

        Returns:
            AnalysisGraph
        """
        self.t = 0.0
        if not os.path.isfile(config_file):
            self.create_bmi_config_file(config_file)

        self.s0 = [
            pd.read_csv(
                config_file, index_col=0, header=None, error_bad_lines=False
            )[1]
            for _ in range(self.res)
        ]
        self.s0_original = self.s0[0].copy(deep=True)

        self.latent_state_vector = self.construct_default_initial_state()

        for n in self.nodes(data=True):
            rv = LatentVar(n[0])
            n[1]["rv"] = rv
            n[1]["update_function"] = self.default_update_function
            rv.dataset = [1.0 for _ in range(self.res)]
            rv.partial_t = self.s0[0][f"∂({n[0]})/∂t"]
            if initialize_indicators:
                for indicator in n[1]["indicators"].values():
                    indicator.samples = np.random.normal(
                        indicator.mean * np.array(n[1]["rv"].dataset),
                        scale=0.01,
                    )


Instruction:
Create a Python 3 function to
advance the model by one time step.

Response:
def update(self, τ: float = 1.0, update_indicators=True, dampen=False):
        """ Advance the model by one time step. """

        for n in self.nodes(data=True):
            n[1]["next_state"] = n[1]["update_function"](n)

        for n in self.nodes(data=True):
            n[1]["rv"].dataset = n[1]["next_state"]

        for n in self.nodes(data=True):
            for i in range(self.res):
                self.s0[i][n[0]] = n[1]["rv"].dataset[i]
                if dampen:
                    self.s0[i][f"∂({n[0]})/∂t"] = self.s0_original[
                        f"∂({n[0]})/∂t"
                    ] * exp(-τ * self.t)
            if update_indicators:
                for indicator in n[1]["indicators"].values():
                    indicator.samples = np.random.normal(
                        indicator.mean * np.array(n[1]["rv"].dataset),
                        scale=0.01,
                    )

        self.t += self.Δt


Instruction:
Here you have a function in Python 3, explain what it does
def export_node(self, n) -> Dict[str, Union[str, List[str]]]:
        node_dict = {
            "name": n[0],
            "units": _get_units(n[0]),
            "dtype": _get_dtype(n[0]),
            "arguments": list(self.predecessors(n[0])),
        }

        if not n[1].get("indicators") is None:
            for indicator in n[1]["indicators"].values():
                if "dataset" in indicator.__dict__:
                    del indicator.__dict__["dataset"]

            node_dict["indicators"] = [
                _process_datetime(indicator.__dict__)
                for indicator in n[1]["indicators"].values()
            ]
        else:
            node_dict["indicators"] = None

        return node_dict

Response: Returns a dict suitable for exporting to JSON.


Instruction:
Explain what the following Python 3 code does
def to_dict(self) -> Dict:
        return {
            "name": self.name,
            "dateCreated": str(self.dateCreated),
            "variables": lmap(
                lambda n: self.export_node(n), self.nodes(data=True)
            ),
            "timeStep": str(self.Δt),
            "edge_data": lmap(export_edge, self.edges(data=True)),
        }

Response: Export the CAG to a dict that can be serialized to JSON.


Instruction:
Create a Python 3 function for
mapping each concept node in the AnalysisGraph instance to one or more tangible quantities known as indicators.

Response:
def map_concepts_to_indicators(
        self, n: int = 1, min_temporal_res: Optional[str] = None
    ):
        """ Map each concept node in the AnalysisGraph instance to one or more
        tangible quantities, known as 'indicators'.

        Args:
            n: Number of matches to keep
            min_temporal_res: Minimum temporal resolution that the indicators
            must have data for.
        """

        for node in self.nodes(data=True):
            query_parts = [
                "select Indicator from concept_to_indicator_mapping",
                f"where `Concept` like '{node[0]}'",
            ]

            # TODO May need to delve into SQL/database stuff a bit more deeply
            # for this. Foreign keys perhaps?

            query = "  ".join(query_parts)
            results = engine.execute(query)

            if min_temporal_res is not None:
                if min_temporal_res not in ["month"]:
                    raise ValueError("min_temporal_res must be 'month'")

                vars_with_required_temporal_resolution = [
                    r[0]
                    for r in engine.execute(
                        "select distinct `Variable` from indicator where "
                        f"`{min_temporal_res.capitalize()}` is not null"
                    )
                ]
                results = [
                    r
                    for r in results
                    if r[0] in vars_with_required_temporal_resolution
                ]

            node[1]["indicators"] = {
                x: Indicator(x, "MITRE12")
                for x in [r[0] for r in take(n, results)]
            }