SQL代写:CSE6242 End to end analysis

使用各种Argo-Lite, SQLite, D3, Open Refine, Flask等工具,对数据进行可视化分析。

Open Refine

Homework Overview

Vast amounts of digital data are generated each day, but raw data are often not immediately “usable”. Instead, we are interested in the information content of the data: what patterns are captured? This assignment covers a few useful tools for acquiring, cleaning, storing, and visualizing datasets.

In Question 1 (Q1), you will collect data using an API for The Movie Database (TMDb). You will construct a graph representation of this data that will show which actors have acted together in various movies, and use

Argo Lite to visualize this graph and highlight patterns that you find. This exercise demonstrates how visualizing and interacting with data can help with discovery.

In Q2, you will construct a TMDb database in SQLite, with tables capturing information such as how well each movie did, which actors acted in each movie, and what the movie was about. You will also partition and combine information in these tables in order to more easily answer questions such as “which actors acted in the highest number of movies?”.

In Q3, you will visualize temporal trends in movie releases, using a JavaScript-based library called D3. This part will show how creating interactive rather than static plots can make data more visually appealing, engaging and easier to parse.

Data analysis and visualization is only as good as the quality of the input data. Real-world data often contain missing values, invalid fields, or entries that are not relevant or of interest. In Q4, you will use OpenRefine to clean data from Mercari, and construct GREL queries to filter the entries in this dataset.

Finally, in Q5, you will build a simple web application that displays a table of TMDb data on a single-page website. To do this, you will use Flask, a Python framework for building web applications that allows you to connect Python data processing on the back end with serving a site that displays these results.

Q1 Collect data from TMDb and visualize co-actor network

Q1.1 Collect data from TMDb and build a graph

For this Q1.1, you will use and submit a Python file. Complete all tasks according to the instructions found in submission.py to complete the Graph class, the TMDbAPIUtils class, and the two global functions. The Graph class will serve as a re-usable way to represent and write out your collected graph data. The TMDbAPIUtils class will be used to work with the TMDB API for data retrieval.

Tasks and point breakdown

  • a) Implementation of the Graph class according to the instructions in submission.py. Note that the graph is undirected, thus {a, b} and {b, a} refer to the same undirected edge in the graph; keep only either {a, b} or {b, a} in the Graph object. A node’s degree is the number of (undirected) edges incident on it. In-degrees and out-degrees are not defined for undirected graphs.
  • b) Implementation of the TMDbAPIUtils class according to the instructions in submission.py. You will use version 3 of the TMDb API to download data about actors and their co-actors. To use the API:
    • Create a TMDb account and follow the instructions on this document to obtain an authentication token.
    • Refer to the TMDB API Documentation as you work on this question. The documentation contains a helpful ‘try-it-out’ feature for interacting with the API calls. - c) Producing correct nodes.csv and edges.csv. You must upload your nodes.csv and edges.csv files to Argo Lite as directed in Q1.2.
    • Reminder: as mentioned in the Python file, if an actor name has comma characters (“,”), remove those characters before writing that name into the csv files (so Argo Lite can parse that name correctly in Q1.2).

Q1.2 Visualizing a graph of co-actors using Argo-Lite

Using Argo Lite, visualize a network of actors and their co-actors. You will produce an Argo Lite graph snapshot using your edges.csv and nodes.csv from Q1.1.c.

Tasks and point breakdown

  • a. To get started, review Argo Lite’s readme on GitHub. Argo Lite has been open-sourced.
  • b. Importing your Graph
    • Launch Argo Lite
    • From the menu bar, click ‘Graph’ ‘Import CSV’. In the dialogue that appears:
      • Select ‘I have both nodes and edges file’
    • Under Nodes, use ‘Choose File’ to select nodes.csv from your computer
      • Leave ‘Has Headers’ selected o Verify ‘Column for Node ID’ is ‘id’
    • Under Edges, use ‘Choose File’ to select edges.csv from your computer
      • Verify ‘Column for Source ID’ is ‘source’
      • Select ‘Column for Target ID’ to ‘target’
      • Verify ‘Selected Delimiter’ is ‘,’
    • At the bottom of the dialogue, verify that ‘After import, show’ is set to ‘All Nodes’
    • Dragging a node will ‘pin’ it, freezing its position. Double-clicking a pinned node unpins it, so its position will once again be computed by the graph layout algorithm. Experiment with pinning and unpinning nodes.

