Jupyter-Notebook
About the Notebook API
The Notebook API is a new feature in the v0.12.0 release of SpectraFit
with major focus on working with Jupyter Notebooks.
The Notebook API is a work in progress and is subject to change.
Jupyter Notebook plugin for SpectraFit.
DataFrameDisplay
¶
Class for displaying a dataframe in different ways.
Source code in spectrafit/plugins/notebook.py
class DataFrameDisplay:
"""Class for displaying a dataframe in different ways."""
def df_display(self, df: pd.DataFrame, mode: Optional[str] = None) -> Optional[Any]:
"""Call the DataframeDisplay class.
!!! info "About `df_display`"
This function is used to display a dataframe in two different ways.
1. Regular display mode:
1. Via `IPython.display` for regular sliced displaying of the dataframe
in the notebook.
2. Via `IPython.display` as Markdown for regular displaying of the
complete dataframe in the notebook.
2. Interactive display mode:
1. Via `itables` for interactive displaying of the dataframe in the
notebook, which allows for sorting, filtering, and jumping. For
more information see [itables](https://github.com/mwouts/itables).
2. Via `dtale` for interactive displaying of the dataframe in the
notebook, which allows advanced data analysis of the dataframe in
an external window. For more information see
[dtale](https://github.com/man-group/dtale).
Args:
df (pd.DataFrame): Dataframe to display.
mode (str, Optional): Display mode. Defaults to None.
Raises:
ValueError: Raises ValueError if mode of displaying is not supported.
Returns:
Optional[Any]: Returns the dtale object for plotting in the Jupyter
notebook, if mode is `dtale`.
"""
if mode == "regular":
self.regular_display(df=df)
elif mode == "markdown":
self.markdown_display(df=df)
elif mode == "interactive":
self.interactive_display(df=df)
elif mode == "dtale":
return self.dtale_display(df=df)
elif mode is not None:
raise ValueError(
f"Invalid mode: {mode}. "
"Valid modes are: regular, interactive, dtale, markdown."
)
return None
@staticmethod
def regular_display(df: pd.DataFrame) -> None:
"""Display the dataframe in a regular way.
Args:
df (pd.DataFrame): Dataframe to display.
"""
display(df)
@staticmethod
def interactive_display(df: pd.DataFrame) -> None:
"""Display the dataframe in an interactive way.
Args:
df (pd.DataFrame): Dataframe to display.
"""
itables_show(df)
@staticmethod
def dtale_display(df: pd.DataFrame) -> Any:
"""Display the dataframe in a dtale way.
Args:
df (pd.DataFrame): Dataframe to display.
Returns:
Any: Returns the dtale object for plotting in the Jupyter notebook.
"""
return dtale_show(df)
@staticmethod
def markdown_display(df: pd.DataFrame) -> None:
"""Display the dataframe in a markdown way.
Args:
df (pd.DataFrame): Dataframe to display.
"""
display_markdown(df.to_markdown(), raw=True)
df_display(df, mode=None)
¶
Call the DataframeDisplay class.
About df_display
This function is used to display a dataframe in two different ways.
- Regular display mode:
- Via
IPython.display
for regular sliced displaying of the dataframe in the notebook. - Via
IPython.display
as Markdown for regular displaying of the complete dataframe in the notebook.
- Via
- Interactive display mode:
- Via
itables
for interactive displaying of the dataframe in the notebook, which allows for sorting, filtering, and jumping. For more information see itables. - Via
dtale
for interactive displaying of the dataframe in the notebook, which allows advanced data analysis of the dataframe in an external window. For more information see dtale.
- Via
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe to display. | required |
mode | (str, Optional) | Display mode. Defaults to None. | None |
Raises:
Type | Description |
---|---|
ValueError | Raises ValueError if mode of displaying is not supported. |
Returns:
Type | Description |
---|---|
Optional[Any] | Optional[Any]: Returns the dtale object for plotting in the Jupyter notebook, if mode is |
Source code in spectrafit/plugins/notebook.py
def df_display(self, df: pd.DataFrame, mode: Optional[str] = None) -> Optional[Any]:
"""Call the DataframeDisplay class.
!!! info "About `df_display`"
This function is used to display a dataframe in two different ways.
1. Regular display mode:
1. Via `IPython.display` for regular sliced displaying of the dataframe
in the notebook.
2. Via `IPython.display` as Markdown for regular displaying of the
complete dataframe in the notebook.
2. Interactive display mode:
1. Via `itables` for interactive displaying of the dataframe in the
notebook, which allows for sorting, filtering, and jumping. For
more information see [itables](https://github.com/mwouts/itables).
2. Via `dtale` for interactive displaying of the dataframe in the
notebook, which allows advanced data analysis of the dataframe in
an external window. For more information see
[dtale](https://github.com/man-group/dtale).
Args:
df (pd.DataFrame): Dataframe to display.
mode (str, Optional): Display mode. Defaults to None.
Raises:
ValueError: Raises ValueError if mode of displaying is not supported.
Returns:
Optional[Any]: Returns the dtale object for plotting in the Jupyter
notebook, if mode is `dtale`.
"""
if mode == "regular":
self.regular_display(df=df)
elif mode == "markdown":
self.markdown_display(df=df)
elif mode == "interactive":
self.interactive_display(df=df)
elif mode == "dtale":
return self.dtale_display(df=df)
elif mode is not None:
raise ValueError(
f"Invalid mode: {mode}. "
"Valid modes are: regular, interactive, dtale, markdown."
)
return None
dtale_display(df)
staticmethod
¶
Display the dataframe in a dtale way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe to display. | required |
Returns:
Name | Type | Description |
---|---|---|
Any | Any | Returns the dtale object for plotting in the Jupyter notebook. |
Source code in spectrafit/plugins/notebook.py
interactive_display(df)
staticmethod
¶
Display the dataframe in an interactive way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe to display. | required |
markdown_display(df)
staticmethod
¶
Display the dataframe in a markdown way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe to display. | required |
regular_display(df)
staticmethod
¶
Display the dataframe in a regular way.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe to display. | required |
DataFramePlot
¶
Class to plot a dataframe.
Source code in spectrafit/plugins/notebook.py
class DataFramePlot:
"""Class to plot a dataframe."""
def plot_2dataframes(
self,
args_plot: PlotAPI,
df_1: pd.DataFrame,
df_2: Optional[pd.DataFrame] = None,
) -> None:
"""Plot two dataframes.
!!! info "About the plot"
The plot is a combination of two plots. The first plot can be the
residual plot of a fit or the _modified_ data. The second plot can be the
fit or the original data.
!!! missing "`line_dash_map`"
Currently, the `line_dash_map` is not working, and the dash is not
plotted. This is likely due to the columns not being labeled in the
dataframe.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df_1 (pd.DataFrame): First dataframe to plot, which will generate
a fit plot with residual plot. The ratio is 70% to 20% with
10% space in between.
df_2 (Optional[pd.DataFrame], optional): Second optional dataframe to
plot for comparison. In this case, the ratio between the first
and second plot will be the same. Defaults to None.
"""
if df_2 is None:
fig = self._plot_single_dataframe(args_plot, df_1)
else:
fig = self._plot_two_dataframes(args_plot, df_1, df_2)
fig.show(
config={
"toImageButtonOptions": {
"format": "png",
"filename": "plot_of_2_dataframes",
"scale": 4,
}
}
)
def _plot_single_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> Figure:
"""Plot a single dataframe with residuals."""
fig = make_subplots(
rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05
)
residual_fig = self._create_residual_plot(df, args_plot)
fit_fig = self._create_fit_plot(df, args_plot)
for trace in residual_fig["data"]:
fig.add_trace(trace, row=1, col=1)
for trace in fit_fig["data"]:
fig.add_trace(trace, row=2, col=1)
self._update_plot_layout(fig, args_plot, df_2_provided=False)
return fig
def _plot_two_dataframes(
self, args_plot: PlotAPI, df_1: pd.DataFrame, df_2: pd.DataFrame
) -> Figure:
"""Plot two dataframes for comparison."""
fig = make_subplots(
rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05
)
fig1 = px.line(df_1, x=args_plot.x, y=args_plot.y)
fig2 = px.line(df_2, x=args_plot.x, y=args_plot.y)
for trace in fig1["data"]:
fig.add_trace(trace, row=1, col=1)
for trace in fig2["data"]:
fig.add_trace(trace, row=2, col=1)
self._update_plot_layout(fig, args_plot, df_2_provided=True)
return fig
def _create_residual_plot(self, df: pd.DataFrame, args_plot: PlotAPI) -> Figure:
"""Create the residual plot."""
return px.line(
df,
x=ColumnNamesAPI().energy,
y=ColumnNamesAPI().residual,
color_discrete_sequence=[args_plot.color.residual],
)
def _create_fit_plot(self, df: pd.DataFrame, args_plot: PlotAPI) -> Figure:
"""Create the fit plot."""
y_columns = df.columns.drop(
[ColumnNamesAPI().energy, ColumnNamesAPI().residual]
)
color_map = {
ColumnNamesAPI().intensity: args_plot.color.intensity,
ColumnNamesAPI().fit: args_plot.color.fit,
**{
key: args_plot.color.components
for key in y_columns.drop(
[ColumnNamesAPI().intensity, ColumnNamesAPI().fit]
)
},
}
line_dash_map = {
ColumnNamesAPI().intensity: "solid",
ColumnNamesAPI().fit: "longdash",
**{
key: "dash"
for key in y_columns.drop(
[ColumnNamesAPI().intensity, ColumnNamesAPI().fit]
)
},
}
return px.line(
df,
x=ColumnNamesAPI().energy,
y=y_columns,
color_discrete_map=color_map,
line_dash_map=line_dash_map,
)
def _update_plot_layout(
self, fig: Figure, args_plot: PlotAPI, df_2_provided: bool
) -> None:
"""Update the plot layout."""
height = args_plot.size[1][0]
self.update_layout_axes(fig, args_plot, height)
xaxis_title = self.title_text(
name=args_plot.xaxis_title.name, unit=args_plot.xaxis_title.unit
)
yaxis_title = self.title_text(
name=args_plot.yaxis_title.name, unit=args_plot.yaxis_title.unit
)
fig.update_xaxes(title_text=xaxis_title, row=1, col=1)
fig.update_xaxes(title_text=xaxis_title, row=2, col=1)
if not df_2_provided:
residual_title = self.title_text(
name=args_plot.residual_title.name, unit=args_plot.residual_title.unit
)
fig["layout"]["yaxis1"].update(domain=[0.8, 1])
fig["layout"]["yaxis2"].update(domain=[0, 0.7])
fig.update_yaxes(title_text=residual_title, row=1, col=1)
else:
fig.update_yaxes(title_text=yaxis_title, row=1, col=1)
fig.update_yaxes(title_text=yaxis_title, row=2, col=1)
def plot_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
"""Plot the dataframe according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df (pd.DataFrame): Dataframe to plot.
"""
fig = px.line(df, x=args_plot.x, y=args_plot.y)
height = args_plot.size[1][0]
self.update_layout_axes(fig, args_plot, height)
fig.update_xaxes(
title_text=self.title_text(
name=args_plot.xaxis_title.name, unit=args_plot.xaxis_title.unit
)
)
fig.update_yaxes(
title_text=self.title_text(
name=args_plot.yaxis_title.name, unit=args_plot.yaxis_title.unit
)
)
fig.show(
config={
"toImageButtonOptions": {
"format": "png",
"filename": "plot_dataframe",
"scale": 4,
}
}
)
def plot_global_fit(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
"""Plot the global dataframe according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df (pd.DataFrame): Dataframe to plot.
"""
num_fits = df.columns.str.startswith(ColumnNamesAPI().fit).sum()
for i in range(1, num_fits + 1):
cols = [col for col in df.columns if col.endswith(f"_{i}")]
cols.append(ColumnNamesAPI().energy)
df_subset = df[cols].rename(
columns={
f"{ColumnNamesAPI().intensity}_{i}": ColumnNamesAPI().intensity,
f"{ColumnNamesAPI().fit}_{i}": ColumnNamesAPI().fit,
f"{ColumnNamesAPI().residual}_{i}": ColumnNamesAPI().residual,
}
)
self.plot_2dataframes(args_plot, df_subset)
def plot_metric(
self,
args_plot: PlotAPI,
df_metric: pd.DataFrame,
bar_criteria: Union[str, List[str]],
line_criteria: Union[str, List[str]],
) -> None:
"""Plot the metric according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df_metric (pd.DataFrame): Metric dataframe to plot.
bar_criteria (Union[str, List[str]]): Criteria to plot as bars.
line_criteria (Union[str, List[str]]): Criteria to plot as lines.
"""
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig_bar = px.bar(
df_metric,
y=bar_criteria,
color_discrete_sequence=args_plot.color.bars,
)
fig_line = px.line(
df_metric,
y=line_criteria,
color_discrete_sequence=args_plot.color.lines,
)
fig_line.update_traces(mode="lines+markers", yaxis="y2")
for trace in fig_bar.data:
fig.add_trace(trace)
for trace in fig_line.data:
fig.add_trace(trace)
fig.update_layout(xaxis_type="category")
height = args_plot.size[1][1]
self.update_layout_axes(fig, args_plot, height)
fig.update_xaxes(
title_text=self.title_text(
name=args_plot.run_title.name, unit=args_plot.run_title.unit
)
)
fig.update_yaxes(
title_text=self.title_text(
name=args_plot.metric_title.name_0, unit=args_plot.metric_title.unit_0
),
secondary_y=False,
)
fig.update_yaxes(
title_text=self.title_text(
name=args_plot.metric_title.name_1, unit=args_plot.metric_title.unit_1
),
secondary_y=True,
)
fig.show(
config={
"toImageButtonOptions": {
"format": "png",
"filename": "plot_metric",
"scale": 4,
}
}
)
def update_layout_axes(
self, fig: Figure, args_plot: PlotAPI, height: int
) -> Figure:
"""Update the layout of the plot.
Args:
fig (Figure): Figure to update.
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
height (int): Height of the plot.
Returns:
Figure: Updated figure.
"""
fig.update_layout(
title=args_plot.title,
legend_title=args_plot.legend_title,
legend=args_plot.legend.model_dump(),
font=args_plot.font.model_dump(),
showlegend=args_plot.show_legend,
width=args_plot.size[0],
height=height,
paper_bgcolor=args_plot.color.paper,
plot_bgcolor=args_plot.color.plot,
)
minor_ticks = self.get_minor(args_plot)
fig.update_xaxes(
minor=minor_ticks,
gridcolor=args_plot.color.grid,
linecolor=args_plot.color.line,
zerolinecolor=args_plot.color.zero_line,
color=args_plot.color.color,
)
fig.update_yaxes(
minor=minor_ticks,
gridcolor=args_plot.color.grid,
linecolor=args_plot.color.line,
zerolinecolor=args_plot.color.zero_line,
color=args_plot.color.color,
)
return fig
@staticmethod
def title_text(name: str, unit: Optional[str] = None) -> str:
"""Return the title text.
Args:
name (str): Name of the variable.
unit (Optional[str], optional): Unit of the variable. Defaults to None.
Returns:
str: Title text.
"""
return f"{name} [{unit}]" if unit else name
def get_minor(self, args_plot: PlotAPI) -> Dict[str, Union[str, bool]]:
"""Get the minor axis arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
Returns:
Dict[str, Union[str, bool]]: Dictionary with the minor axis arguments.
"""
return {
"tickcolor": args_plot.color.ticks,
"showgrid": args_plot.grid.show,
"ticks": args_plot.grid.ticks,
"griddash": args_plot.grid.dash,
}
get_minor(args_plot)
¶
Get the minor axis arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args_plot | PlotAPI | PlotAPI object for the settings of the plot. | required |
Returns:
Type | Description |
---|---|
Dict[str, Union[str, bool]] | Dict[str, Union[str, bool]]: Dictionary with the minor axis arguments. |
Source code in spectrafit/plugins/notebook.py
def get_minor(self, args_plot: PlotAPI) -> Dict[str, Union[str, bool]]:
"""Get the minor axis arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
Returns:
Dict[str, Union[str, bool]]: Dictionary with the minor axis arguments.
"""
return {
"tickcolor": args_plot.color.ticks,
"showgrid": args_plot.grid.show,
"ticks": args_plot.grid.ticks,
"griddash": args_plot.grid.dash,
}
plot_2dataframes(args_plot, df_1, df_2=None)
¶
Plot two dataframes.
About the plot
The plot is a combination of two plots. The first plot can be the residual plot of a fit or the modified data. The second plot can be the fit or the original data.
line_dash_map
Currently, the line_dash_map
is not working, and the dash is not plotted. This is likely due to the columns not being labeled in the dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args_plot | PlotAPI | PlotAPI object for the settings of the plot. | required |
df_1 | DataFrame | First dataframe to plot, which will generate a fit plot with residual plot. The ratio is 70% to 20% with 10% space in between. | required |
df_2 | Optional[DataFrame] | Second optional dataframe to plot for comparison. In this case, the ratio between the first and second plot will be the same. Defaults to None. | None |
Source code in spectrafit/plugins/notebook.py
def plot_2dataframes(
self,
args_plot: PlotAPI,
df_1: pd.DataFrame,
df_2: Optional[pd.DataFrame] = None,
) -> None:
"""Plot two dataframes.
!!! info "About the plot"
The plot is a combination of two plots. The first plot can be the
residual plot of a fit or the _modified_ data. The second plot can be the
fit or the original data.
!!! missing "`line_dash_map`"
Currently, the `line_dash_map` is not working, and the dash is not
plotted. This is likely due to the columns not being labeled in the
dataframe.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df_1 (pd.DataFrame): First dataframe to plot, which will generate
a fit plot with residual plot. The ratio is 70% to 20% with
10% space in between.
df_2 (Optional[pd.DataFrame], optional): Second optional dataframe to
plot for comparison. In this case, the ratio between the first
and second plot will be the same. Defaults to None.
"""
if df_2 is None:
fig = self._plot_single_dataframe(args_plot, df_1)
else:
fig = self._plot_two_dataframes(args_plot, df_1, df_2)
fig.show(
config={
"toImageButtonOptions": {
"format": "png",
"filename": "plot_of_2_dataframes",
"scale": 4,
}
}
)
plot_dataframe(args_plot, df)
¶
Plot the dataframe according to the PlotAPI arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args_plot | PlotAPI | PlotAPI object for the settings of the plot. | required |
df | DataFrame | Dataframe to plot. | required |
Source code in spectrafit/plugins/notebook.py
def plot_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
"""Plot the dataframe according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df (pd.DataFrame): Dataframe to plot.
"""
fig = px.line(df, x=args_plot.x, y=args_plot.y)
height = args_plot.size[1][0]
self.update_layout_axes(fig, args_plot, height)
fig.update_xaxes(
title_text=self.title_text(
name=args_plot.xaxis_title.name, unit=args_plot.xaxis_title.unit
)
)
fig.update_yaxes(
title_text=self.title_text(
name=args_plot.yaxis_title.name, unit=args_plot.yaxis_title.unit
)
)
fig.show(
config={
"toImageButtonOptions": {
"format": "png",
"filename": "plot_dataframe",
"scale": 4,
}
}
)
plot_global_fit(args_plot, df)
¶
Plot the global dataframe according to the PlotAPI arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args_plot | PlotAPI | PlotAPI object for the settings of the plot. | required |
df | DataFrame | Dataframe to plot. | required |
Source code in spectrafit/plugins/notebook.py
def plot_global_fit(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
"""Plot the global dataframe according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df (pd.DataFrame): Dataframe to plot.
"""
num_fits = df.columns.str.startswith(ColumnNamesAPI().fit).sum()
for i in range(1, num_fits + 1):
cols = [col for col in df.columns if col.endswith(f"_{i}")]
cols.append(ColumnNamesAPI().energy)
df_subset = df[cols].rename(
columns={
f"{ColumnNamesAPI().intensity}_{i}": ColumnNamesAPI().intensity,
f"{ColumnNamesAPI().fit}_{i}": ColumnNamesAPI().fit,
f"{ColumnNamesAPI().residual}_{i}": ColumnNamesAPI().residual,
}
)
self.plot_2dataframes(args_plot, df_subset)
plot_metric(args_plot, df_metric, bar_criteria, line_criteria)
¶
Plot the metric according to the PlotAPI arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args_plot | PlotAPI | PlotAPI object for the settings of the plot. | required |
df_metric | DataFrame | Metric dataframe to plot. | required |
bar_criteria | Union[str, List[str]] | Criteria to plot as bars. | required |
line_criteria | Union[str, List[str]] | Criteria to plot as lines. | required |
Source code in spectrafit/plugins/notebook.py
def plot_metric(
self,
args_plot: PlotAPI,
df_metric: pd.DataFrame,
bar_criteria: Union[str, List[str]],
line_criteria: Union[str, List[str]],
) -> None:
"""Plot the metric according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df_metric (pd.DataFrame): Metric dataframe to plot.
bar_criteria (Union[str, List[str]]): Criteria to plot as bars.
line_criteria (Union[str, List[str]]): Criteria to plot as lines.
"""
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig_bar = px.bar(
df_metric,
y=bar_criteria,
color_discrete_sequence=args_plot.color.bars,
)
fig_line = px.line(
df_metric,
y=line_criteria,
color_discrete_sequence=args_plot.color.lines,
)
fig_line.update_traces(mode="lines+markers", yaxis="y2")
for trace in fig_bar.data:
fig.add_trace(trace)
for trace in fig_line.data:
fig.add_trace(trace)
fig.update_layout(xaxis_type="category")
height = args_plot.size[1][1]
self.update_layout_axes(fig, args_plot, height)
fig.update_xaxes(
title_text=self.title_text(
name=args_plot.run_title.name, unit=args_plot.run_title.unit
)
)
fig.update_yaxes(
title_text=self.title_text(
name=args_plot.metric_title.name_0, unit=args_plot.metric_title.unit_0
),
secondary_y=False,
)
fig.update_yaxes(
title_text=self.title_text(
name=args_plot.metric_title.name_1, unit=args_plot.metric_title.unit_1
),
secondary_y=True,
)
fig.show(
config={
"toImageButtonOptions": {
"format": "png",
"filename": "plot_metric",
"scale": 4,
}
}
)
title_text(name, unit=None)
staticmethod
¶
Return the title text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | Name of the variable. | required |
unit | Optional[str] | Unit of the variable. Defaults to None. | None |
Returns:
Name | Type | Description |
---|---|---|
str | str | Title text. |
Source code in spectrafit/plugins/notebook.py
update_layout_axes(fig, args_plot, height)
¶
Update the layout of the plot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fig | Figure | Figure to update. | required |
args_plot | PlotAPI | PlotAPI object for the settings of the plot. | required |
height | int | Height of the plot. | required |
Returns:
Name | Type | Description |
---|---|---|
Figure | Figure | Updated figure. |
Source code in spectrafit/plugins/notebook.py
def update_layout_axes(
self, fig: Figure, args_plot: PlotAPI, height: int
) -> Figure:
"""Update the layout of the plot.
Args:
fig (Figure): Figure to update.
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
height (int): Height of the plot.
Returns:
Figure: Updated figure.
"""
fig.update_layout(
title=args_plot.title,
legend_title=args_plot.legend_title,
legend=args_plot.legend.model_dump(),
font=args_plot.font.model_dump(),
showlegend=args_plot.show_legend,
width=args_plot.size[0],
height=height,
paper_bgcolor=args_plot.color.paper,
plot_bgcolor=args_plot.color.plot,
)
minor_ticks = self.get_minor(args_plot)
fig.update_xaxes(
minor=minor_ticks,
gridcolor=args_plot.color.grid,
linecolor=args_plot.color.line,
zerolinecolor=args_plot.color.zero_line,
color=args_plot.color.color,
)
fig.update_yaxes(
minor=minor_ticks,
gridcolor=args_plot.color.grid,
linecolor=args_plot.color.line,
zerolinecolor=args_plot.color.zero_line,
color=args_plot.color.color,
)
return fig
ExportReport
¶
Bases: SolverResults
Class for exporting results as toml.
Source code in spectrafit/plugins/notebook.py
class ExportReport(SolverResults):
"""Class for exporting results as toml."""
def __init__(
self,
description: DescriptionAPI,
initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]],
pre_processing: DataPreProcessingAPI,
settings_solver_models: SolverModelsAPI,
fname: FnameAPI,
args_out: Dict[str, Any],
df_org: pd.DataFrame,
df_fit: pd.DataFrame,
df_pre: pd.DataFrame = pd.DataFrame(),
) -> None:
"""Initialize the ExportReport class.
Args:
description (DescriptionAPI): Description of the fit project.
initial_model (List[Dict[str, Dict[str, Dict[str, Any]]]]): Initial model
for the fit.
pre_processing (DataPreProcessingAPI): Data pre-processing settings.
settings_solver_models (SolverModelsAPI): Solver models settings.
fname (FnameAPI): Filename of the fit project including the path, prefix,
and suffix.
args_out (Dict[str, Any]): Dictionary of SpectraFit settings and results.
df_org (pd.DataFrame): Dataframe of the original data for performing
the fit.
df_fit (pd.DataFrame): Dataframe of the final fit data.
df_pre (Optional[pd.DataFrame], optional): Dataframe of the pre-processed.
Defaults to pd.DataFrame().
"""
super().__init__(args_out=args_out)
self.description = description
self.initial_model = initial_model
self.pre_processing = pre_processing
self.settings_solver_models = settings_solver_models
self.fname = fname
self.df_org = df_org.to_dict(orient="list")
self.df_fit = df_fit.to_dict(orient="list")
self.df_pre = df_pre.to_dict(orient="list")
@property
def make_input_contribution(self) -> InputAPI:
"""Make input contribution of the report.
Returns:
InputAPI: Input contribution of the report as class.
"""
return InputAPI(
description=self.description,
initial_model=self.initial_model,
pre_processing=self.pre_processing,
method=FitMethodAPI(
global_fitting=self.settings_global_fitting,
confidence_interval=self.settings_conf_interval,
configurations=self.settings_configurations,
settings_solver_models=self.settings_solver_models.model_dump(
exclude_none=True
),
),
)
@property
def make_solver_contribution(self) -> SolverAPI:
"""Make solver contribution of the report.
Returns:
SolverAPI: Solver contribution of the report as class.
"""
return SolverAPI(
goodness_of_fit=self.get_gof,
regression_metrics=self.get_regression_metrics,
descriptive_statistic=self.get_descriptive_statistic,
linear_correlation=self.get_linear_correlation,
component_correlation=self.get_component_correlation,
confidence_interval=self.get_confidence_interval,
covariance_matrix=self.get_covariance_matrix,
variables=self.get_variables,
errorbars=self.get_errorbars,
computational=self.get_computational,
)
@property
def make_output_contribution(self) -> OutputAPI:
"""Make output contribution of the report.
Returns:
OutputAPI: Output contribution of the report as class.
"""
return OutputAPI(df_org=self.df_org, df_fit=self.df_fit, df_pre=self.df_pre)
def __call__(self) -> Dict[str, Any]:
"""Get the complete report as dictionary.
!!! info "About the report and `exclude_none_dictionary`"
The report is generated by using the `ReportAPI` class, which is a
`Pydantic`-definition of the report. The `Pydantic`-definition is
converted to a dictionary by using the `.model_dump()` option of `Pydantic`.
The `recursive_exclude_none` function is used to remove all `None` values
from the dictionary, which are hidden in the nested dictionaries.
Returns:
Dict[str, Any]: Report as dictionary by using the `.dict()` option of
pydantic. `None` is excluded.
"""
report = ReportAPI(
input=self.make_input_contribution,
solver=self.make_solver_contribution,
output=self.make_output_contribution,
).model_dump(exclude_none=True)
report = exclude_none_dictionary(report)
report = transform_nested_types(report)
return report
make_input_contribution: InputAPI
property
¶
Make input contribution of the report.
Returns:
Name | Type | Description |
---|---|---|
InputAPI | InputAPI | Input contribution of the report as class. |
make_output_contribution: OutputAPI
property
¶
Make output contribution of the report.
Returns:
Name | Type | Description |
---|---|---|
OutputAPI | OutputAPI | Output contribution of the report as class. |
make_solver_contribution: SolverAPI
property
¶
Make solver contribution of the report.
Returns:
Name | Type | Description |
---|---|---|
SolverAPI | SolverAPI | Solver contribution of the report as class. |
__call__()
¶
Get the complete report as dictionary.
About the report and exclude_none_dictionary
The report is generated by using the ReportAPI
class, which is a Pydantic
-definition of the report. The Pydantic
-definition is converted to a dictionary by using the .model_dump()
option of Pydantic
. The recursive_exclude_none
function is used to remove all None
values from the dictionary, which are hidden in the nested dictionaries.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Report as dictionary by using the |
Source code in spectrafit/plugins/notebook.py
def __call__(self) -> Dict[str, Any]:
"""Get the complete report as dictionary.
!!! info "About the report and `exclude_none_dictionary`"
The report is generated by using the `ReportAPI` class, which is a
`Pydantic`-definition of the report. The `Pydantic`-definition is
converted to a dictionary by using the `.model_dump()` option of `Pydantic`.
The `recursive_exclude_none` function is used to remove all `None` values
from the dictionary, which are hidden in the nested dictionaries.
Returns:
Dict[str, Any]: Report as dictionary by using the `.dict()` option of
pydantic. `None` is excluded.
"""
report = ReportAPI(
input=self.make_input_contribution,
solver=self.make_solver_contribution,
output=self.make_output_contribution,
).model_dump(exclude_none=True)
report = exclude_none_dictionary(report)
report = transform_nested_types(report)
return report
__init__(description, initial_model, pre_processing, settings_solver_models, fname, args_out, df_org, df_fit, df_pre=pd.DataFrame())
¶
Initialize the ExportReport class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
description | DescriptionAPI | Description of the fit project. | required |
initial_model | List[Dict[str, Dict[str, Dict[str, Any]]]] | Initial model for the fit. | required |
pre_processing | DataPreProcessingAPI | Data pre-processing settings. | required |
settings_solver_models | SolverModelsAPI | Solver models settings. | required |
fname | FnameAPI | Filename of the fit project including the path, prefix, and suffix. | required |
args_out | Dict[str, Any] | Dictionary of SpectraFit settings and results. | required |
df_org | DataFrame | Dataframe of the original data for performing the fit. | required |
df_fit | DataFrame | Dataframe of the final fit data. | required |
df_pre | Optional[DataFrame] | Dataframe of the pre-processed. Defaults to pd.DataFrame(). | DataFrame() |
Source code in spectrafit/plugins/notebook.py
def __init__(
self,
description: DescriptionAPI,
initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]],
pre_processing: DataPreProcessingAPI,
settings_solver_models: SolverModelsAPI,
fname: FnameAPI,
args_out: Dict[str, Any],
df_org: pd.DataFrame,
df_fit: pd.DataFrame,
df_pre: pd.DataFrame = pd.DataFrame(),
) -> None:
"""Initialize the ExportReport class.
Args:
description (DescriptionAPI): Description of the fit project.
initial_model (List[Dict[str, Dict[str, Dict[str, Any]]]]): Initial model
for the fit.
pre_processing (DataPreProcessingAPI): Data pre-processing settings.
settings_solver_models (SolverModelsAPI): Solver models settings.
fname (FnameAPI): Filename of the fit project including the path, prefix,
and suffix.
args_out (Dict[str, Any]): Dictionary of SpectraFit settings and results.
df_org (pd.DataFrame): Dataframe of the original data for performing
the fit.
df_fit (pd.DataFrame): Dataframe of the final fit data.
df_pre (Optional[pd.DataFrame], optional): Dataframe of the pre-processed.
Defaults to pd.DataFrame().
"""
super().__init__(args_out=args_out)
self.description = description
self.initial_model = initial_model
self.pre_processing = pre_processing
self.settings_solver_models = settings_solver_models
self.fname = fname
self.df_org = df_org.to_dict(orient="list")
self.df_fit = df_fit.to_dict(orient="list")
self.df_pre = df_pre.to_dict(orient="list")
ExportResults
¶
Class for exporting results as csv.
Source code in spectrafit/plugins/notebook.py
class ExportResults:
"""Class for exporting results as csv."""
def export_df(self, df: pd.DataFrame, args: FnameAPI) -> None:
"""Export the dataframe as csv.
Args:
df (pd.DataFrame): Dataframe to export.
args (FnameAPI): Arguments for the file export including the path, prefix,
and suffix.
"""
df.to_csv(
self.fname2path(
fname=args.fname,
prefix=args.prefix,
suffix=args.suffix,
folder=args.folder,
),
index=False,
)
def export_report(self, report: Dict[Any, Any], args: FnameAPI) -> None:
"""Export the results as toml file.
Args:
report (Dict[Any, Any]): Results as dictionary to export.
args (FnameAPI): Arguments for the file export including the path, prefix,
and suffix.
"""
with self.fname2path(
fname=args.fname,
prefix=args.prefix,
suffix=args.suffix,
folder=args.folder,
).open(
"wb+",
) as f:
tomli_w.dump(report, f)
@staticmethod
def fname2path(
fname: str,
suffix: str,
prefix: Optional[str] = None,
folder: Optional[str] = None,
) -> Path:
"""Translate string to Path object.
Args:
fname (str): Filename
suffix (str): Name of the suffix of the file.
prefix (Optional[str], optional): Name of the prefix of the file. Defaults
to None.
folder (Optional[str], optional): Folder, where it will be saved.
This folders will be created, if not exist. Defaults to None.
Returns:
Path: Path object of the file.
"""
if prefix:
fname = f"{prefix}_{fname}"
_fname = Path(fname).with_suffix(f".{suffix}")
if folder:
Path(folder).mkdir(parents=True, exist_ok=True)
_fname = Path(folder) / _fname
return _fname
export_df(df, args)
¶
Export the dataframe as csv.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe to export. | required |
args | FnameAPI | Arguments for the file export including the path, prefix, and suffix. | required |
Source code in spectrafit/plugins/notebook.py
def export_df(self, df: pd.DataFrame, args: FnameAPI) -> None:
"""Export the dataframe as csv.
Args:
df (pd.DataFrame): Dataframe to export.
args (FnameAPI): Arguments for the file export including the path, prefix,
and suffix.
"""
df.to_csv(
self.fname2path(
fname=args.fname,
prefix=args.prefix,
suffix=args.suffix,
folder=args.folder,
),
index=False,
)
export_report(report, args)
¶
Export the results as toml file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
report | Dict[Any, Any] | Results as dictionary to export. | required |
args | FnameAPI | Arguments for the file export including the path, prefix, and suffix. | required |
Source code in spectrafit/plugins/notebook.py
def export_report(self, report: Dict[Any, Any], args: FnameAPI) -> None:
"""Export the results as toml file.
Args:
report (Dict[Any, Any]): Results as dictionary to export.
args (FnameAPI): Arguments for the file export including the path, prefix,
and suffix.
"""
with self.fname2path(
fname=args.fname,
prefix=args.prefix,
suffix=args.suffix,
folder=args.folder,
).open(
"wb+",
) as f:
tomli_w.dump(report, f)
fname2path(fname, suffix, prefix=None, folder=None)
staticmethod
¶
Translate string to Path object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fname | str | Filename | required |
suffix | str | Name of the suffix of the file. | required |
prefix | Optional[str] | Name of the prefix of the file. Defaults to None. | None |
folder | Optional[str] | Folder, where it will be saved. This folders will be created, if not exist. Defaults to None. | None |
Returns:
Name | Type | Description |
---|---|---|
Path | Path | Path object of the file. |
Source code in spectrafit/plugins/notebook.py
@staticmethod
def fname2path(
fname: str,
suffix: str,
prefix: Optional[str] = None,
folder: Optional[str] = None,
) -> Path:
"""Translate string to Path object.
Args:
fname (str): Filename
suffix (str): Name of the suffix of the file.
prefix (Optional[str], optional): Name of the prefix of the file. Defaults
to None.
folder (Optional[str], optional): Folder, where it will be saved.
This folders will be created, if not exist. Defaults to None.
Returns:
Path: Path object of the file.
"""
if prefix:
fname = f"{prefix}_{fname}"
_fname = Path(fname).with_suffix(f".{suffix}")
if folder:
Path(folder).mkdir(parents=True, exist_ok=True)
_fname = Path(folder) / _fname
return _fname
SolverResults
¶
Class for storing the results of the solver.
Source code in spectrafit/plugins/notebook.py
class SolverResults:
"""Class for storing the results of the solver."""
def __init__(self, args_out: Dict[str, Any]) -> None:
"""Initialize the SolverResults class.
Args:
args_out (Dict[str, Any]): Dictionary of SpectraFit settings and results.
"""
self.args_out = args_out
@property
def settings_global_fitting(self) -> Union[bool, int]:
"""Global fitting settings.
Returns:
Union[bool, int]: Global fitting settings.
"""
return self.args_out["global_"]
@property
def settings_configurations(self) -> Dict[str, Any]:
"""Configure settings.
Returns:
Dict[str, Any]: Configuration settings.
"""
return self.args_out["fit_insights"]["configurations"]
@property
def get_gof(self) -> Dict[str, float]:
"""Get the goodness of fit values.
Returns:
Dict[str, float]: Goodness of fit values as dictionary.
"""
return self.args_out["fit_insights"]["statistics"]
@property
def get_variables(self) -> Dict[str, Dict[str, float]]:
"""Get the variables of the fit.
Returns:
Dict[str, Dict[str, float]]: Variables of the fit.
"""
return self.args_out["fit_insights"]["variables"]
@property
def get_errorbars(self) -> Dict[str, float]:
"""Get the comments about the error bars of fit values.
Returns:
Dict[str, float]: Comments about the error bars as dictionary or dataframe.
"""
return self.args_out["fit_insights"]["errorbars"]
@property
def get_component_correlation(self) -> Dict[str, Any]:
"""Get the linear correlation of the components.
Returns:
Dict[str, Any]: Linear correlation of the components as dictionary.
"""
return self.args_out["fit_insights"]["correlations"]
@property
def get_covariance_matrix(self) -> Dict[str, Any]:
"""Get the covariance matrix.
Returns:
Dict[str, Any]: Covariance matrix as dictionary.
"""
return self.args_out["fit_insights"]["covariance_matrix"]
@property
def get_regression_metrics(self) -> Dict[str, Any]:
"""Get the regression metrics.
Returns:
Dict[str, Any]: Regression metrics as dictionary.
"""
return self.args_out["regression_metrics"]
@property
def get_descriptive_statistic(self) -> Dict[str, Any]:
"""Get the descriptive statistic.
Returns:
Dict[str, Any]: Descriptive statistic as dictionary of the spectra, fit, and
components as dictionary.
"""
return self.args_out["descriptive_statistic"]
@property
def get_linear_correlation(self) -> Dict[str, Any]:
"""Get the linear correlation.
Returns:
Dict[str, Any]: Linear correlation of the spectra, fit, and components
as dictionary.
"""
return self.args_out["linear_correlation"]
@property
def get_computational(self) -> Dict[str, Any]:
"""Get the computational time.
Returns:
Dict[str, Any]: Computational time as dictionary.
"""
return self.args_out["fit_insights"]["computational"]
@property
def settings_conf_interval(self) -> Union[bool, Dict[str, Any]]:
"""Confidence interval settings.
Returns:
Union[bool, Dict[str, Any]]: Confidence interval settings.
"""
if isinstance(self.args_out["conf_interval"], dict):
self.args_out["conf_interval"] = {
key: value if value is not None else {}
for key, value in self.args_out["conf_interval"].items()
}
return self.args_out["conf_interval"]
@property
def get_confidence_interval(self) -> Dict[Any, Any]:
"""Get the confidence interval.
Returns:
Dict[Any, Any]: Confidence interval as dictionary with or without the
confidence interval results.
"""
if self.args_out["conf_interval"] is False:
return {}
return self.args_out["confidence_interval"]
@property
def get_current_metric(self) -> pd.DataFrame:
"""Get the current metric.
!!! note "About the regression metrics"
For using the regression metrics, the `regression_metrics` must be averaged
to merge the results of the different configurations together with the
`goodness_of_fit` and `variables` results.
Returns:
pd.DataFrame: Current metric based on `regression_metrics` and
`goodness_of_fit` as dataframe.
"""
gof = {key: [value] for key, value in self.get_gof.items()}
reg = {
key: [np.average(val)]
for key, val in zip(
self.get_regression_metrics["index"],
self.get_regression_metrics["data"],
)
}
metric = {**gof, **reg}
return pd.DataFrame(metric)
get_component_correlation: Dict[str, Any]
property
¶
Get the linear correlation of the components.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Linear correlation of the components as dictionary. |
get_computational: Dict[str, Any]
property
¶
Get the computational time.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Computational time as dictionary. |
get_confidence_interval: Dict[Any, Any]
property
¶
Get the confidence interval.
Returns:
Type | Description |
---|---|
Dict[Any, Any] | Dict[Any, Any]: Confidence interval as dictionary with or without the confidence interval results. |
get_covariance_matrix: Dict[str, Any]
property
¶
Get the covariance matrix.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Covariance matrix as dictionary. |
get_current_metric: pd.DataFrame
property
¶
Get the current metric.
About the regression metrics
For using the regression metrics, the regression_metrics
must be averaged to merge the results of the different configurations together with the goodness_of_fit
and variables
results.
Returns:
Type | Description |
---|---|
DataFrame | pd.DataFrame: Current metric based on |
DataFrame |
|
get_descriptive_statistic: Dict[str, Any]
property
¶
Get the descriptive statistic.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Descriptive statistic as dictionary of the spectra, fit, and components as dictionary. |
get_errorbars: Dict[str, float]
property
¶
Get the comments about the error bars of fit values.
Returns:
Type | Description |
---|---|
Dict[str, float] | Dict[str, float]: Comments about the error bars as dictionary or dataframe. |
get_gof: Dict[str, float]
property
¶
Get the goodness of fit values.
Returns:
Type | Description |
---|---|
Dict[str, float] | Dict[str, float]: Goodness of fit values as dictionary. |
get_linear_correlation: Dict[str, Any]
property
¶
Get the linear correlation.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Linear correlation of the spectra, fit, and components as dictionary. |
get_regression_metrics: Dict[str, Any]
property
¶
Get the regression metrics.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Regression metrics as dictionary. |
get_variables: Dict[str, Dict[str, float]]
property
¶
Get the variables of the fit.
Returns:
Type | Description |
---|---|
Dict[str, Dict[str, float]] | Dict[str, Dict[str, float]]: Variables of the fit. |
settings_conf_interval: Union[bool, Dict[str, Any]]
property
¶
Confidence interval settings.
Returns:
Type | Description |
---|---|
Union[bool, Dict[str, Any]] | Union[bool, Dict[str, Any]]: Confidence interval settings. |
settings_configurations: Dict[str, Any]
property
¶
Configure settings.
Returns:
Type | Description |
---|---|
Dict[str, Any] | Dict[str, Any]: Configuration settings. |
settings_global_fitting: Union[bool, int]
property
¶
Global fitting settings.
Returns:
Type | Description |
---|---|
Union[bool, int] | Union[bool, int]: Global fitting settings. |
__init__(args_out)
¶
Initialize the SolverResults class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args_out | Dict[str, Any] | Dictionary of SpectraFit settings and results. | required |
SpectraFitNotebook
¶
Bases: DataFramePlot
, DataFrameDisplay
, ExportResults
Jupyter Notebook plugin for SpectraFit.
Source code in spectrafit/plugins/notebook.py
class SpectraFitNotebook(DataFramePlot, DataFrameDisplay, ExportResults):
"""Jupyter Notebook plugin for SpectraFit."""
args: Dict[str, Any]
global_: Union[bool, int] = False
autopeak: bool = False
df_fit: pd.DataFrame
df_pre: pd.DataFrame = pd.DataFrame()
df_metric: pd.DataFrame = pd.DataFrame()
df_peaks: pd.DataFrame = pd.DataFrame()
initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]]
def __init__(
self,
df: pd.DataFrame,
x_column: str,
y_column: Union[str, List[str]],
oversampling: bool = False,
smooth: int = 0,
shift: float = 0,
energy_start: Optional[float] = None,
energy_stop: Optional[float] = None,
title: Optional[str] = None,
xaxis_title: XAxisAPI = XAxisAPI(name="Energy", unit="eV"),
yaxis_title: YAxisAPI = YAxisAPI(name="Intensity", unit="a.u."),
residual_title: ResidualAPI = ResidualAPI(name="Residual", unit="a.u."),
metric_title: MetricAPI = MetricAPI(
name_0="Metrics", unit_0="a.u.", name_1="Metrics", unit_1="a.u."
),
run_title: RunAPI = RunAPI(name="Run", unit="#"),
legend_title: str = "Spectra",
show_legend: bool = True,
legend: LegendAPI = LegendAPI(
orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1
),
font: FontAPI = FontAPI(family="Open Sans, monospace", size=12, color="black"),
minor_ticks: bool = True,
color: ColorAPI = ColorAPI(),
grid: GridAPI = GridAPI(),
size: Tuple[int, Tuple[int, int]] = (800, (600, 300)),
fname: str = "results",
folder: Optional[str] = None,
description: DescriptionAPI = DescriptionAPI(),
) -> None:
"""Initialize the SpectraFitNotebook class.
!!! info "About `Pydantic`-Definition"
For being consistent with the `SpectraFit` class, the `SpectraFitNotebook`
class refers to the `Pydantic`-Definition of the `SpectraFit` class.
Currently, the following definitions are used:
- `XAxisAPI`: Definition of the x-axis including units
- `YAxisAPI`: Definition of the y-axis including units
- `ResidualAPI`: Definition of the residual including units
- `LegendAPI`: Definition of the legend according to `Plotly`
- `FontAPI`: Definition of the font according to `Plotly`, which can be
replaced by _built-in_ definitions
- `ColorAPI`: Definition of the colors according to `Plotly`, which can be
replace by _built-in_ definitions
- `GridAPI`: Definition of the grid according to `Plotly`
- `DescriptionAPI`: Definition of the description of the fit project
All classes can be replaced by the corresponding `dict`-definition.
```python
LegendAPI(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
```
can be also
```python
dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
```
Args:
df (pd.DataFrame): Dataframe with the data to fit.
x_column (str): Name of the x column.
y_column (Union[str, List[str]]): Name of the y column(s).
oversampling (bool, optional): Activate the oversampling options.
Defaults to False.
smooth (int, optional): Activate the smoothing functions setting an
`int>0`. Defaults to 0.
shift (float, optional): Apply shift to the x-column. Defaults to 0.
energy_start (Optional[float], optional): Energy start. Defaults to None.
energy_stop (Optional[float], optional): Energy stop. Defaults to None.
title (Optional[str], optional): Plot title. Defaults to None.
xaxis_title (XAxisAPI, optional): X-Axis title. Defaults to XAxisAPI().
yaxis_title (YAxisAPI, optional): Y-Axis title. Defaults to YAxisAPI().
residual_title (ResidualAPI, optional): Residual title. Defaults to
ResidualAPI().
metric_title (MetricAPI, optional): Metric title for both axes, bar and
line plot. Defaults to MetricAPI().
run_title (RunAPI, optional): Run title. Defaults to RunAPI().
legend_title (str, optional): Legend title. Defaults to "Spectra".
show_legend (bool, optional): Show legend. Defaults to True.
legend (LegendAPI, optional): Legend options. Defaults to LegendAPI().
font (FontAPI, optional): Font options. Defaults to FontAPI().
minor_ticks (bool, optional): Show minor ticks. Defaults to True.
color (ColorAPI, optional): Color options. Defaults to ColorAPI().
grid (GridAPI, optional): Grid options. Defaults to GridAPI().
size (Tuple[int, Tuple[int, int]] , optional): Size of the fit- and metric-
plot. First width defines the fit, the second the metrics.
Defaults to (800, (600,300)).
fname (str, optional): Filename of the export. Defaults to "results".
folder (Optional[str], optional): Folder of the export. Defaults to None.
description (DescriptionAPI, optional): Description of the data. Defaults
to DescriptionAPI()..
Raises:
ValueError: If the dataframe only contains one column.
"""
self.x_column = x_column
self.y_column = y_column
if df.shape[1] < 2:
raise ValueError("The dataframe must have 2 or more columns.")
if isinstance(self.y_column, list):
self.global_ = 1
self.df = df[[self.x_column, *self.y_column]]
else:
self.df = df[[self.x_column, self.y_column]]
self.df_org = self.df.copy()
self.args_pre = DataPreProcessingAPI(
oversampling=oversampling,
energy_start=energy_start,
energy_stop=energy_stop,
smooth=smooth,
shift=shift,
column=list(self.df.columns),
)
self.args_desc = description
self.args_plot = PlotAPI(
x=self.x_column,
y=self.y_column,
title=title,
xaxis_title=xaxis_title,
yaxis_title=yaxis_title,
residual_title=residual_title,
metric_title=metric_title,
run_title=run_title,
legend_title=legend_title,
show_legend=show_legend,
legend=legend,
font=font,
minor_ticks=minor_ticks,
color=color,
grid=grid,
size=size,
)
self.export_args_df = FnameAPI(fname=fname, folder=folder, suffix="csv")
self.export_args_out = FnameAPI(fname=fname, folder=folder, suffix="lock")
self.settings_solver_models: SolverModelsAPI = SolverModelsAPI()
self.pre_statistic: Dict[str, Any] = {}
@property
def pre_process(self) -> None:
"""Pre-processing class."""
self.df, _pre_statistic = PreProcessing(
df=self.df, args=self.args_pre.model_dump()
)()
self.pre_statistic = _pre_statistic["data_statistic"]
self.df_pre = self.df.copy()
@property
def return_pre_statistic(self) -> Dict[str, Any]:
"""Return the pre-processing statistic."""
return self.pre_statistic
@property
def return_df_org(self) -> pd.DataFrame:
"""Return the original dataframe."""
return self.df_org
@property
def return_df_pre(self) -> Union[pd.DataFrame, None]:
"""Return the pre-processed dataframe."""
return self.df_pre
@property
def return_df(self) -> pd.DataFrame:
"""Return the dataframe."""
return self.df
@property
def return_df_fit(self) -> pd.DataFrame:
"""Return the fit dataframe."""
return self.df_fit
@property
def export_df_act(self) -> None:
"""Export the dataframe."""
self.export_args_df.prefix = "act"
self.export_df(df=self.df, args=self.export_args_df)
@property
def export_df_fit(self) -> None:
"""Export the dataframe."""
self.export_args_df.prefix = "fit"
self.export_df(df=self.df_fit, args=self.export_args_df)
@property
def export_df_org(self) -> None:
"""Export the dataframe."""
self.export_args_df.prefix = "org"
self.export_df(df=self.df_org, args=self.export_args_df)
@property
def export_df_pre(self) -> None:
"""Export the dataframe."""
if self.df_pre.empty is False:
self.export_args_df.prefix = "pre"
self.export_df(df=self.df_pre, args=self.export_args_df)
@property
def export_df_metric(self) -> None:
"""Export the dataframe."""
if self.df_metric.empty is False:
self.export_args_df.prefix = "metric"
self.export_df(df=self.df_metric, args=self.export_args_df)
@property
def export_df_peaks(self) -> None:
"""Export the dataframe."""
if self.df_peaks.empty is False:
self.export_args_df.prefix = "peaks"
self.export_df(df=self.df_peaks, args=self.export_args_df)
@property
def plot_original_df(self) -> None:
"""Plot the original spectra."""
self.plot_dataframe(args_plot=self.args_plot, df=self.df_org)
@property
def plot_current_df(self) -> None:
"""Plot the current spectra."""
self.plot_dataframe(args_plot=self.args_plot, df=self.df)
@property
def plot_preprocessed_df(self) -> None:
"""Plot the current processed spectra."""
self.plot_2dataframes(
args_plot=self.args_plot, df_1=self.df_pre, df_2=self.df_org
)
def plot_fit_df(self) -> None:
"""Plot the fit."""
if self.global_ == 1:
self.plot_global_fit(args_plot=self.args_plot, df=self.df_fit)
else:
self.plot_2dataframes(args_plot=self.args_plot, df_1=self.df_fit)
def plot_current_metric(
self,
bar_criteria: Optional[Union[str, List[str]]] = None,
line_criteria: Optional[Union[str, List[str]]] = None,
) -> None:
"""Plot the current metric.
Args:
bar_criteria (Optional[Union[str, List[str]]], optional): Criteria for the
bar plot. Defaults to None.
line_criteria (Optional[Union[str, List[str]]], optional): Criteria for
the line plot. Defaults to None.
"""
if bar_criteria is None:
bar_criteria = [
"akaike_information",
"bayesian_information",
]
if line_criteria is None:
line_criteria = [
"mean_squared_error",
]
self.plot_metric(
args_plot=self.args_plot,
df_metric=self.df_metric,
bar_criteria=bar_criteria,
line_criteria=line_criteria,
)
@property
def generate_report(self) -> None:
"""Generate the SpectraFit report of the final fit."""
self.export_report(
report=ExportReport(
description=self.args_desc,
initial_model=self.initial_model,
pre_processing=self.args_pre,
settings_solver_models=self.settings_solver_models,
fname=self.export_args_out,
args_out=self.args,
df_org=self.df_org,
df_pre=self.df_pre,
df_fit=self.df_fit,
)(),
args=self.export_args_out,
)
def solver_model(
self,
initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]],
show_plot: bool = True,
show_metric: bool = True,
show_df: bool = False,
show_peaks: bool = False,
conf_interval: Union[bool, Dict[str, Any]] = False,
bar_criteria: Optional[Union[str, List[str]]] = None,
line_criteria: Optional[Union[str, List[str]]] = None,
solver_settings: Optional[Dict[str, Any]] = None,
) -> None:
"""Solves the fit problem based on the proposed model.
Args:
initial_model (List[Dict[str, Dict[str, Dict[str, Any]]]]): List of
dictionary with the initial model and its fitting parameters and
options for the components.
show_plot (bool, optional): Show current fit results as plot.
Defaults to True.
show_metric (bool, optional): Show the metric of the fit. Defaults to True.
show_df (bool, optional): Show current fit results as dataframe. Defaults
to False.
show_peaks (bool, optional): Show the peaks of fit. Defaults to False.
conf_interval (Union[bool,Dict[str, Any]], optional): Bool or dictionary for
the parameter with the parameter for calculating the confidence
interval. Using `conf_interval=False` turns of the calculation of
the confidence interval and accelerate its. Defaults to False.
bar_criteria (Optional[Union[str, List[str]]], optional): Criteria for the
bar plot. It is recommended to use attributes from `goodness of fit`
module. Defaults to None.
line_criteria (Optional[Union[str, List[str]]], optional): Criteria for
the line plot. It is recommended to use attributes from
`regression metric` module. Defaults to None.
solver_settings (Optional[Dict[str, Any]], optional): Settings for
the solver models, which is split into settings for `minimizer` and
`optimizer`. Defaults to None.
!!! info: "About criteria"
The criteria for the bar and line plot are defined as a list of strings.
The supported keywords are defined by the built-in metrics for
`goodness of fit` and `regression` and can be checked in [documentation](
https://anselmoo.github.io/spectrafit/doc/statistics/
).
"""
self.initial_model = initial_model
if isinstance(conf_interval, bool):
conf_interval = (
ConfIntervalAPI().model_dump() if conf_interval is True else False
)
elif isinstance(conf_interval, dict):
conf_interval = ConfIntervalAPI(**conf_interval).dict(exclude_none=True)
if solver_settings is not None and isinstance(solver_settings, dict):
self.settings_solver_models = SolverModelsAPI(**solver_settings)
self.df_fit, self.args = PostProcessing(
self.df,
{
"global_": self.global_,
"conf_interval": conf_interval,
},
*SolverModels(
df=self.df,
args={
"global_": self.global_,
"column": list(self.df.columns),
"autopeak": self.autopeak,
**list2dict(peak_list=self.initial_model),
**self.settings_solver_models.model_dump(),
},
)(),
)()
self.update_metric()
self.update_peaks()
if show_plot:
self.plot_fit_df()
if show_metric:
self.plot_current_metric(
bar_criteria=bar_criteria, line_criteria=line_criteria
)
if show_df:
self.interactive_display(df=self.df_fit)
if show_peaks:
self.interactive_display(df=self.df_peaks)
def update_peaks(self) -> None:
"""Update the peaks dataframe as multi-column dataframe.
The multi-column dataframe is used for the interactive display of the
peaks with initial, current (model), and best fit values.
"""
tuples = []
_list = []
for key_1, _dict in self.args["fit_insights"]["variables"].items():
tuples.extend([(key_1, key_2) for key_2, val in _dict.items()])
_list.extend([val for _, val in _dict.items()])
self.df_peaks = pd.concat(
[
self.df_peaks,
pd.DataFrame(
pd.Series(
_list,
index=pd.MultiIndex.from_tuples(
tuples, names=["component", "parameter"]
),
)
).T,
],
ignore_index=True,
)
def update_metric(self) -> None:
"""Update the metric dataframe."""
self.df_metric = pd.concat(
[self.df_metric, SolverResults(self.args).get_current_metric],
ignore_index=True,
)
def display_fit_df(self, mode: Optional[str] = "regular") -> None:
"""Display the fit dataframe.
Args:
mode (str, optional): Display mode. Defaults to "regular".
"""
self.df_display(df=self.df_fit, mode=mode)
def display_preprocessed_df(self, mode: Optional[str] = "regular") -> None:
"""Display the preprocessed dataframe.
Args:
mode (str, optional): Display mode. Defaults to "regular".
"""
self.df_display(df=self.df_pre, mode=mode)
def display_original_df(self, mode: Optional[str] = "regular") -> None:
"""Display the original dataframe.
Args:
mode (str, optional): Display mode. Defaults to "regular".
"""
self.df_display(df=self.df_org, mode=mode)
def display_current_df(self, mode: Optional[str] = "regular") -> None:
"""Display the current dataframe.
Args:
mode (str, optional): Display mode. Defaults to "regular".
"""
self.df_display(df=self.df, mode=mode)
export_df_act: None
property
¶
Export the dataframe.
export_df_fit: None
property
¶
Export the dataframe.
export_df_metric: None
property
¶
Export the dataframe.
export_df_org: None
property
¶
Export the dataframe.
export_df_peaks: None
property
¶
Export the dataframe.
export_df_pre: None
property
¶
Export the dataframe.
generate_report: None
property
¶
Generate the SpectraFit report of the final fit.
plot_current_df: None
property
¶
Plot the current spectra.
plot_original_df: None
property
¶
Plot the original spectra.
plot_preprocessed_df: None
property
¶
Plot the current processed spectra.
pre_process: None
property
¶
Pre-processing class.
return_df: pd.DataFrame
property
¶
Return the dataframe.
return_df_fit: pd.DataFrame
property
¶
Return the fit dataframe.
return_df_org: pd.DataFrame
property
¶
Return the original dataframe.
return_df_pre: Union[pd.DataFrame, None]
property
¶
Return the pre-processed dataframe.
return_pre_statistic: Dict[str, Any]
property
¶
Return the pre-processing statistic.
__init__(df, x_column, y_column, oversampling=False, smooth=0, shift=0, energy_start=None, energy_stop=None, title=None, xaxis_title=XAxisAPI(name='Energy', unit='eV'), yaxis_title=YAxisAPI(name='Intensity', unit='a.u.'), residual_title=ResidualAPI(name='Residual', unit='a.u.'), metric_title=MetricAPI(name_0='Metrics', unit_0='a.u.', name_1='Metrics', unit_1='a.u.'), run_title=RunAPI(name='Run', unit='#'), legend_title='Spectra', show_legend=True, legend=LegendAPI(orientation='h', yanchor='bottom', y=1.02, xanchor='right', x=1), font=FontAPI(family='Open Sans, monospace', size=12, color='black'), minor_ticks=True, color=ColorAPI(), grid=GridAPI(), size=(800, (600, 300)), fname='results', folder=None, description=DescriptionAPI())
¶
Initialize the SpectraFitNotebook class.
About Pydantic
-Definition
For being consistent with the SpectraFit
class, the SpectraFitNotebook
class refers to the Pydantic
-Definition of the SpectraFit
class. Currently, the following definitions are used:
XAxisAPI
: Definition of the x-axis including unitsYAxisAPI
: Definition of the y-axis including unitsResidualAPI
: Definition of the residual including unitsLegendAPI
: Definition of the legend according toPlotly
FontAPI
: Definition of the font according toPlotly
, which can be replaced by built-in definitionsColorAPI
: Definition of the colors according toPlotly
, which can be replace by built-in definitionsGridAPI
: Definition of the grid according toPlotly
DescriptionAPI
: Definition of the description of the fit project
All classes can be replaced by the corresponding dict
-definition.
can be also
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df | DataFrame | Dataframe with the data to fit. | required |
x_column | str | Name of the x column. | required |
y_column | Union[str, List[str]] | Name of the y column(s). | required |
oversampling | bool | Activate the oversampling options. Defaults to False. | False |
smooth | int | Activate the smoothing functions setting an | 0 |
shift | float | Apply shift to the x-column. Defaults to 0. | 0 |
energy_start | Optional[float] | Energy start. Defaults to None. | None |
energy_stop | Optional[float] | Energy stop. Defaults to None. | None |
title | Optional[str] | Plot title. Defaults to None. | None |
xaxis_title | XAxisAPI | X-Axis title. Defaults to XAxisAPI(). | XAxisAPI(name='Energy', unit='eV') |
yaxis_title | YAxisAPI | Y-Axis title. Defaults to YAxisAPI(). | YAxisAPI(name='Intensity', unit='a.u.') |
residual_title | ResidualAPI | Residual title. Defaults to ResidualAPI(). | ResidualAPI(name='Residual', unit='a.u.') |
metric_title | MetricAPI | Metric title for both axes, bar and line plot. Defaults to MetricAPI(). | MetricAPI(name_0='Metrics', unit_0='a.u.', name_1='Metrics', unit_1='a.u.') |
run_title | RunAPI | Run title. Defaults to RunAPI(). | RunAPI(name='Run', unit='#') |
legend_title | str | Legend title. Defaults to "Spectra". | 'Spectra' |
show_legend | bool | Show legend. Defaults to True. | True |
legend | LegendAPI | Legend options. Defaults to LegendAPI(). | LegendAPI(orientation='h', yanchor='bottom', y=1.02, xanchor='right', x=1) |
font | FontAPI | Font options. Defaults to FontAPI(). | FontAPI(family='Open Sans, monospace', size=12, color='black') |
minor_ticks | bool | Show minor ticks. Defaults to True. | True |
color | ColorAPI | Color options. Defaults to ColorAPI(). | ColorAPI() |
grid | GridAPI | Grid options. Defaults to GridAPI(). | GridAPI() |
size | Tuple[int, Tuple[int, int]] | Size of the fit- and metric- plot. First width defines the fit, the second the metrics. Defaults to (800, (600,300)). | (800, (600, 300)) |
fname | str | Filename of the export. Defaults to "results". | 'results' |
folder | Optional[str] | Folder of the export. Defaults to None. | None |
description | DescriptionAPI | Description of the data. Defaults to DescriptionAPI().. | DescriptionAPI() |
Raises:
Type | Description |
---|---|
ValueError | If the dataframe only contains one column. |
Source code in spectrafit/plugins/notebook.py
def __init__(
self,
df: pd.DataFrame,
x_column: str,
y_column: Union[str, List[str]],
oversampling: bool = False,
smooth: int = 0,
shift: float = 0,
energy_start: Optional[float] = None,
energy_stop: Optional[float] = None,
title: Optional[str] = None,
xaxis_title: XAxisAPI = XAxisAPI(name="Energy", unit="eV"),
yaxis_title: YAxisAPI = YAxisAPI(name="Intensity", unit="a.u."),
residual_title: ResidualAPI = ResidualAPI(name="Residual", unit="a.u."),
metric_title: MetricAPI = MetricAPI(
name_0="Metrics", unit_0="a.u.", name_1="Metrics", unit_1="a.u."
),
run_title: RunAPI = RunAPI(name="Run", unit="#"),
legend_title: str = "Spectra",
show_legend: bool = True,
legend: LegendAPI = LegendAPI(
orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1
),
font: FontAPI = FontAPI(family="Open Sans, monospace", size=12, color="black"),
minor_ticks: bool = True,
color: ColorAPI = ColorAPI(),
grid: GridAPI = GridAPI(),
size: Tuple[int, Tuple[int, int]] = (800, (600, 300)),
fname: str = "results",
folder: Optional[str] = None,
description: DescriptionAPI = DescriptionAPI(),
) -> None:
"""Initialize the SpectraFitNotebook class.
!!! info "About `Pydantic`-Definition"
For being consistent with the `SpectraFit` class, the `SpectraFitNotebook`
class refers to the `Pydantic`-Definition of the `SpectraFit` class.
Currently, the following definitions are used:
- `XAxisAPI`: Definition of the x-axis including units
- `YAxisAPI`: Definition of the y-axis including units
- `ResidualAPI`: Definition of the residual including units
- `LegendAPI`: Definition of the legend according to `Plotly`
- `FontAPI`: Definition of the font according to `Plotly`, which can be
replaced by _built-in_ definitions
- `ColorAPI`: Definition of the colors according to `Plotly`, which can be
replace by _built-in_ definitions
- `GridAPI`: Definition of the grid according to `Plotly`
- `DescriptionAPI`: Definition of the description of the fit project
All classes can be replaced by the corresponding `dict`-definition.
```python
LegendAPI(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
```
can be also
```python
dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
```
Args:
df (pd.DataFrame): Dataframe with the data to fit.
x_column (str): Name of the x column.
y_column (Union[str, List[str]]): Name of the y column(s).
oversampling (bool, optional): Activate the oversampling options.
Defaults to False.
smooth (int, optional): Activate the smoothing functions setting an
`int>0`. Defaults to 0.
shift (float, optional): Apply shift to the x-column. Defaults to 0.
energy_start (Optional[float], optional): Energy start. Defaults to None.
energy_stop (Optional[float], optional): Energy stop. Defaults to None.
title (Optional[str], optional): Plot title. Defaults to None.
xaxis_title (XAxisAPI, optional): X-Axis title. Defaults to XAxisAPI().
yaxis_title (YAxisAPI, optional): Y-Axis title. Defaults to YAxisAPI().
residual_title (ResidualAPI, optional): Residual title. Defaults to
ResidualAPI().
metric_title (MetricAPI, optional): Metric title for both axes, bar and
line plot. Defaults to MetricAPI().
run_title (RunAPI, optional): Run title. Defaults to RunAPI().
legend_title (str, optional): Legend title. Defaults to "Spectra".
show_legend (bool, optional): Show legend. Defaults to True.
legend (LegendAPI, optional): Legend options. Defaults to LegendAPI().
font (FontAPI, optional): Font options. Defaults to FontAPI().
minor_ticks (bool, optional): Show minor ticks. Defaults to True.
color (ColorAPI, optional): Color options. Defaults to ColorAPI().
grid (GridAPI, optional): Grid options. Defaults to GridAPI().
size (Tuple[int, Tuple[int, int]] , optional): Size of the fit- and metric-
plot. First width defines the fit, the second the metrics.
Defaults to (800, (600,300)).
fname (str, optional): Filename of the export. Defaults to "results".
folder (Optional[str], optional): Folder of the export. Defaults to None.
description (DescriptionAPI, optional): Description of the data. Defaults
to DescriptionAPI()..
Raises:
ValueError: If the dataframe only contains one column.
"""
self.x_column = x_column
self.y_column = y_column
if df.shape[1] < 2:
raise ValueError("The dataframe must have 2 or more columns.")
if isinstance(self.y_column, list):
self.global_ = 1
self.df = df[[self.x_column, *self.y_column]]
else:
self.df = df[[self.x_column, self.y_column]]
self.df_org = self.df.copy()
self.args_pre = DataPreProcessingAPI(
oversampling=oversampling,
energy_start=energy_start,
energy_stop=energy_stop,
smooth=smooth,
shift=shift,
column=list(self.df.columns),
)
self.args_desc = description
self.args_plot = PlotAPI(
x=self.x_column,
y=self.y_column,
title=title,
xaxis_title=xaxis_title,
yaxis_title=yaxis_title,
residual_title=residual_title,
metric_title=metric_title,
run_title=run_title,
legend_title=legend_title,
show_legend=show_legend,
legend=legend,
font=font,
minor_ticks=minor_ticks,
color=color,
grid=grid,
size=size,
)
self.export_args_df = FnameAPI(fname=fname, folder=folder, suffix="csv")
self.export_args_out = FnameAPI(fname=fname, folder=folder, suffix="lock")
self.settings_solver_models: SolverModelsAPI = SolverModelsAPI()
self.pre_statistic: Dict[str, Any] = {}
display_current_df(mode='regular')
¶
Display the current dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode | str | Display mode. Defaults to "regular". | 'regular' |
display_fit_df(mode='regular')
¶
Display the fit dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode | str | Display mode. Defaults to "regular". | 'regular' |
display_original_df(mode='regular')
¶
Display the original dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode | str | Display mode. Defaults to "regular". | 'regular' |
display_preprocessed_df(mode='regular')
¶
Display the preprocessed dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode | str | Display mode. Defaults to "regular". | 'regular' |
plot_current_metric(bar_criteria=None, line_criteria=None)
¶
Plot the current metric.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bar_criteria | Optional[Union[str, List[str]]] | Criteria for the bar plot. Defaults to None. | None |
line_criteria | Optional[Union[str, List[str]]] | Criteria for the line plot. Defaults to None. | None |
Source code in spectrafit/plugins/notebook.py
def plot_current_metric(
self,
bar_criteria: Optional[Union[str, List[str]]] = None,
line_criteria: Optional[Union[str, List[str]]] = None,
) -> None:
"""Plot the current metric.
Args:
bar_criteria (Optional[Union[str, List[str]]], optional): Criteria for the
bar plot. Defaults to None.
line_criteria (Optional[Union[str, List[str]]], optional): Criteria for
the line plot. Defaults to None.
"""
if bar_criteria is None:
bar_criteria = [
"akaike_information",
"bayesian_information",
]
if line_criteria is None:
line_criteria = [
"mean_squared_error",
]
self.plot_metric(
args_plot=self.args_plot,
df_metric=self.df_metric,
bar_criteria=bar_criteria,
line_criteria=line_criteria,
)
plot_fit_df()
¶
solver_model(initial_model, show_plot=True, show_metric=True, show_df=False, show_peaks=False, conf_interval=False, bar_criteria=None, line_criteria=None, solver_settings=None)
¶
Solves the fit problem based on the proposed model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
initial_model | List[Dict[str, Dict[str, Dict[str, Any]]]] | List of dictionary with the initial model and its fitting parameters and options for the components. | required |
show_plot | bool | Show current fit results as plot. Defaults to True. | True |
show_metric | bool | Show the metric of the fit. Defaults to True. | True |
show_df | bool | Show current fit results as dataframe. Defaults to False. | False |
show_peaks | bool | Show the peaks of fit. Defaults to False. | False |
conf_interval | Union[bool, Dict[str, Any]] | Bool or dictionary for the parameter with the parameter for calculating the confidence interval. Using | False |
bar_criteria | Optional[Union[str, List[str]]] | Criteria for the bar plot. It is recommended to use attributes from | None |
line_criteria | Optional[Union[str, List[str]]] | Criteria for the line plot. It is recommended to use attributes from | None |
solver_settings | Optional[Dict[str, Any]] | Settings for the solver models, which is split into settings for | None |
!!! info: "About criteria"
The criteria for the bar and line plot are defined as a list of strings.
The supported keywords are defined by the built-in metrics for
`goodness of fit` and `regression` and can be checked in [documentation](
https://anselmoo.github.io/spectrafit/doc/statistics/
).
Source code in spectrafit/plugins/notebook.py
def solver_model(
self,
initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]],
show_plot: bool = True,
show_metric: bool = True,
show_df: bool = False,
show_peaks: bool = False,
conf_interval: Union[bool, Dict[str, Any]] = False,
bar_criteria: Optional[Union[str, List[str]]] = None,
line_criteria: Optional[Union[str, List[str]]] = None,
solver_settings: Optional[Dict[str, Any]] = None,
) -> None:
"""Solves the fit problem based on the proposed model.
Args:
initial_model (List[Dict[str, Dict[str, Dict[str, Any]]]]): List of
dictionary with the initial model and its fitting parameters and
options for the components.
show_plot (bool, optional): Show current fit results as plot.
Defaults to True.
show_metric (bool, optional): Show the metric of the fit. Defaults to True.
show_df (bool, optional): Show current fit results as dataframe. Defaults
to False.
show_peaks (bool, optional): Show the peaks of fit. Defaults to False.
conf_interval (Union[bool,Dict[str, Any]], optional): Bool or dictionary for
the parameter with the parameter for calculating the confidence
interval. Using `conf_interval=False` turns of the calculation of
the confidence interval and accelerate its. Defaults to False.
bar_criteria (Optional[Union[str, List[str]]], optional): Criteria for the
bar plot. It is recommended to use attributes from `goodness of fit`
module. Defaults to None.
line_criteria (Optional[Union[str, List[str]]], optional): Criteria for
the line plot. It is recommended to use attributes from
`regression metric` module. Defaults to None.
solver_settings (Optional[Dict[str, Any]], optional): Settings for
the solver models, which is split into settings for `minimizer` and
`optimizer`. Defaults to None.
!!! info: "About criteria"
The criteria for the bar and line plot are defined as a list of strings.
The supported keywords are defined by the built-in metrics for
`goodness of fit` and `regression` and can be checked in [documentation](
https://anselmoo.github.io/spectrafit/doc/statistics/
).
"""
self.initial_model = initial_model
if isinstance(conf_interval, bool):
conf_interval = (
ConfIntervalAPI().model_dump() if conf_interval is True else False
)
elif isinstance(conf_interval, dict):
conf_interval = ConfIntervalAPI(**conf_interval).dict(exclude_none=True)
if solver_settings is not None and isinstance(solver_settings, dict):
self.settings_solver_models = SolverModelsAPI(**solver_settings)
self.df_fit, self.args = PostProcessing(
self.df,
{
"global_": self.global_,
"conf_interval": conf_interval,
},
*SolverModels(
df=self.df,
args={
"global_": self.global_,
"column": list(self.df.columns),
"autopeak": self.autopeak,
**list2dict(peak_list=self.initial_model),
**self.settings_solver_models.model_dump(),
},
)(),
)()
self.update_metric()
self.update_peaks()
if show_plot:
self.plot_fit_df()
if show_metric:
self.plot_current_metric(
bar_criteria=bar_criteria, line_criteria=line_criteria
)
if show_df:
self.interactive_display(df=self.df_fit)
if show_peaks:
self.interactive_display(df=self.df_peaks)
update_metric()
¶
update_peaks()
¶
Update the peaks dataframe as multi-column dataframe.
The multi-column dataframe is used for the interactive display of the peaks with initial, current (model), and best fit values.
Source code in spectrafit/plugins/notebook.py
def update_peaks(self) -> None:
"""Update the peaks dataframe as multi-column dataframe.
The multi-column dataframe is used for the interactive display of the
peaks with initial, current (model), and best fit values.
"""
tuples = []
_list = []
for key_1, _dict in self.args["fit_insights"]["variables"].items():
tuples.extend([(key_1, key_2) for key_2, val in _dict.items()])
_list.extend([val for _, val in _dict.items()])
self.df_peaks = pd.concat(
[
self.df_peaks,
pd.DataFrame(
pd.Series(
_list,
index=pd.MultiIndex.from_tuples(
tuples, names=["component", "parameter"]
),
)
).T,
],
ignore_index=True,
)
Color Scheme¶
For changing the color scheme of the plots, additional color schemes can be added to the spectrafit.plugins.notebook
module. The color schemes are defined as a pydantic BaseSettings
class with the following attributes:
Color themes for the Plots in Jupyter Notebooks.
ColorBlindColor
¶
Bases: ColorAPI
Color blind theme for SpectraFit.
Source code in spectrafit/plugins/color_schemas.py
class ColorBlindColor(ColorAPI):
"""Color blind theme for SpectraFit."""
intensity: str = "#1f77b4"
residual: str = "#ff7f0e"
fit: str = "#d62728"
bars: List[str] = ["#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f"]
lines: List[str] = ["#8c564b", "#e377c2", "#7f7f7f", "#d62728", "#9467bd"]
components: str = "#2ca02c"
paper: str = "#ffffff"
plot: str = "#ffffff"
color: str = "#000000"
grid: str = "#d9d9d9"
line: str = "#d9d9d9"
zero_line: str = "#1f77b4"
ticks: str = "#000000"
font: str = "#000000"
ColorBlindFont
¶
Bases: FontAPI
Color blind font theme for SpectraFit.
Source code in spectrafit/plugins/color_schemas.py
DevOpsDarkColor
¶
Bases: ColorAPI
GitHub dark color inspired theme for SpectraFit.
Please check, primer/github-vscode-theme
Source code in spectrafit/plugins/color_schemas.py
class DevOpsDarkColor(ColorAPI):
"""GitHub dark color inspired theme for SpectraFit.
Please check, https://github.com/primer/github-vscode-theme
"""
intensity: str = "#1e4f8a"
residual: str = "#d73a49"
fit: str = "#22863a"
bars: List[str] = ["#005cc5", "#6f42c1", "#d73a49", "#22863a", "#d73a49"]
lines: List[str] = ["#d73a49", "#22863a", "#d73a49", "#005cc5", "#6f42c1"]
components: str = "#d73a49"
paper: str = "#0d1117"
plot: str = "#0d1117"
color: str = "#c9d1d9"
grid: str = "#30363d"
line: str = "#30363d"
zero_line: str = "#005cc5"
ticks: str = "#c9d1d9"
font: str = "#c9d1d9"
DevOpsDarkFont
¶
Bases: FontAPI
GitHub dark font inspired theme for SpectraFit.
Please check, primer/github-vscode-theme
Source code in spectrafit/plugins/color_schemas.py
DevOpsLightColor
¶
Bases: ColorAPI
GitHub light color inspired theme for SpectraFit.
Please check, primer/github-vscode-theme
Source code in spectrafit/plugins/color_schemas.py
class DevOpsLightColor(ColorAPI):
"""GitHub light color inspired theme for SpectraFit.
Please check, https://github.com/primer/github-vscode-theme
"""
intensity: str = "#1e4f8a"
residual: str = "#d73a49"
fit: str = "#d73a49"
bars: List[str] = ["#005cc5", "#6f42c1", "#d73a49", "#22863a", "#d73a49"]
lines: List[str] = ["#d73a49", "#22863a", "#d73a49", "#005cc5", "#6f42c1"]
components: str = "#22863a"
paper: str = "#ffffff"
plot: str = "#ffffff"
color: str = "#000000"
grid: str = "#d9d9d9"
line: str = "#d9d9d9"
zero_line: str = "#005cc5"
ticks: str = "#000000"
font: str = "#000000"
DevOpsLightFont
¶
Bases: FontAPI
GitHub light font inspired theme for SpectraFit.
Please check, primer/github-vscode-theme
Source code in spectrafit/plugins/color_schemas.py
DraculaColor
¶
Bases: ColorAPI
Dracula color theme for SpectraFit.
Dracula Color
The Dracula Color is a color theme is used for the dark mode of the SpectraFit
application. This color theme is used in the following way:
- Background #282a36 → paper, plot
- Current Line #44475a → not used
- Foreground #f8f8f2 → color, grid, ticks, font
- Comment #6272a4 → line
- Cyan #8be9fd → zero_line
- Green #50fa7b → fit
- Orange #ffb86c → not used
- Pink #ff79c6 → components
- Purple #bd93f9 → intensity
- Red #ff5555 → residual
- Yellow #f1fa8c → not used
Source code in spectrafit/plugins/color_schemas.py
class DraculaColor(ColorAPI):
"""Dracula color theme for SpectraFit.
!!! info "Dracula Color"
The [Dracula Color](https://draculatheme.com/contribute) is a color theme is
used for the dark mode of the `SpectraFit` application. This color theme is
used in the following way:
* Background #282a36 → **paper**, **plot**
* Current Line #44475a → _not used_
* Foreground #f8f8f2 → **color**, **grid**, **ticks**, **font**
* Comment #6272a4 → **line**
* Cyan #8be9fd → **zero_line**
* Green #50fa7b → **fit**
* Orange #ffb86c → _not used_
* Pink #ff79c6 → **components**
* Purple #bd93f9 → **intensity**
* Red #ff5555 → **residual**
* Yellow #f1fa8c → _not used_
"""
intensity: str = "#bd93f9"
residual: str = "#ff5555"
fit: str = "#50fa7b"
bars: List[str] = ["#803C62", "#FFC4E6", "#FF79C6", "#806273", "#CC609D"]
lines: List[str] = ["#805C36", "#FFDCB8", "#FFB86C", "#806E5C", "#CC9356"]
components: str = "#ff79c6"
paper: str = "#282a36"
plot: str = "#282a36"
color: str = "#f8f8f2"
grid: str = "#f8f8f2"
line: str = "#6272a4"
zero_line: str = "#8be9fd"
ticks: str = "#f8f8f2"
font: str = "#f8f8f2"
DraculaFont
¶
Bases: FontAPI
Dracula font theme for SpectraFit.
Dracula Font
The Dracula Font is a font theme is used for the dark mode of the SpectraFit
application. This font theme is used in the following way:
- Font Family "Fira Code" → family
- Font Size 12 → size
- Font Color dracula white → color
See also: tonsky/FiraCode
Source code in spectrafit/plugins/color_schemas.py
class DraculaFont(FontAPI):
"""Dracula font theme for SpectraFit.
!!! info "Dracula Font"
The [Dracula Font](https://draculatheme.com/contribute) is a font theme is
used for the dark mode of the `SpectraFit` application. This font theme is
used in the following way:
* Font Family "Fira Code" → **family**
* Font Size 12 → **size**
* Font Color dracula white → **color**
See also: https://github.com/tonsky/FiraCode
"""
family: str = __fira_code__
size: int = 12
color: str = "#f8f8f2"
MoonAkiColor
¶
Bases: ColorAPI
MoonAki dark color theme for SpectraFit.
Source code in spectrafit/plugins/color_schemas.py
class MoonAkiColor(ColorAPI):
"""MoonAki dark color theme for SpectraFit."""
intensity: str = "#f92672"
residual: str = "#fd971f"
fit: str = "#a6e22e"
bars: List[str] = ["#66d9ef", "#ae81ff", "#f92672", "#a6e22e", "#fd971f"]
lines: List[str] = ["#f92672", "#a6e22e", "#fd971f", "#66d9ef", "#ae81ff"]
components: str = "#ae81ff"
paper: str = "#272822"
plot: str = "#272822"
color: str = "#f8f8f2"
grid: str = "#49483e"
line: str = "#49483e"
zero_line: str = "#66d9ef"
ticks: str = "#f8f8f2"
font: str = "#f8f8f2"
Running SpectraFit in the builtin Jupyter-Notebook¶
For running SpectraFit
in the builtin Jupyter-Notebook, the following command can be used:
And next, the SpectraFitNotebook
class can be used for fitting the data: