コード例 #1
0
ファイル: utils.py プロジェクト: lagka/sockeye
def plot_attention(attention_matrix: np.ndarray, source_tokens: List[str], target_tokens: List[str], filename: str):
    """
    Uses matplotlib for creating a visualization of the attention matrix.

    :param attention_matrix: The attention matrix.
    :param source_tokens: A list of source tokens.
    :param target_tokens: A list of target tokens.
    :param filename: The file to which the attention visualization will be written to.
    """
    try:
        import matplotlib
    except ImportError:
        raise RuntimeError("Please install matplotlib.")
    matplotlib.use("Agg")
    import matplotlib.pyplot as plt
    assert attention_matrix.shape[0] == len(target_tokens)

    plt.imshow(attention_matrix.transpose(), interpolation="nearest", cmap="Greys")
    plt.xlabel("target")
    plt.ylabel("source")
    plt.gca().set_xticks([i for i in range(0, len(target_tokens))])
    plt.gca().set_yticks([i for i in range(0, len(source_tokens))])
    plt.gca().set_xticklabels(target_tokens, rotation='vertical')
    plt.gca().set_yticklabels(source_tokens)
    plt.tight_layout()
    plt.savefig(filename)
    logger.info("Saved alignment visualization to " + filename)
コード例 #2
0
def make_surface(rgba_array: numpy.ndarray) -> pygame.Surface:
    """
    This function is use for 32-24 bit texture with pixel alphas transparency only

    make_surface(RGBA array) -> Surface

    Return a Surface created with the method frombuffer
    Argument rgba_array is a numpy array containing RGB values + alpha channel.
    This method create a texture with per-pixel alpha values.
    'frombuffer' can display image with disproportional scaling (X&Y scale),
    but the image will be distorted. Use array = original_array.copy(order='C') to
    force frombuffer to accept disproportional image.
    Another method is to scale the original image with irregular X&Y values
    before processing the image with frombuffer (therefore this could create
    undesirable effect in sprite animation, sprite deformation etc).

    :param rgba_array: 3D numpy array created with the method surface.make_array.
                       Combine RGB values and alpha values.
    :return:           Return a pixels alpha texture.This texture contains a transparency value
                       for each pixels.
    """
    assert rgba_array.shape[:2][0] != 0, 'ValueError: Resolution must be positive values '
    assert rgba_array.shape[:2][1] != 0, 'ValueError: Resolution must be positive values '

    assert isinstance(rgba_array, numpy.ndarray), 'Expecting numpy.ndarray for ' \
                                                  'argument rgb_array got %s ' % type(rgba_array)
    return pygame.image.frombuffer((rgba_array.transpose(1, 0, 2)).copy(order='C').astype(numpy.uint8),
                                   (rgba_array.shape[:2][0], rgba_array.shape[:2][1]), 'RGBA')
コード例 #3
0
ファイル: Projections.py プロジェクト: lmhonorio/PySGS
	def __init__(self, data :np.ndarray , world_ref_center:np.ndarray , axis:np.ndarray):
		self.data_to_world_ref_center = data - world_ref_center
		self.mod_pi = [np.linalg.norm(pi) for pi in axis]
		self.projection_over_pi = []
		self.min = []
		self.max = []
		self.delta_lambda = []
		self.volume = 1.0
		self.box_ref_center = []
		self.dimension = data.shape[1]

		for i in range(0,self.dimension):
			projection_over_pi = np.array(np.dot(self.data_to_world_ref_center, axis[i,:]) / self.mod_pi[i])
			vmin = projection_over_pi.min(0)
			vmax = projection_over_pi.max(0)
			self.projection_over_pi.append(projection_over_pi)
			self.min.append(vmin)
			self.max.append(vmax)
			delta_lambda = (np.array(vmax) - np.array(vmin))/2.0
			self.delta_lambda.append(delta_lambda)
			self.volume *= self.volume * delta_lambda 

		#self.center = center + 0.5 * np.dot(np.array(self.min) + np.array(self.max),axis)
		#o min e max se referem ao centro na coordenada do box, para a coordenada do mundo devemos transformar
		# de acordo com o centro do mundo e a matriz de rotação dada pelos autovetores.
		self.box_ref_center = world_ref_center + 0.5 * np.dot(axis.transpose(),np.array(self.min) + np.array(self.max))
