Skip to content

Pydantic validation of scatter plot inputs too strict for color_discrete_map #44

Description

@enryH

Should be possible to set arbitrary color_discrete_map={False: "#2166AC", True: "#B2182B"}, # Blue # Red colormaps. Booleans work in Python, they work with plotly scatter plots and rejected columns from differential analysis are booleans.

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[62], line 4
      1 # %%
      2 
      3 # Generate advanced line plot
----> 4 scatter_plot_adv = vuecore.plots.basic.scatter.create_scatter_plot(
      5     data=anova,
      6     x="log2FC",
      7     y="-log10 pvalue",
      8     color="rejected",
      9     title="Simple Volcano Plot",
     10     subtitle="Visualizing ANOVA results",
     11     labels={
     12         "log2FC": "Log2 Fold Change",
     13         "-log10 pvalue": "-log10(p-value)",
     14         "rejected": "FDR corrected Significant",
     15     },
     16     # color_discrete_map="RdBu",  # {"False": "#0000FF", "True": "#FF0000"},
     17     color_discrete_map={False: "#2166AC", True: "#B2182B"},  # Blue  # Red
     18     # color_discrete_sequence=["red", "blue"],
     19     # colorscale="RdBu",
     20     # opacity=0.8,
     21     marker_line_width=1,
     22     marker_line_color="darkgray",
     23     width=900,
     24     height=600,
     25 )
     27 scatter_plot_adv.show()

File ~/Documents/repos/vuecore/src/vuecore/utils/docs_utils.py:104, in document_pydant_params.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    102 @wraps(func)
    103 def wrapper(*args, **kwargs):
--> 104     return func(*args, **kwargs)

File ~/Documents/repos/vuecore/src/vuecore/plots/basic/scatter.py:66, in create_scatter_plot(data, engine, file_path, **kwargs)
     11 @document_pydant_params(ScatterConfig)
     12 def create_scatter_plot(
     13     data: pd.DataFrame,
   (...)
     16     **kwargs,
     17 ) -> Any:
     18     """
     19     Creates, styles, and optionally saves a scatter plot using the specified engine.
     20 
   (...)
     64     https://github.com/Multiomics-Analytics-Group/vuecore/blob/main/docs/api_examples/scatter_plot.py
     65     """
---> 66     return create_plot(
     67         data=data,
     68         config=ScatterConfig,
     69         plot_type=PlotType.SCATTER,
     70         engine=engine,
     71         file_path=file_path,
     72         **kwargs,
     73     )

File ~/Documents/repos/vuecore/src/vuecore/plots/plot_factory.py:47, in create_plot(data, config, plot_type, engine, file_path, **kwargs)
     16 """
     17 Factory function to create, style, and optionally save plots.
     18 
   (...)
     44     The final plot object returned by the selected engine.
     45 """
     46 # 1. Validate configuration using Pydantic
---> 47 config = config(**kwargs)
     49 # 2. Get the correct builder function from the registry
     50 builder_func = get_builder(plot_type=plot_type, engine=engine)

File ~/miniforge3/envs/acore/lib/python3.11/site-packages/pydantic/main.py:253, in BaseModel.__init__(self, **data)
    251 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    252 __tracebackhide__ = True
--> 253 validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
    254 if self is not validated_self:
    255     warnings.warn(
    256         'A custom validator is returning a value other than `self`.\n'
    257         "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
    258         'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
    259         stacklevel=2,
    260     )

ValidationError: 2 validation errors for ScatterConfig
color_discrete_map.0.[key]
  Input should be a valid string [type=string_type, input_value=False, input_type=bool]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type
color_discrete_map.1.[key]
  Input should be a valid string [type=string_type, input_value=True, input_type=bool]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type

Reference code for the above example using plotly.express:

fig = px.scatter(
    anova,
    x="log2FC",
    y="-log10 pvalue",
    color="rejected",
    title="Simple Volcano Plot",
    # subtitle="Visualizing ANOVA results",
    color_discrete_map={False: "#2166AC", True: "#B2182B"},  # Blue  # Red
    labels={
        "log2FC": "Log2 Fold Change",
        "-log10 pvalue": "-log10(p-value)",
        "rejected": "FDR corrected Significant",
    },
    width=900,
    height=600,
)
fig

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions