Skip to content

Commit

Permalink
GUI improvements (#2)
Browse files Browse the repository at this point in the history
* major update to the gui

* bug fix in gui

* function to generate gui config file

* switched from argparser to sys.argv

* docstring added for gui_config.py

* bug fixs

* bumping version number and editing README GUI description

* gui configuration files for released models

* flake8 tidy

* bug fix in gui_config/update to T_release gui_config file

* kwarg catches added to gui_config and relevant doc strings to docs

* bug fixes and tests for gui_config

* bug fix in tests
  • Loading branch information
htjb authored May 18, 2021
1 parent 31b267b commit 438065d
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 127 deletions.
47 changes: 27 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Introduction

:globalemu: Robust Global 21-cm Signal Emulation
:Author: Harry Thomas Jones Bevins
:Version: 1.2.0
:Version: 1.3.0
:Homepage: https://github.com/htjb/globalemu
:Documentation: https://globalemu.readthedocs.io/

Expand Down Expand Up @@ -98,7 +98,7 @@ a set of parameters by running:
Results are accessed via 'res.z' and 'res.signal'.

The code can also be used to train a network on your own Global 21-cm signal
or neutral fraction simulations using the built in globalemu pre-processing
or neutral fraction simulations using the built in ``globalemu`` pre-processing
techniques. There is some flexibility on the required astrophysical input
parameters but the models are required to subscribe to the astrophysics free
baseline calculation detailed in the ``globalemu`` paper (see below for a reference).
Expand All @@ -110,13 +110,31 @@ More details about training your own network can be found in the documentation.

``globalemu`` also features a GUI that can be invoked from the command line
and used to explore how the structure of the Global 21-cm signal varies with
the values of the astrophysical inputs. The GUI currently relies on the
released emulator models for the Global signal and neutral fraction
history. It can be invoked from the terminal via,
the values of the astrophysical inputs. The GUI needs a configuration file to
run and this can be generated using a built in ``globalemu`` function.
**GUI configuration files can be generated for any trained model.** For example,
if we wanted to generate a configuration file for the released Global signal
emulator we would run,

.. code:: python
from globalemu.gui_config import config
paramnames = [r'$\log(f_*)$', r'$\log(V_c)$', r'$\log(f_X)$',
r'$\tau$', r'$\alpha$', r'$\nu_\mathrm{min}$',
r'$R_\mathrm{mfp}$']
config('T_release/', paramnames, 'data/')
where the directory 'data/' contains the training and testing data (in this
case that corresponding to
`21cmGEM <https://zenodo.org/record/4541500#.YKOTiibTWWg>`__).

The GUI can then be invoked from the terminal via,

.. code:: bash
globalemu
globalemu /path/to/base_dir/T_release/etc/
An image of the GUI is shown below.

Expand All @@ -126,11 +144,10 @@ An image of the GUI is shown below.
:alt: graphical user interface

The GUI can also be used to investigate the physics of the neutral fraction
history by adding the flag ``--xHI`` to the terminal call,

.. code:: bash
history by generating a configuration file for the released trained model and
setting the kwarg ``xHI=True`` in gui_config.config().

globalemu --xHI
Configuration files for the released models are provided.

Documentation
-------------
Expand Down Expand Up @@ -246,16 +263,6 @@ Below is the bibtex,
note = {arXiv: 2104.04336}
}
Poster Presentation
-------------------

A poster presentation of ``globalemu`` has recently been presented at
the `2021 Virtual SKA Science Conference <https://skao.eventsair.com/science21/>`__
and a PDF copy can be viewed `here <https://htjb.github.io/static/globalemu.pdf>`__.

Note that the timings/accuracies reported on the poster were valid at the time
of presenting. However, the emulator has since been made faster.

Requirements
------------

Expand Down
8 changes: 8 additions & 0 deletions T_release/gui_configuration.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
names,mins,maxs,label_min,label_max,logs,xHI
$\log(f_*)$,-3.4579971262630043,-0.3010299956639812,-246.84562,32.171596,0,False
$\log(V_c)$,0.6232492903979004,1.8836614351536176,,,1,
$\log(f_X)$,-6.0,0.9977593286204041,,,2,
$\tau$,0.05550117,0.09999531,,,--,
$\alpha$,1.0,1.5,,,--,
$\nu_\mathrm{min}$,0.1,3.0,,,--,
$R_\mathrm{mfp}$,10.0,50.0,,,--,
6 changes: 6 additions & 0 deletions docs/source/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ globalemu.plotter.signal_plot()
.. automodule:: globalemu.plotter
:members: signal_plot