Argo Lite before reattempting to import any revised .csv files. Errors not recognized by Argo Lite could cause it to become un-responsive. If you suspect this is the case, open the developer tools for your browser and review any console error messages.

  • c. Setting graph display options
    • On “Graph Options” panel, under ‘Nodes’ ‘Modifying All Nodes’, expand ‘Color’ menu o
      • Select Color by ‘degree’, with scale: ‘Linear Scale’
      • Select a color gradient of your choice that will assign lighter colors to nodes with higher node degrees, and darker colors to nodes with lower degrees
    • Collapse the ‘Color’ options, expand the ‘Size’ options.
      • Select ‘Scale by’ to ‘degree’, with scale: Linear Scale’
      • Select meaningful Size Range values of your choice or use the default range.
    • Collapse the ‘Size’ options
    • On the Menu, click ‘Tools’ ‘Data Sheet’
    • On the ‘Data Sheet’ dialogue:
      • Click ‘Hide All’
      • Set ‘10 more nodes with highest degree’
      • Click ‘Show’ and then close the ‘Data Sheet’ dialogue
      • Close the dialogue
    • Click and drag a rectangle selection around the visible nodes
    • With the nodes selected, configure their node visibility by setting the following:
      • Go to ‘Graph Options’ ‘Labels’
      • Click ‘Show Selected’ (if you see ‘Hide Selected’, click it to update it to ‘Show Selected’)
      • At the bottom of the menu, select ‘Label By’ to ‘name’
      • Adjust the ‘Label Length’ so that the full text of the actor name is displayed
    • Show only non-leaf vertices. On the Menu, click ‘Tools’ Data Sheet ‘Show k More Nodes with Highest Degree’. (where k is the input number of nodes such that only nodes with a degree > 1 are visible). To make this easier, we suggest writing a utility function in your Graph class to find the count of leaf nodes in order to determine how many nodes should be shown.
    • Show the labels of at least 10 nodes, ensuring that labels are shown for the 5 nodes with the highest degree. Do not show all of the node labels since it can create extreme visual complexity.

The result of this workflow yields a graph design with the sizing and coloring depend upon the node degree, and the nodes with the highest degrees are emphasized by showing their labels.

