diff --git a/docs/devcontainer/tutorial.ipynb b/docs/devcontainer/tutorial.ipynb index 538d13d9..627c3530 100644 --- a/docs/devcontainer/tutorial.ipynb +++ b/docs/devcontainer/tutorial.ipynb @@ -198,7 +198,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 24, "metadata": {}, "outputs": [ { @@ -216,104 +216,88 @@ "import time\n", "import numbers\n", "\n", - "def opflow_csv_to_json(csvFilePath, jsonFilePath):\n", - " jsonArray = []\n", + "def add_keys(dict, row, tag=\"\"):\n", + " for key in row:\n", + " if tag:\n", + " if tag not in dict.keys():\n", + " dict[tag] = {}\n", + " dict[tag][key] = [] \n", + " else:\n", + " dict[key] = []\n", + " return dict\n", "\n", - " #count the number of lines in the csv file\n", + "def write_json(dict):\n", + " #convert python jsonArray to JSON String and write to file\n", + " with open(jsonFilePath, 'w', encoding='utf-8') as jsonf: \n", + " jsonString = json.dumps(dict, indent=4)\n", + " jsonf.write(jsonString)\n", + "\n", + "def count_lines_in_file(filename):\n", " lines = 0\n", - " with open(csvFilePath, encoding='utf-8') as csvf: \n", + " with open(filename, encoding='utf-8') as csvf: \n", " lines = sum(1 for _ in csvf)\n", " csvf.close()\n", + " return lines\n", + "\n", + "def opflow_csv_to_json(csvFilePath, jsonFilePath):\n", + " #count the number of lines in the csv file\n", + " lines = count_lines_in_file(csvFilePath)\n", "\n", " #read csv file\n", " with open(csvFilePath, encoding='utf-8') as csvf: \n", " #load csv file data using csv library's reader\n", " csvReader = csv.reader(csvf) \n", - "\n", " current_row = 0\n", " temp = {}\n", " keys = True\n", + "\n", + " #add first two rows manually\n", " row = next(csvReader)\n", " current_row += 1\n", - "\n", - " for key in row:\n", - " temp[key] = []\n", + " temp = add_keys(temp, row)\n", " current_keys = row\n", " #check if there's another row\n", " if current_row < lines:\n", " row = next(csvReader)\n", " current_row += 1\n", - "\n", " #add values to corresponding keys\n", " for x in range(0, len(current_keys)):\n", " temp[current_keys[x]].append(float(row[x]))\n", "\n", - " #check if there's another row\n", - " if current_row < lines:\n", - " row = next(csvReader)\n", - " current_row += 1\n", - " temp[\"Bus\"] = {}\n", - " for key in row:\n", - " temp[\"Bus\"][key] = []\n", - " current_keys = row\n", - " \n", - " for k in range(0, 9): #rows under the Bus category \n", + " #hard coded category names and how many rows\n", + " tags = [(\"Bus\", 9), (\"Branch\", 9), (\"Gen\", 3)]\n", + " for pair in tags:\n", + " tag = pair[0]\n", + " num = pair[1]\n", " #check if there's another row\n", " if current_row < lines:\n", " row = next(csvReader)\n", " current_row += 1\n", - " #add values to corresponding keys\n", - " for x in range(0, len(current_keys)):\n", - " temp[\"Bus\"][current_keys[x]].append(float(row[x])) \n", + " temp = add_keys(temp, row, tag)\n", + " current_keys = row\n", "\n", - " #check if there's another row\n", - " if current_row < lines:\n", - " row = next(csvReader)\n", - " current_row += 1\n", - " temp[\"Branch\"] = {}\n", - " for key in row:\n", - " temp[\"Branch\"][key] = []\n", - " current_keys = row\n", - " \n", - " for k in range(0, 9): #rows under the Branch category \n", - " #check if there's another row\n", - " if current_row < lines:\n", - " row = next(csvReader)\n", - " current_row += 1\n", - " #add values to corresponding keys\n", - " for x in range(0, len(current_keys)):\n", - " temp[\"Branch\"][current_keys[x]].append(float(row[x])) \n", - " \n", - " #check if there's another row\n", - " if current_row < lines:\n", - " row = next(csvReader)\n", - " current_row += 1\n", - " \n", - " temp[\"Gen\"] = {}\n", - " for key in row:\n", - " temp[\"Gen\"][key] = []\n", - " current_keys = row\n", - " \n", - " for k in range(0, 3): #rows under the Gen category \n", - " #check if there's another row\n", - " if current_row < lines:\n", - " row = next(csvReader)\n", - " current_row += 1\n", - " #add values to corresponding keys\n", - " for x in range(0, len(current_keys)):\n", - " for j in range(0, len(row)):\n", - " if j != 2:\n", - " row[j] = float(row[j])\n", - " else:\n", - " row[j] = row[2].strip()\n", - " temp[\"Gen\"][current_keys[x]].append(row[x]) \n", + " for k in range(0, num): #rows in each category\n", + " #check if there's another row\n", + " if current_row < lines:\n", + " row = next(csvReader)\n", + " current_row += 1\n", "\n", + " if tag == \"Gen\": #cannot cast the string fields\n", + " #add values to corresponding keys\n", + " for x in range(0, len(current_keys)):\n", + " for j in range(0, len(row)):\n", + " if j != 2:\n", + " row[j] = float(row[j])\n", + " else:\n", + " row[j] = row[2].strip()\n", + " temp[tag][current_keys[x]].append(row[x]) \n", + " else:\n", + " #add values to corresponding keys\n", + " for x in range(0, len(current_keys)):\n", + " temp[tag][current_keys[x]].append(float(row[x]))\n", + " \n", " print(temp)\n", - " jsonArray = temp\n", - " #convert python jsonArray to JSON String and write to file\n", - " with open(jsonFilePath, 'w', encoding='utf-8') as jsonf: \n", - " jsonString = json.dumps(jsonArray, indent=4)\n", - " jsonf.write(jsonString)\n", + " write_json(temp) \n", " return\n", "\n", "#if cell 2 is run, then ./ is the top level of the ExaGO dir\n",