Python numpy.ndenumerate() Examples

The following are 30 code examples for showing how to use numpy.ndenumerate(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: kvae   Author: simonkamronn   File: plotting.py    License: MIT License 6 votes vote down vote up
def hinton(matrix, max_weight=None, ax=None):
    """Draw Hinton diagram for visualizing a weight matrix."""
    ax = ax if ax is not None else plt.gca()

    if not max_weight:
        max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))

    ax.patch.set_facecolor('gray')
    ax.set_aspect('equal', 'box')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    for (x, y), w in np.ndenumerate(matrix):
        color = 'white' if w > 0 else 'black'
        size = np.sqrt(np.abs(w) / max_weight)
        rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
                             facecolor=color, edgecolor=color)
        ax.add_patch(rect)

    ax.autoscale_view()
    ax.invert_yaxis() 
Example 2
Project: scikit-downscale   Author: jhamman   File: core.py    License: Apache License 2.0 6 votes vote down vote up
def xenumerate(arr):
    """
    Multidimensional index iterator for xarray objects

    Return an iterator yielding pairs of array indexers (dicts) and values.

    Parameters
    ----------
    arr : xarray.DataArray
        Input array.

    See Also
    --------
    numpy.ndenumerate
    """

    for index, _ in np.ndenumerate(arr):
        xindex = dict(zip(arr.dims, index))
        yield xindex, arr.isel(**xindex) 
Example 3
Project: reinforcement-learning-an-introduction   Author: ShangtongZhang   File: grid_world.py    License: MIT License 6 votes vote down vote up
def draw_image(image):
    fig, ax = plt.subplots()
    ax.set_axis_off()
    tb = Table(ax, bbox=[0, 0, 1, 1])

    nrows, ncols = image.shape
    width, height = 1.0 / ncols, 1.0 / nrows

    # Add cells
    for (i, j), val in np.ndenumerate(image):
        tb.add_cell(i, j, width, height, text=val,
                    loc='center', facecolor='white')

        # Row and column labels...
    for i in range(len(image)):
        tb.add_cell(i, -1, width, height, text=i+1, loc='right',
                    edgecolor='none', facecolor='none')
        tb.add_cell(-1, i, width, height/2, text=i+1, loc='center',
                    edgecolor='none', facecolor='none')
    ax.add_table(tb) 
Example 4
Project: reinforcement-learning-an-introduction   Author: ShangtongZhang   File: grid_world.py    License: MIT License 6 votes vote down vote up
def draw_image(image):
    fig, ax = plt.subplots()
    ax.set_axis_off()
    tb = Table(ax, bbox=[0, 0, 1, 1])

    nrows, ncols = image.shape
    width, height = 1.0 / ncols, 1.0 / nrows

    # Add cells
    for (i, j), val in np.ndenumerate(image):
        tb.add_cell(i, j, width, height, text=val,
                    loc='center', facecolor='white')

    # Row and column labels...
    for i in range(len(image)):
        tb.add_cell(i, -1, width, height, text=i+1, loc='right',
                    edgecolor='none', facecolor='none')
        tb.add_cell(-1, i, width, height/2, text=i+1, loc='center',
                    edgecolor='none', facecolor='none')

    ax.add_table(tb) 
Example 5
Project: nevergrad   Author: facebookresearch   File: callbacks.py    License: MIT License 6 votes vote down vote up
def load_flattened(self, max_list_elements: int = 24) -> tp.List[tp.Dict[str, tp.Any]]:
        """Loads data from the log file, and splits lists (arrays) into multiple arguments

        Parameters
        ----------
        max_list_elements: int
            Maximum number of elements displayed from the array, each element is given a
            unique id of type list_name#i0_i1_...
        """
        data = self.load()
        flat_data: tp.List[tp.Dict[str, tp.Any]] = []
        for element in data:
            list_keys = {key for key, val in element.items() if isinstance(val, list)}
            flat_data.append({key: val for key, val in element.items() if key not in list_keys})
            for key in list_keys:
                for k, (indices, value) in enumerate(np.ndenumerate(element[key])):
                    if k >= max_list_elements:
                        break
                    flat_data[-1][key + "#" + "_".join(str(i) for i in indices)] = value
        return flat_data 
