Nyanpre commited on
Commit
49e4070
·
verified ·
1 Parent(s): df312cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -23
app.py CHANGED
@@ -147,62 +147,89 @@ def analyze_and_output(my_id, my_pw, target_id, freq_type, progress=gr.Progress(
147
  heat_data.index = ['月','火','水','木','金','土','日']
148
  fig_heat = px.imshow(heat_data, color_continuous_scale='Blues', title="活動時間帯(JST)", labels=dict(x="時間", y="曜日", color="投稿数"))
149
 
150
- # --- 相関図の修正箇所 ---
 
 
151
  top_interactors = [u for u, _ in reply_counts.most_common(8)]
152
  nodes = list(set([target_handle] + top_interactors))
153
  G = nx.Graph()
154
  for u1, u2 in interaction_pairs:
155
- if u1 in nodes and u2 in nodes: G.add_edge(u1, u2)
 
156
 
 
157
  pos = nx.spring_layout(G, k=1.5, seed=42)
 
 
 
 
 
 
158
  fig_net = go.Figure()
159
 
160
- # エッジの描画
161
  for e in G.edges():
162
- fig_net.add_trace(go.Scatter(x=[pos[e[0]][0], pos[e[1]][0]], y=[pos[e[0]][1], pos[e[1]][1]], mode='lines', line=dict(color='#ccc', width=1), hoverinfo='none'))
 
 
 
 
 
 
163
 
164
  node_imgs = []
165
- node_texts = []
166
 
167
- # アイコンの大きさを定義
168
- img_size = 0.18
169
 
170
- for n in G.nodes():
171
  info = user_info_cache.get(n, {"avatar": ""})
 
 
 
172
  if info["avatar"]:
173
  node_imgs.append(dict(
174
  source=info["avatar"], xref="x", yref="y",
175
- x=pos[n][0], y=pos[n][1],
176
  sizex=img_size, sizey=img_size,
177
  xanchor="center", yanchor="middle", layer="above"
178
  ))
179
 
180
- # 関係性ラベル
181
- rel = f"<span style='color:#0085ff;'>◆{random.choice(RELATIONSHIPS)}</span>" if n != target_handle else "<b>(本人)</b>"
182
- # アイコンの下に潜り込まないよう、適度な空白を調整
183
- node_texts.append(f"<b>{n}</b><br>{rel}")
 
 
 
 
 
 
 
184
 
185
- # ノード(透明マーカー + テキスト)の描画
186
  fig_net.add_trace(go.Scatter(
187
- x=[pos[n][0] for n in nodes],
188
- y=[pos[n][1] for n in nodes],
189
  mode='markers+text',
190
  text=node_texts,
191
- # markerのsizeを調整すること、textposition="bottom center"の開始位置をアイコンの外側に逃がす
192
- marker=dict(size=45, color='rgba(0,0,0,0)'),
193
  textposition="bottom center",
194
- textfont=dict(size=11, color="black"),
195
  hoverinfo='none'
196
  ))
197
 
 
198
  fig_net.update_layout(
199
  images=node_imgs,
200
  showlegend=False,
201
- xaxis=dict(visible=False, range=[-1.2, 1.2]),
202
- yaxis=dict(visible=False, range=[-1.2, 1.2]),
203
  plot_bgcolor='white',
204
- height=600,
205
- margin=dict(t=40, b=40, l=0, r=0)
206
  )
207
 
208
  return html, fig_bar, fig_heat, fig_net, "解析完了!"
 
147
  heat_data.index = ['月','火','水','木','金','土','日']
148
  fig_heat = px.imshow(heat_data, color_continuous_scale='Blues', title="活動時間帯(JST)", labels=dict(x="時間", y="曜日", color="投稿数"))
149
 
150
+ # --- 相関図作成セクションの修正コード ---
151
+
152
+ # 1. グラフの構築
153
  top_interactors = [u for u, _ in reply_counts.most_common(8)]
154
  nodes = list(set([target_handle] + top_interactors))
155
  G = nx.Graph()
156
  for u1, u2 in interaction_pairs:
157
+ if u1 in nodes and u2 in nodes:
158
+ G.add_edge(u1, u2)
159
 
160
+ # 2. レイアウト計算
161
  pos = nx.spring_layout(G, k=1.5, seed=42)
162
+
163
+ # 【重要】本人が中心に来るように座標をシフトする
164
+ center_x, center_y = pos[target_handle]
165
+ for n in pos:
166
+ pos[n] = (pos[n][0] - center_x, pos[n][1] - center_y)
167
+
168
  fig_net = go.Figure()
169
 
170
+ # 3. エッジ(線)の描画
171
  for e in G.edges():
172
+ fig_net.add_trace(go.Scatter(
173
+ x=[pos[e[0]][0], pos[e[1]][0]],
174
+ y=[pos[e[0]][1], pos[e[1]][1]],
175
+ mode='lines',
176
+ line=dict(color='#ccc', width=1),
177
+ hoverinfo='none'
178
+ ))
179
 
180
  node_imgs = []
181
+ node_x, node_y, node_texts = [], [], []
182
 
183
+ # アイコンの表示サイズ
184
+ img_size = 0.22
185
 
186
+ for n in nodes:
187
  info = user_info_cache.get(n, {"avatar": ""})
188
+ x_loc, y_loc = pos[n]
189
+
190
+ # アイコン画像を配置
191
  if info["avatar"]:
192
  node_imgs.append(dict(
193
  source=info["avatar"], xref="x", yref="y",
194
+ x=x_loc, y=y_loc,
195
  sizex=img_size, sizey=img_size,
196
  xanchor="center", yanchor="middle", layer="above"
197
  ))
198
 
199
+ # テキスト用の座標と内容
200
+ node_x.append(x_loc)
201
+ node_y.append(y_loc)
202
+
203
+ # 関係性ラベル(本人の場合は特別扱い)
204
+ if n == target_handle:
205
+ rel = "<br><b style='color:#ff4b4b;'>(本人)</b>"
206
+ else:
207
+ rel = f"<br><span style='color:#0085ff;'>◆{random.choice(RELATIONSHIPS)}</span>"
208
+
209
+ node_texts.append(f"<b>{n}</b>{rel}")
210
 
211
+ # 4. ノード(透明マーカーテキスト)の描画
212
  fig_net.add_trace(go.Scatter(
213
+ x=node_x,
214
+ y=node_y,
215
  mode='markers+text',
216
  text=node_texts,
217
+ # markerのsizeでアイコン距離を保つ
218
+ marker=dict(size=50, color='rgba(0,0,0,0)'),
219
  textposition="bottom center",
220
+ textfont=dict(size=12, color="black"),
221
  hoverinfo='none'
222
  ))
223
 
224
+ # 5. レイアウト設定
225
  fig_net.update_layout(
226
  images=node_imgs,
227
  showlegend=False,
228
+ xaxis=dict(visible=False, range=[-1.5, 1.5]), # 範囲を広げて見切れ防止
229
+ yaxis=dict(visible=False, range=[-1.5, 1.5]),
230
  plot_bgcolor='white',
231
+ height=700, # 少し高さを出す
232
+ margin=dict(t=20, b=20, l=0, r=0)
233
  )
234
 
235
  return html, fig_bar, fig_heat, fig_net, "解析完了!"