Пример #1
0
    def visualize(self):
        """
        Turn the state of the Ziptie into an image.
        """
        print(' '.join(['ziptie', str(self.level)]))
        # First list the bundles andthe cables in each.
        i_bundles = self.bundle_map_rows[:self.n_map_entries]
        i_cables = self.bundle_map_cols[:self.n_map_entries]
        i_bundles_unique = np.unique(i_bundles)
        if i_bundles_unique is not None:
            for i_bundle in i_bundles_unique:
                b_cables = list(
                    np.sort(i_cables[np.where(i_bundles == i_bundle)[0]]))
                print(' '.join(
                    ['    bundle',
                     str(i_bundle), 'cables:',
                     str(b_cables)]))

        plot = False
        if plot:
            if self.n_map_entries > 0:
                # Render the bundle map.
                bundle_map = np.zeros(
                    (self.max_num_cables, self.max_num_bundles))
                nb.set_dense_val(bundle_map,
                                 self.bundle_map_rows[:self.n_map_entries],
                                 self.bundle_map_cols[:self.n_map_entries], 1.)
                tools.visualize_array(bundle_map,
                                      label=self.name + '_bundle_map')

                # Render the agglomeration energy.
                label = '_'.join([self.name, 'agg_energy'])
                tools.visualize_array(self.agglomeration_energy, label=label)
                plt.xlabel(str(np.max(self.agglomeration_energy)))

                # Render the nucleation energy.
                label = '_'.join([self.name, 'nuc_energy'])
                tools.visualize_array(self.nucleation_energy, label=label)
                plt.xlabel(str(np.max(self.nucleation_energy)))
Пример #2
0
    def visualize(self):
        """
        Turn the state of the ``ZipTie`` into an image.
        """
        print(' '.join(['ziptie', str(self.level)]))
        # First list the bundles andthe cables in each.
        i_bundles = self.bundle_map_rows[:self.n_map_entries]
        i_cables = self.bundle_map_cols[:self.n_map_entries]
        i_bundles_unique = np.unique(i_bundles)
        if i_bundles_unique is not None:
            for i_bundle in i_bundles_unique:
                b_cables = list(np.sort(i_cables[np.where(
                    i_bundles == i_bundle)[0]]))
                print(' '.join(['    bundle', str(i_bundle),
                                'cables:', str(b_cables)]))

        plot = False
        if plot:
            if self.n_map_entries > 0:
                # Render the bundle map.
                bundle_map = np.zeros((self.max_num_cables,
                                       self.max_num_bundles))
                nb.set_dense_val(bundle_map,
                                 self.bundle_map_rows[:self.n_map_entries],
                                 self.bundle_map_cols[:self.n_map_entries], 1.)
                tools.visualize_array(bundle_map,
                                      label=self.name + '_bundle_map')

                # Render the agglomeration energy.
                label = '_'.join([self.name, 'agg_energy'])
                tools.visualize_array(self.agglomeration_energy, label=label)
                plt.xlabel(str(np.max(self.agglomeration_energy)))

                # Render the nucleation energy.
                label = '_'.join([self.name, 'nuc_energy'])
                tools.visualize_array(self.nucleation_energy, label=label)
                plt.xlabel(str(np.max(self.nucleation_energy)))
