Convert data from a CSV file to Hourly Chart (Heat Map)

Coding rookie here (only experienced with GH), and this is my first attempt with python so bare with me :smiley:

I’m attempting to build an app utilizing the Annual Hourly Chart (heat map), I’d like to read in an unformatted 8760 CSV file, manipulate it with st.sliders and pandas, and then display it using the heat map chart.

I’m struggling with getting the data into the correct format to display with the LB heat map chart. If I’m using a pandas dataframe, I’m guessing I need to “deconstruct” the dataframe, apply ladybug header, and then dump that into the “get_hourly_data_figure”?

As I’m familiar with the LBT workflow, I attempted to do this by creating a createLBdata function, and first had trouble adding a header, figured out i need to define a specific datatype, and then figured out i needed to add a specific unit. But still ending up with the following error…

when fed into the get_hourly_data_figure & st.plotly_chart functions, i get the following error:

Question being… what is the quickest way to get from a raw 8760 pandas dataframe to ladybug HourlyContinuousCollection to feed into the heatmap chart?

I feel like I’m sloppily close, so any help is appreciated!

1 Like

Following for when I boot up my machine tomorrow to aide

So this Pollination Sample App Compare results of a parametric daylight-factor job should cover what you are trying to do I think?
the sauce can be found:
here git\pollination\sample-apps

Hi @jakechevriersg! Glad to see you have started building your first app. :clap::muscle:

Thanks to @devang we now have some official documentation for developing apps. See this one for creating a heatmap chart with an analysis period.

Let us know if you have any other questions!

1 Like

Thank you both for your replies!

My aim is to read and heatmap a raw 8760 CSV file… The example apps have been helpful, but they’re all reading 8760 data from a EPW. I was able to build up parts of the dashboard, but struggling with the chart…

Upload CSV file > Add LB Header with User Input Type & Units > Create Figure & Plotly Chart

Error when I try to plot the chart:

Basically i’m attempting to do the equivalent of this in streamlit…

Again, thank you for any feedback you can offer!

Source code:
hourlyplot_fromcsv.zip (42.8 KB)

Hi @jakechevriersg! Thank you for sharing the code! You are very close.

The only change that you need to make is to flatten the data that comes out of the CSV file and convert them to numbers.

    with open(csv_file, newline='') as f:
        reader = csv.reader(f)
        data = [float(item[0]) for item in list(reader)]

And then you’ll get what you are looking for. :chart_with_upwards_trend::chart_with_downwards_trend::bar_chart:

Here is the updated script:

import pathlib
import csv
import streamlit as st
from ladybug.header import Header
from ladybug.analysisperiod import AnalysisPeriod
from ladybug.datatype.generic import GenericType
from ladybug.datacollection import HourlyContinuousCollection


st.set_page_config(page_title='Testing Hourly Chart')


# Analysis Period ##################################################################
with st.sidebar:
    st.title('ANALYSIS PERIOD')
    st_month = st.number_input(
        'Start month', min_value=1, max_value=12, value=1, key='hourly_data_st_month')
    end_month = st.number_input(
        'End month', min_value=1, max_value=12, value=12, key='hourly_data_end_month')

    st_day = st.number_input(
        'Start day', min_value=1, max_value=31, value=1, key='hourly_data_st_day')
    end_day = st.number_input(
        'End day', min_value=1, max_value=31, value=31, key='hourly_data_end_day')

    st_hour = st.number_input(
        'Start hour', min_value=0, max_value=23, value=0, key='hourly_data_st_hour')
    end_hour = st.number_input(
        'End hour', min_value=0, max_value=23, value=23, key='hourly_data_end_hour')


# Upload CSV ##################################################################
csv_data = st.file_uploader('', type='csv')
if csv_data:
    csv_file = pathlib.Path(f'./data/{csv_data.name}')
    csv_file.parent.mkdir(parents=True, exist_ok=True)
    csv_file.write_bytes(csv_data.read())

    # Create LB Header ##################################################################
    datatype = st.text_input('DATA TYPE:')
    data_units = st.text_input('DATA UNITS:')

    lb_datatype = GenericType(datatype, data_units)
    lb_ap = AnalysisPeriod(st_month, st_day, st_hour,
                           end_month, end_day, end_hour)

    lb_header = Header(lb_datatype, data_units, lb_ap,)

    st.write(lb_header)

    # Read CSV and apply LB Header ##################################################################
    with open(csv_file, newline='') as f:
        reader = csv.reader(f)
        data = [float(item[0]) for item in list(reader)]

    lb_data = HourlyContinuousCollection(lb_header, data)

    st.write(lb_data)

    # Create Chart ##################################################################
    figure = lb_data.heat_map()
    st.plotly_chart(figure, use_container_width=True)
1 Like