File size: 14,030 Bytes
4aabe49
6220b0b
e3d2388
d0e6aa2
a57a561
c900c6b
 
d0e6aa2
a087647
a8f19d1
4091ace
6220b0b
4091ace
6220b0b
 
 
d0e6aa2
ba02932
b91f0fb
7b1a7ea
 
76674dd
d0e6aa2
 
7b1a7ea
d0e6aa2
 
 
7b1a7ea
d0e6aa2
 
7b1a7ea
d0e6aa2
 
7b1a7ea
d0e6aa2
 
 
ba02932
b91f0fb
76674dd
 
 
 
 
 
 
 
d0e6aa2
0632bea
ba02932
b91f0fb
7b1a7ea
 
 
76674dd
d0e6aa2
 
7b1a7ea
d0e6aa2
f4d646d
d0e6aa2
 
 
 
7b1a7ea
d0e6aa2
 
7b1a7ea
d0e6aa2
 
7b1a7ea
d0e6aa2
 
 
 
7b1a7ea
d0e6aa2
a57a561
 
9583c19
b91f0fb
7b1a7ea
 
76674dd
a57a561
d0e6aa2
a57a561
 
 
 
 
9583c19
 
 
 
 
6220b0b
a57a561
6220b0b
 
 
9583c19
6220b0b
a57a561
ba02932
b91f0fb
7b1a7ea
 
 
76674dd
6220b0b
d0e6aa2
a57a561
 
 
 
 
 
 
 
 
 
 
6220b0b
 
e3d2388
76674dd
ba54088
76674dd
ba54088
 
 
 
 
 
0632bea
c7e58c3
0632bea
1c51a0b
8fa22b4
1550b1f
8fa22b4
1550b1f
a087647
0632bea
 
 
 
 
 
 
 
 
 
0320f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fd87e5
0320f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fd87e5
0632bea
0320f0b
 
655972b
8aa6638
0320f0b
655972b
 
0320f0b
655972b
 
0320f0b
655972b
f694579
 
 
 
 
 
 
b7ae97f
ee3ec14
b7ae97f
ee3ec14
 
 
 
b7ae97f
 
ee3ec14
 
 
b7ae97f
 
1550b1f
655972b
76674dd
ba02932
76674dd
b91f0fb
76674dd
 
 
 
 
 
 
b91f0fb
76674dd
 
b91f0fb
76674dd
 
 
 
b91f0fb
76674dd
 
 
 
 
 
 
 
0632bea
76674dd
7b1a7ea
76674dd
9730dcc
ba02932
 
8fa22b4
7d3ab38
 
7b1a7ea
7d3ab38
 
6a3d6f6
7b1a7ea
7d3ab38
 
6a3d6f6
7b1a7ea
7d3ab38
 
6a3d6f6
7b1a7ea
7d3ab38
ba02932
ded7e4c
12e3ab0
6a3d6f6
7d3ab38
 
 
 
12e3ab0
 
6a3d6f6
f65e0e7
ded7e4c
 
12e3ab0
ba02932
6a3d6f6
7d3ab38
 
 
 
12e3ab0
 
7d3ab38
 
9583c19
12e3ab0
7d3ab38
6a3d6f6
f65e0e7
7b1a7ea
76674dd
4091ace
 
99f87a3
9fd87e5
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import sqlite3
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
import imageio

# Function to load data from SQLite database
def load_data(db_file):
    conn = sqlite3.connect(db_file)
    return conn

# Function to fetch genre movie releases by year
def fetch_genre_movie_releases(conn):
    query = r'''
    SELECT startYear, genres
    FROM title_basics
    WHERE titleType = 'movie' AND startYear != '\N' AND genres != '\N'
    '''
    df = pd.read_sql_query(query, conn)

    # Split genres and explode to separate rows
    df['genres'] = df['genres'].str.split(',')
    df = df.explode('genres')

    # Convert startYear to numeric
    df['startYear'] = pd.to_numeric(df['startYear'])

    # Group by startYear and genre, count the number of movies
    genre_counts = df.groupby(['startYear', 'genres']).size().reset_index(name='count')

    return genre_counts