globalemu.gui_config.config()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. automodule:: globalemu.gui_config
:members: config

globalemu.downloads.download()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
129 changes: 129 additions & 0 deletions globalemu/gui_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""
This function can be used to generate a configurate file for the GUI that
is specific to a given trained model. The file gets saved into the supplied
``base_dir`` which should contain the relevant trained model. The user
also needs to supply a path to the ``data_dir`` that contains the relevant
testing and training data. Additional arguments are described below.
A GUI config file is required to be able to visualise the signals with the
GUI and once generated the gui can be run from the command line
.. code:: bash
globalemu /path/to/base_dir/containing/model/and/config/
"""

import numpy as np
import pandas as pd


class config():

r"""
**Parameters:**
base_dir: **string**
| The path to the file containing the trained tensorflow model
that the user wishes to visualise with the GUI. Must end
in '/'.
paramnames: **list of strings**
| This should be a list of parameter names in the correct input
order. For example for the released global signal model this
would correspond to
.. code: python
paramnames = [r'$\log(f_*)$', r'$\log(V_c)$',
r'$\log(f_X)$',
r'$\tau$', r'$\alpha$',
r'$\nu_\mathrm{min}$',
r'$R_\mathrm{mfp}$']
Latex strings can be provided as above.
data_dir: **string**
| The file path to the training and test data which is used to set
the y lims of the GUI graph and ranges/intervals of GUI
sliders.
**Kwargs:**
xHI: **Bool / default: False**
| If True then ``globalemu`` will act as if it is evaluating a
neutral fraction history emulator.
logs: **list / default: [0, 1, 2]**
| The indices corresponding to the astrophysical
parameters that
were logged during training. The default assumes
that the first three columns in "train_data.txt" are
:math:`{f_*}` (star formation efficiency),
:math:`{V_c}` (minimum virial circular velocity) and
:math:`{f_x}` (X-ray efficieny).
"""

def __init__(self, base_dir, paramnames, data_dir, **kwargs):

for key, values in kwargs.items():
if key not in set(
['xHI', 'logs']):
raise KeyError("Unexpected keyword argument in process()")

self.base_dir = base_dir
self.paramnames = paramnames
self.data_dir = data_dir
self.logs = kwargs.pop('logs', [0, 1, 2])
self.xHI = kwargs.pop('xHI', False)

file_kwargs = [self.base_dir, self.data_dir]
file_strings = ['base_dir', 'data_dir']
for i in range(len(file_kwargs)):
if type(file_kwargs[i]) is not str:
raise TypeError("'" + file_strings[i] + "' must be a sting.")
elif file_kwargs[i].endswith('/') is False:
raise KeyError("'" + file_strings[i] + "' must end with '/'.")

if type(self.paramnames) is not list:
raise TypeError("'paramnames' must be a list of strings.")

if type(self.xHI) is not bool:
raise TypeError("'xHI' must be a bool.")

if type(self.logs) is not list:
raise TypeError("'logs' must be a list.")

test_data = np.loadtxt(data_dir + 'test_data.txt')
test_labels = np.loadtxt(data_dir + 'test_labels.txt')
for i in range(test_data.shape[1]):
if i in self.logs:
for j in range(test_data.shape[0]):
if test_data[j, i] == 0:
test_data[j, i] = 1e-6
test_data[:, i] = np.log10(test_data[:, i])

data_mins = test_data.min(axis=0)
data_maxs = test_data.max(axis=0)

full_logs = []
for i in range(len(data_maxs)):
if i in set(self.logs):
full_logs.append(i)
else:
full_logs.append('--')

df = pd.DataFrame({'names': self.paramnames,
'mins': data_mins,
'maxs': data_maxs,
'label_min':
[test_labels.min()] + ['']*(len(data_maxs)-1),
'label_max':
[test_labels.max()] + ['']*(len(data_maxs)-1),
'logs': full_logs,
'xHI':
[self.xHI] + ['']*(len(data_maxs)-1)})

df.to_csv(base_dir + 'gui_configuration.csv', index=False)
Loading

0 comments on commit 438065d

Please sign in to comment.