fantastyczne_gole/notebooks/dataExtraction.ipynb

137 lines
3.7 KiB
Plaintext
Raw Normal View History

2023-11-19 02:25:01 +01:00
{
"cells": [
{
"cell_type": "code",
2023-12-04 11:06:06 +01:00
"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",
2023-12-04 11:06:06 +01:00
"execution_count": 5,
"metadata": {},
2023-11-19 02:25:01 +01:00
"outputs": [],
"source": [
2023-11-28 15:50:50 +01:00
"# 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",
2023-11-28 15:50:50 +01:00
"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",
2023-11-28 15:50:50 +01:00
" 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",
2023-11-28 15:50:50 +01:00
" 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",
2023-12-04 11:06:06 +01:00
" raise Exception(\"invalid data\")"
]
},
{
"cell_type": "code",
2023-12-04 11:06:06 +01:00
"execution_count": 6,
"metadata": {},
"outputs": [
{
2023-11-28 15:50:50 +01:00
"name": "stdout",
"output_type": "stream",
"text": [
2023-12-04 11:06:06 +01:00
"1/100\n"
]
}
],
"source": [
2023-12-04 11:06:06 +01:00
"for i in range(1,2):\n",
2023-11-28 15:50:50 +01:00
" print(str(i) + \"/100\")\n",
" name = \" (\" + str(i) + (\").json\")\n",
2023-12-04 11:06:06 +01:00
" uploadData(name,\"test_dataSet.csv\",True)\n"
2023-11-19 02:25:01 +01:00
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
2023-11-19 02:25:01 +01:00
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-12-06 19:45:52 +01:00
"version": "3.10.0"
2023-11-19 02:25:01 +01:00
}
},
"nbformat": 4,
"nbformat_minor": 2
}