:param sources: a list of sources
        :param weight: a weight function (default .weight)
        :param key: None or 'src_group_id'
        :returns: an iterator over blocks of sources
428763
def block_splitter(self, sources, weight=get_weight, key=lambda src: 1):
        
        ct = self.oqparam.concurrent_tasks or 1
        maxweight = self.csm.get_maxweight(weight, ct, source.MINWEIGHT)
        if not hasattr(self, 'logged'):
            if maxweight == source.MINWEIGHT:
                logging.info('Using minweight=%d', source.MINWEIGHT)
            else:
                logging.info('Using maxweight=%d', maxweight)
            self.logged = True
        return general.block_splitter(sources, maxweight, weight, key)
86064
@Override
    public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException {
        this.weights = new HashMap<>();


        INDArray W;
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_W()))
            W = weights.get(conf.getKERAS_PARAM_NAME_W());
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SimpleRNN layer does not contain parameter " + conf.getKERAS_PARAM_NAME_W());
        this.weights.put(SimpleRnnParamInitializer.WEIGHT_KEY, W);


        INDArray RW;
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_RW()))
            RW = weights.get(conf.getKERAS_PARAM_NAME_RW());
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SimpleRNN layer does not contain parameter " + conf.getKERAS_PARAM_NAME_RW());
        this.weights.put(SimpleRnnParamInitializer.RECURRENT_WEIGHT_KEY, RW);


        INDArray b;
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_B()))
            b = weights.get(conf.getKERAS_PARAM_NAME_B());
        else
            throw new InvalidKerasConfigurationException(
                    "Keras SimpleRNN layer does not contain parameter " + conf.getKERAS_PARAM_NAME_B());
        this.weights.put(SimpleRnnParamInitializer.BIAS_KEY, b);


        if (weights.size() > NUM_TRAINABLE_PARAMS) {
            Set<String> paramNames = weights.keySet();
            paramNames.remove(conf.getKERAS_PARAM_NAME_B());
            paramNames.remove(conf.getKERAS_PARAM_NAME_W());
            paramNames.remove(conf.getKERAS_PARAM_NAME_RW());
            String unknownParamNames = paramNames.toString();
            log.warn("Attemping to set weights for unknown parameters: "
                    + unknownParamNames.substring(1, unknownParamNames.length() - 1));
        }
    }
937105
def prepare_weighted_spans(targets,  # type: List[TargetExplanation]
                           preserve_density=None,  # type: Optional[bool]
                           ):
    # type: (...) -> List[Optional[List[PreparedWeightedSpans]]]
    
    targets_char_weights = [
        [get_char_weights(ws, preserve_density=preserve_density)
         for ws in t.weighted_spans.docs_weighted_spans]
         if t.weighted_spans else None
         for t in targets]  # type: List[Optional[List[np.ndarray]]]
    max_idx = max_or_0(len(ch_w or []) for ch_w in targets_char_weights)

    targets_char_weights_not_None = [
        cw for cw in targets_char_weights
        if cw is not None]  # type: List[List[np.ndarray]]

    spans_weight_ranges = [
        max_or_0(
            abs(x) for char_weights in targets_char_weights_not_None
            for x in char_weights[idx])
        for idx in range(max_idx)]
    return [
        [PreparedWeightedSpans(ws, char_weights, weight_range)
         for ws, char_weights, weight_range in zip(
            t.weighted_spans.docs_weighted_spans,  # type: ignore
            t_char_weights,
            spans_weight_ranges)]
        if t_char_weights is not None else None
        for t, t_char_weights in zip(targets, targets_char_weights)]
286996
@Override
    public void setWeights(Map<String, INDArray> weights) throws InvalidKerasConfigurationException {
        this.weights = new HashMap<>();
        if (weights.containsKey(conf.getKERAS_PARAM_NAME_W()))
            this.weights.put(DefaultParamInitializer.WEIGHT_KEY, weights.get(conf.getKERAS_PARAM_NAME_W()));
        else
            throw new InvalidKerasConfigurationException(
                    "Parameter " + conf.getKERAS_PARAM_NAME_W() + " does not exist in weights");
        if (hasBias) {
            if (weights.containsKey(conf.getKERAS_PARAM_NAME_B()))
                this.weights.put(DefaultParamInitializer.BIAS_KEY, weights.get(conf.getKERAS_PARAM_NAME_B()));
            else
                throw new InvalidKerasConfigurationException(
                        "Parameter " + conf.getKERAS_PARAM_NAME_B() + " does not exist in weights");
        }
        removeDefaultWeights(weights, conf);
    }
746878
def lookup(source, keys, fallback = None):
  """"""
  try:
    for key in keys:
      source = source[key]
    return source
  except (KeyError, AttributeError, TypeError):
    return fallback
516061
def addSource(self, sourceUri, weight):
        
        assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
        self.topicPage["sources"].append({"uri": sourceUri, "wgt": weight})
618356
def add_sources(self, source_info_dict):
        
        self._source_info_dict.update(source_info_dict)
        for key, value in source_info_dict.items():
            self._sources.update(make_sources(key, value))
1139511
public static void importWeights(Hdf5Archive weightsArchive, String weightsRoot, Map<String, KerasLayer> layers,
                                     int kerasVersion, String backend)
            throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
        // check to ensure naming scheme doesn't include forward slash
        boolean includesSlash = false;
        for (String layerName : layers.keySet()) {
            if (layerName.contains("/"))
                includesSlash = true;
        }
        synchronized (KerasModelUtils.class) {
            List<String> layerGroups;
            if (!includesSlash) {
                layerGroups = weightsRoot != null ? weightsArchive.getGroups(weightsRoot) : weightsArchive.getGroups();
            } else {
                layerGroups = new ArrayList<>(layers.keySet());
            }
            /* Set weights in KerasLayer for each entry in weights map. */
            for (String layerName : layerGroups) {
                List<String> layerParamNames;

                // there's a bug where if a layer name contains a forward slash, the first fragment must be appended
                // to the name of the dataset; it appears h5 interprets the forward slash as a data group
                String[] layerFragments = layerName.split("/");

                // Find nested groups when using Tensorflow
                String rootPrefix = weightsRoot != null ? weightsRoot + "/" : "";
                List<String> attributeStrParts = new ArrayList<>();
                String attributeStr = weightsArchive.readAttributeAsString(
                        "weight_names", rootPrefix + layerName
                );
                String attributeJoinStr;
                Matcher attributeMatcher = Pattern.compile(":\\d+").matcher(attributeStr);
                Boolean foundTfGroups = attributeMatcher.find();

                if (foundTfGroups) {
                    for (String part : attributeStr.split("/")) {
                        part = part.trim();
                        if (part.length() == 0)
                            break;
                        Matcher tfSuffixMatcher = Pattern.compile(":\\d+").matcher(part);
                        if (tfSuffixMatcher.find())
                            break;
                        attributeStrParts.add(part);
                    }
                    attributeJoinStr = StringUtils.join(attributeStrParts, "/");
                } else {
                    attributeJoinStr = layerFragments[0];
                }

                String baseAttributes = layerName + "/" + attributeJoinStr;
                if (layerFragments.length > 1) {
                    try {
                        layerParamNames = weightsArchive.getDataSets(rootPrefix + baseAttributes);
                    } catch (Exception e) {
                        layerParamNames = weightsArchive.getDataSets(rootPrefix + layerName);
                    }
                } else {
                    if (foundTfGroups) {
                        layerParamNames = weightsArchive.getDataSets(rootPrefix + baseAttributes);
                    } else {
                        if (kerasVersion == 2) {
                            if (backend.equals("theano") && layerName.contains("bidirectional")) {
                                for (String part : attributeStr.split("/")) {
                                    if (part.contains("forward"))
                                        baseAttributes = baseAttributes + "/" + part;
                                }

                            }
                            if (layers.get(layerName).getNumParams() > 0) {
                                try {
                                    layerParamNames = weightsArchive.getDataSets(rootPrefix + baseAttributes);
                                } catch (Exception e) {
                                    log.warn("No HDF5 group with weights found for layer with name "
                                            + layerName + ", continuing import.");
                                    layerParamNames = Collections.emptyList();
                                }
                            } else {
                                layerParamNames = weightsArchive.getDataSets(rootPrefix + layerName);
                            }

                        } else {
                            layerParamNames = weightsArchive.getDataSets(rootPrefix + layerName);
                        }

                    }
                }
                if (layerParamNames.isEmpty())
                    continue;
                if (!layers.containsKey(layerName))
                    throw new InvalidKerasConfigurationException(
                            "Found weights for layer not in model (named " + layerName + ")");
                KerasLayer layer = layers.get(layerName);


                if (layerParamNames.size() != layer.getNumParams())
                    if (kerasVersion == 2
                            && layer instanceof KerasBidirectional && 2 * layerParamNames.size() != layer.getNumParams())
                        throw new InvalidKerasConfigurationException(
                                "Found " + layerParamNames.size() + " weights for layer with " + layer.getNumParams()
                                        + " trainable params (named " + layerName + ")");
                Map<String, INDArray> weights = new HashMap<>();


                for (String layerParamName : layerParamNames) {
                    String paramName = KerasModelUtils.findParameterName(layerParamName, layerFragments);
                    INDArray paramValue;

                    if (kerasVersion == 2 && layer instanceof KerasBidirectional) {
                        String backwardAttributes = baseAttributes.replace("forward", "backward");
                        INDArray forwardParamValue = weightsArchive.readDataSet(layerParamName,
                                rootPrefix + baseAttributes);
                        INDArray backwardParamValue = weightsArchive.readDataSet(
                                layerParamName, rootPrefix + backwardAttributes);
                        weights.put("forward_" + paramName, forwardParamValue);
                        weights.put("backward_" + paramName, backwardParamValue);
                    } else {
                        if (foundTfGroups) {
                            paramValue = weightsArchive.readDataSet(layerParamName, rootPrefix + baseAttributes);
                        } else {
                            if (layerFragments.length > 1) {
                                paramValue = weightsArchive.readDataSet(
                                        layerFragments[0] + "/" + layerParamName, rootPrefix, layerName);
                            } else {
                                if (kerasVersion == 2) {
                                    paramValue = weightsArchive.readDataSet(
                                            layerParamName, rootPrefix + baseAttributes);
                                } else {
                                    paramValue = weightsArchive.readDataSet(layerParamName, rootPrefix, layerName);
                                }
                            }
                        }
                        weights.put(paramName, paramValue);
                    }
                }
                layer.setWeights(weights);
            }

            /* Look for layers in model with no corresponding entries in weights map. */
            Set<String> layerNames = new HashSet<>(layers.keySet());
            layerNames.removeAll(layerGroups);
            for (String layerName : layerNames) {
                if (layers.get(layerName).getNumParams() > 0)
                    throw new InvalidKerasConfigurationException("Could not find weights required for layer " + layerName);
            }
        }
    }
1022779
def weight(self, context):
        
        lrow, urow = MS.row_extents(context)

        weight = self._manager.ordered_main_table.getcol(
            MS.WEIGHT, startrow=lrow, nrow=urow-lrow)

        # WEIGHT is applied across all channels
        weight = np.repeat(weight, self._manager.channels_per_band, 0)
        return weight.reshape(context.shape).astype(context.dtype)
981516
def reweight(self, weight, edges=None, copy=False):
    ''''''
    if not self.is_weighted():
      warnings.warn('Cannot supply weights for unweighted graph; '
                    'ignoring call to reweight')
      return self
    if edges is None:
      return self._update_edges(weight, copy=copy)
    ii, jj = self.pairs()[edges].T
    return self.add_edges(ii, jj, weight=weight, symmetric=False, copy=copy)
1191758
def on_each_source(self):
        
        return (self.info.applytosources and
                self.info.applytosources == self.source_ids)
778671
def grp_by_src(self):
        
        smodels = []
        grp_id = 0
        for sm in self.source_models:
            src_groups = []
            smodel = sm.__class__(sm.names, sm.weight, sm.path, src_groups,
                                  sm.num_gsim_paths, sm.ordinal, sm.samples)
            for sg in sm.src_groups:
                for src in sg.sources:
                    src.src_group_id = grp_id
                    src_groups.append(
                        sourceconverter.SourceGroup(
                            sg.trt, [src], name=src.source_id, id=grp_id))
                    grp_id += 1
            smodels.append(smodel)
        return self.__class__(self.gsim_lt, self.source_model_lt, smodels,
                              self.optimize_same_id)
1223258
def newer_pairwise_group(sources_groups, targets):
    
    if len(sources_groups) != len(targets):
        raise ValueError("'sources_group' and 'targets' must be the same length")

    # build a pair of lists (sources_groups, targets) where source is newer
    n_sources = []
    n_targets = []
    for i in range(len(sources_groups)):
        if newer_group(sources_groups[i], targets[i]):
            n_sources.append(sources_groups[i])
            n_targets.append(targets[i])

    return n_sources, n_targets
800062
def check_weights(nodes_with_a_weight):
    
    weights = [n['weight'] for n in nodes_with_a_weight]
    if abs(sum(weights) - 1.) > PRECISION:
        raise ValueError('The weights do not sum up to 1: %s' % weights)
    return nodes_with_a_weight
1085721
def _iter(self):
        
        get_weight = (self.transformer_weights or {}).get
        return ((name, trans, get_weight(name))
                for name, trans in self.transformer_list
                if trans is not None)
628435
def pfmerge(self, dest, *sources):
        
        sources = [self.redis_key(k) for k in sources]
        with self.pipe as pipe:
            return pipe.pfmerge(self.redis_key(dest), *sources)
1205223
def _get_node_key(self, node_dict_item):
        """"""
        s = tuple(sorted(node_dict_item['sources']))
        t = tuple(sorted(node_dict_item['targets']))
        return (s, t)