Skip to content

utils.py

make_dataframe

make_dataframe(p: dict) -> DataFrame

Convert a dictionary into a pandas dataframe to prettify it.

The keys becomes the "index" column. The dictionary values become a column labeled "value".

Parameters:

  • p

    (dict) –

    input dictionary

Returns:

  • DataFrame

    dataframe conversion

Source code in .venv/lib/python3.14/site-packages/lvn/base/utils.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def make_dataframe(p: dict) -> DataFrame:
    """
    Convert a dictionary into a pandas dataframe to prettify it.

    The keys becomes the "index" column.
    The dictionary values become a column labeled "value".

    Args:
        p: input dictionary

    Returns:
        dataframe conversion
    """
    df: DataFrame \
        = DataFrame.from_dict(p, orient="index").rename_axis("name")
    df.rename(columns={0:"value"}, inplace=True,)
    return df

make_multisim_title

make_multisim_title(
    p: dict, analysis: dict, a_range: tuple[float, float] | None
) -> str

Define a title string to use when annotating plots.

Parameters:

  • p

    (dict) –

    parameters dictionary

  • analysis

    (dict) –

    analysis dictionary

  • a_range

    (tuple[float, float] | None) –

    span of values of linear coefficient "a"

Returns:

  • str

    title string

Source code in .venv/lib/python3.14/site-packages/lvn/base/utils.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def make_multisim_title(
    p: dict,
    analysis: dict,
    a_range: tuple[float, float] | None,
) -> str:
    """
    Define a title string to use when annotating plots.

    Args:
        p: parameters dictionary
        analysis: analysis dictionary
        a_range: span of values of linear coefficient "a"

    Returns:
        title string
    """
    # a_range may be in reverse order
        # + r"$n_\mathsf{sims}$"+f"={p.n_sims}   " \
    title: str = ""\
        + (
            rf"$a \in $[{min(a_range):0.4f}, {max(a_range):0.4f}]   " 
            if a_range is not None else ""
        ) \
        + rf"$b$={p["linear"]}   "  \
        + rf"$D$={p["diffusion"]}   " \
        + rf"$ς$={p["noise"]}   " \
        + "\n" \
        + rf"$a_c$={(analysis["a_c"]):0.4f}    " \
        + rf"$n_x$={p["n_x"]}   " \
        + rf"$n_y$={p["n_y"]}   "   \
        + rf"$\Delta$$x$={p["Δx"]}   " \
        + rf"$\Delta$$t$={p["Δt"]}   " \
        + rf"$t$={p["t_total"]:g}"
    return title

make_name_title

make_name_title(
    field_name: str,
    p: dict,
    analysis: dict,
    t_total: float | None = None,
    a_range: tuple[float, float] | None = None,
    do_multisim: bool = False,
) -> tuple[str, str]

Define (file) name and (plot) title strings.

Parameters:

  • p

    (dict) –

    parameters dictionary

  • analysis

    (dict) –

    analysis dictionary

  • t_total

    (float | None, default: None ) –

    time span of simulation

  • a_range

    (tuple[float, float] | None, default: None ) –

    span of values of linear coefficient "a"

  • do_multisim

    (bool, default: False ) –

    flag if doing multiple simulations

Returns:

Source code in .venv/lib/python3.14/site-packages/lvn/base/utils.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def make_name_title(
    field_name: str, 
    p: dict,
    analysis: dict,
    t_total: float | None = None,
    a_range: tuple[float, float] | None = None,
    do_multisim: bool = False,
) -> tuple[str,str]:
    """
    Define (file) name and (plot) title strings.

    Args:
        p: parameters dictionary
        analysis: analysis dictionary
        t_total: time span of simulation
        a_range: span of values of linear coefficient "a"
        do_multisim: flag if doing multiple simulations

    Returns:
        name and title strings as tuple
    """
    set_name_: Callable = partial(
        set_name,
        p,
        analysis,
        field_name,
    )
    name:str  = (
        set_name_(t_total=t_total, do_multisim=True,) 
        if do_multisim 
        else set_name_()
    )
    title: str = (
        make_multisim_title(p, analysis, a_range,) if do_multisim
        else make_sim_title(p, analysis, )
    )
    return (name, title,)

make_sim_title

make_sim_title(p: dict, analysis: dict, module: Any, do_omit_a: bool = False) -> str

Define a title string to use when annotating plots.

Parameters:

  • p

    (dict) –

    parameters dictionary

  • analysis

    (dict) –

    analysis dictionary

  • module

    (Any) –

    dplvn or other class module

  • do_omit_a

    (bool, default: False ) –

    skip linear coefficient "a" in string

