Skip to content

Commit

Permalink
fix(results): Ensure coincident peak respects input HOYs
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Oct 14, 2023
1 parent f4cda2b commit 4ccca4e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 5 deletions.
Binary file modified honeybee_grasshopper_radiance/icon/HB Annual Peak Values.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions honeybee_grasshopper_radiance/json/HB_Annual_Peak_Values.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.6.1",
"version": "1.6.2",
"nickname": "PeakValues",
"outputs": [
[
Expand Down Expand Up @@ -50,7 +50,7 @@
}
],
"subcategory": "4 :: Results",
"code": "\nimport os\nimport subprocess\n\ntry:\n from ladybug.futil import write_to_file\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\n\ntry:\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry:\n from honeybee_radiance.postprocess.annualdaylight import _process_input_folder\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_radiance:\\n\\t{}'.format(e))\n\ntry:\n from pollination_handlers.outputs.helper import read_sensor_grid_result\nexcept ImportError as e:\n raise ImportError('\\nFailed to import pollination_handlers:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, list_to_data_tree\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\ndef parse_sun_up_hours(sun_up_hours, hoys, timestep):\n \"\"\"Parse the sun-up hours from the result file .txt file.\n\n Args:\n sun_up_hours: A list of integers for the sun-up hours.\n hoys: A list of 8760 * timestep values for the hoys to select. If an empty\n list is passed, None will be returned.\n timestep: Integer for the timestep of the analysis.\n \"\"\"\n if len(hoys) != 0:\n schedule = [False] * (8760 * timestep)\n for hr in hoys:\n schedule[int(hr * timestep)] = True\n su_pattern = [schedule[int(h * timestep)] for h in sun_up_hours]\n return su_pattern\n\n\ndef peak_values(ill_file, su_pattern, coincident):\n \"\"\"Compute average values for a given result file.\"\"\"\n max_vals, max_i = [], None\n with open(ill_file) as results:\n if coincident:\n all_values = [[float(r) for r in pt_res.split()] for pt_res in results] \\\n if su_pattern is None else \\\n [[float(r) for r, is_hoy in zip(pt_res.split(), su_pattern) if is_hoy]\n for pt_res in results]\n max_val, max_i = 0, 0\n for i, t_step in enumerate(zip(*all_values)):\n tot_val = sum(t_step)\n if tot_val > max_val:\n max_val = tot_val\n max_i = i\n for sensor in all_values:\n max_vals.append(sensor[max_i])\n else:\n if su_pattern is None: # no HOY filter on results\n for pt_res in results:\n values = [float(r) for r in pt_res.split()]\n max_vals.append(max(values))\n else:\n for pt_res in results:\n values = [float(r) for r, is_hoy in zip(pt_res.split(), su_pattern) if is_hoy]\n max_vals.append(max(values))\n return max_vals, max_i\n\n\nif all_required_inputs(ghenv.Component):\n # set up the default values\n grid_filter_ = '*' if grid_filter_ is None else grid_filter_\n res_folder = os.path.dirname(_results[0]) if os.path.isfile(_results[0]) \\\n else _results[0]\n\n # check to see if results use the newer numpy arrays\n if os.path.isdir(os.path.join(res_folder, '__static_apertures__')):\n cmds = [folders.python_exe_path, '-m', 'honeybee_radiance_postprocess',\n 'post-process', 'peak-values', res_folder, '-sf', 'metrics']\n if len(_hoys_) != 0:\n hoys_str = '\\n'.join(str(h) for h in _hoys_)\n hoys_file = os.path.join(res_folder, 'hoys.txt')\n write_to_file(hoys_file, hoys_str)\n cmds.extend(['--hoys-file', hoys_file])\n if grid_filter_ != '*':\n cmds.extend(['--grids-filter', grid_filter_])\n if coincident_:\n cmds.append('--coincident')\n use_shell = True if os.name == 'nt' else False\n process = subprocess.Popen(\n cmds, cwd=res_folder, shell=use_shell,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n stdout = process.communicate() # wait for the process to finish\n if stdout[-1] != '':\n print(stdout[-1])\n raise ValueError('Failed to compute peak values.')\n avg_dir = os.path.join(res_folder, 'metrics', 'peak_values')\n if os.path.isdir(avg_dir):\n values = read_sensor_grid_result(avg_dir, 'peak','full_id', False)\n values = list_to_data_tree(values)\n with open(os.path.join(avg_dir, 'max_hoys.txt'), 'r') as max_hoys:\n hoys = [line.rstrip() for line in max_hoys.readlines()]\n if coincident_:\n hoys = map(int, hoys)\n else:\n hoys = [None] * len(hoys)\n\n else:\n # extract the timestep if it exists\n timestep = 1\n tstep_file = os.path.join(res_folder, 'timestep.txt')\n if os.path.isfile(tstep_file):\n with open(tstep_file) as tf:\n timestep = int(tf.readline())\n \n # parse the sun-up-hours\n grids, sun_up_hours = _process_input_folder(res_folder, grid_filter_)\n su_pattern = parse_sun_up_hours(sun_up_hours, _hoys_, timestep)\n \n # compute the average values\n values, hoys = [], []\n for grid_info in grids:\n ill_file = os.path.join(res_folder, '%s.ill' % grid_info['full_id'])\n dgp_file = os.path.join(res_folder, '%s.dgp' % grid_info['full_id'])\n if os.path.isfile(dgp_file):\n max_list, max_i = peak_values(dgp_file, su_pattern, coincident_)\n else:\n max_list, max_i = peak_values(ill_file, su_pattern, coincident_)\n values.append(max_list)\n if max_i is not None:\n hoys.append(sun_up_hours[max_i])\n else:\n hoys.append(max_i)\n values = list_to_data_tree(values)\n",
"code": "\nimport os\nimport subprocess\n\ntry:\n from ladybug.futil import write_to_file\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\n\ntry:\n from honeybee.config import folders\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee:\\n\\t{}'.format(e))\n\ntry:\n from honeybee_radiance.postprocess.annualdaylight import _process_input_folder\nexcept ImportError as e:\n raise ImportError('\\nFailed to import honeybee_radiance:\\n\\t{}'.format(e))\n\ntry:\n from pollination_handlers.outputs.helper import read_sensor_grid_result\nexcept ImportError as e:\n raise ImportError('\\nFailed to import pollination_handlers:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, list_to_data_tree\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\ndef parse_sun_up_hours(sun_up_hours, hoys, timestep):\n \"\"\"Parse the sun-up hours from the result file .txt file.\n\n Args:\n sun_up_hours: A list of integers for the sun-up hours.\n hoys: A list of 8760 * timestep values for the hoys to select. If an empty\n list is passed, None will be returned.\n timestep: Integer for the timestep of the analysis.\n \"\"\"\n if len(hoys) != 0:\n schedule = [False] * (8760 * timestep)\n for hr in hoys:\n schedule[int(hr * timestep)] = True\n su_pattern = [schedule[int(h * timestep)] for h in sun_up_hours]\n return su_pattern\n\n\ndef peak_values(ill_file, su_pattern, coincident):\n \"\"\"Compute average values for a given result file.\"\"\"\n max_vals, max_i = [], None\n with open(ill_file) as results:\n if coincident:\n all_values = [[float(r) for r in pt_res.split()] for pt_res in results] \\\n if su_pattern is None else \\\n [[float(r) for r, is_hoy in zip(pt_res.split(), su_pattern) if is_hoy]\n for pt_res in results]\n max_val, max_i = 0, 0\n for i, t_step in enumerate(zip(*all_values)):\n tot_val = sum(t_step)\n if tot_val > max_val:\n max_val = tot_val\n max_i = i\n for sensor in all_values:\n max_vals.append(sensor[max_i])\n else:\n if su_pattern is None: # no HOY filter on results\n for pt_res in results:\n values = [float(r) for r in pt_res.split()]\n max_vals.append(max(values))\n else:\n for pt_res in results:\n values = [float(r) for r, is_hoy in zip(pt_res.split(), su_pattern) if is_hoy]\n max_vals.append(max(values))\n return max_vals, max_i\n\n\nif all_required_inputs(ghenv.Component):\n # set up the default values\n grid_filter_ = '*' if grid_filter_ is None else grid_filter_\n res_folder = os.path.dirname(_results[0]) if os.path.isfile(_results[0]) \\\n else _results[0]\n\n # check to see if results use the newer numpy arrays\n if os.path.isdir(os.path.join(res_folder, '__static_apertures__')):\n cmds = [folders.python_exe_path, '-m', 'honeybee_radiance_postprocess',\n 'post-process', 'peak-values', res_folder, '-sf', 'metrics']\n if len(_hoys_) != 0:\n hoys_str = '\\n'.join(str(h) for h in _hoys_)\n hoys_file = os.path.join(res_folder, 'hoys.txt')\n write_to_file(hoys_file, hoys_str)\n cmds.extend(['--hoys-file', hoys_file])\n if grid_filter_ != '*':\n cmds.extend(['--grids-filter', grid_filter_])\n if coincident_:\n cmds.append('--coincident')\n use_shell = True if os.name == 'nt' else False\n process = subprocess.Popen(\n cmds, cwd=res_folder, shell=use_shell,\n stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n stdout = process.communicate() # wait for the process to finish\n if stdout[-1] != '':\n print(stdout[-1])\n raise ValueError('Failed to compute peak values.')\n avg_dir = os.path.join(res_folder, 'metrics', 'peak_values')\n if os.path.isdir(avg_dir):\n values = read_sensor_grid_result(avg_dir, 'peak','full_id', False)\n values = list_to_data_tree(values)\n with open(os.path.join(avg_dir, 'max_hoys.txt'), 'r') as max_hoys:\n hoys = [line.rstrip() for line in max_hoys.readlines()]\n if coincident_:\n hoys = map(int, hoys)\n else:\n hoys = [None] * len(hoys)\n\n else:\n # extract the timestep if it exists\n timestep = 1\n tstep_file = os.path.join(res_folder, 'timestep.txt')\n if os.path.isfile(tstep_file):\n with open(tstep_file) as tf:\n timestep = int(tf.readline())\n \n # parse the sun-up-hours\n grids, sun_up_hours = _process_input_folder(res_folder, grid_filter_)\n su_pattern = parse_sun_up_hours(sun_up_hours, _hoys_, timestep)\n filt_suh = [suh for suh in sun_up_hours if int(suh) in _hoys_] \\\n if len(_hoys_) != 0 else sun_up_hours\n # compute the average values\n values, hoys = [], []\n for grid_info in grids:\n ill_file = os.path.join(res_folder, '%s.ill' % grid_info['full_id'])\n dgp_file = os.path.join(res_folder, '%s.dgp' % grid_info['full_id'])\n if os.path.isfile(dgp_file):\n max_list, max_i = peak_values(dgp_file, su_pattern, coincident_)\n else:\n max_list, max_i = peak_values(ill_file, su_pattern, coincident_)\n values.append(max_list)\n if max_i is not None:\n hoys.append(filt_suh[max_i])\n else:\n hoys.append(max_i)\n values = list_to_data_tree(values)\n",
"category": "HB-Radiance",
"name": "HB Annual Peak Values",
"description": "Get peak irradiance or sum of illuminance values over an annual irradiance or\ndaylight simulation.\n_\nThe _hoys_ input can also be used to filter the data for a particular time period or\nhour/timestep of the simulation.\n-"
Expand Down
7 changes: 4 additions & 3 deletions honeybee_grasshopper_radiance/src/HB Annual Peak Values.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

ghenv.Component.Name = 'HB Annual Peak Values'
ghenv.Component.NickName = 'PeakValues'
ghenv.Component.Message = '1.6.1'
ghenv.Component.Message = '1.6.2'
ghenv.Component.Category = 'HB-Radiance'
ghenv.Component.SubCategory = '4 :: Results'
ghenv.Component.AdditionalHelpFromDocStrings = '2'
Expand Down Expand Up @@ -174,7 +174,8 @@ def peak_values(ill_file, su_pattern, coincident):
# parse the sun-up-hours
grids, sun_up_hours = _process_input_folder(res_folder, grid_filter_)
su_pattern = parse_sun_up_hours(sun_up_hours, _hoys_, timestep)

filt_suh = [suh for suh in sun_up_hours if int(suh) in _hoys_] \
if len(_hoys_) != 0 else sun_up_hours
# compute the average values
values, hoys = [], []
for grid_info in grids:
Expand All @@ -186,7 +187,7 @@ def peak_values(ill_file, su_pattern, coincident):
max_list, max_i = peak_values(ill_file, su_pattern, coincident_)
values.append(max_list)
if max_i is not None:
hoys.append(sun_up_hours[max_i])
hoys.append(filt_suh[max_i])
else:
hoys.append(max_i)
values = list_to_data_tree(values)
Binary file not shown.

0 comments on commit 4ccca4e

Please sign in to comment.