VictorPozsgay / SuPerSim

SuPerSim (Summary for Permafrost Simulations) produces quick and easy visualization of permafrost metrics and time series from ensemble simulations
https://pypi.org/project/SuPerSim/
GNU General Public License v3.0
2 stars 0 forks source link

Separate plotting from data wrangling #2

Closed nicholas512 closed 4 months ago

nicholas512 commented 4 months ago

Currently, all the plotting functions rely on the existence of pre-formatted data files. For someone wanting to re-use the code with slightly different source data, this creates an extra step of reformatting their data into a relatively undocumented data format.

Instead, consider refactoring the code to include (1) plotting functions which are format agnostic and can be easily re-used and (2) data-reading functions, which gather up the necessary data from your specific input data. If desired it is then rather simple to recreate (3) the all-in-one functions which are now a composite of two simpler functions.

The simplest example (and the easiest fix) is plot_visible_skymap_from_horizon_file(hor_path)

This could be broken up into a plotting function that accepts data as arguments, and a data read function that gathers up the data


def plot_visible_skymap(theta, r):  # Now this can be easily re-used, and the required inputs are clearly documented
    """
    theta : np.ndarray
        horizon value in degrees increasing from ....
    r : np.ndarray
        horizon angle in degrees at corresponding theta direction
    """
    # generate plot
    # return figure

def read_horizonpy_horizons(hor_path) -> tuple:  # Here is your data-reading function
    hor_file = pd.read_csv(hor_path, usecols=['azi', 'hang'])
    theta_pre = hor_file['azi']
    theta = [i/360*2*np.pi for i in theta_pre]
    r_pre = hor_file['hang']
    r = [90-i for i in r_pre]
    return theta, r

def plot_visible_skymap_from_horizon_file(hor_path):  # This is your original function but now as a composite of two simple ones
    theta, r = read_horizonpy_horizons(hor_path)
    fig = plot_visible_skymap(theta, r)
    return fig