Spaces:
Running
Running
File size: 10,972 Bytes
2f36052 77dc67f 9327810 77dc67f 9327810 a165958 45a7450 e63418f b951dc3 75a7b9a b951dc3 9327810 75a7b9a b951dc3 77dc67f b951dc3 77dc67f 9327810 77dc67f 9327810 77dc67f 9327810 77dc67f 9327810 77dc67f 01ca6ce 77dc67f 75a7b9a 77dc67f 75a7b9a 77dc67f 75a7b9a 77dc67f 75a7b9a 77dc67f 9327810 b951dc3 9327810 45a7450 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
import pandas as pd
import networkx as nx
import plotly.graph_objects as go
import gradio as gr
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load and preprocess the dataset
file_path = "cbinsights_data.csv" # Replace with your actual file path
try:
data = pd.read_csv(file_path, skiprows=1)
logger.info("CSV file loaded successfully.")
except FileNotFoundError:
logger.error(f"File not found: {file_path}")
raise
except Exception as e:
logger.error(f"Error loading CSV file: {e}")
raise
# Standardize column names: strip whitespace and convert to lowercase
data.columns = data.columns.str.strip().str.lower()
logger.info(f"Standardized Column Names: {data.columns.tolist()}")
# Identify the valuation column dynamically
valuation_columns = [col for col in data.columns if 'valuation' in col.lower()]
if not valuation_columns:
logger.error("No column containing 'Valuation' found in the dataset.")
raise ValueError("Data Error: Unable to find the valuation column. Please check your CSV file.")
elif len(valuation_columns) > 1:
logger.error("Multiple columns containing 'Valuation' found in the dataset.")
raise ValueError("Data Error: Multiple valuation columns detected. Please ensure only one valuation column exists.")
else:
valuation_column = valuation_columns[0]
logger.info(f"Using valuation column: {valuation_column}")
# Clean and prepare data
data["valuation_billions"] = data[valuation_column].replace({'\$': '', ',': ''}, regex=True)
data["valuation_billions"] = pd.to_numeric(data["valuation_billions"], errors='coerce')
logger.info("Valuation data cleaned and converted to numeric.")
# Strip whitespace from all string columns
data = data.apply(lambda col: col.str.strip() if col.dtype == "object" else col)
logger.info("Whitespace stripped from all string columns.")
# Rename columns for consistency
expected_columns = {
"company": "Company",
"valuation_billions": "Valuation_Billions",
"date_joined": "Date_Joined",
"country": "Country",
"city": "City",
"industry": "Industry",
"select_investors": "Select_Investors"
}
missing_columns = set(expected_columns.keys()) - set(data.columns)
if missing_columns:
logger.error(f"Missing columns in the dataset: {missing_columns}")
raise ValueError(f"Data Error: Missing columns {missing_columns} in the dataset.")
data = data.rename(columns=expected_columns)
logger.info("Columns renamed for consistency.")
# Parse the "Select_Investors" column to map investors to companies
def build_investor_company_mapping(df):
mapping = {}
for _, row in df.iterrows():
company = row["Company"]
investors = row["Select_Investors"]
if pd.notnull(investors):
for investor in investors.split(","):
investor = investor.strip()
if investor: # Ensure investor is not an empty string
mapping.setdefault(investor, []).append(company)
return mapping
investor_company_mapping = build_investor_company_mapping(data)
logger.info("Investor to company mapping created.")
# Function to filter investors based on selected country and industry
def filter_investors_by_country_and_industry(selected_country, selected_industry):
filtered_data = data.copy()
logger.info(f"Filtering data for Country: {selected_country}, Industry: {selected_industry}")
if selected_country != "All":
filtered_data = filtered_data[filtered_data["Country"] == selected_country]
logger.info(f"Data filtered by country: {selected_country}. Remaining records: {len(filtered_data)}")
if selected_industry != "All":
filtered_data = filtered_data[filtered_data["Industry"] == selected_industry]
logger.info(f"Data filtered by industry: {selected_industry}. Remaining records: {len(filtered_data)}")
investor_company_mapping_filtered = build_investor_company_mapping(filtered_data)
# Calculate total valuation per investor
investor_valuations = {}
for investor, companies in investor_company_mapping_filtered.items():
total_valuation = filtered_data[filtered_data["Company"].isin(companies)]["Valuation_Billions"].sum()
if total_valuation >= 20: # Investors with >= 20B total valuation
investor_valuations[investor] = total_valuation
logger.info(f"Filtered investors with total valuation >= 20B: {len(investor_valuations)}")
return list(investor_valuations.keys()), filtered_data
# Function to generate the Plotly graph
def generate_graph(selected_investors, filtered_data):
if not selected_investors:
logger.warning("No investors selected. Returning empty figure.")
return go.Figure()
investor_company_mapping_filtered = build_investor_company_mapping(filtered_data)
filtered_mapping = {inv: investor_company_mapping_filtered[inv] for inv in selected_investors if inv in investor_company_mapping_filtered}
logger.info(f"Generating graph for {len(filtered_mapping)} investors.")
# Build the graph
G = nx.Graph()
for investor, companies in filtered_mapping.items():
for company in companies:
G.add_edge(investor, company)
# Generate positions using spring layout
pos = nx.spring_layout(G, k=0.2, seed=42)
# Prepare Plotly traces
edge_x = []
edge_y = []
for edge in G.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
edge_x += [x0, x1, None]
edge_y += [y0, y1, None]
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines'
)
node_x = []
node_y = []
node_text = []
node_size = []
node_color = []
customdata = []
for node in G.nodes():
x, y = pos[node]
node_x.append(x)
node_y.append(y)
if node in filtered_mapping:
node_text.append(f"Investor: {node}")
node_size.append(20) # Investors have larger size
node_color.append('orange')
customdata.append(None) # Investors do not have a single valuation
else:
valuation = filtered_data.loc[filtered_data["Company"] == node, "Valuation_Billions"].sum()
node_text.append(f"Company: {node}<br>Valuation: ${valuation}B")
node_size.append(10 + (valuation / filtered_data["Valuation_Billions"].max()) * 30 if filtered_data["Valuation_Billions"].max() else 10)
node_color.append('green')
customdata.append(f"${valuation}B")
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
text=node_text,
customdata=customdata,
marker=dict(
showscale=False,
colorscale='YlGnBu',
color=node_color,
size=node_size,
line_width=2
)
)
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='Venture Network Visualization',
titlefont_size=16,
showlegend=False,
hovermode='closest',
margin=dict(b=20,l=5,r=5,t=40),
annotations=[ dict(
text="",
showarrow=False,
xref="paper", yref="paper") ],
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False))
)
fig.update_traces(marker=dict(line=dict(width=0.5, color='white')), selector=dict(mode='markers'))
logger.info("Plotly graph generated successfully.")
return fig
# Gradio app function to update CheckboxGroup and filtered data
def app(selected_country, selected_industry):
investor_list, filtered_data = filter_investors_by_country_and_industry(selected_country, selected_industry)
logger.info("Updating CheckboxGroup and filtered data holder.")
# Use gr.update() to create an update dictionary for the CheckboxGroup
return gr.update(
choices=investor_list,
value=investor_list,
visible=True
), filtered_data
# Gradio Interface
def main():
country_list = ["All"] + sorted(data["Country"].dropna().unique())
industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
# Ensure the default values for dropdowns exist
default_country = "United States" if "United States" in country_list else "All"
default_industry = "Enterprise Tech" if "Enterprise Tech" in industry_list else "All"
logger.info(f"Available countries: {country_list}")
logger.info(f"Available industries: {industry_list}")
with gr.Blocks() as demo:
with gr.Row():
# Set default value for country and industry dropdowns
country_filter = gr.Dropdown(choices=country_list, label="Filter by Country", value=default_country)
industry_filter = gr.Dropdown(choices=industry_list, label="Filter by Industry", value=default_industry)
filtered_investor_list = gr.CheckboxGroup(choices=[], label="Select Investors", visible=False)
graph_output = gr.Plot(label="Venture Network Graph")
valuation_display = gr.Markdown(value="Click on a company node to see its valuation.", label="Company Valuation")
filtered_data_holder = gr.State()
# Event handlers for filters
country_filter.change(
app,
inputs=[country_filter, industry_filter],
outputs=[filtered_investor_list, filtered_data_holder]
)
industry_filter.change(
app,
inputs=[country_filter, industry_filter],
outputs=[filtered_investor_list, filtered_data_holder]
)
# Generate graph when investors are selected
filtered_investor_list.change(
generate_graph,
inputs=[filtered_investor_list, filtered_data_holder],
outputs=graph_output
)
# Handle plot click to display valuation
def display_valuation(plotly_event):
if not plotly_event or "points" not in plotly_event or not plotly_event["points"]:
return "Click on a company node to see its valuation."
point_data = plotly_event["points"][0]
if "customdata" in point_data and point_data["customdata"]:
return f"**Valuation:** {point_data['customdata']}"
return "Click on a company node to see its valuation."
graph_output.events().on_click(
fn=display_valuation,
inputs=[graph_output],
outputs=valuation_display
)
demo.launch()
if __name__ == "__main__":
main()
|