# Function to fetch data for filled line chart of movie release years
def fetch_movie_release_years(conn):
    query_release_years = r'''
    SELECT startYear, COUNT(*) as count
    FROM title_basics
    WHERE titleType = 'movie' AND startYear != '\N'
    GROUP BY startYear
    ORDER BY startYear
    '''
    df_release_years = pd.read_sql_query(query_release_years, conn)
    return df_release_years

# Function to fetch data and create box plot of average rating by first_genre
def fetch_and_plot_average_rating_by_genre(conn):
    query = r'''
    SELECT tb.tconst, tb.primaryTitle, tr.averageRating, tb.genres
    FROM title_basics tb
    JOIN title_ratings tr ON tb.tconst = tr.tconst
    WHERE tb.titleType = 'movie' AND tb.genres IS NOT NULL AND tb.genres != '\N'
    '''
    df = pd.read_sql_query(query, conn)

    # Function to extract the first genre from the genres list
    def extract_first_genre(genres):
        if genres:
            return genres.split(',')[0].strip()
        else:
            return None

    # Apply the function to extract the first genre
    df['first_genre'] = df['genres'].apply(extract_first_genre)

    # Drop rows where first_genre is None (shouldn't be necessary if genres column is clean)
    df = df.dropna(subset=['first_genre'])

    # Create a box plot of average rating by first_genre
    fig = px.box(df, x='first_genre', y='averageRating',
                 labels={'first_genre': 'Genre', 'averageRating': 'Average Rating'},
                 title='Average Rating of Movies by First Genre')

    return fig

# Function to create word cloud of genres
def create_genre_wordcloud(conn, mask_path):
    query = r'''
    SELECT genres
    FROM title_basics
    WHERE titleType = 'movie' AND genres IS NOT NULL AND genres != '\N'
    '''
    df = pd.read_sql_query(query, conn)

    # Process genres
    genres = df['genres'].str.split(',', expand=True).stack().replace('\\N', pd.NA).dropna().reset_index(drop=True)
    genre_counts = Counter(genres)

    # Load the mask image
    mask = imageio.imread(mask_path)

    # Generate the word cloud with the mask
    wordcloud = WordCloud(width=800, height=400, background_color='white', mask=mask).generate_from_frequencies(genre_counts)

    # Display the word cloud
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis('off')
    st.pyplot(plt.gcf())

# Function to find best movie of each genre by numVotes * averageRating
def find_best_movies_by_genre(conn):
    query = r'''
    SELECT tb.tconst, tb.primaryTitle, tb.startYear, tb.genres, tr.averageRating, tr.numVotes
    FROM title_basics tb
    JOIN title_ratings tr ON tb.tconst = tr.tconst
    WHERE tb.titleType = 'movie' AND tb.genres IS NOT NULL AND tb.genres != '\N'
    '''
    df = pd.read_sql_query(query, conn)

    # Split genres and select the first genre for each movie
    df['genre'] = df['genres'].str.split(',', expand=True)[0]

    # Calculate score based on numVotes * averageRating
    df['score'] = df['numVotes'] * df['averageRating']

    # Get the best movie (highest score) for each genre
    idx = df.groupby('genre')['score'].idxmax()
    best_movies_by_genre = df.loc[idx, ['genre', 'primaryTitle', 'startYear', 'averageRating', 'numVotes', 'score']] \
        .sort_values(by='score', ascending=False).reset_index(drop=True)

    return best_movies_by_genre

# Function to plot stacked area chart of genre movie releases by year using Plotly Express
def plot_stacked_genre_movie_releases(genre_counts):
    fig = px.area(genre_counts, x='startYear', y='count', color='genres',
                  title='Stacked Genre Movie Releases by Year',
                  labels={'startYear': 'Year', 'count': 'Number of Movies', 'genres': 'Genre'},
                  line_group='genres',  # This groups lines by genre
                  hover_name='genres',  # This sets the genre as the hover label
                  hover_data={'count': ':.0f'})  # Format hover data as integer

    return fig

