text:
import cv2
import numpy as np

net = cv2.dnn.readNet('colab\yolov3_training_final.weights', 'colab\yolov3_training.cfg')

classes = []
with open("colab\classes.txt", "r") as f:
    classes = f.read().splitlines()

cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(100, 3))

while True:
    _, img = cap.read()
    height, width, _ = img.shape

    blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0,0,0), swapRB=True, crop=False)
    net.setInput(blob)
    output_layers_names = net.getUnconnectedOutLayersNames()
    layerOutputs = net.forward(output_layers_names)

    boxes = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.2:
                center_x = int(detection[0]*width)
                center_y = int(detection[1]*height)
                w = int(detection[2]*width)
                h = int(detection[3]*height)

                x = int(center_x - w/2)
                y = int(center_y - h/2)

                boxes.append([x, y, w, h])
                confidences.append((float(confidence)))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.4)

    if len(indexes)>0:
        for i in indexes.flatten():
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidences[i],2))
            color = colors[i]
            cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
            cv2.putText(img, label + " " + confidence, (x, y+20), font, 2, (255,255,255), 2)

    cv2.imshow('Image', img)
    key = cv2.waitKey(1)
    if key==27:
        break

cap.release()
cv2.destroyAllWindows()


text:
from typing import Tuple, List, Set, Optional, Union, Any

import itertools
import numpy as np
import tensorflow as tf
from gym import spaces as gym_spaces

from tf_agents.environments.gym_wrapper import GymWrapper
from tf_agents.environments.tf_py_environment import TFPyEnvironment
from tf_agents.environments.wrappers import ActionRepeat
from tf_agents.environments.parallel_py_environment import ParallelPyEnvironment

import snc.environments.controlled_random_walk as crw
import snc.environments.job_generators.job_generator_interface as jobgen
import snc.environments.state_initialiser as stinit
import snc.utils.snc_tools as snc
import snc.utils.snc_types as snc_types

