fantastyczne_gole/notebooks/dataExtraction.ipynb

137 lines
3.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# librarys:\n",
"\n",
"import json\n",
"import csv\n",
"import os\n",
"import pandas as pd\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Functions\n",
"\n",
"def openGameFile(name):\n",
" path = os.path.join(\"..\",\"data\",\"la_liga_events\",name)\n",
" f = open(path)\n",
" data = json.load(f)\n",
" return data\n",
"\n",
"# Czyszczenie danych\n",
"# nadpisz \n",
"# True - dane zostana dodane do pliku\n",
"# False - plik zostanie nadpisanu\n",
"#\n",
"def uploadData(inputFile, outputFile, nadpisz):\n",
" try:\n",
" jsonFile = openGameFile(inputFile)\n",
" clean_event = []\n",
" for event in jsonFile:\n",
" if(event[\"type\"][\"name\"]==\"Shot\"):\n",
" clean_event = []\n",
" #time\n",
" clean_event.append(event[\"minute\"])\n",
" #location\n",
" clean_event.append(event[\"position\"][\"name\"])\n",
" # shot type\n",
" clean_event.append(event[\"shot\"][\"type\"][\"name\"])\n",
" #outcome\n",
" clean_event.append(event[\"shot\"][\"outcome\"][\"name\"])\n",
" \n",
" clean_event.append(event[\"shot\"][\"technique\"][\"name\"])\n",
"\n",
" m_players = []\n",
" en_players = []\n",
" keeper = []\n",
" for player in event[\"shot\"][\"freeze_frame\"]:\n",
" player_p = 0\n",
" if player[\"teammate\"] :\n",
" m_players.append(player[\"location\"])\n",
" elif player[\"position\"] == 'Goalkeeper':\n",
" keeper = player['location']\n",
" else:\n",
" en_players.append(player[\"location\"])\n",
" for i in range(0,10):\n",
" if(len(m_players)>i):\n",
" clean_event.append(m_players[i])\n",
" else:\n",
" clean_event.append(\"none\")\n",
"\n",
" for i in range(0,9):\n",
" if(len(en_players)> i):\n",
" clean_event.append(en_players[i])\n",
" else:\n",
" clean_event.append(\"none\")\n",
" \n",
" clean_event.append(keeper)\n",
"\n",
" if(event[\"shot\"][\"outcome\"]==\"Goal\"):\n",
" clean_event.append(1)\n",
" else:\n",
" clean_event.append(0)\n",
" \n",
" \n",
" #Saving to a file\n",
" with open(outputFile, 'a', newline='') as file:\n",
" writer = csv.writer(file)\n",
" writer.writerow(clean_event)\n",
"\n",
" except:\n",
" raise Exception(\"invalid data\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/100\n"
]
}
],
"source": [
"for i in range(1,2):\n",
" print(str(i) + \"/100\")\n",
" name = \" (\" + str(i) + (\").json\")\n",
" uploadData(name,\"test_dataSet.csv\",True)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}