def test_find_discretization_on_real_acyclic_matching(matches, filtration, delta, expected_matches): from dmt.matching import Matching from dmt.binning import find_discretization from dmt.binning import ceil matching = Matching(matches=matches) bins = find_discretization(matching, filtration, delta) ceiled_filtration = ceil(filtration, bins) assert np.all(ceiled_filtration >= filtration) assert ceiled_filtration.delta <= delta assert len( matching.induce_filtered_matching(ceil( filtration, bins)).matches) == expected_matches
def test_ceil(): from dmt.binning import ceil filtration = np.array([5, 3, 7, 2, 43, 7, 3, 1, 5, -2]) bins = np.array([0, 2, 5, 10]) assert np.all( ceil(filtration, bins) == [5., 5., 10., 2., np.inf, 10., 5., 2., 5., 0.])
def morse_approx_induced_matching(morse_complex, delta, return_matching=False, deltatrick=True): """ Creates an approximative Morse reduction with the induced matching approach The induced matching approach tries to create an unfiltered acyclic matching with small filtration differences and solves a minimal cut problem on this matching to get an induced filtered acyclic matching. Setting delta=0 results in the same algorithm as morse_reduce_nanda from MN[13] :param morse_complex: MorseComplex to reduce :param delta: Approximation parameter for the filtration and the persistence module :param return_matching: Return a tuple of the reduced complex and the matching used to reduce it :param deltatrick: Use 2*delta approximation from below and shift it down by delta. This allows more reductions. :returns Reduced complex, optionally also the filtered acyclic matching """ delta = 2 * delta if deltatrick else delta global_acyclic_matching = construct_acyclic_matching(morse_complex, delta) bins_aposteriori = find_discretization(global_acyclic_matching, morse_complex.filtration, delta) discrete_filtration = ceil(morse_complex.filtration, bins_aposteriori) - (deltatrick * delta / 2) filtered_acyclic_matching = global_acyclic_matching.induce_filtered_matching( discrete_filtration) reduced_cplx = reduce_by_acyclic_matching(filtered_acyclic_matching) if return_matching: return reduced_cplx, filtered_acyclic_matching return reduced_cplx
def plot_partial_sol(matching, filtration, partial_sol, delta, ax=None, plot_xticks=False, xlim=None): points, intervals = partial_sol ceiled_filtration = ceil(filtration, points) ax = ax or plt.gca() ax.set_aspect(.3) height = .2*(max(filtration) - min(filtration)) / len(matching) for h, match in zip(range(len(matching)), matching): s, e = match begin, end = filtration[s], filtration[e] is_seen = begin <= points[-1] is_cut = is_seen and ceiled_filtration[s] != ceiled_filtration[e] linestyle = "solid" if is_seen else "dashed" color = "red" if is_cut else "k" ax.plot([begin, end], [h * height, h * height], linestyle=linestyle, c=color, linewidth=1) for point in points[:-1]: ax.axvline(point, linestyle="--", c="grey") ax.axvline(points[-1], linestyle="--", c="k") ax.set_ylabel("%s" % len(intervals)) ax.set_ylim([-height, (len(matching) + .5) * height]) ax.set_yticks([]) ax.set_xticks([]) if plot_xticks: xticks = np.arange(0, (max(filtration) + delta) / delta, 1) ax.set_xticks(xticks * delta) ax.set_xticklabels(["%i$\delta$" % i for i in xticks]) if xlim: ax.set_xlim(xlim)
def test_find_discretization(filtration, delta): from dmt.filtration import Filtration from dmt.binning import find_discretization, ceil filtr_len = len(filtration) assert filtr_len % 2 == 0, "Filtration should give pairwise matches, thus has to have an even length" acyclic_matching = list( zip(range(0, filtr_len + 1, 2), range(1, filtr_len + 1, 2))) # Match neighboring pairs filtration = Filtration(filtration) bins = find_discretization(acyclic_matching, filtration, delta) ceiled_filtration = ceil(filtration, bins) assert np.all(ceiled_filtration >= filtration) assert ceiled_filtration.delta <= delta
def test_find_discretization_is_good(): from dmt.matching import Matching from dmt.binning import find_discretization from dmt.binning import ceil acyclic_matching = Matching(matches=[(0, 1), (2, 3), (4, 5)]) filtration = np.array([0, 1, 1, 2, 1, 2]) delta = 1.2 bins = find_discretization(acyclic_matching, filtration, delta) # For a good binning of the given matching the following should hold assert len(bins) >= 2 assert bins[0] <= 1 assert bins[-1] >= 2 assert len( acyclic_matching.induce_filtered_matching(ceil(filtration, bins)).matches) == 2
def test_ceil_equal(): from dmt.binning import ceil filtration = np.array([5, 3, 7, 2, 43, 7, 3, 1, 5, -2]) bins = np.unique(filtration) assert np.all(ceil(filtration, bins) == filtration)