def test_embedding_plot_arrow_integer_axis(embset): emb = embset["red"] fig, ax = mpl.pyplot.subplots() emb.plot( kind="arrow", x_axis=0, y_axis=2, color="blue", x_label="xlabel", y_label="ylabel", title="test plot", annot=False, ) props = { "type": mpl.collections.PolyCollection, "data": np.concatenate((emb.vector[0:1], emb.vector[2:3])), "x_label": "xlabel", "y_label": "ylabel", "title": "test plot", "color": mpl.colors.to_rgba_array("blue"), "aspect": "auto", # Not applicable: label } UV = np.concatenate((ax.collections[1].U, ax.collections[1].V)) assert isinstance(ax.collections[1], props["type"]) assert np.array_equal(UV, props["data"]) assert np.array_equal(ax.collections[1].get_facecolor(), props["color"]) assert ax.texts == [] validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embedding_plot_scatter_emb_axis_with_common_str_axis_metric(embset): emb = embset["red"] fig, ax = mpl.pyplot.subplots() emb.plot( kind="scatter", x_axis=embset["blue"], y_axis=embset["green"], axis_metric="cosine_distance", ) props = { "type": mpl.collections.PathCollection, "data": np.array([ scipy_distance.cosine(emb.vector, embset["blue"].vector), scipy_distance.cosine(emb.vector, embset["green"].vector), ]), "x_label": "blue", "y_label": "green", "color": mpl.colors.to_rgba_array("steelblue"), "title": "", "label": "red", "aspect": "auto", } assert np.array_equal(ax.collections[0].get_offsets()[0], props["data"]) assert isinstance(ax.collections[0], props["type"]) assert ax.texts[0].get_text() == props["label"] validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embeddingset_plot_arrow_mixed_axis(embset): fig, ax = mpl.pyplot.subplots() embset.plot(kind="arrow", x_axis=1, y_axis="red", x_label="xx", color="magenta") vectors = [] for emb in embset.embeddings.values(): vec = [] vec.append(emb.vector[1]) vec.append(emb > embset["red"]) vectors.append(vec) vectors = np.array(vectors) props = { "type": mpl.collections.PolyCollection, "data": vectors, "x_label": "xx", "y_label": "red", "title": "", "label": list(embset.embeddings.keys()), "color": mpl.colors.to_rgba_array("magenta"), "aspect": "auto", } UV = np.concatenate( (ax.collections[1].U[:, None], ax.collections[1].V[:, None]), axis=-1 ) assert isinstance(ax.collections[1], props["type"]) assert np.array_equal(UV, props["data"]) assert [t.get_text() for t in ax.texts] == props["label"] assert np.array_equal(ax.collections[1].get_facecolors(), props["color"]) validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embedding_plot_arrow_emb_axis(embset): emb = embset["red"] + embset["yellow"] fig, ax = mpl.pyplot.subplots() emb.plot( kind="arrow", x_axis=embset["blue"], y_axis=embset["green"], color="yellow", show_ops=True, axis_option="equal", ) props = { "type": mpl.collections.PolyCollection, "data": np.array([emb > embset["blue"], emb > embset["green"]]), "x_label": "blue", "y_label": "green", "color": mpl.colors.to_rgba_array("yellow"), "title": "", "label": "(red + yellow)", "aspect": 1.0, } UV = np.concatenate((ax.collections[1].U, ax.collections[1].V)) assert isinstance(ax.collections[1], props["type"]) assert np.array_equal(UV, props["data"]) assert np.array_equal(ax.collections[1].get_facecolor(), props["color"]) assert ax.texts[0].get_text() == props["label"] validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embeddingset_plot_scatter_str_axis(embset): fig, ax = mpl.pyplot.subplots() embset.plot(kind="scatter", x_axis="blue", y_axis="red") vectors = [] for emb in embset.embeddings.values(): vec = [] vec.append(emb > embset["blue"]) vec.append(emb > embset["red"]) vectors.append(vec) vectors = np.array(vectors) props = { "type": mpl.collections.PathCollection, "data": vectors, "x_label": "blue", "y_label": "red", "title": "", "label": list(embset.embeddings.keys()), "color": mpl.colors.to_rgba_array("steelblue"), "aspect": "auto", } assert isinstance(ax.collections[0], props["type"]) assert np.array_equal(ax.collections[0].get_offsets(), props["data"]) assert [t.get_text() for t in ax.texts] == props["label"] assert np.array_equal(ax.collections[0].get_facecolors(), props["color"]) validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embeddingset_plot_scatter_emb_axis_with_common_str_axis_metric(embset): fig, ax = mpl.pyplot.subplots() embset.plot( kind="scatter", x_axis=embset["green"], y_axis=embset["white"], axis_metric="euclidean", ) vectors = [] for emb in embset.embeddings.values(): vectors.append( [ scipy_distance.euclidean(emb.vector, embset["green"].vector), scipy_distance.euclidean(emb.vector, embset["white"].vector), ] ) vectors = np.array(vectors) props = { "type": mpl.collections.PathCollection, "data": vectors, "x_label": "green", "y_label": "white", "title": "", "label": list(embset.embeddings.keys()), "color": mpl.colors.to_rgba_array("steelblue"), "aspect": "auto", } assert isinstance(ax.collections[0], props["type"]) assert np.array_equal(ax.collections[0].get_offsets(), props["data"]) assert [t.get_text() for t in ax.texts] == props["label"] assert np.array_equal(ax.collections[0].get_facecolors(), props["color"]) validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embeddingset_plot_arrow_integer_axis_with_str_axis_metric(embset): fig, ax = mpl.pyplot.subplots() embset.plot( kind="arrow", x_axis=1, y_axis=2, axis_metric="cosine_distance", x_label="1", y_label="2", color="yellow", axis_option="scaled", ) vectors = np.concatenate((embset.to_X()[:, 1:2], embset.to_X()[:, 2:3]), axis=-1) props = { "type": mpl.collections.PolyCollection, "data": vectors, "x_label": "1", "y_label": "2", "title": "", "label": list(embset.embeddings.keys()), "color": mpl.colors.to_rgba_array("yellow"), "aspect": 1.0, } UV = np.concatenate( (ax.collections[1].U[:, None], ax.collections[1].V[:, None]), axis=-1 ) assert isinstance(ax.collections[1], props["type"]) assert np.array_equal(UV, props["data"]) assert [t.get_text() for t in ax.texts] == props["label"] assert np.array_equal(ax.collections[1].get_facecolors(), props["color"]) validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embedding_plot_text_integer_axis(embset): emb = embset["red"] fig, ax = mpl.pyplot.subplots() emb.plot(kind="text", x_axis=1, y_axis=2) props = { "data": np.concatenate((emb.vector[1:2] + 0.01, emb.vector[2:3])), "x_label": "Dimension 1", "y_label": "Dimension 2", "title": "", "label": "red", "aspect": "auto", # Not applicable: type, color } assert np.array_equal(ax.texts[0].get_position(), props["data"]) assert ax.collections == [] assert ax.texts[0].get_text() == props["label"] validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embedding_plot_scatter_integer_axis(embset): emb = embset["red"] fig, ax = mpl.pyplot.subplots() emb.plot(kind="scatter", x_axis=0, y_axis=1) props = { "type": mpl.collections.PathCollection, "data": emb.vector[0:2], "x_label": "Dimension 0", "y_label": "Dimension 1", "title": "", "color": mpl.colors.to_rgba_array("steelblue"), "label": "red", "aspect": "auto", } assert np.array_equal(ax.collections[0].get_offsets()[0], props["data"]) assert isinstance(ax.collections[0], props["type"]) assert np.array_equal(ax.collections[0].get_facecolor(), props["color"]) assert ax.texts[0].get_text() == props["label"] validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embeddingset_plot_scatter_integer_axis(embset): fig, ax = mpl.pyplot.subplots() embset.plot(kind="scatter", x_axis=0, y_axis=1, annot=False, color="black") vectors = np.concatenate((embset.to_X()[:, 0:1], embset.to_X()[:, 1:2]), axis=-1) props = { "type": mpl.collections.PathCollection, "data": vectors, "x_label": "Dimension 0", "y_label": "Dimension 1", "title": "", "color": mpl.colors.to_rgba_array("black"), "aspect": "auto", # Not applicable: label } assert isinstance(ax.collections[0], props["type"]) assert np.array_equal(ax.collections[0].get_offsets(), props["data"]) assert ax.texts == [] assert np.array_equal(ax.collections[0].get_facecolors(), props["color"]) validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)
def test_embeddingset_plot_arrow_emb_axis_with_different_axis_metric(embset): fig, ax = mpl.pyplot.subplots() embset.plot( kind="arrow", x_axis=embset["blue"], y_axis="red", axis_metric=[scipy_distance.correlation, "cosine_similarity"], x_label="xx", color="magenta", ) vectors = [] for emb in embset.embeddings.values(): vectors.append( [ scipy_distance.correlation(emb.vector, embset["blue"].vector), 1.0 - scipy_distance.cosine(emb.vector, embset["red"].vector), ] ) vectors = np.array(vectors) props = { "type": mpl.collections.PolyCollection, "data": vectors, "x_label": "xx", "y_label": "red", "title": "", "label": list(embset.embeddings.keys()), "color": mpl.colors.to_rgba_array("magenta"), "aspect": "auto", } UV = np.concatenate( (ax.collections[1].U[:, None], ax.collections[1].V[:, None]), axis=-1 ) assert isinstance(ax.collections[1], props["type"]) assert np.array_equal(UV, props["data"]) assert [t.get_text() for t in ax.texts] == props["label"] assert np.array_equal(ax.collections[1].get_facecolors(), props["color"]) validate_plot_general_properties(ax, props) mpl.pyplot.close(fig)