If you want to save your Argo Lite graph visualization snapshot locally to your device, so you can continue working on it later, we recommend the following workflow.

  • Select ‘Graph’ ‘Save Snapshot’
    • In the ‘Save Snapshot` dialog, click ‘Copy to Clipboard’
    • Open an external text editor program such as TextEdit or Notepad. Paste the clipboard contents of the graph snapshot, and save it to a file with a .json extension. You should be able to accomplish this with a default text editor on your computer by overriding the default file extension and manually entering ‘.json’.
    • You may save your progress by saving the snapshot and loading them into Argo Lite to continue your work.
  • To load a snapshot, choose ‘Graph’ ‘Open Snapshot’
  • Select the graph snapshot you created.
    • d. Publish and share your graph snapshot
  • Name your graph: On the top navigation bar, click on the label ‘Untitled Graph’. In the ‘Rename Snapshot’ dialogue window that appears, enter your GTUsername as the ‘Snapshot Name’ and click ‘Done’
  • Select ‘Graph ‘ ‘Publish and Share Snapshot’ ‘Continue’
  • Click ‘Copy to Clipboard’ to copy the generated shapshot URL
  • Return the URL in the return_argo_lite_snapshot() function in submission.py


SQLite is a lightweight, serverless, embedded database that can easily handle multiple gigabytes of data. It is one of the world’s most popular embedded database systems. It is convenient to share data stored in an SQLite database just one cross-platform file which does not need to be parsed explicitly (unlike CSV files, which must be parsed).

You will modify the given Q2_SQL.py file by adding SQL statements to it. We suggest that you consider testing your SQL locally on your computer using interactive tools to speed up testing and debugging, such as DB Browser for SQLite (https://sqlitebrowser.org).

a. Create tables and import data.

  • i. Create two tables (via two separate methods, part_ai_1 and part_ai_2, respectively in Q2_SQL.py) named movies and movie_cast with columns having the indicated data types:
    • movies
      • id (integer)
      • title (text)
      • score (real)
    • movie_cast
      • movie_id (integer)
      • cast_id (integer)
      • cast_name (text)
      • birthday (text)
      • popularity (real)
  • ii. Import the provided movies.csv file into the movies table and movie_cast.csv into the movie_cast table
    • You will write Python code that imports the .csv files into the individual tables. This will include looping though the file and using the ‘INSERT INTO’ SQL command. You must only use relative paths while importing files since absolute/local paths are specific locations that exist only on your computer and will cause the auto-grader to fail.
  • iii. Vertical Database Partitioning. Database partitioning is an important technique that divides large tables into smaller tables, which may help speed up queries. Create a new table cast_bio from the movie_cast table (i.e., columns in cast_bio will be a subset of those in movie_cast). Do not edit the movie_cast table. Be sure that the values are unique when inserting into the new cast_bio table. Read this page for an example of vertical database partitioning.


  1. cast_id (integer)
  2. cast_name (text)
  3. birthday (text)
  4. popularity (real)

b. Create indexes.

Create the following indexes. Indexes increase data retrieval speed; though the speed improvement may be negligible for this small database, it is significant for larger databases.

  1. movie_index for the id column in movies table
  2. cast_index for the cast_id column in movie_cast table
  3. cast_bio_index for the cast_id column in cast_bio table

c. Calculate a proportion.

Find the proportion of movies having both a score > 50 and the substring ‘war’ in the name. Note that the ‘war’ search should be case-insensitive. Treat each row as a different movie. The proportion should be calculated as a percentage and should only be based on the total number of rows in the movie table. Format all decimals to two places using printf(). Do NOT use the ROUND() function as in some rare cases it works differently on different platforms.

d. Find the most prolific actors.

List 5 cast members with the highest number of movie appearances that have a popularity > 10. Sort the results by the number of appearances in descending order, then by cast_name in alphabetical order.

e. Find the highest scoring movies with the smallest cast.

List the 5 highest-scoring movies that have the fewest cast members. Sort the intermediate result by score in descending order, then by number of cast members in ascending order, then by movie name in alphabetical order. Format all decimals to two places using printf().

f. Get high scoring actors.

Find the top ten cast members who have the highest average movie scores. Format all decimals to two decimal places using printf().

  • Sort the output by average score in descending order, then by cast_name in alphabetical order.
  • First exclude movies with score < 25 in the average score calculation.
  • Next exclude cast members who have appeared in two or fewer movies.

g. Creating views. Create a view (virtual table) called good_collaboration that lists pairs of actors who have had a good collaboration as defined here. Each row in the view describes one pair of actors who appeared in at least 3 movies together AND the average score of these movies is >= 40.

For symmetrical or mirror pairs, only keep the row in which cast_member_id1 has a lower numeric value. For example, for ID pairs (1, 2) and (2, 1), keep the row with IDs (1, 2). There should not be any “self pair” where the value of cast_member_id1 is the same as that of cast_member_id2.

Remember that creating a view will not produce any output, so you should test your view with a few simple select statements during development. One such test has already been added to the code as part of the auto-grading.

i. Find the best collaborators.

Get the 5 cast members with the highest average scores from the good_collaboration view, and call this score the collaboration_score. This score is the average of the average_movie_score corresponding to each cast member, including actors in cast_member_id1 as well as cast_member_id2. Format all decimals to two places using printf().

  • Order your output by the printf-formatted collaboration_score in descending order, then by cast_name alphabetically.

h. SQLite supports simple but powerful Full Text Search (FTS) for fast text-based querying (FTS documentation). Import movie overview data from the movie_overview.csv into a new FTS table called movie_overview with the schema.

Q3 D3 (v5) Warmup

Read chapters 4-8 of Scott Murray’s Interactive Data Visualization for the Web, 2nd edition. You may also briefly review chapters 1-3 if you need additional background on web development. This simple reading provides important foundation you will need for Homework 2. This question uses D3 version v5, while the book covers D3 v4. What you learn from the book is transferable to v5. In Homework 2, you will work with D3 extensively.

  1. You will need to setup an HTTP server to run your D3 visualizations as discussed in the D3 lecture (OMS students: the video “Week 5 - Data Visualization for the Web (D3) - Prerequisites: JavaScript and SVG”. Campus students: see lecture PDF.). The easiest way is to use http.server for Python 3.x. Run your local HTTP server in the hw1-skeleton/Q3 folder.
  2. We have provided sections of code along with comments in the skeleton to help you complete the implementation. While you do not need to remove them, you may need to write additional code to make things work.
  3. All d3*.js files in the lib folder are referenced using relative paths in your html file. For example, since the file “Q3/submission.html” uses d3.
  4. In your html / js code, use a relative path to read the dataset file. For example, since Q3 requires reading data from the q3.csv file, the path must be “q3.csv” and NOT an absolute path such as “C:/Users/polo/HW1skeleton/Q3/q3.csv”. Absolute (local) paths are specific locations that exist only on your computer, which means your code will NOT run on our machines when we grade (and you will lose points). Gradescope will provide a copy of the q3.csv dataset using the same directory structure provided in the HW skeleton.
  5. Load the data from q3.csv using D3 fetch methods. We recommend d3.dsv(). Handle any data conversions that might be needed, e.g., strings that need to be converted to integer.
  6. IMPORTANT: use the Margin Convention guide for specifying chart dimensions and layout. The autograder will assume this convention has been followed for grading purposes. submission.html : when run in a browser, it should display a vertical barplot with the following specifications:
    • a. The barplot must display one bar per row in the q3.csv dataset. Each bar corresponds to the running total of movies for a given year. The height of each bar represents the running total.
    • b. The bars must have the same fixed width, and there must be some space between two bars, so that the bars do not overlap.
    • c. The plot must have visible X and Y axes that scale according to the generated bars. That is, the axes are driven by the data that they are representing. Likewise, the ticks on these axes must adjust automatically based on the values within the datasets, i.e., they must not be hard-coded.
    • d. Set x-axis label to ‘Year’ and y-axis label to ‘Running Total’. The x-axis label must be a [text] element having the id: “x_axis_label” and the y-axis label must be a [text] element having the id: “y_axis_label”.
    • e. Use a linear scale for the Y axis to represent the running total (recommended function: d3.scaleLinear()).
    • f. Use a time scale for the x-axis to represent year (recommended function: d3.scaleTime()). It may be necessary to use time parsing / formatting when you load and display the year data. The axis would be overcrowded if you display every year value so set the xaxis ticks to display one tick for every 10 years.
    • g. Set the HTML title tag and display a title for the plot. Set the HTML title tag (i.e., [title] Running Total of TMDb Movies by Year [/title]). Position the title “Running Total of TMDb Movies by Year” above the barplot. The title must be a [text] element having the id: “title”
    • h. Add your GT username (usually includes a mix of letters and numbers) to the area beneath the bottom-right of the plot (see example image). The GT username must be a [text] element having the id: “credit”
  7. Gradescope will render your plot using Chrome and present you with a Dropbox link to view the screenshot of your plot with the solution plot in both a side-by-side and an overlay display.
    The visual feedback helps you make adjustments and identify errors, e.g., a blank plot likely indicates a serious error. It is not necessary that your design replicates the solution plot.

Q4 OpenRefine

OpenRefine is a Java application and requires Java JRE to run. Download and install Java if you do not have it (you can verify by typing ‘java -version’ in your computer’s terminal or command prompt).

Watch the videos on OpenRefine’s homepage for an overview of its features. Then, download and install OpenRefine release 3.3. The direct link to release 3.3 is https://github.com/OpenRefine/OpenRefine/releases/tag/3.3.

  • a. Import Dataset
    • Run OpenRefine and point your browser at
    • We use a products dataset from Mercari, derived from a Kaggle competition (Mercari Price Suggestion Challenge). If you are interested in the details, visit the data description page. We have sampled a subset of the dataset provided as “properties.csv”.
    • Choose “Create Project” This Computer properties.csv”. Click “Next”.
    • You will now see a preview of the data. Click “Create Project” at the upper right corner
  • b. Clean/Refine the data
    • i. Select the category_name column and choose ‘Facet by Blank’ (Facet Customized
      Facets Facet by blank) to filter out the records that have blank values in this column. Provide the number of rows that return True in Q4Observations.txt. Exclude these rows.
    • ii. Split the column category_name into multiple columns without removing the original column. For example, a row with “Kids/Toys/Dolls & Accessories” in the category_name column would be split across the newly created columns as “Kids”, “Toys” and “Dolls & Accessories”. Use the existing functionality in OpenRefine that creates multiple columns from an existing column based on a separator (i.e., in this case ‘/‘) and does not remove the original category_name column. Provide the number of new columns that are created by this operation, excluding the original category_name column.
    • iii. Select the column name and apply the Text Facet (Facet Text Facet). Cluster by using (Edit Cells Cluster and Edit ) this opens a window where you can choose different “methods” and “keying functions” to use while clustering. Choose the keying function that produces the smallest number of clusters under the “Key Collision” method. Click ‘Select All’ and ‘Merge Selected & Close’. Provide the name of the keying function and the number of clusters that was produced.
    • iv. Replace the null values in the brand_name column with the text “Unknown” (Edit Cells > Transform). Provide the General Refine Evaluation Language (GREL) expression used.
    • v. Create a new column high_priced with the values 0 or 1 based on the “price” column with the following conditions: if the price is greater than 90, high_priced should be set as 1, else 0. Provide the GREL expression used to perform this.
    • vi. Create a new column has_offer with the values 0 or 1 based on the item_description column with the following conditions: If it contains the text “discount” or “offer” or “sale”, then set the value in has_offer as 1, else 0. Provide the GREL expression used to

Q5 Introduction to Python Flask

Flask is a lightweight web application framework written in Python that provides you with tools, libraries and technologies to quickly build a web application. It allows you to scale up your application as needed.

Username()- Update the username() method inside wrangling.py by including your GTUsername.

  • Get started by installing Flask on your machine by running pip install Flask (Note that you can optionally create a virtual environment by following the steps here. Creating a virtual environment is purely optional and can be skipped.)
  • To run the code, you must navigate to the Q5 folder in your terminal/command prompt and execute the following command: python run.py. After running the command go to on your browser. This will open up index.html showing a table in which the rows returned by data_wrangling() are displayed.
  • You must solve the following 2 sub-questions:
    • a. Read the first 100 rows using the data_wrangling() method.
    • b. [3 points]: Sort the table in descending order of the values i.e., with larger values at the top and smaller values at the bottom of the table in the last (3rd) column.