def test_two_dimensional_tensor(pts): """Verify that the oneDimensionalCover fails for an input with more than one dimension, and that the CubicalCover does not.""" one_d = OneDimensionalCover() with pytest.raises(ValueError): one_d.fit(pts) cubical = CubicalCover() _ = cubical.fit(pts)
def test_cubical_fit_transform_consistent_with_OneD(filter, kind, n_intervals, overlap_fraction): """Check that CubicalCover gives the same results as OneDimensionalCover, on one-d data """ one_d = OneDimensionalCover(kind, n_intervals, overlap_fraction) cubical = CubicalCover(kind, n_intervals, overlap_fraction) x_one_d = one_d.fit_transform(filter) x_cubical = cubical.fit_transform(filter) assert_almost_equal(x_one_d, x_cubical)
def test_pipeline_cloned(X, clone_pipeline, layout_dim, color_by_columns_dropdown): """Verify that the pipeline is changed on interaction if and only if `clone_pipeline` is False (with `layout_dim` set to 2 or 3).""" # TODO: Monitor development of the ipytest project to convert these into # true notebook tests integrated with pytest params = { "cover": { "initial": {"n_intervals": 10, "kind": "uniform", "overlap_frac": 0.1}, "new": {"n_intervals": 15, "kind": "balanced", "overlap_frac": 0.2} }, "clusterer": { "initial": {"affinity": "euclidean"}, "new": {"affinity": "manhattan"} }, "contract_nodes": {"initial": True, "new": False}, "min_intersection": {"initial": 4, "new": 1}, } pipe = make_mapper_pipeline( cover=CubicalCover(**params["cover"]["initial"]), clusterer=FirstSimpleGap(**params["clusterer"]["initial"]), contract_nodes=params["contract_nodes"]["initial"], min_intersection=params["min_intersection"]["initial"] ) fig = plot_interactive_mapper_graph( pipe, X, clone_pipeline=clone_pipeline, layout_dim=layout_dim, color_by_columns_dropdown=color_by_columns_dropdown ) # Get relevant widgets and change their states, then check final values for step, values in params.items(): if step in ["cover", "clusterer"]: for param_name, initial_param_value in values["initial"].items(): new_param_value = values["new"][param_name] widgets = _get_widgets_by_trait(fig, "description", param_name) for w in widgets: w.set_state({'value': new_param_value}) final_param_value_actual = \ pipe.get_mapper_params()[f"{step}__{param_name}"] final_param_value_expected = \ initial_param_value if clone_pipeline else new_param_value assert final_param_value_actual == final_param_value_expected else: initial_param_value = values["initial"] new_param_value = values["new"] widgets = _get_widgets_by_trait(fig, "description", step) for w in widgets: w.set_state({'value': new_param_value}) final_param_value_actual = \ pipe.get_mapper_params()[f"{step}"] final_param_value_expected = \ initial_param_value if clone_pipeline else new_param_value assert final_param_value_actual == final_param_value_expected
def Mappe(data, intervals): filter_func = Eccentricity() cover = CubicalCover(n_intervals=intervals, overlap_frac=0.3) clusterer = DBSCAN() n_jobs = 2 pipe = make_mapper_pipeline( filter_func=filter_func, cover=cover, clusterer=clusterer, verbose=False, n_jobs=n_jobs, ) g = pipe.fit_transform(data) A = g.get_edgelist() G = nx.Graph(A) return G
def main(): directory = DOTENV_KEY2VAL["DATA_DIR"] image_dir = directory + "/patch_92/" diagnosis_json = "collected_diagnoses_complete.json" ( cn_patients, mci_patients, ad_patients, ) = utils.get_earliest_available_diagnosis(directory + diagnosis_json) images_all = utils.get_arrays_from_dir( image_dir, cn_patients + mci_patients + ad_patients) cn_patient_list = [ 1 for patient in range(len(cn_patients) - 1) ] # substracting one due to unfound MRI for one CN patient mci_patient_list = [2 for patient in range(len(mci_patients))] ad_patient_list = [3 for patient in range(len(ad_patients))] diags = np.array(cn_patient_list + mci_patient_list + ad_patient_list).reshape(-1, 1) ohe = OneHotEncoder() labels = ohe.fit_transform(diags).toarray() images = [] for image in images_all: images.append(image.flatten()) images_all = np.asarray(images) pca = PCA(n_components=440) pca.fit(images_all) fig, ax0 = plt.subplots(nrows=1, sharex=True, figsize=(6, 6)) ax0.plot( np.arange(1, pca.n_components_ + 1), pca.explained_variance_ratio_, "+", linewidth=2, ) ax0.set_ylabel("PCA explained variance ratio") ax0.legend(prop=dict(size=12)) plt.savefig(DOTENV_KEY2VAL["GEN_FIGURES_DIR"] + "elbow_plot.png") n_components = 3 pca = PCA(n_components=n_components) images_all_projected = pca.fit_transform(images_all) images_all_projected = np.append(images_all_projected, labels, axis=1) mapper_pipeline = make_mapper_pipeline( filter_func=Projection(columns=[index for index in range(2)]), cover=CubicalCover(n_intervals=10, overlap_frac=0.25), clusterer=DBSCAN(eps=0.5, min_samples=5), verbose=True, n_jobs=4, ) plotly_params = {"node_trace": {"marker_colorscale": "Blues"}} fig = plot_static_mapper_graph( mapper_pipeline, images_all_projected, layout_dim=3, color_by_columns_dropdown=True, plotly_params=plotly_params, ) fig.write_html(DOTENV_KEY2VAL["GEN_FIGURES_DIR"] + "mapper_2_dimensional_reduction.html") images_all_projected = pd.DataFrame(images_all_projected) fig = px.scatter_3d( images_all_projected, x=0, y=1, z=2, color=3, title="3D scatterplot of the PCA of the image data", ) fig.write_html(DOTENV_KEY2VAL["GEN_FIGURES_DIR"] + "scatterplot_pca_3d.html")