class RLControlledRandomWalk(crw.ControlledRandomWalk):
    def __init__(self,
                 cost_per_buffer: snc_types.StateSpace,
                 capacity: snc_types.StateSpace,
                 constituency_matrix: snc_types.ConstituencyMatrix,
                 job_generator: jobgen.JobGeneratorInterface,
                 state_initialiser: stinit.CRWStateInitialiser,
                 job_conservation_flag: bool = False,
                 list_boundary_constraint_matrices: Optional[List[np.ndarray]] = None,
                 model_type: str = 'push',
                 index_phys_resources: Optional[Tuple] = None,
                 ind_surplus_buffers: Optional[List[int]] = None,
                 max_episode_length: Optional[int] = None,
                 normalise_observations: bool = True) -> None:
        """
        Environment for testing stochastic network control with reinforcement learning agents:

        This environment is a child of the ControlledRandomWalk class and adds a definition of the
        action space and observation space as required for standard RL implementations.

        We consider a network of (possibly) multiple resources, each one with (possibly) multiple
        buffers. The resources can schedule in which of its buffers they process jobs at every time
        step, and where to route the processed jobs.

        The controlled random walk (CRW) describes the stochastic evolution of jobs in each
        buffer, under some policy that controls how the resources schedule and route the jobs,
        and where the arrival of new jobs and the processing of the current jobs are stochastic.
        The arrivals follow a job generator, which could be e.g. a Poisson process, such that the
        number of new jobs at each time step follows a Poisson distribution, with mean given by
        some demand rate vector. The success of finishing a job when a resource is working on it
        is also given by the job generator, which could be e.g. another Poisson process.

        Each buffer can start filled with some amount of jobs.
        The episode ends when all buffers are empty.
        Reward is negative (i.e., cost) and linear with the number of jobs in each buffer.
        This is a single agent environment.

        The action space is defined as a series of action sub-spaces one for each set of coupled
        resources. Resources may be coupled by environment constraints. The series of action
        sub-spaces are then independent. The action subspaces are designed to receive one-hot
        actions or a distribution over actions which can then be interpreted by the environment as a
        binary vector of activities for each coupled resource set which can then be added together
        across resource sets to attain an action for the full environment.

        The observation is unbounded and defined as a continuous space of dimension equal to the
        number of buffers. The environment is fully observable where the state is the length of the
        buffers.

        :param cost_per_buffer: cost per unit of inventory per buffer.
        :param capacity: List with maximum number of jobs allowed at each buffer.
        :param constituency_matrix:  Matrix whose s-th row corresponds to resource s; and each
            entry, C_{si} in {0, 1}, specifies whether activity i
            corresponds to resource s (examples of activities are scheduling a buffer or routing to
            another resource).
        :param job_generator: object to generate events from
        :param state_initialiser: initialiser for state
        :param job_conservation_flag: boolean flag that controls whether:
            'True' = we want to ensure energy conservation when updating the step (moving only jobs
            that exist), or 'False' = We leave this task to the policy. This parameter is False by
            default.
        :param list_boundary_constraint_matrices: List of binary matrices, one per resource, that
            indicates conditions (number of rows) on which buffers cannot be empty to avoid idling.
            If this parameter is not passed, it assumes no boundary constraints by default.
        :param model_type: String indicating if this is a `'pull'` or `'push'` model.
        :param index_phys_resources: Tuple indexing the rows of constituency matrix that correspond
            to physical resources (as opposed to other rows that might represent coupled constraints
            among physical resources). If this parameter is not passed, then it considers all rows
            of the constituency matrix as corresponding to physical resources by default.
        :param ind_surplus_buffers: List of integers indicating the location of the surplus
            buffers in the buffer processing matrix.
        :param max_episode_length: Integer number of time steps to allow in each episode. Defaults
            to None meaning infinite episodes.
        :param normalise_observations: Boolean determining whether the observation of the state is
            normalised such that its L1 norm is unity. This should be set to False for PPO agents as
            they have internal observation normalisation.
        """
        self._is_initialising = True
        super().__init__(
            cost_per_buffer,
            capacity,
            constituency_matrix,
            job_generator,
            state_initialiser,
            job_conservation_flag,
            list_boundary_constraint_matrices,
            model_type,
            index_phys_resources,
            ind_surplus_buffers,
            max_episode_length)

        # The RL environment wrapper augments the initialisation with method calls to construct the
        # action space and construction of the observation space.
        self._construct_action_space()
        self._observation_space = gym_spaces.Box(
            low=0, high=np.inf, shape=(self._num_buffers,), dtype=np.float32)
        self._max_episode_length = max_episode_length
        self._is_initialising = False
        self._normalise_obs = normalise_observations

    def _construct_action_space(self) -> None:
        """
        Builds the action space for an RL agent as a tuple of subspaces one for each independent
        resource set.

        A resource set is a collection of resources for which the action space is coupled due to
        activity constraints. For example, if resources 1 and 2 are constrained such that the action
        of resource 1 could possibly restrict the action space of resource 2 their actions are not
        independent and therefore they form a resource set.
        """
        # Attain the constituency matrix with shadow resources used to invoke constraints removed.
        unconstrained_constituency_matrix = self._constituency_matrix[
            list(self._index_phys_resources)]

        # If there are no constraints to be considered each resource can act independently.
        if len(self._index_phys_resources) == self._num_resources:
            # Resources function independently and can therefore each have their own action space.
            # First work out how many activities are associated with each resource and then the
            # number of actions is the number of possible activities + 1 as activities are mutually
            # independent but there is the possibility of not pursuing any activity (idling).
            actions_per_resource = unconstrained_constituency_matrix.sum(axis=1) + 1
            # Define the action space as a series of binary action spaces.
            action_spaces = list(
                map(lambda d: gym_spaces.Box(low=0.0, high=1.0, shape=(1, int(d)),
                                             dtype=np.float32), actions_per_resource))
            # We now build a list of action vectors where each resource either idles or pursues one
            # of the activities associated with it.
            action_vectors_list = []
            for resource in range(unconstrained_constituency_matrix.shape[0]):
                # First account for the idle operation
                action_vectors_list.append(np.zeros(self._num_activities))
                # Then find which activities are related to the resource and produce one-hot
                # encodings for them.
                for activity in np.where(unconstrained_constituency_matrix[resource] == 1)[0]:
                    action_vectors_list.append(np.eye(self._num_activities)[activity])
        else:
            # There are cross-resource dependencies to handle making the action space more complex.
            # First attain a matrix which represents the constraints.
            constraint_indices = set(range(self._num_resources)) - set(self._index_phys_resources)
            constraints = self._constituency_matrix[list(constraint_indices)]
            # Ensure that constraints are valid by ensuring that they consider at least two
            # activities. Note that constraints are free to reimpose the mutual exclusivity
            # constraint over activities assumed for each resource. These constraints are
            # unnecessary but valid nonetheless and simply add to the computational load.
            assert np.all(constraints.sum(axis=1) >= 2)
            # Use the constituency matrix and constraints matrix to build the more complex action
            # space with resources coupled by constraints.
            action_spaces, action_vectors_list = self._action_spaces_from_resource_sets(
                unconstrained_constituency_matrix, constraints)
        # Flatten the list of action vectors to get all actions.
        all_action_vectors = action_vectors_list
        self._action_vectors = np.vstack(all_action_vectors)
        # The final action space from an RL point of view is a tuple of the action subspaces for
        # each resource set.
        self._rl_action_space = gym_spaces.Tuple(action_spaces)
        # The action space in terms of activities is a binary space with dimension equal to the
        # number of activities in the network. However, it is implemented as a Box space so that
        # when we wrap the environment for tf_agents the action space takes values of type float as
        # are provided by the neural network.
        self._activities_action_space = gym_spaces.Box(
            low=0.0, high=1.0, shape=(1, self._num_activities), dtype=np.float32
        )

    def _action_spaces_from_resource_sets(
            self,
            constituency_matrix: snc_types.ConstituencyMatrix,
            constraints: np.ndarray) -> Tuple[List[gym_spaces.Box], List[np.ndarray]]:
        """
        Calculates the dimension of action spaces for networks where constraints require that some
        resources have dependent action sets. The action spaces are designed to receive one-hot
        actions which correspond to the activities being pursued by the set of resources associated
        with the action subspaces. These actions can then be summed over resource sets to attain an
        action for the full network.

        :param constituency_matrix: The binary matrix representing which activities are associated
            with which resources. Note that this matrix is for physical resources only and hence is
            of shape (num_physical_resources, num_actions)
        :param constraints: The binary matrix representing which actions are constrained not to
            occur together
        :return: A list of action spaces one for each resource set and a list of lists of action
            vectors denoting activities to perform associated with each dimension of the (binary)
            action spaces.
        """
        # Attain a binary matrix representing which resources are affected by which constraints.
        # The resulting binary matrix is of shape (num_resources, num_constraints)
        resource_constraints_matrix = (constituency_matrix @ constraints.T) > 0
        coupled_resource_sets = self._find_coupled_resource_sets(resource_constraints_matrix)
        # Attain one set of all of the resources which cannot be treated independently to aid in
        # checking when to utilise the more complex action space construction code.
        all_coupled_resources = set.union(*coupled_resource_sets)

        # Find which activities are associated with each resource.
        activities_for_resources = np.apply_along_axis(
            lambda ary: set(np.where(ary)[0]),
            axis=1,
            arr=constituency_matrix
        )
        # Initialise empty lists which will be used to build up the action spaces and the associated
        # binary activity tuples which are associated with each action.
        action_spaces = []
        all_action_vectors = []
        # A further set is initialised to store the set of resources which have already been
        # considered to aid in avoiding double counting.
        processed_resources: Set[int] = set()

        # Loop over resources and then build the related action space. This provides a specific
        # ordering so that the action subspaces will always be constructed in the same order and the
        # related resources can be reverse engineered.
        for i in range(constituency_matrix.shape[0]):
            # If we have already considered the resource then do nothing. This will occur when the
            # resource is in fact coupled with a previously processed resource through a constraint.
            if i in processed_resources:
                pass
            elif i in all_coupled_resources:
                # The resource has not been considered yet and is coupled to other resources by at
                # least one constraint.

                # Find which resource set the current resource belongs to and then process the
                # resource set.
                for rs in coupled_resource_sets:
                    if i in rs:
                        # Find the set of possible actions for this resource set by taking the
                        # product of the action spaces for each resource independently and excluding
                        # any actions that violate the constraints.
                        # The action spaces are considered in terms of the activities that each
                        # resource controls. We are careful to include no-activity (idle) as None.
                        # This builds actions as a set of activity indices which are later used to
                        # attain binary action vectors (1 denotes activity is active else 0).

                        # Find all actions (including those which would violate constraints) using
                        # itertools to take the product of the action spaces articulated as activity
                        # indices (as a list `afr` actions for resource). This is a product of the
                        # actions spaces for all resources in the current resource set (`rs`) where
                        # each resource is treated as though it may act independently. The actions
                        # which violate constraints are removed later. `None` is used to denote
                        # idling since there is no valid index for idling as this is a vector of
                        # zeros when articulated in the binary action space (of dimension
                        # `num_activities`).
                        all_actions = [*itertools.product(*[[None] + list(afr) for afr
                                                            in activities_for_resources[list(rs)]])]
                        # Initialise a list of valid action vectors.
                        action_vectors = []
                        # Consider each possible action, encode it as a binary action and determine
                        # whether or not it is valid.
                        for action in all_actions:
                            # Determine which activities are active under each action by filtering
                            # out Nones. This then provides the indices to set to 1 in the binary
                            # action vector.
                            activities = list(filter(lambda a: a is not None, action))
                            # Set up the binary action vector
                            binary_action = np.zeros(self._num_activities)
                            if activities:
                                binary_action[activities] = 1
                            # Determine whether the action violates any constraints. It violates a
                            # constraint if at least two of the activities in the constraint are
                            # being pursued since the constraints impose that at most one of the
                            # activities they mark can be active at one time.
                            if not np.any(constraints.dot(binary_action) >= 2):
                                # If the action is valid append it to the list of action vectors.
                                action_vectors.append(binary_action)
                        # Add the list of possible actions for this resource set to the list of
                        # action sets.
                        all_action_vectors.append(
                            np.reshape(np.array(action_vectors), (-1, self._num_activities))
                        )
                        # Set up the gym action space using the fact that each dimension of the gym
                        # action space corresponds to one of the action vectors. The gym space is a
                        # Box so that when we wrap the environment for tf_agents the action space
                        # takes values of type float as are provided by the neural network.
                        num_actions = len(action_vectors)
                        action_spaces.append(
                            gym_spaces.Box(
                                low=0.0, high=1.0, shape=(1, num_actions), dtype=np.float32
                            )
                        )
                        # Ensure that we mark the resources in the current set as processed.
                        processed_resources = processed_resources.union(rs)
            else:
                # The resource can act independently of all others and therefore the action space
                # is relatively simple.
                # The number of actions is equal to the number of activities associated with the
                # resource + 1 as each activity is mutually exclusive and it is also possible to do
                # nothing.
                num_actions = int(constituency_matrix[i].sum() + 1)
                # action_vectors is a list of binary vectors denoting which activities are being
                # taken.
                action_vectors = [np.zeros(self._num_activities)]
                # To build action vectors we need to find which activities this resource can
                # possibly undertake.
                possible_activities = np.where(constituency_matrix[i] == 1)[0]
                # The possible activities then form actions when represented by one-hot encodings
                # in this case (as we only consider one resource so one activity with idle already
                # included).
                action_vectors += [np.eye(self._num_activities)[k] for k in possible_activities]
                # Add the list of action vectors to the list of lists of action vectors for each
                # resource set.
                action_vector_array = np.array(action_vectors)
                all_action_vectors.append(
                    np.reshape(action_vector_array, (-1, self._num_activities))
                )
                # Finally set up the gym action space where each dimension corresponds to one action
                # vector.
                # The gym space is a Box so that when we wrap the environment for tf_agents the
                # action space takes values of type float as are provided by the neural network.
                action_spaces.append(
                    gym_spaces.Box(low=0.0, high=1.0, shape=(1, num_actions), dtype=np.float32))
        return action_spaces, all_action_vectors

    @staticmethod
    def _find_coupled_resource_sets(resource_constraints_matrix: np.ndarray) -> List[set]:
        """
        Determines which resources are coupled by the constraints. This is done by initially finding
        which resources are affected by each constraint and then find whether there are chains in
        the constraints which link multiple resource sets.

        e.g. constraint 1 affects resources 1 and 2, constraint 2 affects resources 3 and 4 and
        constraint 3 affects resources 1 and 4 such that overall resources 1, 2, 3 and 4 have to
        be treated jointly.

        :param resource_constraints_matrix:  Binary matrix representing which resources are affected
            by which constraints. This matrix should have shape (num_resources, num_constraints).
        :return: A list of sets of resource sets where resources are grouped according to the
            constraints which cause their activities (and therefore their action spaces) to be
            dependent.
        """
        # Find which resources are linked by each constraint (without then considering the deeper
        # links occurring from multiple constraints).
        resource_sets = np.apply_along_axis(
            lambda ary: set(np.where(ary)[0]),
            axis=0,
            arr=resource_constraints_matrix
        )
        # Initialise a list which will ultimately contain sets of resources which are grouped.
        combined_resource_sets: List[Set[int]] = []
        # Consider each resource set from the initial shallow parsing of constraints.
        for rs in resource_sets:
            # If there is already at least one group of resources then we must consider if the new
            # resource set links them.
            if combined_resource_sets:
                # Initialise a resource set which combines resources coupled by multiple
                # constraints.
                combined_rs = rs
                # Initialise a list to keep track of the indices of the existing resource sets which
                # will be superseded by the new larger set so that they can be removed from the list
                # of resource sets later.
                rs_indices = []

                # Consider each existing resource set and see whether it needs to be merged with the
                # resource set currently being considered.
                for i, existing_rs in enumerate(combined_resource_sets):
                    # The resource sets need to be merged if the same resource appears in both.
                    if existing_rs.intersection(combined_rs):
                        # Track the index of the existing resource set which has been merged and is
                        # therefore now obsolete.
                        rs_indices.append(i)
                        # Merge the resource sets
                        combined_rs = combined_rs.union(existing_rs)
                # Now remove all of the resource sets which have been merged so that no resource
                # appears in more than one resource set. Note that we do this from the largest index
                # to the smallest to avoid messing with the indexing for later removals.
                for i in reversed(rs_indices):
                    combined_resource_sets.pop(i)
                # Finally add the enlarged resource set to the list of resource sets.
                combined_resource_sets.append(combined_rs)
            else:
                # There are no other resource sets to consider yet so simply add this resource set
                # as the first one in the list.
                combined_resource_sets.append(rs)
        return combined_resource_sets

    def _interpret_rl_action(self, binary_action: np.ndarray) -> np.ndarray:
        """
        Takes an action from the RL agent and puts it in terms of activities in the network.

        :param binary_action: The action vector(s) from the RL agent which is formed from the
            concatenation of the one-hot actions for each resource set.
        :return: A binary action vector where the 1s denote active activities.
        """
        # Ensure that the action resulting from the RL agent is binary.
        # Binarisation should be handled in the agent.
        assert snc.is_binary(binary_action)
        # Determine which action vectors are selected.
        action_indices = np.where(binary_action == 1)[-1]
        # Use action indices to attain constituent action vectors (for each resource set)
        action_vectors = self._action_vectors[action_indices]
        # The overall action combines the activities across resource sets. This can be achieved by
        # summing over resource sets
        action = np.sum(action_vectors, axis=0)
        # Check that the action is binary (i.e. has no entries outside of {0, 1} which could occur
        # if the action vectors are not set up correctly)
        assert snc.is_binary(action)
        return action

    def preprocess_action(
            self,
            action: Union[np.ndarray, tf.Tensor, Tuple[Union[tf.Tensor, tf.Variable, np.ndarray]]]
    ) -> np.ndarray:
        """
        Prepares the action(s) for the original environment by putting them in the right format and
        transforming them from a series of one-hot vectors per resource set to a single binary
        action vector where 1 denotes active activities and 0 denotes idle activities.

        :param action: The actions for the resource sets in the environment as a numpy array or
            TensorFlow tensor if there is only one resource set or a tuple of one-hot vectors (one
            for each resource set).
        :return: The binary action as a numpy array of shape (num_activities, 1)
        """
        # If a tuple is provided but there is only one resource set then we can simply take the
        # action out of the tuple.
        if isinstance(action, tuple) and len(action) == 1:
            action = action[0]
        # Otherwise we need to concatenate actions across resource sets to attain an over action for
        # the network. This is done carefully accounting for different concatenation approaches in
        # numpy and TensorFlow.
        if isinstance(action, tuple):
            if isinstance(action[0], np.ndarray):
                action = np.concatenate(action, axis=-1)
            # If the actions for each subspace are valid TensorFlow 2 objects they will have a numpy
            # method to attain a numpy array from the Tensor/Variable.
            elif all([hasattr(a, "numpy") for a in action]):
                action = tf.concat(action, axis=-1).numpy()
            else:
                # Actions must be supplied as numpy arrays or TensorFlow tensors.
                raise ValueError(
                    "Action needs to be a (tuple of) valid TensorFlow or numpy objects."
                )
        # If the action is in a TensorFlow format attain it as a numpy array so that the environment
        # need only handle numpy arrays.
        elif hasattr(action, "numpy"):
            action = action.numpy()  # type: ignore
        # In the case that the action is already in the correct format then simply pass it through.
        elif isinstance(action, np.ndarray):
            pass
        else:
            # Catch any malformed actions (in terms of type).
            raise ValueError("Action needs to be a (tuple of) valid TensorFlow or numpy objects.")
        # Use the _interpret_rl_action method to transform the set of one-hot actions to a binary
        # action in the format required.
        action_in_activity_space = self._interpret_rl_action(action)
        # The environment expects column vectors so reshape if needed.
        if len(action_in_activity_space.shape) == 1:
            action_in_activity_space = action_in_activity_space[:, None]
        return action_in_activity_space

    def step(
            self,
            action: Union[np.ndarray, Tuple[np.ndarray]]
    ) -> Tuple[snc_types.StateSpace, float, bool, Any]:
        """
        Processes the action from the agent and then returns the environment state, reward,
        termination flag and information from the environment.

        :param action: The actions for the resource sets in the environment as a numpy array or
            TensorFlow tensor, if there is only one resource set or a tuple of one-hot vectors (one
            for each resource set).
        :return: next_state, reward, done, info
        """
        # Process the action(s) provided and then pass them on to the step method of the underlying
        # ControlledRandomWalk class.
        action = self.preprocess_action(action)
        state, reward, done, info = super().step(action)
        # Scale the state as it is used as an input to a neural net.
        scaled_state = self.normalise_state(state)
        # Transpose the state to ensure that is (batch_size x num_buffers) as required for
        # TensorFlow.
        # At least 1D used to ensure that in the case of a 1 dimensional state an array is returned
        # rather than a scalar.
        return np.atleast_1d(np.squeeze(scaled_state.T)), reward, done, info

    def normalise_state(self, state: snc_types.StateSpace) -> snc_types.StateSpace:
        """
        Normalises the observed state by dividing by the sum of the entries to make it more amenable
        to neural networks.

        :param state: The unscaled state from the ControlledRandomWalk instance.
        :return: The scaled state.
        """
        scaled_state = state.astype(np.float32)
        if self._num_buffers > 1 and scaled_state.sum() > 0:
            scaled_state = scaled_state / scaled_state.sum()
        return scaled_state

    def reset(self) -> snc_types.StateSpace:
        """
        Wraps the reset method of the original environment to manipulate the returned state such
        that it is of a form amenable to TensorFlow (shape batch_size x observation_dimension)

        :return: numpy array of the current state.
        """
        state = super().reset()

        # Handle the initial call from super().__init__
        if self._is_initialising:
            return state

        # Scale the state as it is used as an input to a neural net.
        if self._normalise_obs:
            scaled_state = self.normalise_state(state)
        else:
            scaled_state = state.astype(np.float32)

        # At least 1D used to ensure that in the case of a 1 dimensional state an array is
        # returned rather than a scalar.
        return np.atleast_1d(np.squeeze(scaled_state.T))

    @property
    def activities_action_space(self):
        return self._activities_action_space

    @property
    def action_space(self):
        return self._rl_action_space

    @property
    def observation_space(self):
        return self._observation_space

    @property
    def action_vectors(self):
        return self._action_vectors

    @property
    def num_buffers(self):
        return self._num_buffers

    @property
    def normalise_obs(self):
        return self._normalise_obs

    @normalise_obs.setter
    def normalise_obs(self, normalise: bool):
        self._normalise_obs = normalise

    def render(self, mode='human'):
        raise NotImplementedError

