# Path: megaman/geometry/adjacency.py
# def compute_adjacency_matrix(X, method='auto', **kwargs):
#     """Compute an adjacency matrix with the given method"""
#     if method == 'auto':
#         if X.shape[0] > 10000:
#             method = 'cyflann'
#         else:
#             method = 'kd_tree'
#     return Adjacency.init(method, **kwargs).adjacency_graph(X.astype('float'))


    import matplotlib
    import matplotlib.pyplot as plt
import sys, time
import numpy as np
from numpy import polyfit
from megaman.geometry.adjacency import compute_adjacency_matrix
    matplotlib.use('Agg')
except ImportError:
    MATPLOTLIB_LOADED = False




def compute_largest_radius_distance(X, rad, adjacency_method, adjacency_kwds):
    print('computing distance matrix...')
    adjacency_kwds['radius'] = rad
    t0 = time.time()
    dists = compute_adjacency_matrix(X,adjacency_method,**adjacency_kwds)
    print("Symmetrizing distance matrix...")
    dists = (dists + dists.T) # symmetrize, removes zero on diagonal
    dists.data = 0.5 * dists.data
    print("largest distance found: " + str(np.max(dists.data)))
    return(dists)

def neighborhood_analysis(dists, radii):
    n_samples = dists.shape[0]; nradii = len(radii)
    avg_neighbors = np.zeros(nradii); num_no_nbrs = np.zeros(nradii);
    max_neighbors = np.zeros(nradii); min_neighbors = np.zeros(nradii)
    dists = dists.tocsr()
    for ii in range(nradii):
        print("=========================================")
        print("Step " + str(ii +1) + " of " + str(nradii))
        ii = nradii - 1 - ii
        print(ii)
        rad = radii[ii]
        print("radius: " + str(radii[ii]))

# Path: megaman/geometry/adjacency.py
# def compute_adjacency_matrix(X, method='auto', **kwargs):
#     """Compute an adjacency matrix with the given method"""
#     if method == 'auto':
#         if X.shape[0] > 10000:
#             method = 'cyflann'
#         else:
#             method = 'kd_tree'
#     return Adjacency.init(method, **kwargs).adjacency_graph(X.astype('float'))


# Path: megaman/utils/analyze_dimension_and_radius.py
    import matplotlib
    import matplotlib.pyplot as plt
import sys, time
import numpy as np
from numpy import polyfit
from megaman.geometry.adjacency import compute_adjacency_matrix

    matplotlib.use('Agg')
except ImportError:
    MATPLOTLIB_LOADED = False




def compute_largest_radius_distance(X, rad, adjacency_method, adjacency_kwds):
    print('computing distance matrix...')
    adjacency_kwds['radius'] = rad
    t0 = time.time()
    dists = compute_adjacency_matrix(X,adjacency_method,**adjacency_kwds)
    print("Symmetrizing distance matrix...")
    dists = (dists + dists.T) # symmetrize, removes zero on diagonal
    dists.data = 0.5 * dists.data
    print("largest distance found: " + str(np.max(dists.data)))
    return(dists)

def neighborhood_analysis(dists, radii):
    n_samples = dists.shape[0]; nradii = len(radii)
    avg_neighbors = np.zeros(nradii); num_no_nbrs = np.zeros(nradii);
    max_neighbors = np.zeros(nradii); min_neighbors = np.zeros(nradii)
    dists = dists.tocsr()
    for ii in range(nradii):
        print("=========================================")
        print("Step " + str(ii +1) + " of " + str(nradii))
        ii = nradii - 1 - ii
        print(ii)
        rad = radii[ii]
        print("radius: " + str(radii[ii]))

        print("censoring distance matrix...")