Пример #3
0
    def _grow_bundles(self, masked_cable_activities):
        """
        Update an estimate of co-activity between all cables.
        """
        # Incrementally accumulate agglomeration energy.
        #nb.agglomeration_energy_gather(self.bundle_activities,
        #                               self.nonbundle_activities,
        #                               self.num_bundles,
        #                               self.agglomeration_energy)
        #nb.agglomeration_energy_gather(self.bundle_activities,
        #                               self.cable_activities,
        #                               self.num_bundles,
        #                               self.agglomeration_energy)
        nb.agglomeration_energy_gather(self.bundle_activities,
                                       masked_cable_activities,
                                       self.num_bundles,
                                       self.agglomeration_energy)

        # Don't accumulate agglomeration energy between cables already
        # in the same bundle
        val = 0.
        if self.n_map_entries > 0:
            nb.set_dense_val(self.agglomeration_energy,
                             self.bundle_map_rows[:self.n_map_entries],
                             self.bundle_map_cols[:self.n_map_entries], val)

        results = -np.ones(3)
        nb.max_dense(self.agglomeration_energy, results)
        max_energy = results[0]
        cable_index = int(results[2])
        bundle_index = int(results[1])

        # Add a new bundle if appropriate
        if max_energy > self.agglomeration_threshold:
            # Find which cables are in the new bundle.
            cables = [cable_index]
            for i in range(self.n_map_entries):
                if self.bundle_map_rows[i] == bundle_index:
                    cables.append(self.bundle_map_cols[i])
            # Check whether the agglomeration is already in the bundle map.
            candidate_bundles = np.arange(self.num_bundles)
            for cable in cables:
                matches = np.where(self.bundle_map_cols == cable)[0]
                candidate_bundles = np.intersect1d(
                    candidate_bundles,
                    self.bundle_map_rows[matches],
                    assume_unique=True)
            if candidate_bundles.size != 0:
                # The agglomeration has already been used to create a
                # bundle. Ignore and reset they count. This can happen
                # under normal circumstances, because of how nonbundle
                # activities are calculated.
                self.agglomeration_energy[bundle_index, cable_index] = 0.
                return

            # Make a copy of the growing bundle.
            for i in range(self.n_map_entries):
                if self.bundle_map_rows[i] == bundle_index:
                    self.bundle_map_rows[self.n_map_entries] = self.num_bundles
                    self.bundle_map_cols[self.n_map_entries] = (
                        self.bundle_map_cols[i])
                    self.increment_n_map_entries()
            # Add in the new cable.
            self.bundle_map_rows[self.n_map_entries] = self.num_bundles
            self.bundle_map_cols[self.n_map_entries] = cable_index
            self.increment_n_map_entries()
            self.num_bundles += 1

            if self.debug:
                print(' '.join([
                    '    ', self.name, 'bundle',
                    str(self.num_bundles), 'added: bundle',
                    str(bundle_index), 'and cable',
                    str(cable_index)
                ]))

            # Check whether the Ziptie's capacity has been reached.
            if self.num_bundles == self.max_num_bundles:
                self.bundles_full = True

            # Reset the accumulated nucleation and agglomeration energy
            # for the two cables involved.
            self.nucleation_energy[cable_index, :] = 0.
            self.nucleation_energy[cable_index, :] = 0.
            self.nucleation_energy[:, cable_index] = 0.
            self.nucleation_energy[:, cable_index] = 0.
            self.agglomeration_energy[:, cable_index] = 0.
            self.agglomeration_energy[bundle_index, :] = 0.
Пример #4
0
    def _grow_bundles(self):
        """
        Update an estimate of co-activity between all cables.
        """
        # Incrementally accumulate agglomeration energy.
        nb.agglomeration_energy_gather(self.bundle_activities,
                                       self.nonbundle_activities,
                                       self.num_bundles,
                                       self.agglomeration_energy)

        # Don't accumulate agglomeration energy between cables already
        # in the same bundle
        val = 0.
        if self.n_map_entries > 0:
            nb.set_dense_val(self.agglomeration_energy,
                             self.bundle_map_rows[:self.n_map_entries],
                             self.bundle_map_cols[:self.n_map_entries],
                             val)

        results = -np.ones(3)
        nb.max_dense(self.agglomeration_energy, results)
        max_energy = results[0]
        cable_index = int(results[2])
        bundle_index = int(results[1])

        # Add a new bundle if appropriate
        if max_energy > self.agglomeration_threshold:
            # Find which cables are in the new bundle.
            cables = [cable_index]
            for i in range(self.n_map_entries):
                if self.bundle_map_rows[i] == bundle_index:
                    cables.append(self.bundle_map_cols[i])
            # Check whether the agglomeration is already in the bundle map.
            candidate_bundles = np.arange(self.num_bundles)
            for cable in cables:
                matches = np.where(self.bundle_map_cols == cable)[0]
                candidate_bundles = np.intersect1d(
                    candidate_bundles,
                    self.bundle_map_rows[matches],
                    assume_unique=True)
            if candidate_bundles.size != 0:
                # The agglomeration has already been used to create a
                # bundle. Ignore and reset they count. This can happen
                # under normal circumstances, because of how nonbundle
                # activities are calculated.
                self.agglomeration_energy[bundle_index, cable_index] = 0.
                return

            # Make a copy of the growing bundle.
            for i in range(self.n_map_entries):
                if self.bundle_map_rows[i] == bundle_index:
                    self.bundle_map_rows[self.n_map_entries] = self.num_bundles
                    self.bundle_map_cols[self.n_map_entries] = (
                        self.bundle_map_cols[i])
                    self.increment_n_map_entries()
            # Add in the new cable.
            self.bundle_map_rows[self.n_map_entries] = self.num_bundles
            self.bundle_map_cols[self.n_map_entries] = cable_index
            self.increment_n_map_entries()
            self.num_bundles += 1

            print(' '.join(['    ', self.name,
                            'bundle', str(self.num_bundles),
                            'added: bundle', str(bundle_index),
                            'and cable', str(cable_index)]))

            # Check whether the ``ZipTie``'s capacity has been reached.
            if self.num_bundles == self.max_num_bundles:
                self.bundles_full = True

            # Reset the accumulated nucleation and agglomeration energy
            # for the two cables involved.
            self.nucleation_energy[cable_index, :] = 0.
            self.nucleation_energy[cable_index, :] = 0.
            self.nucleation_energy[:, cable_index] = 0.
            self.nucleation_energy[:, cable_index] = 0.
            self.agglomeration_energy[:, cable_index] = 0.
            self.agglomeration_energy[bundle_index, :] = 0.