In [41]:
%matplotlib qt
import pandas as pd
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import mia
Loading the hologic and synthetic datasets.
In [3]:
hologic = pd.DataFrame.from_csv("real_texture.csv")
hologic.drop(hologic.columns[:2], axis=1, inplace=True)
hologic.drop('breast_area', axis=1, inplace=True)
phantom = pd.DataFrame.from_csv("synthetic_texture.csv")
phantom.drop(phantom.columns[:2], axis=1, inplace=True)
phantom.drop('breast_area', axis=1, inplace=True)
Loading the meta data for the real and synthetic datasets.
In [4]:
hologic_meta = mia.analysis.create_hologic_meta_data(hologic, "meta_data/real_meta.csv")
phantom_meta = mia.analysis.create_synthetic_meta_data(phantom, "meta_data/synthetic_meta.csv")
phantom_meta.index.name = 'img_name'
Prepare the BI-RADS/VBD labels for both datasets.
In [5]:
hologic_labels = hologic_meta.drop_duplicates().BIRADS
phantom_labels = phantom_meta['VBD.1']
class_labels = pd.concat([hologic_labels, phantom_labels])
class_labels.index.name = "img_name"
labels = mia.analysis.remove_duplicate_index(class_labels)[0]
Create blob features from distribution of blobs
In [6]:
hologic_texture_features = mia.analysis.group_by_scale_space(hologic)
phantom_texture_features = mia.analysis.group_by_scale_space(phantom)
Take a random subset of the phantom mammograms. This is important so that each case is not over represented.
In [7]:
syn_feature_meta = mia.analysis.remove_duplicate_index(phantom_meta)
phantom_texture_features['phantom_name'] = syn_feature_meta.phantom_name.tolist()
phantom_texture_features_subset = mia.analysis.create_random_subset(phantom_texture_features, 'phantom_name')
# hologic_texture_features['patient_id'] = hologic_meta['patient_id'].drop_duplicates()
# hologic_texture_features_subset = mia.analysis.create_random_subset(hologic_texture_features, 'patient_id')
Combine the features from both datasets.
In [8]:
features = pd.concat([hologic_texture_features, phantom_texture_features_subset])
assert features.shape[0] == 366
features.head()
Out[8]:
Filter some features, such as the min, to remove noise.
In [9]:
selected_features = features.copy()
Compare the distributions of features detected from the real mammograms and the phantoms using the Kolmogorov-Smirnov two sample test.
In [80]:
ks_stats = [list(stats.ks_2samp(hologic_texture_features[col],
phantom_texture_features[col]))
for col in hologic_texture_features.columns]
ks_test = pd.DataFrame(ks_stats, columns=['KS', 'p-value'], index=hologic_texture_features.columns)
ks_test.to_latex("tables/texture_features_ks.tex")
ks_test
Out[80]:
In [11]:
kwargs = {
'learning_rate': 300,
'perplexity': 30,
'verbose': 1
}
In [12]:
SNE_mapping_2d, error = mia.analysis.tSNE(selected_features, n_components=2, **kwargs)
In [13]:
mia.plotting.plot_mapping_2d(SNE_mapping_2d, hologic_texture_features.index, phantom_texture_features_subset.index, labels)
plt.savefig('figures/mappings/texture_SNE_mapping_2d.png', dpi=300)
Running t-SNE to obtain a 3 dimensional mapping
In [14]:
SNE_mapping_3d, error = mia.analysis.tSNE(selected_features, n_components=3, **kwargs)
In [42]:
mia.plotting.plot_mapping_3d(SNE_mapping_3d, hologic_texture_features.index, phantom_texture_features_subset.index, labels)
Out[42]:
In [16]:
iso_kwargs = {
'n_neighbors': 10,
}
In [17]:
iso_mapping_2d, error = mia.analysis.isomap(selected_features, n_components=2, **iso_kwargs)
In [18]:
mia.plotting.plot_mapping_2d(iso_mapping_2d, hologic_texture_features.index, phantom_texture_features_subset.index, labels)
plt.savefig('figures/mappings/texture_iso_mapping_2d.png', dpi=300)
In [19]:
iso_mapping_3d, error = mia.analysis.isomap(selected_features, n_components=3, **iso_kwargs)
In [43]:
mia.plotting.plot_mapping_3d(iso_mapping_3d, hologic_texture_features.index, phantom_texture_features_subset.index, labels)
Out[43]:
In [21]:
lle_kwargs = {
'n_neighbors': 10,
}
In [22]:
lle_mapping_2d, error = mia.analysis.lle(selected_features, n_components=2, **lle_kwargs)
In [23]:
mia.plotting.plot_mapping_2d(lle_mapping_2d, hologic_texture_features.index, phantom_texture_features_subset.index, labels)
plt.savefig('figures/mappings/texture_lle_mapping_2d.png', dpi=300)
In [24]:
lle_mapping_3d, error = mia.analysis.lle(selected_features, n_components=3, **lle_kwargs)
In [44]:
mia.plotting.plot_mapping_3d(lle_mapping_3d, hologic_texture_features.index, phantom_texture_features_subset.index, labels)
Out[44]:
Assess the quality of the DR against measurements from the co-ranking matrices. First create co-ranking matrices for each of the dimensionality reduction mappings
In [26]:
max_k = 10
In [27]:
SNE_mapping_2d_cm = mia.coranking.coranking_matrix(selected_features, SNE_mapping_2d)
iso_mapping_2d_cm = mia.coranking.coranking_matrix(selected_features, iso_mapping_2d)
lle_mapping_2d_cm = mia.coranking.coranking_matrix(selected_features, lle_mapping_2d)
SNE_mapping_3d_cm = mia.coranking.coranking_matrix(selected_features, SNE_mapping_3d)
iso_mapping_3d_cm = mia.coranking.coranking_matrix(selected_features, iso_mapping_3d)
lle_mapping_3d_cm = mia.coranking.coranking_matrix(selected_features, lle_mapping_3d)
In [28]:
SNE_trustworthiness_2d = [mia.coranking.trustworthiness(SNE_mapping_2d_cm, k) for k in range(1, max_k)]
iso_trustworthiness_2d = [mia.coranking.trustworthiness(iso_mapping_2d_cm, k) for k in range(1, max_k)]
lle_trustworthiness_2d = [mia.coranking.trustworthiness(lle_mapping_2d_cm, k) for k in range(1, max_k)]
In [29]:
trustworthiness_df = pd.DataFrame([SNE_trustworthiness_2d,
iso_trustworthiness_2d,
lle_trustworthiness_2d],
index=['SNE', 'Isomap', 'LLE']).T
trustworthiness_df.plot()
plt.savefig('figures/quality_measures/texture_trustworthiness_2d.png', dpi=300)
In [30]:
SNE_continuity_2d = [mia.coranking.continuity(SNE_mapping_2d_cm, k) for k in range(1, max_k)]
iso_continuity_2d = [mia.coranking.continuity(iso_mapping_2d_cm, k) for k in range(1, max_k)]
lle_continuity_2d = [mia.coranking.continuity(lle_mapping_2d_cm, k) for k in range(1, max_k)]
In [31]:
continuity_df = pd.DataFrame([SNE_continuity_2d,
iso_continuity_2d,
lle_continuity_2d],
index=['SNE', 'Isomap', 'LLE']).T
continuity_df.plot()
plt.savefig('figures/quality_measures/texture_continuity_2d.png', dpi=300)
In [32]:
SNE_lcmc_2d = [mia.coranking.LCMC(SNE_mapping_2d_cm, k) for k in range(2, max_k)]
iso_lcmc_2d = [mia.coranking.LCMC(iso_mapping_2d_cm, k) for k in range(2, max_k)]
lle_lcmc_2d = [mia.coranking.LCMC(lle_mapping_2d_cm, k) for k in range(2, max_k)]
In [33]:
lcmc_df = pd.DataFrame([SNE_lcmc_2d,
iso_lcmc_2d,
lle_lcmc_2d],
index=['SNE', 'Isomap', 'LLE']).T
lcmc_df.plot()
plt.savefig('figures/quality_measures/texture_lcmc_2d.png', dpi=300)
In [34]:
SNE_trustworthiness_3d = [mia.coranking.trustworthiness(SNE_mapping_3d_cm, k) for k in range(1, max_k)]
iso_trustworthiness_3d = [mia.coranking.trustworthiness(iso_mapping_3d_cm, k) for k in range(1, max_k)]
lle_trustworthiness_3d = [mia.coranking.trustworthiness(lle_mapping_3d_cm, k) for k in range(1, max_k)]
In [35]:
trustworthiness3d_df = pd.DataFrame([SNE_trustworthiness_3d,
iso_trustworthiness_3d,
lle_trustworthiness_3d],
index=['SNE', 'Isomap', 'LLE']).T
trustworthiness3d_df.plot()
plt.savefig('figures/quality_measures/texture_trustworthiness_3d.png', dpi=300)
In [36]:
SNE_continuity_3d = [mia.coranking.continuity(SNE_mapping_3d_cm, k) for k in range(1, max_k)]
iso_continuity_3d = [mia.coranking.continuity(iso_mapping_3d_cm, k) for k in range(1, max_k)]
lle_continuity_3d = [mia.coranking.continuity(lle_mapping_3d_cm, k) for k in range(1, max_k)]
In [37]:
continuity3d_df = pd.DataFrame([SNE_continuity_3d,
iso_continuity_3d,
lle_continuity_3d],
index=['SNE', 'Isomap', 'LLE']).T
continuity3d_df.plot()
plt.savefig('figures/quality_measures/texture_continuity_3d.png', dpi=300)
In [38]:
SNE_lcmc_3d = [mia.coranking.LCMC(SNE_mapping_3d_cm, k) for k in range(2, max_k)]
iso_lcmc_3d = [mia.coranking.LCMC(iso_mapping_3d_cm, k) for k in range(2, max_k)]
lle_lcmc_3d = [mia.coranking.LCMC(lle_mapping_3d_cm, k) for k in range(2, max_k)]
In [39]:
lcmc3d_df = pd.DataFrame([SNE_lcmc_3d,
iso_lcmc_3d,
lle_lcmc_3d],
index=['SNE', 'Isomap', 'LLE']).T
lcmc3d_df.plot()
plt.savefig('figures/quality_measures/texture_lcmc_3d.png', dpi=300)