コード例 #4
0
ファイル: PowerSpectrum.py プロジェクト: Xrispies/solspec
 def weight_spectrum(self, spec_in: np.ndarray, kind="cubic"):
     """
     Weights a spectrum by a normalized spectrum, e.g. absorption, reflection, transmission at wavelengths in nm
     :param kind: (str or int, optional)interpolation method specification in scipy.interpolat.interp1d:
     Specifies the kind of interpolation as a string (‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic, ‘cubic’
     where ‘slinear’, ‘quadratic’ and ‘cubic’ refer to a spline interpolation of first, second or third order) or as
     an integer specifying the order of the spline interpolator to use. Default is ‘linear’.
     :param spec_in: (np.ndarray) a 2-D array with wavelengths in nm in spec_in[:,0] and normalized vaules in
     spec_in[:,1]
     :return: (np.ndarray) a weighted spectrum in the same format as spec_in
     """
     if spec_in.shape[1] != 2:
         try:
             spec_in = spec_in.transpose()
         except:
             pass
     spec_in = np.squeeze(spec_in)
     assert spec_in.shape[1] == 2, "Weight spectrum is not a 2D numpy array."
     spec_fun = interpolate.interp1d(spec_in[:, 0], spec_in[:, 1], kind=kind)
     if spec_in[0, 0] != self.start_w or spec_in[-1, 0] != self.stop_w:
         self.spectrum = self.sub_spectrum(spec_in[0, 0], spec_in[-1, 0])
     spec_wt = self.spectrum
     spec_wt[:, 1] = spec_fun(spec_wt[:, 0]) * spec_wt[:, 1]
     return
コード例 #5
0
ファイル: bm3d_ctypes.py プロジェクト: vsridha/sv-mbirct
def bm3d_step(mode: BM3DStages,
              z: np.ndarray,
              psd: np.ndarray,
              single_dim_psd: bool,
              profile: BM3DProfile,
              t_fwd: np.ndarray,
              t_inv: np.ndarray,
              t_fwd_3d: dict,
              t_inv_3d: dict,
              wwin: np.ndarray,
              channel_count: int,
              pre_block_matches: np.ndarray,
              refiltering=False,
              y_hat=None) -> (np.array, np.array):
    """
    Perform either BM3D Hard-thresholding or Wiener step through an external library call.
    :param mode: BM3DStages.HARD_THRESHOLDING or BM3DStages.WIENER_FILTERING
    :param z: The noisy image, np.ndarray of MxNxChannels
    :param psd: PSD matching the noisy image, or a (list of) noise standard deviation(s)
    :param single_dim_psd: True if giving stds, False if giving full PSDs
    :param profile: BM3DProfile object containing the chosen parameters.
    :param t_fwd: forward transform for 2-D transform
    :param t_inv: inverse transform for 2-D transform
    :param t_fwd_3d: 1-D stack transform
    :param t_inv_3d: 1-D stack transform
    :param wwin: Aggregation window
    :param channel_count: number of channels of z
    :param pre_block_matches: [0], [1] or block matches array
    :param refiltering: True if this is the refiltering step
    :param y_hat: previous estimate, same size as z, only for Wiener
    :return: tuple (estimate, blockmatches), where estimate is the same size as z, and blockmatches either
             empty or containing the blockmatch data if pre_block_matches was [1]
    """
    z_shape = z.shape
    z = z.transpose((2, 1, 0)).flatten()
    t_inv = t_inv.T.flatten()
    t_fwd = t_fwd.T.flatten()
    t_inv_3d_flat = flatten_transf(t_inv_3d)
    t_fwd_3d_flat = flatten_transf(t_fwd_3d)
    wwin = wwin.T.flatten()

    psd = format_psd(psd, single_dim_psd)

    # HT only
    lambda_thr3d = profile.lambda_thr3d if not refiltering else profile.lambda_thr3d_re

    if mode == BM3DStages.HARD_THRESHOLDING:
        lambdas = [llen(lambda_thr3d)
                   ] + lambda_thr3d if llen(lambda_thr3d) > 1 else [0]
        c_lambdas = conv_to_array(
            np.ascontiguousarray(np.array(lambdas), dtype=np.float32))
        c_y_hat = None
    else:
        c_lambdas = None
        c_y_hat = conv_to_array(
            np.ascontiguousarray(y_hat.transpose(2, 1, 0).flatten(),
                                 dtype=np.float32))

    c_z = conv_to_array(np.ascontiguousarray(z, dtype=np.float64),
                        ctype=ctypes.c_double)

    c_psd = conv_to_array(np.ascontiguousarray(psd, dtype=np.float32))
    c_t_fwd = conv_to_array(np.ascontiguousarray(t_fwd, dtype=np.float32))
    c_t_inv = conv_to_array(np.ascontiguousarray(t_inv, dtype=np.float32))
    c_t_fwd_3d_flat = conv_to_array(
        np.ascontiguousarray(t_fwd_3d_flat, dtype=np.float32))
    c_t_inv_3d_flat = conv_to_array(
        np.ascontiguousarray(t_inv_3d_flat, dtype=np.float32))

    c_wwin = conv_to_array(np.ascontiguousarray(wwin, dtype=np.float64),
                           ctype=ctypes.c_double)

    c_pre_block_matches = conv_to_array(np.ascontiguousarray(pre_block_matches,
                                                             dtype=np.int32),
                                        ctype=ctypes.c_int)

    c_thr3d = ctypes.c_float(
        lambda_thr3d[0] if type(lambda_thr3d) is list else lambda_thr3d)
    c_step = ctypes.c_int(profile.get_step_size(mode))
    c_bs = ctypes.c_int(profile.get_block_size(mode))
    c_max_3d_size = ctypes.c_int(profile.get_max_3d_size(mode))
    c_sz1 = ctypes.c_int(z_shape[0])
    c_sz2 = ctypes.c_int(z_shape[1])
    c_thrclose = ctypes.c_float(
        profile.get_block_threshold(mode) *
        np.power(profile.get_block_size(mode), 2) / (255 * 255))
    c_search_win_size = ctypes.c_int(
        (profile.get_search_window(mode) - 1) // 2)
    c_channel_count = ctypes.c_int(channel_count)
    c_nf = ctypes.c_int(profile.nf)
    c_k = ctypes.c_int(profile.k)
    c_gamma = ctypes.c_float(profile.gamma)

    dll = ctypes.CDLL(
        get_dll_names()[0 if mode == BM3DStages.HARD_THRESHOLDING else 1])

    if mode == BM3DStages.HARD_THRESHOLDING:
        func = dll.bm3d_threshold_colored_interface
        func.argtypes = ARGTYPES_THR
        func.restype = ctypes.POINTER(ctypes.POINTER(ctypes.c_float))

        ans = func(c_z, c_thr3d, c_step, c_bs, c_max_3d_size, c_sz1, c_sz2,
                   c_thrclose, c_search_win_size,
                   None if len(t_fwd) == 0 else c_t_fwd,
                   None if len(t_inv) == 0 else c_t_inv,
                   None if len(t_fwd_3d_flat) == 0 else c_t_fwd_3d_flat,
                   None if len(t_inv_3d_flat) == 0 else c_t_inv_3d_flat,
                   c_lambdas, c_wwin, c_psd, c_nf, c_k, c_gamma,
                   c_channel_count, c_pre_block_matches)

    else:
        func = dll.bm3d_wiener_colored_interface
        func.argtypes = ARGTYPES_WIE
        func.restype = ctypes.POINTER(ctypes.POINTER(ctypes.c_float))

        ans = func(c_z, c_y_hat, c_step, c_bs, c_max_3d_size, c_sz1, c_sz2,
                   c_thrclose, c_search_win_size,
                   None if len(t_fwd) == 0 else c_t_fwd,
                   None if len(t_inv) == 0 else c_t_inv,
                   None if len(t_fwd_3d_flat) == 0 else c_t_fwd_3d_flat,
                   None if len(t_inv_3d_flat) == 0 else c_t_inv_3d_flat,
                   c_lambdas, c_wwin, c_psd, c_nf, c_k, c_channel_count,
                   c_pre_block_matches)

    # Get the contents of the returned array possibly with some type of magic
    # (get the address, add the offset, and for some reason requires a cast to a float pointer after that)
    r0 = ans[0]
    holder = ctypes.POINTER(ctypes.c_float)
    returns = []

    width = z_shape[0]
    height = z_shape[1]
    for k in range(channel_count):
        ret_arr = np.zeros((height, width))
        for i in range(height):
            for j in range(width):
                ret_arr[i][j] = ctypes.cast(
                    ctypes.addressof(r0) + ctypes.sizeof(ctypes.c_float) *
                    (width * height * k + i * width + j),
                    holder).contents.value
        returns.append(ret_arr.T)

    # Blockmatching stuff if needed
    bm_array = []
    if pre_block_matches[0] == 1:
        # Acquire the blockmatch data array...
        r0 = ans[0]
        holder = ctypes.POINTER(ctypes.c_int)
        # The size of image returns
        starting_point = ctypes.sizeof(
            ctypes.c_float) * (width * height * channel_count)

        # The first element of the BM contains its total size of int
        bm_array = np.zeros(ctypes.cast(
            ctypes.addressof(r0) + starting_point, holder).contents.value,
                            dtype=np.intc)
        for i in range(bm_array.size):
            bm_array[i] = ctypes.cast(
                ctypes.addressof(r0) + starting_point +
                ctypes.sizeof(ctypes.c_int) * i, holder).contents.value

    return np.array(np.atleast_3d(returns)).transpose((1, 2, 0)), bm_array
コード例 #6
0
 def forward(self,
             x: np.ndarray) -> Tuple[np.ndarray, List[Any], Dict[str, Any]]:
     y = (np.matmul(x.transpose(
         (0, 2, 1)), self.W.data) + self.b.data).transpose((0, 2, 1))
     assert y.shape == (x.shape[0], self.output_dim, 1)
     return y, [], {}
コード例 #7
0
ファイル: utils.py プロジェクト: sunilsurineni/DNNV
def as_mipverify(
    layers: List[Layer],
    weights_file: IO,
    sample_input: np.ndarray,
    linf=None,
    translator_error: Type[VerifierTranslatorError] = VerifierTranslatorError,
) -> Iterable[str]:
    yield "using MIPVerify, Gurobi, MAT"
    yield f'parameters = matread("{weights_file.name}")'
    layer_names = []  # type: List[str]
    layer_parameters = {}  # type: Dict[str, np.ndarray]
    input_layer = layers[0]
    if not isinstance(input_layer, InputLayer):
        raise translator_error(
            f"Unsupported input layer type: {type(input_layer).__name__}")
    last_layer = layers[-1]
    if not isinstance(last_layer, FullyConnected):
        raise translator_error(
            f"Unsupported output layer type: {type(last_layer).__name__}")
    elif last_layer.bias.shape[0] == 1:
        last_layer.bias = np.array([-last_layer.bias[0], last_layer.bias[0]])
        last_layer.weights = np.stack(
            [-last_layer.weights[:, 0], last_layer.weights[:, 0]], 1)
    shape = np.asarray(input_layer.shape)[[0, 2, 3, 1]]
    seen_fullyconnected = False
    for layer_id, layer in enumerate(layers[1:], 1):
        layer_name = f"layer{layer_id}"
        if isinstance(layer, FullyConnected):
            if layer_id == 1:
                layer_names.append(f"Flatten({sample_input.ndim})")
            elif isinstance(layers[layer_id - 1], Convolutional):
                layer_names.append(f"Flatten(4)")
            weights = layer.weights.astype(np.float32)
            weights = weights[layer.w_permutation]
            if not seen_fullyconnected:
                seen_fullyconnected = True
                if len(shape) == 4:
                    weights = weights[(np.arange(np.product(shape)).reshape(
                        shape[[0, 3, 1, 2]]).transpose(
                            (0, 2, 3, 1)).flatten())]
            layer_parameters[f"{layer_name}_weight"] = weights
            yield f'{layer_name}_W = parameters["{layer_name}_weight"]'
            if len(layer.bias) > 1:
                layer_parameters[f"{layer_name}_bias"] = layer.bias
                yield f'{layer_name}_b = dropdims(parameters["{layer_name}_bias"], dims=1)'
            else:
                yield f"{layer_name}_b = [{layer.bias.item()}]"
            yield f"{layer_name} = Linear({layer_name}_W, {layer_name}_b)"
            layer_names.append(layer_name)
            if layer.activation == "relu":
                layer_names.append("ReLU()")
            elif layer.activation is not None:
                raise translator_error(
                    f"{layer.activation} activation is currently unsupported")
        elif isinstance(layer, Convolutional):
            if any(s != layer.strides[0] for s in layer.strides):
                raise translator_error(
                    "Different strides in height and width are not supported")
            input_shape = shape.copy()
            shape[3] = layer.weights.shape[0]  # number of output channels
            shape[1] = np.ceil(
                float(input_shape[1]) /
                float(layer.strides[0]))  # output height
            shape[2] = np.ceil(
                float(input_shape[2]) /
                float(layer.strides[1]))  # output width

            pad_along_height = max(
                (shape[1] - 1) * layer.strides[0] + layer.kernel_shape[0] -
                input_shape[1],
                0,
            )
            pad_along_width = max(
                (shape[2] - 1) * layer.strides[1] + layer.kernel_shape[1] -
                input_shape[2],
                0,
            )
            pad_top = pad_along_height // 2
            pad_bottom = pad_along_height - pad_top
            pad_left = pad_along_width // 2
            pad_right = pad_along_width - pad_left

            same_pads = [pad_top, pad_left, pad_bottom, pad_right]
            if any(p1 != p2 for p1, p2 in zip(layer.pads, same_pads)):
                raise translator_error("Non-SAME padding is not supported")
            weights = layer.weights.transpose((2, 3, 1, 0))
            layer_parameters[f"{layer_name}_weight"] = weights
            yield f'{layer_name}_W = parameters["{layer_name}_weight"]'
            if len(layer.bias) > 1:
                layer_parameters[f"{layer_name}_bias"] = layer.bias
                yield f'{layer_name}_b = dropdims(parameters["{layer_name}_bias"], dims=1)'
            else:
                yield f"{layer_name}_b = [{layer.bias.item()}]"
            yield f"{layer_name} = Conv2d({layer_name}_W, {layer_name}_b, {layer.strides[0]})"
            layer_names.append(layer_name)
            if layer.activation == "relu":
                layer_names.append("ReLU()")
            elif layer.activation is not None:
                raise translator_error(
                    f"{layer.activation} activation is currently unsupported")
        elif hasattr(layer, "as_julia"):
            for line in layer.as_julia(layers):
                yield line
        else:
            raise translator_error(
                f"Unsupported layer type: {type(layer).__name__}")
    yield "nn = Sequential(["
    for layer_name in layer_names:
        yield f"\t{layer_name},"
    yield f'], "{str(weights_file.name)[:-4]}")'
    if sample_input.ndim == 4:
        sample_input = sample_input.transpose((0, 2, 3, 1))
    layer_parameters["input"] = sample_input
    yield f'input = parameters["input"]'
    yield 'print(nn(input), "\\n")'
    perturbation = ""
    if linf is not None:
        perturbation = (
            f", pp=MIPVerify.LInfNormBoundedPerturbationFamily({linf}), norm_order=Inf"
        )
    # class 1 (1-indexed) if property is FALSE
    yield f"d = MIPVerify.find_adversarial_example(nn, input, 1, GurobiSolver(){perturbation}, solve_if_predicted_in_targeted=false, cache_model=false, rebuild=true)"
    yield 'print((d[:PredictedIndex] == 2) ? d[:SolveStatus] : "TRIVIAL", "\\n")'

    scipy.io.savemat(weights_file.name, layer_parameters)
コード例 #8
0
def make_gif(array: np.ndarray,
             save_as: str,
             delay: int = 10,
             time_first: bool = True,
             v_bounds: tuple = (None, None),
             dpi: int = None,
             cells: list = None,
             paths: list = None,
             extent: list = None,
             origin: str = None):
    """

    Make a gif file from a numpy array, using Matplotlib imshow over the x-y
    coordinates. Useful for plotting cell tracks and heat equation plots.
    Relies on the linux command line tool 'convert'.

    Parameters
    ----------
    array         A 3D numpy array of shape (T, X, Y) or (X, Y, T).
    save_as       String file name or path to save gif to. No file extension.
    delay         Time delay in ms between gif frames
    time_first    Boolean specifying whether time dimension appears first
    v_bounds      tuple of (vmin, vmax) to be passed to imshow.
    dpi           Resolution in dots per inch

        If working with pictures of cells:

    add_LOG       Whether to add a Laplacian of Gaussian circles to identify cells
    threshold     The threshold to use with LOG
    add_paths     Whether to plot particle paths

    """

    # check if the t-dimension is in index 0
    if not time_first:
        array = array.transpose((2, 0, 1))

    # useful for keeping heat equation time frames consistent
    vmin, vmax = v_bounds
    T, by, bx = array.shape

    # use a system of binary sequences to name the files (helps keep them in order ;P)
    bits = int(np.ceil(np.log2(T)))

    path_to_gif = '/'.join(save_as.split('/')[:-1]) + '/'
    if path_to_gif == '/':
        path_to_gif = './'

    if not os.path.exists(path_to_gif + 'tmp'):
        os.mkdir(path_to_gif + 'tmp')

    path_to_png = path_to_gif + 'tmp/'

    T0 = time.time()

    for t in tqdm(range(T), desc='Plotting images'):
        # sort name stuff
        name = bin(t)[2:]
        name = '0' * (bits - len(name)) + name
        # plot the image
        image = array[t, :, :]
        fig, ax = plt.subplots()
        plt.imshow(image, vmin=vmin, vmax=vmax, extent=extent, origin=origin)
        plt.title('t={:.2f}'.format(t))
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_xlim([0, bx])
        ax.set_ylim([0, by])

        # add Laplacian of Gaussians
        if cells is not None:
            for x, y, r in cells[t]:
                c = plt.Circle((x, y),
                               r,
                               color='red',
                               linewidth=0.5,
                               fill=False)
                ax.add_patch(c)

        if paths is not None:
            colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
            i = 0
            for path in paths:
                if path.shape[0] > 1:
                    path_to_plot = path[path[:, 0] < t]
                    plt.plot(path_to_plot[:, 1],
                             path_to_plot[:, 2],
                             color=colors[i % len(colors)],
                             linewidth=1,
                             alpha=0.75)
                    i += 1
            # plt.show()

        # save each png file
        plt.savefig(path_to_png + '{}.png'.format(name), dpi=dpi)
        plt.close()

    time.sleep(0.1)
    print('Creating gif')

    # convert pngs to gif
    check_output([
        'convert', '-delay', '{}'.format(delay), path_to_png + '*.png',
        save_as + '.gif'
    ])

    for file_name in os.listdir(path_to_png):
        if '.png' in file_name:
            os.remove(path_to_png + file_name)

    os.rmdir(path_to_png)

    print('Done')
コード例 #9
0
def deshape(img: np.ndarray):
    img = np.stack((img[3], img[2], img[1]))
    return img.transpose(1, 2, 0)
コード例 #10
0
def kf_log_likelihood(v: np.ndarray, S: np.ndarray) -> np.ndarray:
    v = v.T[..., np.newaxis]
    nis = v.transpose(0, 2, 1) @ np.linalg.solve(S, v)  # (N x 1 x 1)
    nis = nis.flatten()  # (N,)
    return - nis / 2.0 - np.log(2.0 * np.pi) - np.log(np.linalg.det(S)) / 2.0
コード例 #11
0
ファイル: util.py プロジェクト: fdamken/bachelors-thesis_code
def symmetric_batch(a: np.ndarray) -> torch.Tensor:
    return (a + a.transpose((0, 2, 1))) / 2.0
コード例 #12
0
 def extract_bits_from_pix(pix: np.ndarray) -> np.ndarray:
     pix_bits = np.array([(channel >> i) & 0x1
                          for channel in pix.transpose(2, 0, 1)
                          for i in range(7, -1, -1)],
                         dtype=np.uint8)
     return pix_bits
コード例 #13
0
def _save_img(img: np.ndarray, path):
    img = img.transpose(1, 2, 0)
    image = Image.fromarray(img)
    image.save(path)
コード例 #14
0
def l2(theta: np.ndarray) -> float:
    if theta.size == 0:
        return None
    array_sum = theta.transpose().dot(theta)
    return float(array_sum - theta[0]**2)
コード例 #15
0
def vis_points_with_array(raw: np.ndarray, points: nx.DiGraph,
                          voxel_size: np.ndarray):
    ngid = itertools.count(start=1)

    neuroglancer.set_server_bind_address("0.0.0.0")
    viewer = neuroglancer.Viewer()

    nodes = []
    edges = []

    for node_a, node_b in points.edges:
        a = points.nodes[node_a]["location"][::-1]
        b = points.nodes[node_b]["location"][::-1]

        pos_u = a
        pos_v = b

        nodes.append(
            neuroglancer.EllipsoidAnnotation(center=pos_u,
                                             radii=(3, 3, 3) / voxel_size,
                                             id=next(ngid)))
        edges.append(
            neuroglancer.LineAnnotation(point_a=pos_u,
                                        point_b=pos_v,
                                        id=next(ngid)))
    nodes.append(
        neuroglancer.EllipsoidAnnotation(center=pos_v,
                                         radii=(1, 1, 1) / voxel_size,
                                         id=next(ngid)))

    print(raw.shape)

    max_raw = np.max(raw)
    min_raw = np.min(raw)
    diff_raw = max_raw - min_raw

    try:
        raw = ((raw - min_raw) / float(diff_raw) * 255).astype("uint8")
    except Exception as e:
        print(min_raw, max_raw)
        raise e

    with viewer.txn() as s:
        s.layers["raw"] = neuroglancer.ImageLayer(
            source=neuroglancer.LocalVolume(data=raw.transpose([2, 1, 0]),
                                            voxel_size=voxel_size))
        s.layers["edges"] = neuroglancer.AnnotationLayer(
            voxel_size=voxel_size,
            filter_by_segmentation=False,
            annotation_color="#add8e6",
            annotations=edges,
        )
        s.layers["nodes"] = neuroglancer.AnnotationLayer(
            voxel_size=voxel_size,
            filter_by_segmentation=False,
            annotation_color="#ff00ff",
            annotations=nodes,
        )
        position = np.array(raw.shape) // 2
        s.navigation.position.voxelCoordinates = tuple(position)
    print(viewer)

    input("done?")
コード例 #16
0
 def predict_on_batch(self, x: np.ndarray) -> np.ndarray:
     x = torch.from_numpy(x.transpose(0, 3, 1, 2)).to(self.__device)
     with torch.no_grad():
         return self(x).cpu().numpy()
コード例 #17
0
    def cluster_with_k(k, data: np.ndarray):
        centroids_idx = np.random.randint(data.shape[0], size=k)
        centroids = data.loc[centroids_idx, :]
        data_trans = data.transpose()
        data_len = np.linalg.norm(data, axis=1)
        feature_num = data.shape[1]

        r_list = []
        label_list = []
        centroids_list = []
        for j in range(1):
            print(str(j), end=' ')
            for i in range(50):
                centroids_len = np.linalg.norm(centroids, axis=1)
                centroids_len = np.transpose(np.array([centroids_len]))
                cent_data = centroids @ data_trans
                # cent_data = np.matmul(centroids, data_trans)
                tmp1 = centroids_len.reshape(k, 1)
                tmp2 = data_len.reshape(1, data.shape[0])
                tmp3 = np.matmul(tmp1, tmp2)
                cent_data = cent_data / tmp3
                labels = cent_data.idxmax(axis=0)
                data['label'] = labels
                # sample = df.sample(k)
                # df.at[sample.index[:], 'label'] = np.arange(k)
                centroids = data.groupby(['label']).mean()
                if centroids.shape[0] < k:
                    le = k - centroids.shape[0]
                    for ii in range(centroids.shape[0],
                                    centroids.shape[0] + le):
                        centroids.loc[np.random.randint(
                            1000, 100000)] = np.random.normal(
                                np.zeros(feature_num),
                                np.ones(feature_num) * 100)
                centroids = centroids.to_numpy()
                del data['label']

            centroids_len = np.linalg.norm(centroids, axis=1)
            centroids_len = np.transpose(np.array([centroids_len]))
            cent_data = centroids @ data_trans
            # cent_data = np.matmul(centroids, data_trans)
            tmp1 = centroids_len.reshape(k, 1)
            tmp2 = data_len.reshape(1, data.shape[0])
            tmp3 = np.matmul(tmp1, tmp2)
            cent_data = cent_data / tmp3
            labels = cent_data.idxmax(axis=0)
            data['label'] = labels
            # sample = df.sample(k)
            # df.at[sample.index[:], 'label'] = np.arange(k)
            centroids = data.groupby(['label']).mean()
            joi = data.join(centroids,
                            on='label',
                            rsuffix="cent",
                            lsuffix="data")
            m = joi.loc[:, '0data':(str(feature_num - 1) + 'data')].to_numpy()
            n = joi.loc[:, '0cent':(str(feature_num - 1) + 'cent')].to_numpy()
            RSS = ((m - n)**2).sum()
            r_list.append(RSS)
            label_list.append(labels)
            centroids = centroids.to_numpy()
            centroids_list.append(centroids)
            del data['label']
        maxx = np.argmax(r_list)
        return label_list[maxx], r_list[maxx], centroids_list[maxx]
コード例 #18
0
def make_surface(rgba_array: numpy.ndarray) -> pygame.Surface:
    assert isinstance(rgba_array, numpy.ndarray), 'Expecting numpy.ndarray for ' \
                                                  'argument rgb_array got %s ' % type(rgba_array)

    return pygame.image.frombuffer((rgba_array.transpose(1, 0, 2)).copy(order='C').astype(numpy.uint8),
                                   (rgba_array.shape[:2][0], rgba_array.shape[:2][1]), 'RGBA')
コード例 #19
0
 def __call__(self, image: np.ndarray) -> torch.Tensor:
     return torch.from_numpy(image.transpose((2, 0, 1))).to(DEVICE)