Returns:

  • str

    title string

Source code in .venv/lib/python3.14/site-packages/lvn/base/utils.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def make_sim_title(
    p: dict,
    analysis: dict,
    module: Any,
    do_omit_a: bool=False,
) -> str:
    """
    Define a title string to use when annotating plots.

    Args:
        p: parameters dictionary
        analysis: analysis dictionary
        module: dplvn or other class module
        do_omit_a: skip linear coefficient "a" in string

    Returns:
        title string

    """
    def grid_topology(i: int) -> str:
        return "bnd" if p["grid_topologies"][i]==module.BOUNDED else "pdc"

    def boundary_condition(i: int) -> str:
        match p["boundary_conditions"][i]:
            case module.FIXED_VALUE:
                return "fxd"
            case module.FIXED_FLUX:
                return "flx"
            case module.FLOATING:
                return "flt"
            case _:
                return "";

    title: str = ""\
        + (
            rf"$a$={p["linear"]:0.5f}   " if not do_omit_a 
            else rf"$a_c \approx ${analysis["a_c"]:0.5f}              "
        ) \
        + rf"$b$={p["quadratic"]}   " \
        + rf"$D$={p["diffusion"]}   " \
        + rf"$η$={p["noise"]}" \
        + (
            rf"      $rs$={p["random_seed"]}      " if not do_omit_a 
            else "          "
        ) \
        + (
            rf"$a_c \approx ${analysis["a_c"]:0.5f}" if not do_omit_a 
            else ""
        ) \
        + "\n" \
        + rf"$n_x$={p["grid_size"][0]}  " \
        + rf"$n_y$={p["grid_size"][1]}   "   \
        + rf"$\Delta$$x$={p["dx"]}   " \
        + rf"$\Delta$$t$={p["dt"]}   " \
        + rf"gt:({grid_topology(0)}, {grid_topology(1)})  " \
        + rf"bc:({boundary_condition(0)}, {boundary_condition(1)}, " \
            +rf"{boundary_condition(2)}, {boundary_condition(3)})   " 
        # + (rf"$t$={t_epoch:08.2f}     " if t_epoch is not None else "")
    return title

set_name

set_name(
    p: dict,
    a: dict,
    field_name: str | None = None,
    suffix: str = "",
    t_epoch: float | None = None,
    do_parent: bool = False,
    do_dir: bool = False,
) -> str

Define a simulation name string that includes model parameters for info.

Parameters:

  • p

    (dict) –

    parameters dictionary

  • a

    (dict) –

    analysis dictionary

  • field_name

    (str | None, default: None ) –

    name (e.g. 'ρ') of Langevin field variable

  • suffix

    (str, default: '' ) –

    optional

  • t_epoch

    (float | None, default: None ) –

    time slice of sim

  • do_parent

    (bool, default: False ) –

    generate a 'parent' folder name

  • do_dir

    (bool, default: False ) –

    generate a detailed folder name

Returns:

  • str

    name string

Source code in .venv/lib/python3.14/site-packages/lvn/base/utils.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def set_name(
    p: dict,
    a: dict,
    field_name: str | None=None,
    suffix: str="",
    t_epoch: float | None=None,
    do_parent: bool=False,
    do_dir: bool=False,
) -> str:
    """
    Define a simulation name string that includes model parameters for info.

    Args:
        p: parameters dictionary
        a: analysis dictionary
        field_name: name (e.g. 'ρ') of Langevin field variable
        suffix: optional
        t_epoch: time slice of sim
        do_parent: generate a 'parent' folder name
        do_dir: generate a detailed folder name

    Returns:
        name string
    """
    to = lambda x: ((f"{x}").replace(".","p",)).replace("-","neg",)
    to5 = lambda x: ((f"{x:0.5f}").replace(".","p",)).replace("-","neg",)
    name: str
    if do_dir:
        name = f"a{to5(p["linear"])}"
    else:
        name = \
            (field_name+"_" if field_name is not None else "") \
            + (f"" if do_parent else f"a{to5(p["linear"])}_") \
            + f"b{to(p["quadratic"])}" \
            + f"_D{to(p["diffusion"])}" \
            + f"_η{to(p["noise"])}" \
            + f"_x{p["grid_size"][0]}" \
            + f"_y{p["grid_size"][1]}" \
            + f"_Δx{to(p["dx"])}" \
            + f"_Δt{to(p["dt"])}" \
            + (to(f"_t{t_epoch:08.2f}") if t_epoch is not None else "") \
            + suffix
            # + f"_rs{to(p["random_seed"])}" 
    return name