def rl_env_from_snc_env(env: crw.ControlledRandomWalk, discount_factor: float,
                        tf_action_repetitions: int = 1, parallel_environments: int = 1,
                        for_tf_agent: bool = True, normalise_observations: bool = True) \
        -> Tuple[Union[TFPyEnvironment, RLControlledRandomWalk], Tuple[int, ...]]:
    """
    Wraps a standard environment to make a TensorFlow compatible environment.

    :param env: The original ControlledRandomWalk environment.
    :param discount_factor: The discount factor applied to future returns.
    :param tf_action_repetitions: Number of time steps each selected action is repeated for.
        TensorFlow environment only.
    :param parallel_environments: Number of environments to run in parallel.
    :param for_tf_agent: Boolean determining whether to wrap the ControlledRandomWalk to be fully
        TensorFlow compatible (defaults to True). This may be false in cases where we just want
        to augment the environment with RL action interpretation methods.
    :param normalise_observations: Boolean determining whether the observation of the state is
            normalised such that its L1 norm is unity.
    :return: A wrapped environment ready for Reinforcement Learning/TensorFlow Agents alongside
        a tuple describing the action space dimensions.
    """
    assert isinstance(tf_action_repetitions, int), 'Action repetitions must be an integer.'
    assert tf_action_repetitions > 0, 'Each action must be repeated a positive number of times.'
    # First initialise an RLControlledRandomWalk as defined above.
    base_env = RLControlledRandomWalk(
        cost_per_buffer=env.cost_per_buffer,
        capacity=env.capacity,
        constituency_matrix=env.constituency_matrix,
        job_generator=env.job_generator,
        state_initialiser=env.state_initialiser,
        job_conservation_flag=env.energy_conservation_flag,
        list_boundary_constraint_matrices=env.list_boundary_constraint_matrices,
        model_type=env.model_type,
        index_phys_resources=env.index_phys_resources,
        max_episode_length=env.max_episode_length,
        normalise_observations=normalise_observations
    )
    action_space_dims = tuple(space.shape[-1] for space in base_env.action_space.spaces)

    # Test that the action dimensions aggregate over the network to provide a valid full action set.
    assert sum(action_space_dims) == len(base_env.action_vectors)

    if not for_tf_agent:
        return base_env, action_space_dims

    def env_constructor(rl_env, seed):
        """Function used to set up new environments for each process when multiprocessing."""
        rl_env.reset_with_random_state(seed)
        # Wrap the RLControlledRandomWalk ready for a Python implementation of a TFAgent
        # (using GymWrapper) and wrap for action repetitions if necessary.
        if tf_action_repetitions > 1:
            rl_env = ActionRepeat(GymWrapper(rl_env, discount=discount_factor),
                                  tf_action_repetitions)
        else:
            rl_env = GymWrapper(rl_env, discount=discount_factor)
        return rl_env

    # Set up the parallel environments from a list of constructors and wrap the result ready for
    # a TensorFlow agent.
    env_constructors = [lambda j=i: env_constructor(base_env,
                                                    (base_env.job_generator.seed or 0) + j)
                        for i in range(parallel_environments)]
    parallel_env = TFPyEnvironment(ParallelPyEnvironment(env_constructors))
    return parallel_env, action_space_dims

ext: py

max_stars_repo_name: dmcnamee/snc

max_issues_repo_name: dmcnamee/snc

max_forks_repo_name: dmcnamee/snc

reason: import numpy


text:
import numpy as np
from matplotlib import pyplot as plt
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Conv2D
from tensorflow.python.keras.layers import MaxPooling2D
from tensorflow.python.keras.layers import UpSampling2D
from tensorflow.python.keras.layers import Input
from tensorflow.python import keras as K

from dataset.dataset_handling import load_data_gray

EPOCHS_NUM = 50
LEARNING_RATE = 1e-4
BATCH_SIZE = 20

data = load_data_gray('../dataset/Train')
train_Y, train_X = data['sharp'], data['blur']

data_test = load_data_gray('../dataset/Test')
test_Y, test_X = data_test['sharp'], data_test['blur']

# scaling images to values [0 ... 1]
train_X = train_X.astype("float32") / 255.0
test_X = test_X.astype("float32") / 255.0

images_blur = np.reshape(train_X[1], (100, 100)) * 255.0
images_sharp = np.reshape(train_Y[1], (100, 100)) * 255.0

model = Sequential()
model.add(Input(shape=(100, 100, 1)))

model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))

model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(UpSampling2D((2, 2)))

model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))

model.add(Conv2D(1, (3, 3), activation='sigmoid', padding='same'))

model.compile(loss='mse', optimizer=K.optimizers.Adam(lr=LEARNING_RATE))
model.build()
model.summary()

early_stopping = K.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')
history = model.fit(train_X, train_Y,
                    batch_size=BATCH_SIZE,
                    epochs=EPOCHS_NUM,
                    validation_data=(test_X, test_Y),
                    callbacks=[early_stopping]
                    )

# history = model.fit(train_X, train_Y,
#                     batch_size=20,
#                     epochs=200,
#                     validation_data=(test_X, test_X),
#                     callbacks=[early_stopping]
#                     )