Example 6
Project: seagull   Author: ljvmiranda921   File: CellularSprites.py    License: MIT License 6 votes vote down vote up
def add_outline(mat: np.ndarray) -> np.ndarray:
    """Pad the matrix"""
    m = np.ones(mat.shape)
    for idx, orig_val in np.ndenumerate(mat):
        x, y = idx
        neighbors = [(x, y + 1), (x + 1, y), (x, y - 1), (x - 1, y)]
        if orig_val == 0:
            m[idx] = 0  # Set the coordinate in the new matrix as 0
            for n_coord in neighbors:
                try:
                    m[n_coord] = 0.5 if mat[n_coord] == 1 else 0
                except IndexError:
                    pass

    m = np.pad(m, mode="constant", pad_width=1, constant_values=1)
    # Let's do a switcheroo, I know this isn't elegant but please feel free to
    # do a PR to make this more efficient!
    m[m == 1] = np.inf
    m[m == 0.5] = 1
    m[m == np.inf] = 0.5

    return m 
Example 7
Project: MatchZoo-py   Author: NTMC-Community   File: matching_histogram.py    License: Apache License 2.0 6 votes vote down vote up
def transform(self, input_: list) -> list:
        """Transform the input text."""
        text_left, text_right = input_
        matching_hist = np.ones((len(text_left), self._hist_bin_size),
                                dtype=np.float32)
        embed_left = self._embedding_matrix[text_left]
        embed_right = self._embedding_matrix[text_right]
        matching_matrix = embed_left.dot(np.transpose(embed_right))
        for (i, j), value in np.ndenumerate(matching_matrix):
            bin_index = int((value + 1.) / 2. * (self._hist_bin_size - 1.))
            matching_hist[i][bin_index] += 1.0
        if self._mode == 'NH':
            matching_sum = matching_hist.sum(axis=1)
            matching_hist = matching_hist / matching_sum[:, np.newaxis]
        elif self._mode == 'LCH':
            matching_hist = np.log(matching_hist)
        return matching_hist.tolist() 
Example 8
Project: Computable   Author: ktraunmueller   File: test_index_tricks.py    License: MIT License 6 votes vote down vote up
def test_ndindex():
    x = list(np.ndindex(1, 2, 3))
    expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))]
    assert_array_equal(x, expected)

    x = list(np.ndindex((1, 2, 3)))
    assert_array_equal(x, expected)

    # Test use of scalars and tuples
    x = list(np.ndindex((3,)))
    assert_array_equal(x, list(np.ndindex(3)))

    # Make sure size argument is optional
    x = list(np.ndindex())
    assert_equal(x, [()])

    x = list(np.ndindex(()))
    assert_equal(x, [()])

    # Make sure 0-sized ndindex works correctly
    x = list(np.ndindex(*[0]))
    assert_equal(x, []) 
Example 9
Project: ektelo   Author: ektelo   File: dataset.py    License: Apache License 2.0 6 votes vote down vote up
def reduce_data(self,p_grid,data):
        """reduce data to the domain indicated by p_grid"""
        assert data.shape == p_grid.shape, 'Shape of x and shape of partition vector must match.'
        #get out_shape
        if p_grid.ndim == 1:
            out_shape =(len(numpy.unique(p_grid)), )
        else:
            out = []
            for i in range(p_grid.ndim):
                ind_slice = [0] * p_grid.ndim
                ind_slice[i] = slice(0, p_grid.shape[i])
                out.append(len(numpy.unique(p_grid[ind_slice])))

            # This is doesn't work for dimension >2, the compressed array is not a strip of the domain,
            # but the first element along the axis. It could be an nd array itself.
            #out = [len(numpy.unique(numpy.compress([True], p_grid, axis=i))) for i in range(p_grid.ndim)]
            #out.reverse()   # rows/cols need to be reversed here
            out_shape = tuple(out)
        #reduce
        unique, indices, inverse, counts = numpy.unique(p_grid, return_index=True, return_inverse=True, return_counts=True)
        res = numpy.zeros_like(unique, dtype=float)
        for index, c in numpy.ndenumerate(data.ravel()):   # needs to be flattened for parallel indexing with output of unique
            res[ inverse[index] ] += c
        return numpy.array(res).reshape(out_shape) 
Example 10
Project: cs224d   Author: PDFangeltop1   File: rnnlmWithHierarchicalSoftmax.py    License: MIT License 6 votes vote down vote up
def grad_check_hierarchicalU(self,node,grad_computed,grad_approx,eps,x,y):
        if node.isLeaf == True:
            return
        if node.grad == None:
            return

        theta = node.hActs
        for ij,v in np.ndenumerate(node.hActs):
            tij = theta[ij]
            theta[ij] = tij + eps
            Jplus = self.compute_loss(x,y)
            theta[ij] = tij - eps
            Jminus = self.compute_loss(x, y)
            theta[ij] = tij # reset
            approx = (Jplus - Jminus)/(2*eps)
            grad_computed.append(node.grad[ij])
            grad_approx.append(approx)

        self.grad_check_hierarchicalU(node.left,grad_computed,grad_approx,eps,x,y)
        self.grad_check_hierarchicalU(node.right,grad_computed,grad_approx,eps,x,y) 
Example 11
Project: python3_ios   Author: holzschu   File: hinton_demo.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def hinton(matrix, max_weight=None, ax=None):
    """Draw Hinton diagram for visualizing a weight matrix."""
    ax = ax if ax is not None else plt.gca()

    if not max_weight:
        max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))

    ax.patch.set_facecolor('gray')
    ax.set_aspect('equal', 'box')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    for (x, y), w in np.ndenumerate(matrix):
        color = 'white' if w > 0 else 'black'
        size = np.sqrt(np.abs(w) / max_weight)
        rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
                             facecolor=color, edgecolor=color)
        ax.add_patch(rect)

    ax.autoscale_view()
    ax.invert_yaxis() 
Example 12
Project: read_thermal.py   Author: Nervengift   File: flir_image_extractor.py    License: Creative Commons Zero v1.0 Universal 6 votes vote down vote up
def export_thermal_to_csv(self, csv_filename):
        """
        Convert thermal data in numpy to json
        :return:
        """

        with open(csv_filename, 'w') as fh:
            writer = csv.writer(fh, delimiter=',')
            writer.writerow(['x', 'y', 'temp (c)'])

            pixel_values = []
            for e in np.ndenumerate(self.thermal_image_np):
                x, y = e[0]
                c = e[1]
                pixel_values.append([x, y, c])

            writer.writerows(pixel_values) 
Example 13
Project: safepy   Author: baryshnikova-lab   File: safe_io.py    License: GNU General Public License v3.0 6 votes vote down vote up
def calculate_edge_lengths(G, verbose=True):

    # Calculate the lengths of the edges

    if verbose:
        print('Calculating edge lengths...')

    x = np.matrix(G.nodes.data('x'))[:, 1]
    y = np.matrix(G.nodes.data('y'))[:, 1]

    node_coordinates = np.concatenate([x, y], axis=1)
    node_distances = squareform(pdist(node_coordinates, 'euclidean'))

    adjacency_matrix = np.array(nx.adjacency_matrix(G).todense())
    adjacency_matrix = adjacency_matrix.astype('float')
    adjacency_matrix[adjacency_matrix == 0] = np.nan

    edge_lengths = np.multiply(node_distances, adjacency_matrix)

    edge_attr_dict = {index: v for index, v in np.ndenumerate(edge_lengths) if ~np.isnan(v)}
    nx.set_edge_attributes(G, edge_attr_dict, 'length')

    return G 
Example 14
Project: color_recognizer   Author: michtesar   File: hinton.py    License: MIT License 6 votes vote down vote up
def hinton(matrix, max_weight=None, ax=None):
    """Draw Hinton diagram for visualizing a weight matrix."""
    ax = ax if ax is not None else plt.gca()

    if not max_weight:
        max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))

    ax.patch.set_facecolor('gray')
    ax.set_aspect('equal', 'box')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    for (x, y), w in np.ndenumerate(matrix):
        color = 'white' if w > 0 else 'black'
        size = np.sqrt(np.abs(w) / max_weight)
        rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
                             facecolor=color, edgecolor=color)
        ax.add_patch(rect)

    ax.autoscale_view()
    ax.invert_yaxis()

    return ax 
Example 15
Project: PyBloqs   Author: man-group   File: core.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def _flatten_data(data, chart_cfg, switch_zy=False):
        plot_axes_def = [(0, XAxis), (1, YAxis)]

        # Inject categories into the axis definitions of the plot
        if isinstance(data, NDFrame):
            for i, plot_axis in plot_axes_def[:data.ndim]:
                categories = data.axes[i]
                # Skip numeric indices
                if not categories.is_numeric():
                    chart_cfg = chart_cfg.inherit_many(plot_axis(categories=list(categories)))

        data = [list(index) + [value] for index, value in list(np.ndenumerate(data))]

        if switch_zy:
            for i in range(len(data)):
                tmp = data[i][-1]
                data[i][-1] = data[i][-2]
                data[i][-2] = tmp

        return data, chart_cfg 
Example 16
Project: MobileNetV2-PoseEstimation   Author: PINTO0309   File: WindowDistance.py    License: MIT License 6 votes vote down vote up
def generateDistanceMatrix(width, height):
	"""
	Generates a matrix specifying the distance of each point in a window to its centre.
	"""
	
	# Determine the coordinates of the exact centre of the window
	originX = width / 2
	originY = height / 2
	
	# Generate the distance matrix
	distances = zerosFactory((height,width), dtype=np.float)
	for index, val in np.ndenumerate(distances):
		y,x = index
		distances[(y,x)] = math.sqrt( math.pow(x - originX, 2) + math.pow(y - originY, 2) )
	
	return distances 
Example 17
Project: GDMC   Author: mcgreentn   File: schematic.py    License: ISC License 6 votes vote down vote up
def toSchematic(self):
        schem = MCSchematic(shape=self.Size, mats=self._mat)
        for (x, y, z), value in ndenumerate(self._blocks):
            b_id, b_data = value
            schem.Blocks[x, z, y] = b_id
            schem.Data[x, z, y] = b_data
            
        for (x, y, z), value in ndenumerate(self._tile_entities):
            if not value:
                continue
            tag = value
            tag["x"] = nbt.TAG_Int(x)
            tag["y"] = nbt.TAG_Int(y)
            tag["z"] = nbt.TAG_Int(z)
            schem.addTileEntity(tag)
        
        entity_list = nbt.TAG_List()
        for e in self._entities:
            entity_list.append(e)
        schem.root_tag["Entities"] = entity_list
            
        return schem 
Example 18
Project: GDMC   Author: mcgreentn   File: schematic.py    License: ISC License 6 votes vote down vote up
def fromSchematic(cls, schematic):
        structure = cls(size=(schematic.Width, schematic.Height, schematic.Length), mats=namedMaterials[getattr(schematic, "Materials", 'Alpha')])
        schematic = copy.deepcopy(schematic)
        
        for (x, z, y), b_id in ndenumerate(schematic.Blocks):
            data = schematic.Data[x, z, y]
            structure._blocks[x, y, z] = (b_id, data)
            
        for te in schematic.TileEntities:
            x, y, z = te["x"].value, te["y"].value, te["z"].value
            del te["x"]
            del te["y"]
            del te["z"]
            structure._tile_entities[x, y, z] = te
            
        for e in schematic.Entities:
            structure._entities.append(e)
        return structure 