# Function to plot global map of total films per region using Plotly Express

def plot_global_map():
    # Read data from CSV file
    df = pd.read_csv('movie_region.csv')
    
    # Country code to name mapping
    country_mapping = {
        'AF': 'Afghanistan', 'AX': 'Åland Islands', 'AL': 'Albania', 'DZ': 'Algeria', 'AS': 'American Samoa',
        'AD': 'Andorra', 'AO': 'Angola', 'AI': 'Anguilla', 'AQ': 'Antarctica', 'AG': 'Antigua and Barbuda',
        'AR': 'Argentina', 'AM': 'Armenia', 'AW': 'Aruba', 'AU': 'Australia', 'AT': 'Austria',
        'AZ': 'Azerbaijan', 'BS': 'Bahamas', 'BH': 'Bahrain', 'BD': 'Bangladesh', 'BB': 'Barbados',
        'BY': 'Belarus', 'BE': 'Belgium', 'BZ': 'Belize', 'BJ': 'Benin', 'BM': 'Bermuda',
        'BT': 'Bhutan', 'BO': 'Bolivia', 'BA': 'Bosnia and Herzegovina', 'BW': 'Botswana', 'BR': 'Brazil',
        'BN': 'Brunei Darussalam', 'BG': 'Bulgaria', 'BF': 'Burkina Faso', 'BI': 'Burundi', 'KH': 'Cambodia',
        'CM': 'Cameroon', 'CA': 'Canada', 'CV': 'Cape Verde', 'KY': 'Cayman Islands', 'CF': 'Central African Republic',
        'TD': 'Chad', 'CL': 'Chile', 'CN': 'China', 'CO': 'Colombia', 'KM': 'Comoros',
        'CG': 'Congo', 'CD': 'Congo, Democratic Republic of the', 'CK': 'Cook Islands', 'CR': 'Costa Rica', 'HR': 'Croatia',
        'CU': 'Cuba', 'CY': 'Cyprus', 'CZ': 'Czech Republic', 'DK': 'Denmark', 'DJ': 'Djibouti',
        'DM': 'Dominica', 'DO': 'Dominican Republic', 'EC': 'Ecuador', 'EG': 'Egypt', 'SV': 'El Salvador',
        'GQ': 'Equatorial Guinea', 'ER': 'Eritrea', 'EE': 'Estonia', 'ET': 'Ethiopia', 'FJ': 'Fiji',
        'FI': 'Finland', 'FR': 'France', 'GA': 'Gabon', 'GM': 'Gambia', 'GE': 'Georgia',
        'DE': 'Germany', 'GH': 'Ghana', 'GR': 'Greece', 'GL': 'Greenland', 'GD': 'Grenada',
        'GU': 'Guam', 'GT': 'Guatemala', 'GN': 'Guinea', 'GW': 'Guinea-Bissau', 'GY': 'Guyana',
        'HT': 'Haiti', 'VA': 'Holy See (Vatican City State)', 'HN': 'Honduras', 'HK': 'Hong Kong', 'HU': 'Hungary',
        'IS': 'Iceland', 'IN': 'India', 'ID': 'Indonesia', 'IR': 'Iran', 'IQ': 'Iraq',
        'IE': 'Ireland', 'IL': 'Israel', 'IT': 'Italy', 'CI': "Cote d'Ivoire", 'JM': 'Jamaica',
        'JP': 'Japan', 'JO': 'Jordan', 'KZ': 'Kazakhstan', 'KE': 'Kenya', 'KI': 'Kiribati',
        'KP': "Korea, Democratic People's Republic of", 'KR': 'Korea, Republic of', 'KW': 'Kuwait', 'KG': 'Kyrgyzstan', 'LA': "Lao People's Democratic Republic",
        'LV': 'Latvia', 'LB': 'Lebanon', 'LS': 'Lesotho', 'LR': 'Liberia', 'LY': 'Libyan Arab Jamahiriya',
        'LI': 'Liechtenstein', 'LT': 'Lithuania', 'LU': 'Luxembourg', 'MO': 'Macao', 'MK': 'Macedonia, The Former Yugoslav Republic of',
        'MG': 'Madagascar', 'MW': 'Malawi', 'MY': 'Malaysia', 'MV': 'Maldives', 'ML': 'Mali',
        'MT': 'Malta', 'MH': 'Marshall Islands', 'MR': 'Mauritania', 'MU': 'Mauritius', 'YT': 'Mayotte',
        'MX': 'Mexico', 'FM': 'Micronesia', 'MD': 'Moldova, Republic of', 'MC': 'Monaco', 'MN': 'Mongolia',
        'ME': 'Montenegro', 'MS': 'Montserrat', 'MA': 'Morocco', 'MZ': 'Mozambique', 'MM': 'Myanmar',
        'NA': 'Namibia', 'NR': 'Nauru', 'NP': 'Nepal', 'NL': 'Netherlands', 'AN': 'Netherlands Antilles',
        'NC': 'New Caledonia', 'NZ': 'New Zealand', 'NI': 'Nicaragua', 'NE': 'Niger', 'NG': 'Nigeria',
        'NU': 'Niue', 'NF': 'Norfolk Island', 'MP': 'Northern Mariana Islands', 'NO': 'Norway', 'OM': 'Oman',
        'PK': 'Pakistan', 'PW': 'Palau', 'PS': 'Palestinian Territory, Occupied', 'PA': 'Panama', 'PG': 'Papua New Guinea',
        'PY': 'Paraguay', 'PE': 'Peru', 'PH': 'Philippines', 'PN': 'Pitcairn', 'PL': 'Poland',
        'PT': 'Portugal', 'PR': 'Puerto Rico', 'QA': 'Qatar', 'RO': 'Romania', 'RU': 'Russian Federation',
        'RW': 'Rwanda', 'RE': 'Reunion', 'BL': 'Saint Barthelemy', 'SH': 'Saint Helena', 'KN': 'Saint Kitts and Nevis',
        'LC': 'Saint Lucia', 'MF': 'Saint Martin', 'PM': 'Saint Pierre and Miquelon', 'VC': 'Saint Vincent and the Grenadines', 'WS': 'Samoa',
        'SM': 'San Marino', 'ST': 'Sao Tome and Principe', 'SA': 'Saudi Arabia', 'SN': 'Senegal', 'RS': 'Serbia',
        'SC': 'Seychelles', 'SL': 'Sierra Leone', 'SG': 'Singapore', 'SK': 'Slovakia', 'SI': 'Slovenia',
        'SB': 'Solomon Islands', 'SO': 'Somalia', 'ZA': 'South Africa', 'GS': 'South Georgia and the South Sandwich Islands', 'ES': 'Spain',
        'LK': 'Sri Lanka', 'SD': 'Sudan', 'SR': 'Suriname', 'SJ': 'Svalbard and Jan Mayen', 'SZ': 'Swaziland',
        'SE': 'Sweden', 'CH': 'Switzerland', 'SY': 'Syrian Arab Republic', 'TW': 'Taiwan', 'TJ': 'Tajikistan',
        'TZ': 'Tanzania, United Republic of', 'TH': 'Thailand', 'TL': 'Timor-Leste', 'TG': 'Togo', 'TK': 'Tokelau',
        'TO': 'Tonga', 'TT': 'Trinidad and Tobago', 'TN': 'Tunisia', 'TR': 'Turkey', 'TM': 'Turkmenistan',
        'TV': 'Tuvalu', 'UG': 'Uganda', 'UA': 'Ukraine', 'AE': 'United Arab Emirates', 'GB': 'United Kingdom',
        'US': 'United States', 'UY': 'Uruguay', 'UZ': 'Uzbekistan', 'VU': 'Vanuatu', 'VE': 'Venezuela',
        'VN': 'Viet Nam', 'VG': 'Virgin Islands, British', 'VI': 'Virgin Islands, U.S.', 'WF': 'Wallis and Futuna', 'EH': 'Western Sahara',
        'YE': 'Yemen', 'ZM': 'Zambia', 'ZW': 'Zimbabwe'
    }

    # Replace country codes with full country names
    df['region'] = df['region'].map(country_mapping)
    

    # Group by country and count the number of films
    df_grouped = df.groupby('region').size().reset_index(name='total_films')

    # Apply log transformation to handle outliers
    df_grouped['log_total_films'] = np.log1p(df_grouped['total_films'])

    # Create a choropleth map with the log-transformed data
    fig = px.choropleth(
        locations=df_grouped['region'],
        locationmode='country names',
        color=df_grouped['log_total_films'],
        hover_name=df_grouped['region'],
        color_continuous_scale='solar'
    )
    
    # Update layout for title and other attributes
    fig.update_layout(
        title_font=dict(size=28, family='Arial, sans-serif'),
        title_x=0.5,
        title_y=0.95,
        title_text='Total Number of Movies by Country',
        geo=dict(
            showframe=False,
            showcoastlines=True,
            projection_type="natural earth"
        )
    )
    
    return fig

