{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Introduction\n\nThe aim of this set of three notebooks is to export a graph for each one of the 1449 buildings in PNG format:\n\n- [ASHRAE - PNG graphs for buildings 0-482](https://www.kaggle.com/juanmah/ashrae-png-graphs-for-buildings-0-482)\n- [ASHRAE - PNG graphs for buildings 483-965](https://www.kaggle.com/juanmah/ashrae-png-graphs-for-buildings-483-965)\n- [ASHRAE - PNG graphs for buildings 966-1448](https://www.kaggle.com/juanmah/ashrae-png-graphs-for-buildings-966-1448)\n\nThis notebook can also be configured to export the graphs to SVG format. The format to export can be changed in the EXPORT_SVG and EXPORT_PNG constants in the next cell.\n\nThere are two methods to export the graph, depending if it is used in edition mode or committing mode:\n\n- `orca` for commits. It saves the graphs into files. The files can be downloaded in the 'Output Files' section, clicking on the 'Download All' button.\n- `iplot` for edition mode. It downloads the graphs into the computer.\n\nThe method can be changed in the METHOD constant in the next cell."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false,"_kg_hide-input":true,"_kg_hide-output":false},"cell_type":"code","source":"EXPORT_PNG = True\nEXPORT_SVG = False\nMETHOD = 'orca' # 'iplot', 'orca'\n\nf = open('README.md', 'a')\nf.write('If all the saved files are images, the output section of the notebook is shown only as *Output Visualizations*.')\nf.write('')\nf.write('This file is created to have the possibility of download all files in a zip file.')\nf.write('')\nf.write('The image files can be downloaded in the *Output Files* section, clicking on the *Download All* button, despite not being shown in the output files list.')\nf.close()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"cell_type":"code","source":"if METHOD == 'orca':\n    !conda install -y -c plotly plotly-orca\n    !/usr/bin/apt-get --yes install libxss1 libgconf2-4","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pickle\nimport re\n\nimport plotly.graph_objects as go\nfrom ipywidgets import widgets\nfrom plotly.offline import iplot\nfrom tqdm.auto import trange","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"trusted":true},"cell_type":"code","source":"data_path = '../input/ashrae-data-wrangling-csv-to-pickle/'\nwith open(data_path + 'X_train.pickle', 'rb') as f:\n    X_train = pickle.load(f)\nwith open(data_path + 'nan.pickle', 'rb') as f:\n    nan = pickle.load(f)    \nwith open(data_path + 'building_metadata.pickle', 'rb') as f:\n    building_metadata = pickle.load(f)\nwith open(data_path + 'weather_train.pickle', 'rb') as f:\n    weather_train = pickle.load(f)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"def get_data(building_id, meter, xy):\n    return X_train[(X_train['building_id']==str(building_id)) &\\\n                   (X_train['meter']==meter)][xy]\n\nelectricity = go.Scatter(x=[],\n                         y=[],\n                         line=dict(\n                             width=2\n                         ),\n                         name='Electricity')\n\nhotwater = go.Scatter(x=[],\n                      y=[],\n                     line=dict(\n                         width=2\n                     ),\n                      name='Hot water')\n\nchilledwater = go.Scatter(x=[],\n                          y=[],\n                         line=dict(\n                             width=2\n                         ),\n                          name='Chilled water')\n\nsteam = go.Scatter(x=[],\n                   y=[],\n                 line=dict(\n                     width=2\n                 ),\n                   name='Steam')\n\ng = go.FigureWidget(data=[electricity, hotwater, chilledwater, steam],\n                    layout=go.Layout(\n                        showlegend=True,\n                        height=800,\n                        xaxis=dict(\n                            dtick='M1'\n                        ),\n                        yaxis=dict(\n                            ticksuffix=' '\n                        ),\n                        margin=dict(\n                            l=100,\n                            r=300\n                        ),\n                        font=dict(\n                            family='droid sans',\n                            size=32\n                        )\n                    )\n                   )\ndisplay(g)\n    \nfor building_id in range(966, 1449):\n    electricity_ts = get_data(building_id, 'electricity', 'timestamp')\n    electricity_reading = get_data(building_id, 'electricity', 'meter_reading')\n    hotwater_ts = get_data(building_id, 'hotwater', 'timestamp')\n    hotwater_reading = get_data(building_id, 'hotwater', 'meter_reading')\n    chilledwater_ts = get_data(building_id, 'chilledwater', 'timestamp')\n    chilledwater_reading = get_data(building_id, 'chilledwater', 'meter_reading')\n    steam_ts = get_data(building_id, 'steam', 'timestamp')\n    steam_reading = get_data(building_id, 'steam', 'meter_reading')\n    with g.batch_update():\n        g.layout.title.text = f'Energy for building {building_id}'\n        g.data[0].x = electricity_ts\n        g.data[0].y = electricity_reading\n        g.data[1].x = hotwater_ts\n        g.data[1].y = hotwater_reading\n        g.data[2].x = chilledwater_ts\n        g.data[2].y = chilledwater_reading\n        g.data[3].x = steam_ts\n        g.data[3].y = steam_reading\n    zero_nan = pd.DataFrame(columns=['Energy aspect', 'Zero count', 'NaN count'])\n    energy_aspects = []\n    if len(electricity_ts) > 0:\n        energy_aspects.append('electricity')\n        zero_nan = zero_nan.append({'Energy aspect': 'Electricity',\n                                    'Zero count': (electricity_reading == 0).sum(),\n                                    'NaN count': 366 * 24 - len(electricity_ts)},\n                                   ignore_index=True)\n    if len(hotwater_ts) > 0:\n        energy_aspects.append('hotwater')\n        zero_nan = zero_nan.append({'Energy aspect': 'Hot water',\n                                    'Zero count': (hotwater_reading == 0).sum(),\n                                    'NaN count': 366 * 24 - len(hotwater_ts)},\n                                   ignore_index=True)\n    if len(chilledwater_ts) > 0:\n        energy_aspects.append('chilledwater')\n        zero_nan = zero_nan.append({'Energy aspect': 'Chilled water ',\n                                    'Zero count': (chilledwater_reading == 0).sum(),\n                                    'NaN count': 366 * 24 - len(chilledwater_ts)},\n                                   ignore_index=True)\n    if len(steam_ts) > 0:\n        energy_aspects.append('steam')\n        zero_nan = zero_nan.append({'Energy aspect': 'Steam ',\n                                    'Zero count': (steam_reading == 0).sum(),\n                                    'NaN count': 366 * 24 - len(steam_ts)},\n                                   ignore_index=True)\n#     results.value = f\"{zero_nan.style.hide_index().set_table_attributes('class=''table''').render()}\"    \n#     display(g)\n    filename = f\"building_id-{building_id:0>4}-\\\nsite_id-{building_metadata['site_id'][building_id]:0>2}-\\\n{re.sub('/', '-', building_metadata['primary_use'].unique()[1])}-\\\n{'-'.join(energy_aspects)}\"\n    if METHOD == 'iplot':\n        if EXPORT_SVG:\n            iplot(g, image='svg', filename=filename, image_width=3840, image_height=2160)\n        if EXPORT_PNG:\n            iplot(g, image='png', filename=filename, image_width=3840, image_height=2160)\n    if METHOD == 'orca':\n        if EXPORT_SVG:\n            g.write_image(filename + '.svg', width=3840, height=2160)\n        if EXPORT_PNG:\n            g.write_image(filename + '.png', width=3840, height=2160)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}