Convert prior_predictive samples to xarray.
422359
def prior_predictive_to_xarray(self):
        """"""
        prior = self.prior
        prior_predictive = self.prior_predictive
        data = get_draws(prior, variables=prior_predictive)
        return dict_to_dataset(data, library=self.pystan, coords=self.coords, dims=self.dims)
934635
def transform(self, X):
        """"""
        # Note: This maps to mean of distribution, we could alternatively
        # sample from Gaussian distribution
        return self.sess.run(self.z_mean, feed_dict={self.x: X})
1267366
def _sample_orthonormal_to(mu):
    """"""
    v = np.random.randn(mu.shape[0])
    proj_mu_v = mu * np.dot(mu, v) / np.linalg.norm(mu)
    orthto = v - proj_mu_v
    return orthto / np.linalg.norm(orthto)
1282811
def invcdf(x):
    """"""
    x_flat = np.ravel(x)
    x_trans = np.array([flib.ppnd16(y, 1) for y in x_flat])
    return np.reshape(x_trans, np.shape(x))
493750
def sample_stats_to_xarray(self):
        """"""
        if self.model_fn is None or self.observed is None:
            return None

        log_likelihood = []
        sample_size = self.posterior[0].shape[0]

        for i in range(sample_size):
            variables = {}
            for var_i, var_name in enumerate(self.var_names):
                variables[var_name] = self.posterior[var_i][i]

            with self.ed.interception(self._value_setter(variables)):
                log_likelihood.append((self.model_fn().distribution.log_prob(self.observed)))

        data = {}
        if self.dims is not None:
            coord_name = self.dims.get("obs")
        else:
            coord_name = None
        dims = {"log_likelihood": coord_name}

        with self.tf.Session() as sess:
            data["log_likelihood"] = np.expand_dims(
                sess.run(log_likelihood, feed_dict=self.feed_dict), axis=0
            )
        return dict_to_dataset(data, library=self.tfp, coords=self.coords, dims=dims)
