Conditional Thresholds on Ladybug-hourlyplot not working

Hi @mostapha, @chriswmackey , and @dev,

I am trying to plot the hourly chart using the conditional statement thresholds and it was working fine before but now its not!

from ladybug.epw import EPW
from ladybug.legend import LegendParameters
from ladybug.hourlyplot import HourlyPlot
from ladybug.analysisperiod import AnalysisPeriod
from ladybug.datacollection import HourlyContinuousCollection
from plotly.graph_objects import Figure

epw_file = r'C:\ladybug\AUS_QLD_Brisbane.945760_TMYx\AUS_QLD_Brisbane.945760_TMYx.epw'
global_epw = EPW(epw_file)
_wea_data = global_epw.import_data_by_field(6)

def get_hourly_data_figure_conditional(_hourly_data: HourlyContinuousCollection, st_month: int, st_day: int, st_hour: int, end_month: int,
        end_day: int, end_hour: int) -> Figure:
    
    lb_ap = AnalysisPeriod(st_month, st_day, st_hour, end_month, end_day, end_hour)
    
    _hourly_data = _wea_data.filter_by_analysis_period(lb_ap)

           
    hourly_plot = HourlyPlot(_hourly_data)

    return hourly_plot.plot(title=str(_hourly_data.header.data_type), show_title=True)

filtered_hourly_data= _wea_data.filter_by_analysis_period(AnalysisPeriod(1,1,0,12,31,23))
    
data_work_hours = filtered_hourly_data.filter_by_conditional_statement('a>={} and a<={}'.format(21,24))
    
Hourly_conditional_figure = get_hourly_data_figure_conditional(data_work_hours, 1, 1,
                0, 12, 31,
                23)

Hourly_conditional_figure

I guess there might be sth wrong with the latest ladybug.hourlyplot module.

Thanks
Amir

I Will look into this

2 Likes

Sorry for the delay in responding to this @amirtabadkani.

I believe there is a bug in the snippet you shared. I added some comments to your code snippet. Please let me know if I missed something.

from ladybug.epw import EPW
from ladybug.legend import LegendParameters
from ladybug.hourlyplot import HourlyPlot
from ladybug.analysisperiod import AnalysisPeriod
from ladybug.datacollection import HourlyContinuousCollection
from plotly.graph_objects import Figure

epw_file = r'C:\ladybug\AUS_QLD_Brisbane.945760_TMYx\AUS_QLD_Brisbane.945760_TMYx.epw'
global_epw = EPW(epw_file)
_wea_data = global_epw.import_data_by_field(6)

# The data type of the _hourly_data argument here should be HourlyDiscontinuousCollection
def get_hourly_data_figure_conditional(_hourly_data: HourlyContinuousCollection, st_month: int, st_day: int, st_hour: int, end_month: int,
        end_day: int, end_hour: int) -> Figure:
    
    
    lb_ap = AnalysisPeriod(st_month, st_day, st_hour, end_month, end_day, end_hour)
    
    # You are overriding the function argument here and assigning it a new value.
    # Did you mean to apply the filter on the _hourly_data instead of _wea_data?
    _hourly_data = _wea_data.filter_by_analysis_period(lb_ap)

           
    hourly_plot = HourlyPlot(_hourly_data)

    return hourly_plot.plot(title=str(_hourly_data.header.data_type), show_title=True)

filtered_hourly_data= _wea_data.filter_by_analysis_period(AnalysisPeriod(1,1,0,12,31,23))
    
data_work_hours = filtered_hourly_data.filter_by_conditional_statement('a>={} and a<={}'.format(21,24))
    
Hourly_conditional_figure = get_hourly_data_figure_conditional(data_work_hours, 1, 1,
                0, 12, 31,
                23)

Hourly_conditional_figure

GPT 3.5 also identified this correctly. The following is its response when I dropped your original message.


It seems there’s a problem with your code structure. You’re overwriting the _hourly_data variable within the get_hourly_data_figure_conditional function. Since you’re filtering data based on certain conditions, it’s recommended to use a different variable name to avoid confusion.

Let’s refactor the code to fix this issue:

from ladybug.epw import EPW
from ladybug.legend import LegendParameters
from ladybug.hourlyplot import HourlyPlot
from ladybug.analysisperiod import AnalysisPeriod
from ladybug.datacollection import HourlyContinuousCollection
from plotly.graph_objects import Figure

epw_file = r'C:\ladybug\AUS_QLD_Brisbane.945760_TMYx\AUS_QLD_Brisbane.945760_TMYx.epw'
global_epw = EPW(epw_file)
_wea_data = global_epw.import_data_by_field(6)

def get_hourly_data_figure_conditional(_hourly_data: HourlyContinuousCollection, st_month: int, st_day: int, st_hour: int, end_month: int,
        end_day: int, end_hour: int) -> Figure:
    
    lb_ap = AnalysisPeriod(st_month, st_day, st_hour, end_month, end_day, end_hour)
    
    filtered_hourly_data = _hourly_data.filter_by_analysis_period(lb_ap)

    hourly_plot = HourlyPlot(filtered_hourly_data)

    return hourly_plot.plot(title=str(filtered_hourly_data.header.data_type), show_title=True)

filtered_hourly_data = _wea_data.filter_by_analysis_period(AnalysisPeriod(1,1,0,12,31,23))
    
data_work_hours = filtered_hourly_data.filter_by_conditional_statement('a>={} and a<={}'.format(21,24))
    
Hourly_conditional_figure = get_hourly_data_figure_conditional(data_work_hours, 1, 1,
                0, 12, 31,
                23)

Hourly_conditional_figure

In this version, I’ve changed _hourly_data to filtered_hourly_data within the get_hourly_data_figure_conditional function to avoid overwriting the original data. Now, it should work as expected.

2 Likes

Thanks @devang - you re completely right - :slightly_smiling_face: