
    1-Ph$                     l    d dl Zd dlmZ d dlmZ ddlmZ ddlm	Z	 ddl
mZ d Zddd	dd
ddZddZdS )    N)sparse)csgraph   )_raveled_offsets_and_distances)	map_array)_safe_downcast_indicesc                 6    t          j        | |z
            |z  S )a  A default edge function for complete image graphs.

    A pixel graph on an image with no edge values and no mask is a very
    boring regular lattice, so we define a default edge weight to be the
    absolute difference between values *weighted* by the distance
    between them.

    Parameters
    ----------
    values0 : array
        The pixel values for each node.
    values1 : array
        The pixel values for each neighbor.
    distances : array
        The distance between each node and its neighbor.

    Returns
    -------
    edge_values : array of float
        The computed values: abs(values0 - values1) * distances.
    )npabs)values0values1	distancess      T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/graph/_graph.py_weighted_abs_diffr   	   s    , 6'G#$$y00       matrix)maskedge_functionconnectivityspacingsparse_typec                   |.| j         t          k    r| }nt          j        | t                    }|| j         t          k    rd }nt          }t          j        |ddd          }t          j        |          }t          |j        ||          \  }}	|ddt          j	        f         |z   }
t          j
        |	|
j                  }t          j        |          }t          j        |j                  }t          |
||          }|                    d	          |
         }t          j        |d
          }t          j        ||          }t          j        ||          }||         }||         }t          |||          }|                     d	          } |||         ||         |          }|j        }t#          j        |||ff||f          }|dk    rt#          j        |          }n|dk    rd| }t)          |          ||fS )a  Create an adjacency graph of pixels in an image.

    Pixels where the mask is True are nodes in the returned graph, and they are
    connected by edges to their neighbors according to the connectivity
    parameter. By default, the *value* of an edge when a mask is given, or when
    the image is itself the mask, is the Euclidean distance between the pixels.

    However, if an int- or float-valued image is given with no mask, the value
    of the edges is the absolute difference in intensity between adjacent
    pixels, weighted by the Euclidean distance.

    Parameters
    ----------
    image : array
        The input image. If the image is of type bool, it will be used as the
        mask as well.
    mask : array of bool
        Which pixels to use. If None, the graph for the whole image is used.
    edge_function : callable
        A function taking an array of pixel values, and an array of neighbor
        pixel values, and an array of distances, and returning a value for the
        edge. If no function is given, the value of an edge is just the
        distance.
    connectivity : int
        The square connectivity of the pixel neighborhood: the number of
        orthogonal steps allowed to consider a pixel a neighbor. See
        `scipy.ndimage.generate_binary_structure` for details.
    spacing : tuple of float
        The spacing between pixels along each axis.
    sparse_type : {"matrix", "array"}, optional
        The return type of `graph`, either `scipy.sparse.csr_array` or
        `scipy.sparse.csr_matrix` (default).

    Returns
    -------
    graph : scipy.sparse.csr_matrix or scipy.sparse.csr_array
        A sparse adjacency matrix in which entry (i, j) is 1 if nodes i and j
        are neighbors, 0 otherwise. Depending on `sparse_type`, this can be
        returned as a `scipy.sparse.csr_array`.
    nodes : array of int
        The nodes of the graph. These correspond to the raveled indices of the
        nonzero pixels in the mask.
    N)dtypec                     |S )N )xyr   s      r   r   z"pixel_graph.<locals>.edge_function_   s      r   r   constantF)modeconstant_values)r   r   axis)shaper   arrayz/`sparse_type` must be 'array' or 'matrix', got )r   boolr
   	ones_liker   padflatnonzeror   r%   newaxisbroadcast_toarangesizer   reshapesumrepeatr   	csr_array
csr_matrix
ValueError)imager   r   r   r   r   paddednodes_paddedneighbor_offsets_paddeddistances_paddedneighbors_paddedneighbor_distances_fullnodesnodes_sequential	neighborsneighbors_masknum_neighborsindicesindices_sequentialneighbor_indicesneighbor_distancesneighbor_indices_sequentialimage_rdatamgraphmsgs                              r   pixel_graphrK   "   sG   h |;$DD<T222D;$! ! ! ! /M2 VD!*eDDDF>&))L0N<1 1 1-- $AAArzM25LL o.>@P@VWWN4  Ey,, *L%@@I^^B''(89NF>222Mi}--G#3]CC 00@"+,<eEU"V"VmmBG='"235G D 	A	"$?@A!Q  E h!%((			MMMoo%<r   d   c                    |t          j        | j        d                   }|d}nt          d| j        d         |z            }t	          | t           j        d          \  | _        | _        t          j        | j        d                   }g }t          j        ||          D ]V}t          j
        | d|          }t          j        |          }	|                    t          j        |	d                     Wt          j        |          }
t          j        |
          }t          j        |
|                   }|||                  }|t          j        ||          }n|}||
fS )	a  Find the pixel with the highest closeness centrality.

    Closeness centrality is the inverse of the total sum of shortest distances
    from a node to every other node.

    Parameters
    ----------
    graph : scipy.sparse.csr_array or scipy.sparse.csr_matrix
        The sparse representation of the graph.
    nodes : array of int
        The raveled index of each node in graph in the image. If not provided,
        the returned value will be the index in the input graph.
    shape : tuple of int
        The shape of the image in which the nodes are embedded. If provided,
        the returned coordinates are a NumPy multi-index of the same
        dimensionality as the input shape. Otherwise, the returned coordinate
        is the raveled index provided in `nodes`.
    partition_size : int
        This function computes the shortest path distance between every pair
        of nodes in the graph. This can result in a very large (N*N) matrix.
        As a simple performance tweak, the distance values are computed in
        lots of `partition_size`, resulting in a memory requirement of only
        partition_size*N.

    Returns
    -------
    position : int or tuple of int
        If shape is given, the coordinate of the central pixel in the image.
        Otherwise, the raveled index of that pixel.
    distances : array of float
        The total sum of distances from each node to each other reachable
        node.
    Nr   r   r   z"index values too large for csgraphF)directedrA   r#   )r
   r-   r%   maxr   int32rA   indptrarray_splitr   shortest_path
nan_to_numappendr0   concatenater*   argminunravel_index)rI   r<   r%   partition_size
num_splitsidxstotal_shortest_path_len_list	partitionshortest_pathsshortest_paths_no_inftotal_shortest_path_lennonzeromin_spraveled_indexcentrals                  r   central_pixelre      s_   D }	%+a.))

EKNn<==
"8rx=# #EM5< 9U[^$$D#% ^D*55 S S	 .uuiXXX "n = =$++BF3Hq,Q,Q,QRRRR n-IJJn455GY.w788F'&/*M"=%88+++r   )NNrL   )numpyr
   scipyr   scipy.sparser   morphology._utilr   util._map_arrayr   'segmentation.random_walker_segmentationr   r   rK   re   r   r   r   <module>rl      s                          = = = = = = ' ' ' ' ' ' L L L L L L1 1 18 
~ ~ ~ ~ ~B9, 9, 9, 9, 9, 9,r   