258718
def _predict_with_options(self, dataset, with_ground_truth,
                              postprocess=True, confidence_threshold=0.001,
                              iou_threshold=None,
                              verbose=True):
        
        if iou_threshold is None: iou_threshold = self.non_maximum_suppression_threshold
        _raise_error_if_not_detection_sframe(dataset, self.feature, self.annotations,
                                             require_annotations=with_ground_truth)
        from ._sframe_loader import SFrameDetectionIter as _SFrameDetectionIter
        from ._detection import (yolo_map_to_bounding_boxes as _yolo_map_to_bounding_boxes,
                                 non_maximum_suppression as _non_maximum_suppression,
                                 bbox_to_ybox as _bbox_to_ybox)

        from .._mxnet import _mxnet_utils
        import mxnet as _mx
        loader = _SFrameDetectionIter(dataset,
                                      batch_size=self.batch_size,
                                      input_shape=self.input_image_shape[1:],
                                      output_shape=self._grid_shape,
                                      anchors=self.anchors,
                                      class_to_index=self._class_to_index,
                                      loader_type='stretched',
                                      load_labels=with_ground_truth,
                                      shuffle=False,
                                      epochs=1,
                                      feature_column=self.feature,
                                      annotations_column=self.annotations)

        num_anchors = len(self.anchors)
        preds_per_box = 5 + len(self.classes)
        output_size = preds_per_box * num_anchors

        # If prediction is done with ground truth, two sframes of the same
        # structure are returned, the second one containing ground truth labels
        num_returns = 2 if with_ground_truth else 1

        sf_builders = [
            _tc.SFrameBuilder([int, str, float, float, float, float, float],
                              column_names=['row_id', 'label', 'confidence',
                                            'x', 'y', 'width', 'height'])
            for _ in range(num_returns)
        ]

        num_mxnet_gpus = _mxnet_utils.get_num_gpus_in_use(max_devices=self.batch_size)
        use_mps = _use_mps() and num_mxnet_gpus == 0
        if use_mps:
            if not hasattr(self, '_mps_inference_net') or self._mps_inference_net is None:
                mxnet_params = self._model.collect_params()
                mps_net_params = { k : mxnet_params[k].data().asnumpy()
                                   for k in mxnet_params }
                mps_config = {
                    'mode': _MpsGraphMode.Inference,
                    'od_include_network': True,
                    'od_include_loss': False,
                }
                mps_net = _get_mps_od_net(input_image_shape=self.input_image_shape,
                                          batch_size=self.batch_size,
                                          output_size=output_size,
                                          anchors=self.anchors,
                                          config=mps_config,
                                          weights=mps_net_params)
                self._mps_inference_net = mps_net

        dataset_size = len(dataset)
        ctx = _mxnet_utils.get_mxnet_context()
        done = False
        last_time = 0
        raw_results = []
        for batch in loader:
            if batch.pad is not None:
                size = self.batch_size - batch.pad
                b_data = _mx.nd.slice_axis(batch.data[0], axis=0, begin=0, end=size)
                b_indices = _mx.nd.slice_axis(batch.label[1], axis=0, begin=0, end=size)
                b_oshapes = _mx.nd.slice_axis(batch.label[2], axis=0, begin=0, end=size)
            else:
                b_data = batch.data[0]
                b_indices = batch.label[1]
                b_oshapes = batch.label[2]
                size = self.batch_size

            if b_data.shape[0] < len(ctx):
                ctx0 = ctx[:b_data.shape[0]]
            else:
                ctx0 = ctx

            split_data = _mx.gluon.utils.split_and_load(b_data, ctx_list=ctx0, even_split=False)
            split_indices = _mx.gluon.utils.split_data(b_indices, num_slice=len(ctx0), even_split=False)
            split_oshapes = _mx.gluon.utils.split_data(b_oshapes, num_slice=len(ctx0), even_split=False)

            for data, indices, oshapes in zip(split_data, split_indices, split_oshapes):
                if use_mps:
                    mps_data = _mxnet_to_mps(data.asnumpy())
                    n_samples = mps_data.shape[0]
                    if mps_data.shape[0] != self.batch_size:
                        mps_data_padded = _np.zeros((self.batch_size,) + mps_data.shape[1:],
                                                    dtype=mps_data.dtype)
                        mps_data_padded[:mps_data.shape[0]] = mps_data
                        mps_data = mps_data_padded
                    mps_float_array = self._mps_inference_net.predict(mps_data)
                    mps_z = mps_float_array.asnumpy()[:n_samples]
                    z = _mps_to_mxnet(mps_z)
                else:
                    z = self._model(data).asnumpy()
                if not postprocess:
                    raw_results.append(z)
                    continue

                ypred = z.transpose(0, 2, 3, 1)
                ypred = ypred.reshape(ypred.shape[:-1] + (num_anchors, -1))

                zipped = zip(indices.asnumpy(), ypred, oshapes.asnumpy())
                for index0, output0, oshape0 in zipped:
                    index0 = int(index0)
                    x_boxes, x_classes, x_scores = _yolo_map_to_bounding_boxes(
                            output0[_np.newaxis], anchors=self.anchors,
                            confidence_threshold=confidence_threshold,
                            nms_thresh=None)

                    x_boxes0 = _np.array(x_boxes).reshape(-1, 4)

                    # Normalize
                    x_boxes0[:, 0::2] /= self.input_image_shape[1]
                    x_boxes0[:, 1::2] /= self.input_image_shape[2]

                    # Re-shape to original input size
                    x_boxes0[:, 0::2] *= oshape0[0]
                    x_boxes0[:, 1::2] *= oshape0[1]

                    # Clip the boxes to the original sizes
                    x_boxes0[:, 0::2] = _np.clip(x_boxes0[:, 0::2], 0, oshape0[0])
                    x_boxes0[:, 1::2] = _np.clip(x_boxes0[:, 1::2], 0, oshape0[1])

                    # Non-maximum suppression (also limit to 100 detection per
                    # image, inspired by the evaluation in COCO)
                    x_boxes0, x_classes, x_scores = _non_maximum_suppression(
                            x_boxes0, x_classes, x_scores,
                            num_classes=self.num_classes, threshold=iou_threshold,
                            limit=100)

                    for bbox, cls, s in zip(x_boxes0, x_classes, x_scores):
                        cls = int(cls)
                        values = [index0, self.classes[cls], s] + list(_bbox_to_ybox(bbox))
                        sf_builders[0].append(values)

                    if index0 == len(dataset) - 1:
                        done = True

                    cur_time = _time.time()
                    # Do not print process if only a few samples are predicted
                    if verbose and (dataset_size >= 5 and cur_time > last_time + 10 or done):
                        print('Predicting {cur_n:{width}d}/{max_n:{width}d}'.format(
                            cur_n=index0 + 1, max_n=dataset_size, width=len(str(dataset_size))))
                        last_time = cur_time

                    if done:
                        break

            # Ground truth
            if with_ground_truth:
                zipped = _itertools.islice(zip(batch.label[1].asnumpy(), batch.raw_bboxes, batch.raw_classes), size)
                for index0, bbox0, cls0 in zipped:
                    index0 = int(index0)
                    for bbox, cls in zip(bbox0, cls0):
                        cls = int(cls)
                        if cls == -1:
                            break
                        values = [index0, self.classes[cls], 1.0] + list(bbox)
                        sf_builders[1].append(values)

                    if index0 == len(dataset) - 1:
                        break

        if postprocess:
            ret = tuple([sb.close() for sb in sf_builders])
            if len(ret) == 1:
                return ret[0]
            else:
                return ret
        else:
            return _np.concatenate(raw_results, axis=0)
487251
def predict_files(self, files):
        '''
        
        '''
        imgs = [0]*len(files)
        for i, file in enumerate(files):
            img = cv2.imread(file).astype('float64')
            img = cv2.resize(img, (224,224))
            img = preprocess_input(img)
            if img is None:
                print('failed to open: {}, continuing...'.format(file))
            imgs[i] = img
        return self.model.predict(np.array(imgs))
1302728
def posterior_to_xarray(self):
        """"""
        columns = self.posterior[0].columns

        # filter posterior_predictive and log_likelihood
        posterior_predictive = self.posterior_predictive
        if posterior_predictive is None or (
            isinstance(posterior_predictive, str) and posterior_predictive.lower().endswith(".csv")
        ):
            posterior_predictive = []
        elif isinstance(posterior_predictive, str):
            posterior_predictive = [
                col for col in columns if posterior_predictive == col.split(".")[0]
            ]
        else:
            posterior_predictive = [
                col
                for col in columns
                if any(item == col.split(".")[0] for item in posterior_predictive)
            ]

        log_likelihood = self.log_likelihood
        if log_likelihood is None:
            log_likelihood = []
        else:
            log_likelihood = [col for col in columns if log_likelihood == col.split(".")[0]]

        invalid_cols = posterior_predictive + log_likelihood
        valid_cols = [col for col in columns if col not in invalid_cols]
        data = _unpack_dataframes([item[valid_cols] for item in self.posterior])
        return dict_to_dataset(data, coords=self.coords, dims=self.dims)
286636
def observed_data_to_xarray(self):
        """"""
        if self.observed is None:
            return None

        observed_data = {}
        if isinstance(self.observed, self.tf.Tensor):
            with self.tf.Session() as sess:
                vals = sess.run(self.observed, feed_dict=self.feed_dict)
        else:
            vals = self.observed

        if self.dims is None:
            dims = {}
        else:
            dims = self.dims

        name = "obs"
        val_dims = dims.get(name)
        vals = np.atleast_1d(vals)
        val_dims, coords = generate_dims_coords(vals.shape, name, dims=val_dims, coords=self.coords)
        # coords = {key: xr.IndexVariable((key,), data=coords[key]) for key in val_dims}

        observed_data[name] = xr.DataArray(vals, dims=val_dims, coords=coords)
        return xr.Dataset(data_vars=observed_data, attrs=make_attrs(library=self.tfp))
1315101
def sample_stats_prior_to_xarray(self):
        """"""
        prior = self.prior
        data = get_sample_stats(prior)
        return dict_to_dataset(data, library=self.pystan, coords=self.coords, dims=self.dims)
143438
def _build_model(self, x):
    """"""
    with tf.variable_scope('init'):
      x = self._conv('init_conv', x, 3, x.shape[3], 16,
                     self._stride_arr(1))

    strides = [1, 2, 2]
    activate_before_residual = [True, False, False]
    if self.hps.use_bottleneck:
      res_func = self._bottleneck_residual
      filters = [16, 64, 128, 256]
    else:
      res_func = self._residual
      filters = [16, 16, 32, 64]
      # Uncomment the following codes to use w28-10 wide residual
      # network.
      # It is more memory efficient than very deep residual network and
      # has
      # comparably good performance.
      # https://arxiv.org/pdf/1605.07146v1.pdf
      # filters = [16, 160, 320, 640]
      # Update hps.num_residual_units to 4

    with tf.variable_scope('unit_1_0'):
      x = res_func(x, filters[0], filters[1],
                   self._stride_arr(strides[0]),
                   activate_before_residual[0])
    for i in six.moves.range(1, self.hps.num_residual_units):
      with tf.variable_scope('unit_1_%d' % i):
        x = res_func(x, filters[1], filters[1],
                     self._stride_arr(1), False)

    with tf.variable_scope('unit_2_0'):
      x = res_func(x, filters[1], filters[2],
                   self._stride_arr(strides[1]),
                   activate_before_residual[1])
    for i in six.moves.range(1, self.hps.num_residual_units):
      with tf.variable_scope('unit_2_%d' % i):
        x = res_func(x, filters[2], filters[2],
                     self._stride_arr(1), False)

    with tf.variable_scope('unit_3_0'):
      x = res_func(x, filters[2], filters[3],
                   self._stride_arr(strides[2]),
                   activate_before_residual[2])
    for i in six.moves.range(1, self.hps.num_residual_units):
      with tf.variable_scope('unit_3_%d' % i):
        x = res_func(x, filters[3], filters[3],
                     self._stride_arr(1), False)

    with tf.variable_scope('unit_last'):
      x = self._layer_norm('final_bn', x)
      x = self._relu(x, self.hps.relu_leakiness)
      x = self._global_avg_pool(x)

    with tf.variable_scope('logit'):
      logits = self._fully_connected(x, self.hps.nb_classes)
      predictions = tf.nn.softmax(logits)

    return logits, predictions
135447
def preprocess_xarray(func):
    
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        args = tuple(a.metpy.unit_array if isinstance(a, xr.DataArray) else a for a in args)
        kwargs = {name: (v.metpy.unit_array if isinstance(v, xr.DataArray) else v)
                  for name, v in kwargs.items()}
        return func(*args, **kwargs)
    return wrapper
411276
def image_rmse(predictions, labels, weights_fn=common_layers.weights_all):
  """"""
  if common_layers.shape_list(predictions)[-1] == 1:
    predictions = tf.squeeze(predictions, axis=[-1])
  else:
    predictions = tf.argmax(predictions, axis=-1)
  return padded_rmse(predictions, labels, weights_fn)
230773
def NdarrayToEntry(self, x):
    """"""
    row_counts = []
    for row in x:
      try:
        rc = np.count_nonzero(~np.isnan(row))
        if rc != 0:
          row_counts.append(rc)
      except TypeError:
        try:
          row_counts.append(row.size)
        except AttributeError:
          row_counts.append(1)

    data_type = self.DtypeToType(x.dtype)
    converter = self.DtypeToNumberConverter(x.dtype)
    flattened = x.ravel()
    orig_size = len(flattened)

    # Remove all None and nan values and count how many were removed.
    flattened = flattened[flattened != np.array(None)]
    if converter:
      flattened = converter(flattened)
    if data_type == self.fs_proto.STRING:
      flattened_temp = []
      for x in flattened:
        try:
          if str(x) != 'nan':
            flattened_temp.append(x)
        except UnicodeEncodeError:
          if x.encode('utf-8') != 'nan':
            flattened_temp.append(x)
      flattened = flattened_temp
    else:
      flattened = flattened[~np.isnan(flattened)].tolist()
    missing = orig_size - len(flattened)
    return {
        'vals': flattened,
        'counts': row_counts,
        'missing': missing,
        'type': data_type
    }
