File size: 9,515 Bytes
2f36052
 
 
 
97568f5
2f36052
 
3e1452c
2f36052
 
 
3e1452c
 
2f36052
a7cb589
 
 
 
 
 
 
 
 
 
 
 
 
 
97568f5
 
 
 
 
 
 
 
 
a7cb589
97568f5
 
a7cb589
 
 
97568f5
a7cb589
97568f5
a7cb589
 
 
 
 
2f36052
3e1452c
2f36052
 
 
 
 
 
 
 
e7cb59a
2f36052
 
 
 
 
 
97568f5
 
2f36052
 
 
 
 
47c5bfd
97568f5
47c5bfd
97568f5
 
 
2f36052
f7d5c33
cf8a69c
2f36052
3e1452c
cf8a69c
 
 
97568f5
9e2bc99
97568f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
970f3bc
cf8a69c
97568f5
970f3bc
97568f5
 
970f3bc
97568f5
4c7d8a6
97568f5
 
 
 
 
 
 
 
 
970f3bc
97568f5
 
 
47c5bfd
 
970f3bc
97568f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
970f3bc
97568f5
 
 
3e1452c
47c5bfd
97568f5
970f3bc
f7d5c33
97568f5
 
 
 
 
97aee79
a7cb589
97568f5
 
 
 
 
 
 
 
 
b919e7d
97568f5
 
 
d7696b8
97568f5
 
 
 
 
cf8a69c
b919e7d
97568f5
b919e7d
47c5bfd
2dd8f7d
 
97568f5
 
 
 
 
 
3e1452c
97568f5
 
9e2bc99
97568f5
 
 
 
 
 
 
2f36052
3e1452c
45a7450
e63418f
 
97568f5
47c5bfd
97568f5
 
 
 
 
 
a7cb589
97568f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6f1a9e
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
import pandas as pd
import networkx as nx
import plotly.graph_objects as go
import gradio as gr
import re
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
data.columns = data.columns.str.strip().str.lower()
logger.info(f"Standardized Column Names: {data.columns.tolist()}")

# Identify the valuation column
valuation_columns = [col for col in data.columns if 'valuation' in col.lower()]
if len(valuation_columns) != 1:
    logger.error("Unable to identify a single valuation column.")
    raise ValueError("Dataset should contain exactly one column with 'valuation' in its name.")

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')
data = data.apply(lambda col: col.str.strip() if col.dtype == "object" else col)
data.rename(columns={
    "company": "Company",
    "date_joined": "Date_Joined",
    "country": "Country",
    "city": "City",
    "industry": "Industry",
    "select_investors": "Select_Investors"
}, inplace=True)

logger.info("Data cleaned and columns renamed.")

# Build investor-company mapping
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:
                    mapping.setdefault(investor, []).append(company)
    return mapping

investor_company_mapping = build_investor_company_mapping(data)
logger.info("Investor to company mapping created.")

# Filter investors by country, industry, investor selection, and company selection
def filter_investors(selected_country, selected_industry, selected_investors, selected_company):
    filtered_data = data.copy()
    if selected_country != "All":
        filtered_data = filtered_data[filtered_data["Country"] == selected_country]
    if selected_industry != "All":
        filtered_data = filtered_data[filtered_data["Industry"] == selected_industry]
    if selected_investors:
        pattern = '|'.join([re.escape(inv) for inv in selected_investors])
        filtered_data = filtered_data[filtered_data["Select_Investors"].str.contains(pattern, na=False)]
    if selected_company != "All":
        filtered_data = filtered_data[filtered_data["Company"] == selected_company]

    investor_company_mapping_filtered = build_investor_company_mapping(filtered_data)
    filtered_investors = list(investor_company_mapping_filtered.keys())
    return filtered_investors, filtered_data

# Generate Plotly graph
def generate_graph(investors, filtered_data):
    if not investors:
        logger.warning("No investors selected.")
        return go.Figure()

    # Create a color map for investors
    unique_investors = investors
    num_colors = len(unique_investors)
    color_palette = [
        "#377eb8",  # Blue
        "#e41a1c",  # Red
        "#4daf4a",  # Green
        "#984ea3",  # Purple
        "#ff7f00",  # Orange
        "#ffff33",  # Yellow
        "#a65628",  # Brown
        "#f781bf",  # Pink
        "#999999",  # Grey
    ]
    while num_colors > len(color_palette):
        color_palette.extend(color_palette)
    
    investor_color_map = {investor: color_palette[i] for i, investor in enumerate(unique_investors)}
    
    G = nx.Graph()
    for investor in investors:
        companies = filtered_data[filtered_data["Select_Investors"].str.contains(re.escape(investor), na=False)]["Company"].tolist()
        for company in companies:
            G.add_node(company)
            G.add_node(investor)
            G.add_edge(investor, company)
    
    pos = nx.spring_layout(G, seed=142)
    edge_x = []
    edge_y = []
    
    for edge in G.edges():
        x0, y0 = pos[edge[0]]
        x1, y1 = pos[edge[1]]
        edge_x.extend([x0, x1, None])
        edge_y.extend([y0, y1, None])
    
    edge_trace = go.Scatter(
        x=edge_x,
        y=edge_y,
        line=dict(width=0.5, color='#aaaaaa'),
        hoverinfo='none',
        mode='lines'
    )
    
    node_x = []
    node_y = []
    node_text = []
    node_color = []
    node_size = []
    node_hovertext = []
    
    for node in G.nodes():
        x, y = pos[node]
        node_x.append(x)
        node_y.append(y)
        if node in investors:
            node_text.append(node)
            node_color.append(investor_color_map[node])
            node_size.append(30)
            node_hovertext.append(f"Investor: {node}")
        else:
            valuation = filtered_data.loc[filtered_data["Company"] == node, "Valuation_Billions"].values
            industry = filtered_data.loc[filtered_data["Company"] == node, "Industry"].values
            if len(valuation) > 0 and not pd.isnull(valuation[0]):
                size = valuation[0] * 5
                if size < 10:
                    size = 10
            else:
                size = 15
            node_size.append(size)
            node_text.append("")
            node_color.append("#a6d854")
            hovertext = f"Company: {node}"
            if len(industry) > 0 and not pd.isnull(industry[0]):
                hovertext += f"<br>Industry: {industry[0]}"
            if len(valuation) > 0 and not pd.isnull(valuation[0]):
                hovertext += f"<br>Valuation: ${valuation[0]}B"
            node_hovertext.append(hovertext)
    
    node_trace = go.Scatter(
        x=node_x,
        y=node_y,
        text=node_text,
        mode='markers+text',
        hoverinfo='text',
        hovertext=node_hovertext,
        marker=dict(
            showscale=False,
            size=node_size,
            color=node_color,
            line=dict(width=0.5, color='#333333')
        ),
        textposition="middle center",
        textfont=dict(size=15, color="#000000")
    )
    
    legend_items = []
    for investor in unique_investors:
        legend_items.append(
            go.Scatter(
                x=[None],
                y=[None],
                mode='markers',
                marker=dict(
                    size=14,
                    color=investor_color_map[investor]
                ),
                legendgroup=investor,
                showlegend=False,
                name=investor
            )
        )
    
    fig = go.Figure(data=legend_items + [edge_trace, node_trace])
    fig.update_layout(
        title="Venture Capital Networks in September 2024",
        titlefont_size=24,
        margin=dict(l=20, r=20, t=20, b=20),
        hovermode='closest',
        width=1400,
        height=1000
    )
    
    fig.update_layout(
        autosize=True,
        xaxis={'showgrid': False, 'zeroline': False, 'visible': False},
        yaxis={'showgrid': False, 'zeroline': False, 'visible': False}
    )
    
    return fig

# Gradio app
def app(selected_country, selected_industry, selected_company, selected_investors):
    investors, filtered_data = filter_investors(selected_country, selected_industry, selected_investors, selected_company)
    if not investors:
        return "No investors found with the selected filters.", go.Figure()
    graph = generate_graph(investors, filtered_data)
    return ', '.join(investors), graph

# Main function
def main():
    country_list = ["All"] + sorted(data["Country"].dropna().unique())
    industry_list = ["All"] + sorted(data["Industry"].dropna().unique())
    company_list = ["All"] + sorted(data["Company"].dropna().unique())
    investor_list = sorted(investor_company_mapping.keys())
    
    with gr.Blocks(title="Venture Networks Visualization") as demo:
        gr.Markdown("""
        # Venture Networks Visualization
        Explore the connections between investors and companies in the venture capital ecosystem. Use the filters below to customize the network graph.
        """)
        with gr.Row():
            country_filter = gr.Dropdown(choices=country_list, label="Country", value="All")
            industry_filter = gr.Dropdown(choices=industry_list, label="Industry", value="All")
            company_filter = gr.Dropdown(choices=company_list, label="Company", value="All")
            investor_filter = gr.Dropdown(choices=investor_list, label="Select Investors", value=[], multiselect=True)
        graph_output = gr.Plot(label="Network Graph")
        
        inputs = [country_filter, industry_filter, company_filter, investor_filter]
        outputs = [investor_output, graph_output]
        
        country_filter.change(app, inputs, outputs)
        industry_filter.change(app, inputs, outputs)
        company_filter.change(app, inputs, outputs)
        investor_filter.change(app, inputs, outputs)
        
        gr.Markdown("""
        **Instructions:** Use the dropdowns to filter the network graph.
        """)
    
    demo.launch()

if __name__ == "__main__":
    main()