# plt.plot(history.history['loss'])
# plt.plot(history.history['val_loss'])
# plt.title('Model loss')
# plt.ylabel('Loss')
# plt.xlabel('Epoch')
# plt.legend(['Train', 'Test'], loc='upper left')
# plt.show()

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model_nn.h5")
print("Saved model to disk")

path_to_model = "./model_nn_predict.h5"
predictions = model.predict(test_X, batch_size=BATCH_SIZE)
model.save(path_to_model)

# predictions_0 = predictions[0] * 255.0
# predictions_0 = predictions_0.reshape(258, 540)
# plt.imshow(predictions_0, cmap='gray')


text:
import numpy as np
from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, MultiCursor

from ..algorithm.regiongrow import RegionGrow
from ..algorithm.tools import slide_win_smooth, VlineMover
from ..algorithm.meshtool import get_n_ring_neighbor, LabelAssessment

class SurfaceRGDialog(QtGui.QDialog):

    rg_types = ['srg', 'arg', 'crg']

    def __init__(self, model, surf_view, parent=None):
        super(SurfaceRGDialog, self).__init__(parent)
        self.setWindowTitle("surfRG")
        self._surf_view = surf_view
        self.model = model

        self.rg_type = 'crg'
        self.seeds_id = []
        self.stop_criteria = [500]
        self.n_ring = 1
        self._data_selection = ["current map", "current series"]
        self.group_idx = 'new'  # specify current seed group's index
        self.cut_line = []  # a list of vertices' id which plot a line
        self.r_idx_sm = None
        self.smoothness = None
        self.vertices_count = None
        self._is_cutting = False

        self._init_gui()
        self._create_actions()

        self._surf_view.seed_flag = True

    def _init_gui(self):
        # Initialize widgets
        rg_type_label = QtGui.QLabel('RG-type')
        self._rg_type_combo = QtGui.QComboBox()
        self._rg_type_combo.addItems(self.rg_types)
        self._rg_type_combo.setCurrentIndex(self.rg_types.index(self.rg_type))

        group_idx_label = QtGui.QLabel('seed group:')
        self._group_idx_combo = QtGui.QComboBox()
        self._group_idx_combo.addItem(self.group_idx)

        seeds_label = QtGui.QLabel("seeds:")
        self._seeds_edit = QtGui.QLineEdit()
        self._seeds_edit.setText('peak value vertex')

        self._stop_label = QtGui.QLabel("stop_criteria:")
        self._stop_edit = QtGui.QLineEdit()
        self._stop_edit.setText(str(self.stop_criteria[0]))
        self._stop_label.setVisible(False)
        self._stop_edit.setVisible(False)

        ring_label = QtGui.QLabel("n_ring:")
        self._ring_spin = QtGui.QSpinBox()
        self._ring_spin.setMinimum(1)
        self._ring_spin.setValue(self.n_ring)

        self._data_label = QtGui.QLabel("data:")
        self._data_combo = QtGui.QComboBox()
        self._data_combo.addItems(self._data_selection)
        self._data_label.setVisible(False)
        self._data_combo.setVisible(False)

        self._mask_label = QtGui.QLabel("mask:")
        self._mask_combo = QtGui.QComboBox()
        self._fill_mask_box()
        self._mask_label.setVisible(False)
        self._mask_combo.setVisible(False)

        self._threshold_label = QtGui.QLabel("threshold:")
        self._threshold_edit = QtGui.QLineEdit()
        self._init_thr_editor()

        self._cutoff_button1 = QtGui.QPushButton('start cutoff')
        self._cutoff_button2 = QtGui.QPushButton('stop cutoff')
        self._cutoff_button2.setEnabled(False)

        self._ok_button = QtGui.QPushButton("OK")
        self._cancel_button = QtGui.QPushButton("Cancel")

        # layout
        grid_layout = QtGui.QGridLayout()
        grid_layout.addWidget(rg_type_label, 0, 0)
        grid_layout.addWidget(self._rg_type_combo, 0, 1)
        grid_layout.addWidget(group_idx_label, 1, 0)
        grid_layout.addWidget(self._group_idx_combo, 1, 1)
        grid_layout.addWidget(seeds_label, 2, 0)
        grid_layout.addWidget(self._seeds_edit, 2, 1)
        grid_layout.addWidget(self._stop_label, 3, 0)
        grid_layout.addWidget(self._stop_edit, 3, 1)
        grid_layout.addWidget(ring_label, 4, 0)
        grid_layout.addWidget(self._ring_spin, 4, 1)
        grid_layout.addWidget(self._data_label, 5, 0)
        grid_layout.addWidget(self._data_combo, 5, 1)
        grid_layout.addWidget(self._mask_label, 6, 0)
        grid_layout.addWidget(self._mask_combo, 6, 1)
        grid_layout.addWidget(self._threshold_label, 7, 0)
        grid_layout.addWidget(self._threshold_edit, 7, 1)
        grid_layout.addWidget(self._cutoff_button1, 8, 0)
        grid_layout.addWidget(self._cutoff_button2, 8, 1)
        grid_layout.addWidget(self._ok_button, 9, 0)
        grid_layout.addWidget(self._cancel_button, 9, 1)
        self.setLayout(grid_layout)

    def _create_actions(self):
        # connect
        self._rg_type_combo.currentIndexChanged.connect(self._set_rg_type)
        self._group_idx_combo.currentIndexChanged.connect(self._set_group_idx)
        self.connect(self._seeds_edit, QtCore.SIGNAL("textEdited(QString)"), self._set_seeds_id)
        self.connect(self._stop_edit, QtCore.SIGNAL("textEdited(QString)"), self._set_stop_criteria)
        self.connect(self._ring_spin, QtCore.SIGNAL("valueChanged"), self._set_n_ring)
        self.connect(self._cutoff_button1, QtCore.SIGNAL("clicked()"), self._start_cutoff)
        self.connect(self._cutoff_button2, QtCore.SIGNAL("clicked()"), self._stop_cutoff)
        self.connect(self._ok_button, QtCore.SIGNAL("clicked()"), self._start_surfRG)
        self.connect(self._cancel_button, QtCore.SIGNAL("clicked()"), self.close)
        self._surf_view.seed_picked.connect(self._set_seeds_edit_text)
        self.model.rowsMoved.connect(self._fill_mask_box)
        self.model.rowsMoved.connect(self._init_thr_editor)
        self.model.rowsInserted.connect(self._fill_mask_box)
        self.model.rowsInserted.connect(self._init_thr_editor)
        self.model.rowsRemoved.connect(self._fill_mask_box)
        self.model.rowsRemoved.connect(self._init_thr_editor)
        self.model.dataChanged.connect(self._fill_mask_box)
        self.model.dataChanged.connect(self._init_thr_editor)
        self.connect(self.model, QtCore.SIGNAL("currentIndexChanged"), self._fill_mask_box)
        self.connect(self.model, QtCore.SIGNAL("currentIndexChanged"), self._init_thr_editor)

    def _start_cutoff(self):
        self._cutoff_button1.setEnabled(False)
        self._cutoff_button2.setEnabled(True)
        self._surf_view.seed_flag = False
        self._surf_view.scribing_flag = True
        self.cut_line = []
        self._is_cutting = True

    def _stop_cutoff(self):
        self._surf_view.seed_flag = True
        self._surf_view.scribing_flag = False
        self._cutoff_button1.setEnabled(True)
        self._cutoff_button2.setEnabled(False)
        self.cut_line = list(self._surf_view.path)
        self._surf_view.plot_start = None
        self._surf_view.path = []
        self._is_cutting = False

    def _set_group_idx(self):

        self.group_idx = str(self._group_idx_combo.currentText())
        if self.group_idx == 'new':
            self._seeds_edit.setText('')
        else:
            idx = int(self.group_idx)
            text = ','.join(map(str, self.seeds_id[idx]))
            self._seeds_edit.setText(text)

    def _set_rg_type(self):
        self.rg_type = str(self._rg_type_combo.currentText())
        if self.rg_type == 'crg':
            self._stop_label.setVisible(False)
            self._stop_edit.setVisible(False)
            self._data_label.setVisible(False)
            self._data_combo.setVisible(False)
            self._mask_label.setVisible(False)
            self._mask_combo.setVisible(False)
            self._threshold_label.setVisible(True)
            self._threshold_edit.setVisible(True)
            self._cutoff_button1.setVisible(True)
            self._cutoff_button2.setVisible(True)
            self._init_thr_editor()
        else:
            self._stop_label.setVisible(True)
            self._stop_edit.setVisible(True)
            self._data_label.setVisible(True)
            self._data_combo.setVisible(True)
            self._mask_label.setVisible(True)
            self._mask_combo.setVisible(True)
            self._threshold_label.setVisible(False)
            self._threshold_edit.setVisible(False)
            self._cutoff_button1.setVisible(False)
            self._cutoff_button2.setVisible(False)
            if self._is_cutting:
                self._stop_cutoff()
                self.cut_line = []

    def _set_seeds_edit_text(self):

        if self.group_idx == 'new':
            idx = len(self.seeds_id)
            self.seeds_id.append([self._surf_view.point_id])
            self.group_idx = str(idx)
            self._group_idx_combo.addItem(self.group_idx)
            self._group_idx_combo.setCurrentIndex(idx+1)
        else:
            idx = int(self.group_idx)
            self.seeds_id[idx].append(self._surf_view.point_id)
        text = ','.join(map(str, self.seeds_id[idx]))
        self._seeds_edit.setText(text)

    def _set_stop_criteria(self):

        text_list = self._stop_edit.text().split(',')
        while '' in text_list:
            text_list.remove('')
        if len(text_list) == len(self.seeds_id):
            self.stop_criteria = np.array(text_list, dtype="int")
        elif len(text_list) == 0:
            pass
        else:
            # If the number of stop_criteria is not equal to seeds,
            # then we use its first stop criteria for all seeds.
            self.stop_criteria = np.array([text_list[0]], dtype="int")

    def _set_seeds_id(self):

        text_list = self._seeds_edit.text().split(',')
        while '' in text_list:
            text_list.remove('')
        if self.group_idx == 'new':
            if text_list:
                idx = len(self.seeds_id)
                self.seeds_id.append(map(int, text_list))
                self.group_idx = str(idx)
                self._group_idx_combo.addItem(self.group_idx)
                self._group_idx_combo.setCurrentIndex(idx+1)
        else:
            idx = int(self.group_idx)
            if text_list:
                self.seeds_id[idx] = map(int, text_list)
            else:
                end_item_idx = len(self.seeds_id)
                self.seeds_id.pop(idx)
                self._group_idx_combo.removeItem(end_item_idx)
                self.group_idx = 'new'
                self._group_idx_combo.setCurrentIndex(0)
        self._set_stop_criteria()

    def _set_n_ring(self):
        self.n_ring = int(self._ring_spin.value())

    def _fill_mask_box(self):
        self._mask_combo.clear()
        overlay_name_list = self.model.get_overlay_list()
        self._mask_combo.addItem("None")
        self._mask_combo.addItems(overlay_name_list)

        index = self.model.current_index()
        depth = self.model.index_depth(index)
        if depth == 2:
            name = self.model.data(index, QtCore.Qt.DisplayRole)
            row = overlay_name_list.index(name) + 1
        else:
            row = 0

        self._mask_combo.setCurrentIndex(row)

    def _get_current_mask(self):
        row = self._mask_combo.currentIndex()
        if row == 0:
            return None
        else:
            row -= 1
            surface_idx = self.model.get_surface_index()
            mask_idx = self.model.index(row, 0, surface_idx)
            mask = self.model.data(mask_idx, QtCore.Qt.UserRole + 10)
            if self.model.data(mask_idx, QtCore.Qt.UserRole + 7):
                mask = mask != 0
            else:
                thresh = self.model.data(mask_idx, QtCore.Qt.UserRole)
                mask = mask > thresh
            return mask

    def _init_thr_editor(self):

        index = self.model.current_index()
        depth = self.model.index_depth(index)
        if depth == 2:
            thr = self.model.data(index, QtCore.Qt.UserRole)
            self._threshold_edit.setText(str(thr))
            self._threshold_edit.setEnabled(True)
        else:
            self._threshold_edit.setText("None")
            self._threshold_edit.setEnabled(False)

    def _start_surfRG(self):

        self.rg_qmodel_idx = self.model.current_index()
        depth = self.model.index_depth(self.rg_qmodel_idx)
        if depth == 1:
            geometry = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 6)
        elif depth == 2:
            geometry = self.model.data(self.rg_qmodel_idx.parent(), QtCore.Qt.UserRole + 6)
        else:
            QtGui.QMessageBox.warning(
                self, 'Error',
                'Get surface failed!\nYou may have not selected any surface!',
                QtGui.QMessageBox.Yes
            )
            return None
        self.vertices_count = geometry.vertices_count()

        rg = RegionGrow()
        if self.rg_type == 'arg':
            if depth != 2:
                QtGui.QMessageBox.warning(
                    self,
                    'Warning',
                    'Get overlay failed!\nYou may have not selected any overlay!',
                    QtGui.QMessageBox.Yes
                )
                return None
            if self._data_combo.currentText() == self._data_selection[0]:
                scalar_data = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 10)
                scalar_data = np.atleast_2d(scalar_data).T
            else:
                scalar_data = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 5)
            mask = self._get_current_mask()

            # ------------------select a assessment function-----------------
            assess_type, ok = QtGui.QInputDialog.getItem(
                    self,
                    'select a assessment function',
                    'assessments:',
                    rg.get_assess_types()
            )

            # ------------------If ok, start arg!-----------------
            if ok and assess_type != '':
                rg.set_assessment(assess_type)
                rg.surf2regions(geometry, scalar_data, mask, self.n_ring)
                rg_result, self.evolved_regions, self.region_assessments, self.assess_step, r_outer_mean, r_inner_min\
                    = rg.arg_parcel(self.seeds_id, self.stop_criteria, whole_results=True)

                # -----------------plot diagrams------------------
                num_axes = len(self.evolved_regions)
                fig, self.axes = plt.subplots(num_axes, 2)
                if num_axes == 1:
                    self.axes = np.array([self.axes])
                self.vline_movers = np.zeros_like(self.axes[:, 0])  # store vline movers
                self.cursors = np.zeros_like(self.axes)  # store cursors, hold references
                self.slider_axes = np.zeros_like(self.axes[:, 0])
                self.sm_sliders = []  # store smooth sliders, hold references
                for r_idx, r in enumerate(self.evolved_regions):
                    # plot region outer boundary assessment curve
                    self.axes[r_idx][1].plot(r_outer_mean[r_idx], 'b.-', label='r_outer_mean')
                    self.axes[r_idx][1].plot(r_inner_min[r_idx], 'r.-', label='r_inner_min')
                    self.axes[r_idx][1].legend()
                    self.axes[r_idx][1].set_ylabel('amplitude')
                    self.axes[r_idx][1].set_title('related values for seed {}'.format(r_idx))

                    # plot assessment curve
                    self.r_idx_sm = r_idx
                    self.smoothness = 0
                    self._sm_update_axes()

                    # add slider
                    ax_pos = self.axes[r_idx][0].get_position()
                    slider_ax = fig.add_axes([ax_pos.x1-0.15, ax_pos.y0+0.005, 0.15, 0.015])
                    sm_slider = Slider(slider_ax, 'smoothness', 0, 10, 0, '%d', dragging=False)
                    sm_slider.on_changed(self._on_smooth_changed)
                    self.slider_axes[r_idx] = slider_ax
                    self.sm_sliders.append(sm_slider)

                self.axes[-1][0].set_xlabel('contrast step/component')
                self.axes[-1][1].set_xlabel('contrast step/component')
                self.cursor = MultiCursor(fig.canvas, self.axes.ravel(),
                                          ls='dashed', lw=0.5, c='g', horizOn=True)
                fig.canvas.set_window_title('assessment curves')
                fig.canvas.mpl_connect('button_press_event', self._on_clicked)
                plt.show()
            else:
                QtGui.QMessageBox.warning(
                    self,
                    'Warning',
                    'You have to specify a assessment function for arg!',
                    QtGui.QMessageBox.Yes
                )
                return None

        elif self.rg_type == 'srg':
            if depth != 2:
                QtGui.QMessageBox.warning(
                    self,
                    'Warning',
                    'Get overlay failed!\nYou may have not selected any overlay!',
                    QtGui.QMessageBox.Yes
                )
                return None
            if self._data_combo.currentText() == self._data_selection[0]:
                scalar_data = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 10)
                scalar_data = np.atleast_2d(scalar_data).T
            else:
                scalar_data = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 5)
            mask = self._get_current_mask()

            rg.surf2regions(geometry, scalar_data, mask, self.n_ring)
            rg_result = rg.srg_parcel(self.seeds_id, self.stop_criteria)

        elif self.rg_type == 'crg':

            if depth == 1:
                mask = np.ones(np.max(geometry.faces) + 1)
                mask[self.cut_line] = 0
                edge_list = get_n_ring_neighbor(geometry.faces, n=self.n_ring, mask=mask)
                rg_result = rg.connectivity_grow(self.seeds_id, edge_list)

            elif depth == 2:
                scalar_data = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 10)
                neighbors = get_n_ring_neighbor(geometry.faces)

                self.thresholds = self._threshold_edit.text().split(',')
                while '' in self.thresholds:
                    self.thresholds.remove('')

                self.crg_results = list()
                for thr in self.thresholds:
                    mask = np.ones(np.max(geometry.faces) + 1)
                    if thr != "None":
                        mask[scalar_data <= float(thr)] = 0
                    mask[self.cut_line] = 0
                    edge_list = get_n_ring_neighbor(geometry.faces, n=self.n_ring, mask=mask)

                    self.crg_results.append(rg.connectivity_grow(self.seeds_id, edge_list))

                if len(self.thresholds) > 1:
                    n_region = len(self.seeds_id)
                    # get assessments, and rg_result
                    self.crg_results = np.array(self.crg_results)
                    label_assess = LabelAssessment()
                    self.region_assessments = list()
                    rg_result = list()
                    for r_idx in range(n_region):
                        multi_thr_regions = map(list, self.crg_results[:, r_idx])
                        assessment = list()
                        for region in multi_thr_regions:
                            assessment.append(label_assess.transition_level(region,
                                                                            scalar_data.reshape(scalar_data.shape[0],
                                                                                1),
                                                                            None, neighbors))
                        best_thr_idx = np.argmax(assessment)
                        rg_result.append(self.crg_results[best_thr_idx, r_idx])
                        self.region_assessments.append(assessment)

                    # plot
                    fig, self.axes = plt.subplots(n_region)
                    _ = np.zeros(1) if not isinstance(self.axes, np.ndarray) else np.zeros(self.axes.shape)
                    self.axes = np.c_[self.axes, _]
                    self.vline_movers = np.zeros_like(self.axes[:, 0])  # store vline movers
                    self.cursors = np.zeros_like(self.axes)  # store cursors, hold references
                    self.slider_axes = np.zeros_like(self.axes[:, 0])
                    self.sm_sliders = []  # store smooth sliders, hold references
                    for r_idx in range(n_region):
                        # plot assessment curve
                        self.r_idx_sm = r_idx
                        self.smoothness = 0
                        self._sm_update_axes()

                        # add slider
                        ax_pos = self.axes[r_idx][0].get_position()
                        slider_ax = fig.add_axes([ax_pos.x1 - 0.15, ax_pos.y0 + 0.005, 0.15, 0.015])
                        sm_slider = Slider(slider_ax, 'smoothness', 0, 10, 0, '%d', dragging=False)
                        sm_slider.on_changed(self._on_smooth_changed)
                        self.slider_axes[r_idx] = slider_ax
                        self.sm_sliders.append(sm_slider)

                    self.axes[-1][0].set_xlabel('thresholds')
                    self.cursor = MultiCursor(fig.canvas, self.axes[:, 0],
                                              ls='dashed', lw=0.5, c='g', horizOn=True)
                    fig.canvas.set_window_title('assessment curves')
                    fig.canvas.mpl_connect('button_press_event', self._on_clicked)
                    plt.show()
                else:
                    rg_result = self.crg_results[0]
            else:
                return

        else:
            raise RuntimeError("The region growing type must be arg, srg and crg at present!")

        self._show_result(rg_result)
        self.close()

    def _show_result(self, rg_result):
        """
        Add RG's result as tree items
        """
        data = np.zeros((self.vertices_count,), np.uint8)
        label = 1
        for r in rg_result:
            if self.rg_type == 'srg' or self.rg_type == 'arg':
                labeled_vertices = r.get_vertices()
            elif self.rg_type == 'crg':
                labeled_vertices = list(r)
            else:
                raise RuntimeError("The region growing type must be arg, srg and crg at present!")

            if np.any(data[labeled_vertices]):
                QtGui.QMessageBox.warning(self, 'Warning',
                                          "surfRG Group{0}'s result has overlap with other groups".format(label-1),
                                          QtGui.QMessageBox.Yes)
            data[labeled_vertices] = label
            label += 1

        name = 'rg_' + self.model.data(self.rg_qmodel_idx, QtCore.Qt.DisplayRole)
        if self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 11):
            map_idx = self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 9)
            name += '_' + str(map_idx)
        self.model.add_item(self.rg_qmodel_idx, data, islabel=True, colormap='blue', name=name)

    def _on_clicked(self, event):
        if event.button == 3 and event.inaxes in self.axes[:, 0]:
            # do something on right click
            r_idx = np.where(self.axes[:, 0] == event.inaxes)[0][0]
            index = int(self.vline_movers[r_idx].x[0])

            if self.rg_type == "arg":
                # find current evolved region
                r = self.evolved_regions[r_idx]

                # get vertices included in the evolved region
                end_index = int((index+1) * self.assess_step)
                labeled_vertices = set()
                for region in r.get_component()[:end_index]:
                    labeled_vertices.update(region.get_vertices())
                labeled_vertices = list(labeled_vertices)
            elif self.rg_type == "crg":
                labeled_vertices = list(self.crg_results[index][r_idx])
            else:
                return

            # visualize these labeled vertices
            data = np.zeros((self.vertices_count,), np.int)
            data[labeled_vertices] = 1
            name = 'rg_' + self.model.data(self.rg_qmodel_idx, QtCore.Qt.DisplayRole)
            if self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 11):
                name += str(self.model.data(self.rg_qmodel_idx, QtCore.Qt.UserRole + 9))
            name += '_{}'.format(r_idx)
            self.model.add_item(self.model.current_index(), data,
                                islabel=True, colormap='blue', name=name)
        elif event.button == 1 and event.inaxes in self.slider_axes:
            # do something on left click
            # find current evolved region
            self.r_idx_sm = np.where(self.slider_axes == event.inaxes)[0][0]
            if self.smoothness is not None:
                # indicate that self._on_click is performed later than self._on_smooth_changed
                # to ensure that self.r_idx_sm is got before smoothing
                self._sm_update_axes()

    def _on_smooth_changed(self, val):
        self.smoothness = int(val)
        if self.r_idx_sm is not None:
            # indicate that self._on_click is performed earlier than self._on_smooth_changed
            # to ensure that self.r_idx_sm is got before smoothing
            self._sm_update_axes()

    def _sm_update_axes(self):
        self.axes[self.r_idx_sm][0].cla()
        smoothed_curve = slide_win_smooth(self.region_assessments[self.r_idx_sm], self.smoothness)
        if self.rg_type == "crg":
            self.axes[self.r_idx_sm][0].plot(self.thresholds, smoothed_curve, "b.-")
        elif self.rg_type == "arg":
            self.axes[self.r_idx_sm][0].plot(smoothed_curve, "b.-")
        else:
            return
        self.axes[self.r_idx_sm][0].set_title('curve for seed {}'.format(self.r_idx_sm))
        self.axes[self.r_idx_sm][0].set_ylabel('assessed value')

        # initialize vline
        max_index = np.argmax(smoothed_curve)
        vline = self.axes[self.r_idx_sm][0].axvline(max_index)
        # instance VlineMover
        self.vline_movers[self.r_idx_sm] = VlineMover(vline, True)

        # reset
        self.r_idx_sm = None
        self.smoothness = None

    def close(self):

        self._surf_view.seed_picked.disconnect(self._set_seeds_edit_text)
        self.model.rowsMoved.disconnect(self._fill_mask_box)
        self.model.rowsMoved.disconnect(self._init_thr_editor)
        self.model.rowsInserted.disconnect(self._fill_mask_box)
        self.model.rowsInserted.disconnect(self._init_thr_editor)
        self.model.rowsRemoved.disconnect(self._fill_mask_box)
        self.model.rowsRemoved.disconnect(self._init_thr_editor)
        self.model.dataChanged.disconnect(self._fill_mask_box)
        self.model.dataChanged.disconnect(self._init_thr_editor)
        self.disconnect(self.model, QtCore.SIGNAL("currentIndexChanged"), self._fill_mask_box)
        self.disconnect(self.model, QtCore.SIGNAL("currentIndexChanged"), self._init_thr_editor)

        if self._is_cutting:
            self._stop_cutoff()
            self.cut_line = []

        self._surf_view.seed_flag = False
        QtGui.QDialog.close(self)