28777
def predict(self, X):
        

        if X.ndim==1: X = X[None,:]
        ps = self.model.param_array.copy()
        means = []
        stds = []
        for s in self.hmc_samples:
            if self.model._fixes_ is None:
                self.model[:] = s
            else:
                self.model[self.model._fixes_] = s
            self.model._trigger_params_changed()
            m, v = self.model.predict(X)
            means.append(m)
            stds.append(np.sqrt(np.clip(v, 1e-10, np.inf)))
        self.model.param_array[:] = ps
        self.model._trigger_params_changed()
        return means, stds
1002615
def posterior_to_xarray(self):
        """"""
        # Do not make pyro a requirement
        from pyro.infer import EmpiricalMarginal

        try:  # Try pyro>=0.3 release syntax
            data = {
                name: np.expand_dims(samples.enumerate_support().squeeze(), 0)
                if self.posterior.num_chains == 1
                else samples.enumerate_support().squeeze()
                for name, samples in self.posterior.marginal(
                    sites=self.latent_vars
                ).empirical.items()
            }
        except AttributeError:  # Use pyro<0.3 release syntax
            data = {}
            for var_name in self.latent_vars:
                # pylint: disable=no-member
                samples = EmpiricalMarginal(
                    self.posterior, sites=var_name
                ).get_samples_and_weights()[0]
                data[var_name] = np.expand_dims(samples.numpy().squeeze(), 0)
        return dict_to_dataset(data, library=self.pyro, coords=self.coords, dims=self.dims)
936300
def fill_with_sample_data(self):
        """"""
        # replace these sample data with another dataset later
        # this function is deprecated as soon as a common file format for this
        # type of data will be available
        self.p01 = np.array([[0.576724636119866,  0.238722774405744,  0.166532122130638,  0.393474644666218],
                             [0.303345245644811,  0.0490956843857575, 0.0392403031072856, 0.228441890034704]])

        self.p10 = np.array([[0.158217002255554,  0.256581140990052,  0.557852226779526,  0.422638238585814],
                             [0.0439831163244427, 0.0474928027621488, 0.303675296728195,  0.217512052135178]])

        self.pxx = np.array([[0.265058361624580,  0.504696084604205,  0.275615651089836,  0.183887116747968],
                             [0.652671638030746,  0.903411512852094,  0.657084400164519,  0.554046057830118]])

        self.wxx = np.array([[[0.188389148850583, 0.0806836453984190, 0.0698113025807722, 0.0621499191745602],
                              [0.240993281622128, 0.0831019646519721, 0.0415130545715575, 0.155284541403192]],
                             [[0.190128959522795, 0.129220679033862,  0.0932213021787505, 0.193080698516532],
                              [0.196379692358065, 0.108549414860949,  0.0592714297292217, 0.0421945385836429]],
                             [[0.163043672107111, 0.152063537378127,  0.102823783410167,  0.0906028835221283],
                              [0.186579466868095, 0.189705690316132,  0.0990207345993082, 0.107831389238912]],
                             [[0.197765724699431, 0.220046257566978,  0.177876233348082,  0.261288786454262],
                              [0.123823472714948, 0.220514673922285,  0.102486496386323,  0.101975538893918]],
                             [[0.114435243444815, 0.170857634762767,  0.177327072603662,  0.135362730582518],
                              [0.0939211776723413,0.174291820501902,  0.125275822078525,  0.150842841725936]],
                             [[0.0988683809545079, 0.152323481100248, 0.185606883566286, 0.167242856061538],
                              [0.0760275616817939, 0.127275603247149,  0.202466168603738,  0.186580243138018]],
                             [[0.0473688704207573, 0.0948047647595988, 0.193333422312280,  0.0902721256884624],
                              [0.0822753470826286, 0.0965608324996108, 0.369966294031327,  0.255290907016382]]])