Example 19
Project: MatchZoo   Author: NTMC-Community   File: matching_histogram.py    License: Apache License 2.0 6 votes vote down vote up
def transform(self, input_: list) -> list:
        """Transform the input text."""
        text_left, text_right = input_
        matching_hist = np.ones((len(text_left), self._hist_bin_size),
                                dtype=np.float32)
        embed_left = self._embedding_matrix[text_left]
        embed_right = self._embedding_matrix[text_right]
        matching_matrix = embed_left.dot(np.transpose(embed_right))
        for (i, j), value in np.ndenumerate(matching_matrix):
            bin_index = int((value + 1.) / 2. * (self._hist_bin_size - 1.))
            matching_hist[i][bin_index] += 1.0
        if self._mode == 'NH':
            matching_sum = matching_hist.sum(axis=1)
            matching_hist = matching_hist / matching_sum[:, np.newaxis]
        elif self._mode == 'LCH':
            matching_hist = np.log(matching_hist)
        return matching_hist.tolist() 
Example 20
Project: NeuralResponseRanking   Author: yangliuy   File: pair_generator.py    License: MIT License 6 votes vote down vote up
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        d1len = len(self.data1[t1])
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[self.data1[t1]]
            t2_rep = self.embed[self.data2[t2]]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist 
Example 21
Project: NeuralResponseRanking   Author: yangliuy   File: list_generator.py    License: MIT License 6 votes vote down vote up
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        d1len = len(self.data1[t1])
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[self.data1[t1]]
            t2_rep = self.embed[self.data2[t2]]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist 
Example 22
Project: NeuralResponseRanking   Author: yangliuy   File: point_generator.py    License: MIT License 6 votes vote down vote up
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        d1len = len(self.data1[t1])
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[self.data1[t1]]
            t2_rep = self.embed[self.data2[t2]]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist 
Example 23
Project: CNNArt   Author: thomaskuestner   File: matplotlibwidget.py    License: Apache License 2.0 6 votes vote down vote up
def Weights_opt(self, matrix, max_weight=None, ax=None):

        ax = ax if ax is not None else plt.gca()

        if not max_weight:
            max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))

        ax.patch.set_facecolor('gray')
        ax.set_aspect('equal', 'box')
        ax.xaxis.set_major_locator(plt.NullLocator())
        ax.yaxis.set_major_locator(plt.NullLocator())

        for (x, y), w in np.ndenumerate(matrix):
            color = 'white' if w > 0 else 'black'
            size = np.sqrt(np.abs(w))
            rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
                                 facecolor=color, edgecolor=color)
            ax.add_patch(rect)

        ax.autoscale_view()
        ax.invert_yaxis() 
Example 24
Project: quantumflow   Author: rigetti   File: gates.py    License: Apache License 2.0 6 votes vote down vote up
def print_gate(gate: Gate, ndigits: int = 2,
               file: TextIO = None) -> None:
    """Pretty print a gate tensor

    Args:
        gate:
        ndigits:
        file: Stream to which to write. Defaults to stdout
    """
    N = gate.qubit_nb
    gate_tensor = gate.vec.asarray()
    lines = []
    for index, amplitude in np.ndenumerate(gate_tensor):
        ket = "".join([str(n) for n in index[0:N]])
        bra = "".join([str(index[n]) for n in range(N, 2*N)])
        if round(abs(amplitude)**2, ndigits) > 0.0:
            lines.append('{} -> {} : {}'.format(bra, ket, amplitude))
    lines.sort(key=lambda x: int(x[0:N]))
    print('\n'.join(lines), file=file) 
Example 25
Project: quantumflow   Author: rigetti   File: states.py    License: Apache License 2.0 6 votes vote down vote up
def __str__(self) -> str:
        state = self.vec.asarray()
        s = []
        count = 0
        MAX_ELEMENTS = 64
        for index, amplitude in np.ndenumerate(state):
            if not np.isclose(amplitude, 0.0):
                ket = '|' + ''.join([str(n) for n in index]) + '>'
                s.append('({c.real:0.04g}{c.imag:+0.04g}i) {k}'
                         .format(c=amplitude, k=ket))
                count += 1
                if count > MAX_ELEMENTS:
                    s.append('...')
                    break
        return ' + '.join(s)