max_stars_repo_name: sunshineDrizzle/FreeROI

max_issues_repo_name: sunshineDrizzle/FreeROI

max_forks_repo_name: sunshineDrizzle/FreeROI


text:
# AUTOGENERATED! DO NOT EDIT! File to edit: 01_uhi.ipynb (unless otherwise specified).

__all__ = ['Uhi', 'OtherDf', 'Util']

# Cell
from .map import Map
from .map import UHIOptions
from .map import MapOptions
import pandas as pd
import numpy as np
import os
import gc
from datetime import datetime, timedelta

# Cell
class Uhi:
    """Represents an image(s) of urban heat island(s)

    Attributes:
      path_file: string
      station_label: string
      datetime_label: string
      temperature_label: string
      latitude_label: string
      longitude_label: string
    """
    def __init__(self,path_file,station_label,datetime_label,temperature_label,latitude_label,longitude_label):
      self.path_file = path_file
      self.station_label = station_label
      self.datetime_label = datetime_label
      self.temperature_label = temperature_label
      self.latitude_label = latitude_label
      self.longitude_label = longitude_label
      self.df=pd.read_csv(self.path_file,decimal=",",parse_dates=True,index_col=datetime_label,usecols=[station_label,datetime_label,temperature_label,latitude_label,longitude_label])
      self.dfToFloat()

    def dfToFloat(self):
      self.df=self.df.dropna(subset=[self.temperature_label])
      self.df[self.longitude_label] = self.df[self.longitude_label].astype(np.float)
      self.df[self.latitude_label] = self.df[self.latitude_label].astype(np.float)
      self.df[self.temperature_label] = self.df[self.temperature_label].astype(np.float)

    def plotImagesPerDay(self,dfDay,outputFolder,extent,hasBorders=None,hasLimits=False,transects=[],
                         Z_UNIT="°C",otherDf_id=None,otherDf_name=None,mapbox_username="",mapbox_token="",
                         prefixFile="",hasImage=True, hasScaleBar=True, hasCompassRose=True,hasLegend=True, hasColorbar=True,
                         transparency=.7, showPoints=False,contour="contourLine",drawMinMaxT=False):
      hours=dfDay.groupby(dfDay.index)
      dfListDay=[]
      for hour in hours.groups.keys():
        df_hour=hours.get_group(str(hour))
        if len(df_hour)>4:
          print(hour)
          dfListDay.append(df_hour)
          otherDf = None
          if otherDf_name is not None and otherDf_id is not None:
            otherDf_df = df_hour[df_hour[self.station_label]==otherDf_id]
            otherDf = OtherDf(otherDf_df,otherDf_id,otherDf_name)
            df_hour = df_hour[df_hour[self.station_label]!=otherDf_id]
          varMap = Map(df_hour,transects=transects,otherDf=otherDf,mapbox_username=mapbox_username,mapbox_token=mapbox_token)
          vMin=None
          vMax=None
          if hasLimits:
            vMin=dfDay[self.temperature_label].min()
            vMax=dfDay[self.temperature_label].max()

          uhiOptions = UHIOptions(hasBorders=hasBorders, vMin=vMin, vMax=vMax,
                                  transparency=transparency, showPoints=showPoints,contour=contour,drawMinMaxT=drawMinMaxT,
                                  LON_LABEL=self.longitude_label, LAT_LABEL=self.latitude_label,Z_LABEL=self.temperature_label, Z_UNIT=Z_UNIT)

          mapOptions = MapOptions(show=False, file=outputFolder+"/{} {}".format(prefixFile,hour), hasImage=hasImage,
                                  hasScaleBar=hasScaleBar, hasCompassRose=hasCompassRose,hasLegend=hasLegend, hasColorbar=hasColorbar)

          varMap.plot(extent,uhiOptions=uhiOptions,mapOptions=mapOptions)
        print("Limpiando...")
        df_hour=None
        varMap=None
        gc.collect()

      return dfListDay

    def plotImagesPerDaysArray(self,outputFolder,daysArray,extent,hasBorders=None,hasLimits=False,
                               Z_UNIT="°C",otherDf_id=None,otherDf_name=None,mapbox_username="",mapbox_token="",
                               prefixFile="",hasImage=True,hasScaleBar=True, hasCompassRose=True,hasLegend=True, hasColorbar=True,
                               transparency=.7, showPoints=False,contour="contourLine",drawMinMaxT=False):
      """

      Input:
      outputFolder: string of folder where the images of urban heat islands are saved.
      daysArray: array of strings of days to plot the urban heat islands.
      extent: array of 4 coordinates of the extent of the map.
      hasBorders: geojson to border the urban heat island in a map. (default:None)
      hasLimits: if has temperature's day range or temperature's hour range (default:False)
      Z_UNIT: unit of measure to be displayed in colobar. (default: '°C')
      otherDf_id: id of the temperature station that will not be included in the UHI's triangulation but will be displayed. (default: None)
      otherDf_name: name of the temperature station that will not be included in the UHI's triangulation but will be displayed. (default: None)
      mapbox_username: mapbox's username to plot background map. (default: '')
      mapbox_token: mapbox's token to plot background map. (default: '')
      prefixFile: string of the output prefix filename. (default:'')
      hasImage: boolean that decides if the image needs a Mapbox Map or not.
      hasScaleBar: boolean that decides if the image needs a Scaler Bar or not.
      hasCompassRose: boolean that decides if the image needs a Compass Rose or not.
      hasLegend: boolean that decides if the image neede a Legend or not.
      hasColorbar: boolean that decides if the image needs a color bar or not.
      transparency: trasparency of the UHI's triangulation.
      showPoints: boolean that decides if the image needs show station points.
      contour: show contourLine or only triangulation (default:'contourLine')
      drawMinMaxT: show station points with minimun and maximum temperature.
      Output: None
      """
      if not os.path.exists(outputFolder):
        try:
            os.makedirs(outputFolder)
        except OSError:
            print ("Creation of the directory %s failed" % outputFolder)
        else:
            print ("Successfully created the directory %s" % outputFolder)

      for day in daysArray:
        print("Plotting UHI Map of ",day)
        df1=self.df.loc[np.in1d(self.df.index.date, pd.to_datetime([day]).date)]
        print("...")
        self.plotImagesPerDay(df1,outputFolder,extent,hasBorders=hasBorders,hasLimits=hasLimits,transects=[],
                              Z_UNIT=Z_UNIT,otherDf_id=otherDf_id,otherDf_name=otherDf_name,mapbox_username=mapbox_username,mapbox_token=mapbox_token,
                              prefixFile=prefixFile,hasImage=hasImage,hasScaleBar=hasScaleBar, hasCompassRose=hasCompassRose,hasLegend=hasLegend, hasColorbar=hasColorbar,
                              transparency=transparency, showPoints=showPoints,contour=contour,drawMinMaxT=drawMinMaxT)
        print("Plot completed",day)
class OtherDf:
  def __init__(self,df,id,name):
    self.df = df
    self.id = id
    self.name = name

class Util:
  def getDaysFromRange(start,end):
    dates = [start, end]
    start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
    return [(start + timedelta(x)).strftime(r"%Y-%m-%d") for x in range((end - start).days)]


text:
"""
Skew the dataset so that it turns into generating a uniform distribution.
"""
import json
import time

import numpy as np
from PIL import Image
from skvideo.io import vwrite
from torch import nn as nn
from torch.optim import Adam

import railrl.pythonplusplus as ppp
import railrl.torch.vae.skew.skewed_vae as sv
from railrl.core import logger
from railrl.misc.html_report import HTMLReport
from railrl.visualization.visualization_util import gif
from railrl.torch.vae.skew.common import (
    Dynamics, plot_curves,
    visualize_samples,
    prob_to_weight,
)
import railrl.torch.pytorch_util as ptu
from railrl.torch.vae.skew.datasets import project_samples_square_np
from railrl.torch.vae.skew.histogram import Histogram
from railrl.torch.vae.skew.plotting import (
    visualize_vae_samples,
    visualize_vae,
    visualize_histogram,
    progressbar,
)

K = 6

"""
Plotting
"""

def train_from_variant(variant):
    train(full_variant=variant, **variant)

def train(
        dataset_generator,
        n_start_samples,
        projection=project_samples_square_np,
        n_samples_to_add_per_epoch=1000,
        n_epochs=100,
        z_dim=1,
        hidden_size=32,
        save_period=10,
        append_all_data=True,
        full_variant=None,
        dynamics_noise=0,
        decoder_output_var='learned',
        num_bins=5,
        skew_config=None,
        use_perfect_samples=False,
        use_perfect_density=False,
        vae_reset_period=0,
        vae_kwargs=None,
        use_dataset_generator_first_epoch=True,
        **kwargs
):

    """
    Sanitize Inputs
    """
    assert skew_config is not None
    if not (use_perfect_density and use_perfect_samples):
        assert vae_kwargs is not None
    if vae_kwargs is None:
        vae_kwargs = {}

    report = HTMLReport(
        logger.get_snapshot_dir() + '/report.html',
        images_per_row=10,
    )
    dynamics = Dynamics(projection, dynamics_noise)
    if full_variant:
        report.add_header("Variant")
        report.add_text(
            json.dumps(
                ppp.dict_to_safe_json(
                    full_variant,
                    sort=True),
                indent=2,
            )
        )

    vae, decoder, decoder_opt, encoder, encoder_opt = get_vae(
        decoder_output_var,
        hidden_size,
        z_dim,
        vae_kwargs,
    )
    vae.to(ptu.device)

    epochs = []
    losses = []
    kls = []
    log_probs = []
    hist_heatmap_imgs = []
    vae_heatmap_imgs = []
    sample_imgs = []
    entropies = []
    tvs_to_uniform = []
    entropy_gains_from_reweighting = []
    p_theta = Histogram(num_bins)
    p_new = Histogram(num_bins)

    orig_train_data = dataset_generator(n_start_samples)
    train_data = orig_train_data
    start = time.time()
    for epoch in progressbar(range(n_epochs)):
        p_theta = Histogram(num_bins)
        if epoch == 0 and use_dataset_generator_first_epoch:
            vae_samples = dataset_generator(n_samples_to_add_per_epoch)
        else:
            if use_perfect_samples and epoch != 0:
                # Ideally the VAE = p_new, but in practice, it won't be...
                vae_samples = p_new.sample(n_samples_to_add_per_epoch)
            else:
                vae_samples = vae.sample(n_samples_to_add_per_epoch)
        projected_samples = dynamics(vae_samples)
        if append_all_data:
            train_data = np.vstack((train_data, projected_samples))
        else:
            train_data = np.vstack((orig_train_data, projected_samples))

        p_theta.fit(train_data)
        if use_perfect_density:
            prob = p_theta.compute_density(train_data)
        else:
            prob = vae.compute_density(train_data)
        all_weights = prob_to_weight(prob, skew_config)
        p_new.fit(train_data, weights=all_weights)
        if epoch == 0 or (epoch + 1) % save_period == 0:
            epochs.append(epoch)
            report.add_text("Epoch {}".format(epoch))
            hist_heatmap_img = visualize_histogram(p_theta, skew_config, report)
            vae_heatmap_img = visualize_vae(
                vae, skew_config, report,
                resolution=num_bins,
            )
            sample_img = visualize_vae_samples(
                epoch, train_data, vae, report, dynamics,
            )

            visualize_samples(
                p_theta.sample(n_samples_to_add_per_epoch),
                report,
                title="P Theta/RB Samples",
            )
            visualize_samples(
                p_new.sample(n_samples_to_add_per_epoch),
                report,
                title="P Adjusted Samples",
            )
            hist_heatmap_imgs.append(hist_heatmap_img)
            vae_heatmap_imgs.append(vae_heatmap_img)
            sample_imgs.append(sample_img)
            report.save()

            Image.fromarray(hist_heatmap_img).save(
                logger.get_snapshot_dir() + '/hist_heatmap{}.png'.format(epoch)
            )
            Image.fromarray(vae_heatmap_img).save(
                logger.get_snapshot_dir() + '/hist_heatmap{}.png'.format(epoch)
            )
            Image.fromarray(sample_img).save(
                logger.get_snapshot_dir() + '/samples{}.png'.format(epoch)
            )

        """
        train VAE to look like p_new
        """
        if sum(all_weights) == 0:
            all_weights[:] = 1
        if vae_reset_period > 0 and epoch % vae_reset_period == 0:
            vae, decoder, decoder_opt, encoder, encoder_opt = get_vae(
                decoder_output_var,
                hidden_size,
                z_dim,
                vae_kwargs,
            )
            vae.to(ptu.device)
        vae.fit(train_data, weights=all_weights)
        epoch_stats = vae.get_epoch_stats()

        losses.append(np.mean(epoch_stats['losses']))
        kls.append(np.mean(epoch_stats['kls']))
        log_probs.append(np.mean(epoch_stats['log_probs']))
        entropies.append(p_theta.entropy())
        tvs_to_uniform.append(p_theta.tv_to_uniform())
        entropy_gain = p_new.entropy() - p_theta.entropy()
        entropy_gains_from_reweighting.append(entropy_gain)

        for k in sorted(epoch_stats.keys()):
            logger.record_tabular(k, epoch_stats[k])

        logger.record_tabular("Epoch", epoch)
        logger.record_tabular('Entropy ', p_theta.entropy())
        logger.record_tabular('KL from uniform', p_theta.kl_from_uniform())
        logger.record_tabular('TV to uniform', p_theta.tv_to_uniform())
        logger.record_tabular('Entropy gain from reweight', entropy_gain)
        logger.record_tabular('Total Time (s)', time.time() - start)
        logger.dump_tabular()
        logger.save_itr_params(epoch, {
            'vae': vae,
            'train_data': train_data,
            'vae_samples': vae_samples,
            'dynamics': dynamics,
        })

    report.add_header("Training Curves")
    plot_curves(
        [
            ("Training Loss", losses),
            ("KL", kls),
            ("Log Probs", log_probs),
            ("Entropy Gain from Reweighting", entropy_gains_from_reweighting),
        ],
        report,
    )
    plot_curves(
        [
            ("Entropy", entropies),
            ("TV to Uniform", tvs_to_uniform),
        ],
        report,
    )
    report.add_text("Max entropy: {}".format(p_theta.max_entropy()))
    report.save()

    for filename, imgs in [
        ("hist_heatmaps", hist_heatmap_imgs),
        ("vae_heatmaps", vae_heatmap_imgs),
        ("samples", sample_imgs),
    ]:
        video = np.stack(imgs)
        vwrite(
            logger.get_snapshot_dir() + '/{}.mp4'.format(filename),
            video,
        )
        local_gif_file_path = '{}.gif'.format(filename)
        gif_file_path = '{}/{}'.format(
            logger.get_snapshot_dir(),
            local_gif_file_path
        )
        gif(gif_file_path, video)
        report.add_image(local_gif_file_path, txt=filename, is_url=True)
    report.save()

