]> code.communitydata.science - cdsc_reddit.git/blobdiff - visualization/tsne_vis.py
Merge branch 'master' of code:cdsc_reddit into excise_reindex
[cdsc_reddit.git] / visualization / tsne_vis.py
index a644ecf5ae7c77f9b1b753b94e83b2839bb17903..eb6a6be840c0497810c49a8c84f0610fb828d9db 100644 (file)
@@ -14,7 +14,7 @@ def base_plot(plot_data):
 
     cluster_dropdown = alt.binding_select(options=[str(c) for c in sorted(set(plot_data.cluster))])
 
 
     cluster_dropdown = alt.binding_select(options=[str(c) for c in sorted(set(plot_data.cluster))])
 
-    subreddit_dropdown = alt.binding_select(options=sorted(plot_data.subreddit))
+    #    subreddit_dropdown = alt.binding_select(options=sorted(plot_data.subreddit))
 
     cluster_click_select = alt.selection_single(on='click',fields=['cluster'], bind=cluster_dropdown, name=' ')
     # cluster_select = alt.selection_single(fields=['cluster'], bind=cluster_dropdown, name='cluster')
 
     cluster_click_select = alt.selection_single(on='click',fields=['cluster'], bind=cluster_dropdown, name=' ')
     # cluster_select = alt.selection_single(fields=['cluster'], bind=cluster_dropdown, name='cluster')
@@ -22,8 +22,12 @@ def base_plot(plot_data):
     #
     #    subreddit_select = alt.selection_single(on='click',fields=['subreddit'],bind=subreddit_dropdown,name='subreddit_click')
     
     #
     #    subreddit_select = alt.selection_single(on='click',fields=['subreddit'],bind=subreddit_dropdown,name='subreddit_click')
     
+    base_scale = alt.Scale(scheme={"name":'category10',
+                                   "extent":[0,100],
+                                   "count":10})
+
     color = alt.condition(cluster_click_select ,
     color = alt.condition(cluster_click_select ,
-                          alt.Color(field='color',type='nominal',scale=alt.Scale(scheme='category10')),
+                          alt.Color(field='color',type='nominal',scale=base_scale),
                           alt.value("lightgray"))
   
     
                           alt.value("lightgray"))
   
     
@@ -42,7 +46,7 @@ def zoom_plot(plot_data):
     chart = base_plot(plot_data)
 
     chart = chart.interactive()
     chart = base_plot(plot_data)
 
     chart = chart.interactive()
-    chart = chart.properties(width=1275,height=1000)
+    chart = chart.properties(width=1275,height=800)
 
     return chart
 
 
     return chart
 
@@ -84,6 +88,11 @@ def viewport_plot(plot_data):
     return chart
 
 def assign_cluster_colors(tsne_data, clusters, n_colors, n_neighbors = 4):
     return chart
 
 def assign_cluster_colors(tsne_data, clusters, n_colors, n_neighbors = 4):
+    isolate_color = 101
+
+    cluster_sizes = clusters.groupby('cluster').count()
+    singletons = set(cluster_sizes.loc[cluster_sizes.subreddit == 1].reset_index().cluster)
+
     tsne_data = tsne_data.merge(clusters,on='subreddit')
     
     centroids = tsne_data.groupby('cluster').agg({'x':np.mean,'y':np.mean})
     tsne_data = tsne_data.merge(clusters,on='subreddit')
     
     centroids = tsne_data.groupby('cluster').agg({'x':np.mean,'y':np.mean})
@@ -120,15 +129,17 @@ def assign_cluster_colors(tsne_data, clusters, n_colors, n_neighbors = 4):
     color_assignments = np.repeat(-1,len(centroids))
 
     for i in range(len(centroids)):
     color_assignments = np.repeat(-1,len(centroids))
 
     for i in range(len(centroids)):
-        knn = indices[i]
-        knn_colors = color_assignments[knn]
-        available_colors = color_ids[list(set(color_ids) - set(knn_colors))]
-
-        if(len(available_colors) > 0):
-            color_assignments[i] = available_colors[0]
+        if (centroids.iloc[i].name == -1) or (i in singletons):
+            color_assignments[i] = isolate_color
         else:
         else:
-            raise Exception("Can't color this many neighbors with this many colors")
+            knn = indices[i]
+            knn_colors = color_assignments[knn]
+            available_colors = color_ids[list(set(color_ids) - set(knn_colors))]
 
 
+            if(len(available_colors) > 0):
+                color_assignments[i] = available_colors[0]
+            else:
+                raise Exception("Can't color this many neighbors with this many colors")
 
     centroids = centroids.reset_index()
     colors = centroids.loc[:,['cluster']]
 
     centroids = centroids.reset_index()
     colors = centroids.loc[:,['cluster']]
@@ -139,11 +150,20 @@ def assign_cluster_colors(tsne_data, clusters, n_colors, n_neighbors = 4):
 
 def build_visualization(tsne_data, clusters, output):
 
 
 def build_visualization(tsne_data, clusters, output):
 
+    # tsne_data = "/gscratch/comdata/output/reddit_tsne/subreddit_author_tf_similarities_10000.feather"
+    # clusters = "/gscratch/comdata/output/reddit_clustering/subreddit_author_tf_similarities_10000.feather"
+
     tsne_data = pd.read_feather(tsne_data)
     tsne_data = pd.read_feather(tsne_data)
+    tsne_data = tsne_data.rename(columns={'_subreddit':'subreddit'})
     clusters = pd.read_feather(clusters)
 
     tsne_data = assign_cluster_colors(tsne_data,clusters,10,8)
 
     clusters = pd.read_feather(clusters)
 
     tsne_data = assign_cluster_colors(tsne_data,clusters,10,8)
 
+    sr_per_cluster = tsne_data.groupby('cluster').subreddit.count().reset_index()
+    sr_per_cluster = sr_per_cluster.rename(columns={'subreddit':'cluster_size'})
+
+    tsne_data = tsne_data.merge(sr_per_cluster,on='cluster')
+
     term_zoom_plot = zoom_plot(tsne_data)
 
     term_zoom_plot.save(output)
     term_zoom_plot = zoom_plot(tsne_data)
 
     term_zoom_plot.save(output)

Community Data Science Collective || Want to submit a patch?