# End class State 
Example 26
Project: quantumflow   Author: rigetti   File: states.py    License: Apache License 2.0 6 votes vote down vote up
def print_probabilities(state: State, ndigits: int = 4,
                        file: TextIO = None) -> None:
    """
    Pretty print state probabilities.

    Args:
        state:
        ndigits: Number of digits of accuracy
        file: Output stream (Defaults to stdout)
    """
    prob = bk.evaluate(state.probabilities())
    for index, prob in np.ndenumerate(prob):
        prob = round(prob, ndigits)
        if prob == 0.0:
            continue
        ket = "".join([str(n) for n in index])
        print(ket, ":", prob, file=file)


# --  Mixed Quantum States -- 
Example 27
Project: quantumflow   Author: rigetti   File: test_states.py    License: Apache License 2.0 6 votes vote down vote up
def test_state_to_density():
    density = qf.ghz_state(4).asdensity()
    assert list(density.vec.asarray().shape) == [2]*8

    prob = qf.asarray(density.probabilities())
    assert prob[0, 0, 0, 0] - 0.5 == ALMOST_ZERO
    assert prob[0, 1, 0, 0] == ALMOST_ZERO
    assert prob[1, 1, 1, 1] - 0.5 == ALMOST_ZERO

    ket = qf.random_state(3)
    density = ket.asdensity()
    ket_prob = qf.asarray(ket.probabilities())
    density_prob = qf.asarray(density.probabilities())

    for index, prob in np.ndenumerate(ket_prob):
        assert prob - density_prob[index] == ALMOST_ZERO 
Example 28
Project: uis-rnn   Author: google   File: uisrnn.py    License: Apache License 2.0 6 votes vote down vote up
def _calculate_score(self, beam_state, look_ahead_seq):
    """Calculate negative log likelihoods for all possible state allocations
       of a look ahead sequence, according to the current beam state.

    Args:
      beam_state: A BeamState object.
      look_ahead_seq: Look ahead sequence, size: look_ahead*D.
        look_ahead: number of step to look ahead in the beam search.
        D: observation dimension

    Returns:
      beam_score_set: a set of scores for each possible state allocation.
    """

    look_ahead, _ = look_ahead_seq.shape
    beam_num_clusters = len(beam_state.mean_set)
    beam_score_set = float('inf') * np.ones(
        beam_num_clusters + 1 + np.arange(look_ahead))
    for cluster_seq, _ in np.ndenumerate(beam_score_set):
      updated_beam_state = self._update_beam_state(beam_state,
                                                   look_ahead_seq, cluster_seq)
      beam_score_set[cluster_seq] = updated_beam_state.neg_likelihood
    return beam_score_set 
Example 29
Project: NPRF   Author: ucasir   File: matrix.py    License: Apache License 2.0 6 votes vote down vote up
def hist_from_matrix(text_maxlen, hist_size, sim_mat):
  '''

    References: https://github.com/faneshion/MatchZoo/blob/master/matchzoo/inputs/preprocess.py#L425
  Args:
    text_maxlen:
    hist_size:
    sim_mat:

  Returns:

  '''
  hist = np.zeros((text_maxlen, hist_size), dtype=np.float32)
  # mm = sim_mat
  for (i, j), v in np.ndenumerate(sim_mat):
    if i >= text_maxlen:
      break
    vid = int((v + 1.) / 2. * (hist_size - 1.))
    hist[i][vid] += 1
  hist += 1
  hist = np.log10(hist)
  # yield hist
  return hist 
Example 30
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_ndenumerate_crash(self):
        # Ticket 1140
        # Shouldn't crash:
        list(np.ndenumerate(np.array([[]])))