def get_vae(decoder_output_var, hidden_size, z_dim, vae_kwargs):
    encoder = sv.Encoder(
        nn.Linear(2, hidden_size),
        nn.ReLU(),
        nn.Linear(hidden_size, hidden_size),
        nn.ReLU(),
        nn.Linear(hidden_size, z_dim * 2),
    )
    if decoder_output_var == 'learned':
        last_layer = nn.Linear(hidden_size, 4)
    else:
        last_layer = nn.Linear(hidden_size, 2)
    decoder = sv.Decoder(
        nn.Linear(z_dim, hidden_size),
        nn.ReLU(),
        nn.Linear(hidden_size, hidden_size),
        nn.ReLU(),
        last_layer,
        output_var=decoder_output_var,
        output_offset=-1,
    )
    encoder_opt = Adam(encoder.parameters())
    decoder_opt = Adam(decoder.parameters())
    vae = sv.VAE(encoder=encoder, decoder=decoder, z_dim=z_dim, **vae_kwargs)
    return vae, decoder, decoder_opt, encoder, encoder_opt


text:
from __future__ import print_function
import torch.utils.data as data
import os
import sys
import numpy as np
import h5py

class MLMLoader(data.Dataset):
    def __init__(self, data_path, partition, mismatch=0.5):

        if data_path == None:
            raise Exception('No data path specified.')

        if partition is None or partition not in ['train', 'val', 'test']:
            raise Exception('Unknown partition type %s.' % partition)
        else:
            self.partition = partition

        self.h5f = h5py.File(os.path.join(data_path, f'{partition}.h5'), 'r')

        self.ids = self.h5f['ids']
        self.mismatch = mismatch

    def __getitem__(self, index):
        instanceId = self.ids[index]
        # we force 50 percent of them to be a mismatch
        match = np.random.uniform() > self.mismatch if self.partition == 'train' else True

        target = match and 1 or -1

        if target == 1:  # load positive samples

            all_img = self.h5f[f'{instanceId}_images'][()]
            if self.partition == 'train':
                img = all_img[np.random.choice(range(all_img.shape[0]))]
            else:
                img = all_img[0]

            summaries = self.h5f[f'{instanceId}_summaries'][()]
            multi_wiki = summaries[np.random.choice(range(np.shape(summaries)[0]))]
            triple = self.h5f[f'{instanceId}_classes'][()]
            triple = triple[np.random.choice(range(triple.shape[0]))]
            cluster = self.h5f[f'{instanceId}_onehot'][()]

        else:
            # Negative samples are generated by picking random images
            # Other modalities remain unchanged
            all_idx = range(len(self.ids))
            rndindex = np.random.choice(all_idx)
            # random index to pick image ids at random
            while rndindex == index:
                rndindex = np.random.choice(all_idx)  # pick a random index

            # load negative samples of images
            rndId = self.ids[rndindex]
            all_img = self.h5f[f'{rndId}_images']

            # negative samples are in train only
            if self.partition == 'train':
                img = all_img[np.random.choice(range(all_img.shape[0]))]
            else:
                img = all_img[0]

            # other modalities remain unchanged
            summaries = self.h5f[f'{rndId}_summaries'][()]
            multi_wiki = summaries[np.random.choice(range(np.shape(summaries)[0]))]
            triple = self.h5f[f'{instanceId}_classes'][()]
            triple = triple[np.random.choice(range(triple.shape[0]))]
            cluster = self.h5f[f'{instanceId}_onehot'][()]

        # output
        output = {
            'image': img,
            'multi_wiki': multi_wiki,
            'target': target,
            'cluster': cluster,
            'triple': triple
        }

        if self.partition != 'train':
            output['id'] = instanceId

        return output

    def __len__(self):
        return len(self.ids)

max_stars_repo_name: GOALCLEOPATRA/MLM

max_issues_repo_name: GOALCLEOPATRA/MLM

max_forks_repo_name: GOALCLEOPATRA/MLM


text:
#Dependencies
import spotipy as sp
from spotipy.oauth2 import SpotifyClientCredentials
import pandas as pd
import numpy as np

#Importing Spotify client_ID and secret_code
from keys import client_id, client_secret

scope = "user-library-read"

sp = sp.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))

def my_search(table):
    #running spotipy search function
    song_search = table['song_title']
    # print(song_search)
    results = sp.search(q='track:' + table['song_title'], type = 'track')
    tracks = results ['tracks']['items']

    if len(tracks) == 0:
        print(f'Song details not found for {song_search}')

    else:
        #extracting spotify ID for the track **Important to make further API calls**
        table['track_spotify_ID'] = tracks[0]['id']
        table['artist_spotify_ID'] = tracks[0]['album']['artists'][0]['id']

        #top_100_scrapped_df: song_duration,release_date

        try:
            table['song_duration[ms]'] = tracks[0]['duration_ms']
        except:
            table['song_duration[ms]'] = np.nan

        try:
            table['song_release_date'] = tracks[0]['album']['release_date']
        except:
            table['song_release_date'] = np.nan

        try:
            table['spotify_popularity'] = tracks[0]['popularity']
        except:
            table['spotify_popularity'] = np.nan

        # album_table: album_type
        try:
            table['album_name'] = tracks[0]['album']['name']
        except:
            table['album_name'] = np.nan

        # album_table: album_type
        try:
            table['album_type'] = tracks[0]['album']['album_type']
        except:
            table['album_type'] = np.nan

    return table

max_stars_repo_path: Data sources/search_function.py

max_issues_repo_path: Data sources/search_function.py

max_forks_repo_path: Data sources/search_function.py


text:
"""
Functions specifically for working with QC/DQRs from
the Atmospheric Radiation Measurement Program (ARM).

"""

import requests
import datetime as dt
import numpy as np

def add_dqr_to_qc(obj, variable=None, assessment='incorrect,suspect',
                  exclude=None, include=None, normalize_assessment=True,
                  add_qc_variable=None):
    """
    Function to query the ARM DQR web service for reports and
    add as a qc test.  See online documentation from ARM Data
    Quality Office on the use of the DQR web service

    https://code.arm.gov/docs/dqrws-examples/wikis/home

    Parameters
    ----------
    obj : xarray Dataset
        Data object
    variable : string or list
        Variables to check DQR web service for
    assessment : string
        assessment type to get DQRs for
    exclude : list of strings
        DQRs to exclude from adding into QC
    include : list of strings
        List of DQRs to use in flagging of data
    normalize_assessment : boolean
        The DQR assessment term is different than the embedded QC
        term. Embedded QC uses "Bad" and "Indeterminate" while
        DQRs use "Incorrect" and "Suspect". Setting this will ensure
        the same terms are used for both.
    add_qc_variable : string or list
        Variables to add QC information to

    Returns
    -------
    obj : xarray Dataset
        Data object

    """

    # DQR Webservice goes off datastreams, pull from object
    if 'datastream' in obj.attrs:
        datastream = obj.attrs['datastream']
    elif '_datastream' in obj.attrs:
        datastream = obj.attrs['_datastream']
    else:
        raise ValueError('Object does not have datastream attribute')

    # Clean up QC to conform to CF conventions
    obj.clean.cleanup()

    # In order to properly flag data, get all variables if None. Exclude QC variables.
    if variable is None:
        variable = list(set(obj.data_vars) - set(obj.clean.matched_qc_variables))

    # Check to ensure variable is list
    if not isinstance(variable, (list, tuple)):
        variable = [variable]

    # If add_qc_variable is none, set to variables list
    if add_qc_variable is None:
        add_qc_variable = variable

    if not isinstance(add_qc_variable, (list, tuple)):
        add_qc_variable = [add_qc_variable]

    # Loop through each variable and call web service for that variable
    for var_name in variable:
        # Create URL
        url = 'http://www.archive.arm.gov/dqrws/ARMDQR?datastream='
        url += datastream
        url += '&varname=' + var_name
        url += ''.join(['&searchmetric=', assessment,
                        '&dqrfields=dqrid,starttime,endtime,metric,subject'])

        # Call web service
        req = requests.get(url)

        # Check status values and raise error if not successful
        status = req.status_code
        if status == 400:
            raise ValueError('Check parameters')
        if status == 500:
            raise ValueError('DQR Webservice Temporarily Down')

        # Get data and run through each dqr
        dqrs = req.text.splitlines()
        time = obj['time'].values
        dqr_results = {}
        for line in dqrs:
            line = line.split('|')
            dqr_no = line[0]

            # Exclude DQRs if in list
            if exclude is not None and dqr_no in exclude:
                continue

            # Only include if in include list
            if include is not None and dqr_no not in include:
                continue

            starttime = np.datetime64(dt.datetime.utcfromtimestamp(int(line[1])))
            endtime = np.datetime64(dt.datetime.utcfromtimestamp(int(line[2])))
            ind = np.where((time >= starttime) & (time <= endtime))
            if ind[0].size == 0:
                continue

            if dqr_no in dqr_results.keys():
                dqr_results[dqr_no]['index'] = np.append(dqr_results[dqr_no]['index'], ind)
            else:
                dqr_results[dqr_no] = {'index': ind, 'test_assessment': line[3],
                                       'test_meaning': ': '.join([dqr_no, line[-1]])}

        for key, value in dqr_results.items():
            obj.qcfilter.add_test(var_name, index=value['index'],
                                  test_meaning=value['test_meaning'],
                                  test_assessment=value['test_assessment'])

        if normalize_assessment:
            obj.clean.normalize_assessment(variables=var_name)

    return obj

max_stars_repo_name: zssherman/ACT

max_issues_repo_name: zssherman/ACT

max_forks_repo_name: zssherman/ACT