File size: 6,495 Bytes
2f36052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7cb59a
2f36052
 
 
e7cb59a
2f36052
cf8a69c
 
 
 
 
 
2f36052
 
 
 
 
cf8a69c
2f36052
 
 
 
 
 
 
cf8a69c
2f36052
cf8a69c
2f36052
cf8a69c
2f36052
 
 
 
 
 
 
 
e7cb59a
2f36052
 
 
 
 
 
cf8a69c
 
2f36052
 
 
 
 
e7cb59a
2f36052
cf8a69c
 
 
 
 
 
2f36052
e7cb59a
cf8a69c
 
 
970f3bc
 
 
cf8a69c
970f3bc
 
 
 
e7cb59a
cf8a69c
 
 
 
 
 
 
 
 
970f3bc
cf8a69c
 
970f3bc
 
 
 
 
cf8a69c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
970f3bc
 
cf8a69c
 
 
970f3bc
 
 
 
 
 
cf8a69c
 
 
 
 
 
 
970f3bc
 
 
 
cf8a69c
 
 
 
 
 
 
970f3bc
 
e7cb59a
1e9f79e
cf8a69c
 
 
2f36052
45a7450
e63418f
 
cf8a69c
9327810
 
cf8a69c
 
 
e7cb59a
cf8a69c
e7cb59a
 
 
 
 
 
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
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
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",
    "valuation_billions": "Valuation_Billions",
    "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, and valuation threshold
def filter_investors(selected_country, selected_industry, valuation_threshold):
    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]

    investor_company_mapping_filtered = build_investor_company_mapping(filtered_data)
    investor_valuations = {
        investor: filtered_data[filtered_data["Company"].isin(companies)]["Valuation_Billions"].sum()
        for investor, companies in investor_company_mapping_filtered.items()
    }
    filtered_investors = [investor for investor, total in investor_valuations.items() if total >= valuation_threshold]
    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()

    G = nx.Graph()
    for investor in investors:
        companies = filtered_data[filtered_data["Select_Investors"].str.contains(investor, na=False)]["Company"].tolist()
        for company in companies:
            G.add_edge(investor, company)

    pos = nx.spring_layout(G, seed=42)
    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='#888'),
        hoverinfo='none',
        mode='lines'
    )

    node_x = []
    node_y = []
    node_text = []
    node_color = []

    for node in G.nodes():
        x, y = pos[node]
        node_x.append(x)
        node_y.append(y)
        node_text.append(node)
        if node in investors:
            node_color.append(20)  # Fixed color value for investors
        else:
            valuation = filtered_data.loc[filtered_data["Company"] == node, "Valuation_Billions"].sum()
            node_color.append(valuation)

    node_trace = go.Scatter(
        x=node_x,
        y=node_y,
        text=node_text,
        mode='markers',
        hoverinfo='text',
        marker=dict(
            showscale=True,
            colorscale='YlGnBu',
            size=10,
            color=node_color,
            colorbar=dict(
                thickness=15,
                title="Valuation (B)",
                xanchor='left',
                titleside='right'
            )
        )
    )

    fig = go.Figure(data=[edge_trace, node_trace])
    fig.update_layout(
        showlegend=False,
        title="Venture Networks",
        titlefont_size=16,
        margin=dict(l=40, r=40, t=40, b=40),
        hovermode='closest'
    )
    return fig

# Gradio app
def app(selected_country, selected_industry, valuation_threshold):
    investors, filtered_data = filter_investors(selected_country, selected_industry, valuation_threshold)
    graph = generate_graph(investors, filtered_data)
    return investors, graph

def main():
    country_list = ["All"] + sorted(data["Country"].dropna().unique())
    industry_list = ["All"] + sorted(data["Industry"].dropna().unique())

    with gr.Blocks() as demo:
        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")
            valuation_slider = gr.Slider(0, 50, value=20, step=1, label="Valuation Threshold (B)")

        investor_output = gr.Textbox(label="Filtered Investors")
        graph_output = gr.Plot(label="Network Graph")

        country_filter.change(app, [country_filter, industry_filter, valuation_slider], [investor_output, graph_output])
        industry_filter.change(app, [country_filter, industry_filter, valuation_slider], [investor_output, graph_output])
        valuation_slider.change(app, [country_filter, industry_filter, valuation_slider], [investor_output, graph_output])

    demo.launch()

if __name__ == "__main__":
    main()