# Function to fetch summary info
def fetch_summary_info(conn):
    # Fetch total count of movies
    query_total_movies = r'''
        SELECT COUNT(*) as total_movies
        FROM title_basics
        WHERE titleType = 'movie'
    '''
    total_movies = pd.read_sql_query(query_total_movies, conn).iloc[0]['total_movies']

    # Fetch total count of years
    query_total_years = r'''
        SELECT COUNT(DISTINCT startYear) as total_years
        FROM title_basics
        WHERE titleType = 'movie' AND startYear IS NOT NULL AND startYear != '\N'
    '''
    total_years = pd.read_sql_query(query_total_years, conn).iloc[0]['total_years']

    # Fetch average rating of movies
    query_avg_rating = r'''
        SELECT AVG(averageRating) as avg_rating
        FROM title_ratings
    '''
    avg_rating = pd.read_sql_query(query_avg_rating, conn).iloc[0]['avg_rating']

    return total_movies, total_years, avg_rating

# Main Streamlit app
def run_app():
    st.title('IMDb Movie Data Analysis')

    # Load data from SQLite database
    conn = load_data('imdb_data.db')
    genre_counts = fetch_genre_movie_releases(conn)
    total_movies, total_years, avg_rating = fetch_summary_info(conn)

    # Layout for summary info in three columns
    col1, col2, col3 = st.columns(3)

    with col1:
        st.subheader('Total Movies')
        st.metric(label='zzz', value=total_movies)

    with col2:
        st.subheader('Total Years of Movie Data')
        st.metric(label='zzz', value=total_years)

    with col3:
        st.subheader('Average Movie Rating')
        st.metric(label='zzz', value=f'{avg_rating:.2f}')

    # Find and display best movies by genre
    best_movies_by_genre = find_best_movies_by_genre(conn)
    fig_global_map = plot_global_map()
    fig_genre_movie_releases = plot_stacked_genre_movie_releases(genre_counts)
    
    # Layout for best movies by genre in two columns
    col1, col2 = st.columns(2)

    with col1:
        st.subheader('Genre Movie Releases by Year')
        st.plotly_chart(fig_genre_movie_releases)

    with col2:
        st.subheader('Global Map of Films')
        st.plotly_chart(fig_global_map, use_container_width=True)

    fig_avg_rating_by_genre = fetch_and_plot_average_rating_by_genre(conn)

    # Layout for Plotly charts in three columns
    col1, col2, col3 = st.columns(3)

    with col1:
        st.subheader('Best Movies by Genre')
        st.dataframe(best_movies_by_genre)

    with col2:
        create_genre_wordcloud(conn, 'mask.png')

    with col3:
        st.subheader('Average Rating by Genre')
        st.plotly_chart(fig_avg_rating_by_genre)

    # Close database connection
    conn.close()

if __name__ == '__main__':
    run_app()