DataParams

DataParams offers a simple way to change the plot’s data themselves.
Internally, original data are kept as such so that it’s possible to set a new DataParams without having to recreate the plot.

Warning

Using this kind of parameters will affect displayed statistics.

Set of parameters related to plot's data.

Parameters
----------
x_limits
    Crop the plot's data to the provided limits [x_min, x_max]
y_limits
    Crop the plot's data to the provided limits [y_min, y_max]
z_limits
    Crop the plot's data to the provided limits [z_min, z_max]
time_limits
    [Map's raw plots only] Crop the plot's data to the provided
    limits [time_min, time_max]
remove_nan
    Whether NaN values should be removed before plotting or not.
normalize
    Whether histograms should be normalized or not
points_min_radius
    Minimum radius allowed between two points when plotting raw data on maps.
    The radius is expressed in meters.
    Underlying function:
    https://unidata.github.io/MetPy/latest/api/generated/metpy.calc.reduce_point_density.html
center
    Centering options for the data.
    Allowed input values are:

        * numerical values (int/float) -> data_ctr = data - center
        * statistic ("mean", "min", ...) -> data_ctr = data - np.nanmean(data)
transform
    Transformation options for the data.
    Allowed input values (including center parameter ones) are:

        * a function returning an array shaped like data -> data_ctr = f(data)
        * a clip characters string containing the VALUE word (at least once),
          numerical values, arithmetic operations and statistic names:
          "3*VALUE - MEAN/2 + 10" -> data_ctr = 3*data - np.nanmean(data)/2 + 10
DataParams can be set using the set_data_params() method on an existing plot.

Data limits & NaN removal

Tip

Removing numpy.nan values allows to visually join points separated by numpy.nan values on plots represented as curves.

Example: Reducing plot’s data to a date interval and removing undefined values.
from casys import CasysPlot, DataParams, DateHandler

param = DataParams(
    x_limits=(
        DateHandler("2019/06/02T07:00:00"),
        DateHandler("2019/06/07T08:30:00")
    ),
    y_limits=(12.5, 13.5),
    remove_nan=True
)

plot = CasysPlot(
    data=ad,
    data_name="SIG0 pass",
    stat="mean",
    data_params=param
)
plot.show()
../_images/data_params_2_0.png
Example: Taking a close look at 2 passes.
from casys import CasysPlot, DataParams, DateHandler, PlotParams, TextParams

plot_par = PlotParams(
    fig_width=5.5,
    fig_height=6.8,
    color_limits=(0, 17),
    x_limits=(90, 130),
    y_limits=(-20, 30),
    color_bar=False,
    color_map="autumn",
)

pass_1 = (
    DateHandler.from_orf("C_J3_GDRD", 122, 1, pos="first"),
    DateHandler.from_orf("C_J3_GDRD", 122, 1, pos="last"),
)

pass_25 = (
    DateHandler.from_orf("C_J3_GDRD", 122, 25, pos="first"),
    DateHandler.from_orf("C_J3_GDRD", 122, 25, pos="last"),
)

plot_1 = CasysPlot(
    data=ad,
    data_name="Sigma 0",
    plot="map",
    plot_params=plot_par,
    data_params=DataParams(time_limits=pass_1),
    text_params=TextParams(legend=f"Sigma 0 (P1)"),
)

plot_2 = CasysPlot(
    data=ad,
    data_name="Sigma 0",
    plot="map",
    plot_params=plot_par,
    data_params=DataParams(time_limits=pass_25),
    text_params=TextParams(legend=f"Sigma 0 (P25)"),
)

# Adding stats for each plot
plot_1.add_stat_bar()
plot_2.add_stat_bar()

# Merging plots
plot_1.add_color_bar()
plot_1.add_plot(plot_2)

plot_1.show()
../_images/data_params_3_0.png

Transforming and centering Data

CasysPlot data centering and transforming are applied using center and transform parameters in the DataParams given to CasysPlot (via the data_params parameter).
The center parameter is used for simple centering operations, while the transform parameter can be used for more complex operations, like scaling or applying a function.
CasysPlot methods are also available:
Center the plot data.

Parameters
----------
center
    Value, clip like str or function to use on the plot data.
Transform the plot data.

Parameters
----------
transform
    Clip like str or function to use on the plot data.
The data centering and data transforming operations are applied on the data used by the CasysPlot object.

Therefore, plot’s elements will be affected by the centering operation:

  • colorbar

  • statistic bar

  • histogram bar

  • statistic graph

The values before and after data centering and the center/transform value are available in the attributes of the CasysPlot.data_used attribute.

Center parameter

Possible values for the center parameter are:

  • a float/int (ex 10.5) -> ctrd_data = data - 10.5

  • a statistic name (ex "mean") -> ctrd_data = data - mean(data)

Example: Using a numerical value, 10.5.
from casys import create_image_grid

plot_par = PlotParams(color_limits="auto")

plot_1 = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    plot_params=plot_par,
)
plot_1.add_stat_bar(position="bottom")
plot_1.add_stat_graph(for_axis="x", position="bottom")
plot_1.add_hist_bar(for_axis="z")

plot_2 = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    data_params=DataParams(center=10.5),
    plot_params=plot_par,
)
plot_2.add_stat_bar()
plot_2.add_stat_graph(for_axis="x", position="top")
plot_2.add_hist_bar(for_axis="z", position="top")

create_image_grid(
    figure_size=(15, 11),
    plots=[plot_1, plot_2],
    columns_nb=1,
)
../_images/data_params_6_0.png

Looking at the attributes of the data_used object:

plot_2.data_used.attrs
{'initial_stats': {MIN: np.float64(0.0369),
  MAX: np.float64(0.1983),
  NBR: 5117,
  MEAN: np.float64(0.09860080125073285),
  MEDIAN: np.float64(0.0971),
  STD: np.float64(0.026405127690720866),
  VAR: np.float64(0.0006972307683632738)},
 'transform': 'value - 10.5',
 'transformed_stats': {MIN: np.float64(-10.4631),
  MAX: np.float64(-10.3017),
  NBR: 5117,
  MEAN: np.float64(-10.401399198749267),
  MEDIAN: np.float64(-10.4029),
  STD: np.float64(0.026405127690720866),
  VAR: np.float64(0.0006972307683632738)}}
Example: Using a statistic name, "mean".
plot = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    data_params=DataParams(center="mean"),
    plot_params=plot_par,
)
plot.add_stat_bar()
plot.add_stat_graph(for_axis="x", position="top")

plot.show()
../_images/data_params_8_0.png
Example: Using the center_data() method with a statistic name, "median".
plot = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    plot_params=plot_par,
)
plot.center_data(center="median")
plot.add_stat_bar()
plot.add_stat_graph(for_axis="x", position="top")

plot.show()
../_images/data_params_9_0.png

Transform parameter

Possible values for the transform parameter are:

  • a callable/function (ex ‘transform_data’)) -> transf_data = transform_data(data)

  • a CLIP like character chain.

For the last option, the character chain must contain:

  • the "VALUE" term at least one time, representing the data to center,

  • arithmetic operations: "+", "-", "*", "/",

  • statistic names as "MEAN", "MAX", "STD", "COUNT", …,

  • numerical values : "10.5".

Note

Terms can be uppercase or lowercase.
For instance: transform = "vaLue - 3 * STd + 4.3" -> transf_data = data - 3 * std(data) + 4.3.
A preset is also available: transform = 'scale', equivalent to transform = "( VALUE - MEAN ) / STD" (-> transf_data = ( data - mean(data) ) / std(data)).
For statistic names as center parameter value: center = "mean" is equivalent to transform = "VALUE - MEAN".
Example: Using a callable, transform = transform_data.
import numpy as np

def transform_data(data):
    return (data - np.nanmedian(data)) / 100
plot_1 = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    plot_params=plot_par,
)
plot_1.add_stat_bar(position="bottom")
plot_1.add_stat_graph(for_axis="x", position="bottom")

plot_2 = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    data_params=DataParams(transform=transform_data),
    plot_params=plot_par,
)
plot_2.add_stat_bar()
plot_2.add_stat_graph(for_axis="x", position="top")

create_image_grid(
    figure_size=(15, 11),
    plots=[plot_1, plot_2],
    columns_nb=1,
)
../_images/data_params_11_0.png
In the attributes of the data_used object, the "transform" key contain the function representation:
print(plot_2.data_used.attrs["transform"])
def transform_data(data):
    return (data - np.nanmedian(data)) / 100

Example: Using a CLIP like character chain, transform = "( VALUE - MEDIAN ) / 100".
plot = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    data_params=DataParams(transform="( VALUE - MEDIAN ) / 100"),
    plot_params=plot_par,
)
plot.add_stat_bar()
plot.add_stat_graph(for_axis="x", position="top")

plot.show()
../_images/data_params_13_0.png
Example: Using the preconfigurated option, transform = "scale".
plot = CasysPlot(
    data=ad,
    data_name="Ku-band range std fct (swh, wind_speed)",
    stat="median",
    data_params=DataParams(transform="scale"),
    plot_params=plot_par,
)
plot.add_stat_bar()
plot.add_stat_graph(for_axis="x", position="top")

plot.show()
../_images/data_params_14_0.png
Example: Using the transform_data() method with "scale".
plot = CasysPlot(
    data=ad,
    data_name="Sigma 0 by day",
    stat="mean",
    plot_params=plot_par,
)
plot.transform_data(transform="scale")
plot.add_stat_bar()
plot.add_hist_bar(position="top")

plot.show()
../_images/data_params_15_0.png