Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.
Python agent-skillsai-scientistbioinformaticschemoinformaticsclaudeclaude-skillsclaudecodeclinical-researchcomputational-biologydata-analysisdrug-discoverygenomicsmaterials-sciencemetabolomicsproteomicsscientific-computingscientific-visualization
Update scientific visualization guidance
Timothy Kassis38cd20421c78
diff --git a/skills/scientific-visualization/SKILL.md b/skills/scientific-visualization/SKILL.mdindex b267ca61..5bdd87a4 100644--- a/skills/scientific-visualization/SKILL.md+++ b/skills/scientific-visualization/SKILL.md@@ -1,776 +1,291 @@---name: scientific-visualization-description: Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib/seaborn/plotly with publication styles. For quick exploration use seaborn or plotly directly.-license: MIT license-metadata: {"version": "1.0", "skill-author": "K-Dense Inc."}+description: Create and audit truthful, accessible, publication-ready scientific figures with Matplotlib, Seaborn, or Plotly. Use for figure design, multi-panel layouts, uncertainty and missing-data displays, color/contrast review, image metadata validation, and journal export planning.+license: MIT+compatibility: Requires Python 3.11+ and uv for pinned examples. Bundled CLIs are network-free and load Matplotlib, Pillow, or pypdf only when needed. Plotly static export with Kaleido v1 requires a compatible Chrome/Chromium installation.+allowed-tools:+ - Read+ - Write+ - Edit+ - Bash+ - Glob+ - Grep+metadata:+ version: "1.1"+ skill-author: K-Dense Inc.---# Scientific Visualization-## Overview+Build figures that preserve scientific meaning before optimizing appearance. Separate universal principles from dated publisher rules, preserve raw data and transformations, use color redundantly, and inspect delivered files rather than trusting plotting defaults.-Scientific visualization transforms data into clear, accurate figures for publication. Create journal-ready plots with multi-panel layouts, error bars, significance markers, and colorblind-safe palettes. Export as PDF/EPS/TIFF using matplotlib, seaborn, and plotly for manuscripts.+## Non-negotiable guardrails-## When to Use This Skill+- Never alter, hide, invent, or selectively enhance data to improve a figure.+- Preserve raw tables/images, exclusions, missing-value codes, analysis code, normalization, binning, image adjustments, and random seeds.+- Do not infer journal requirements. Identify the exact journal, article type, figure type, and submission phase; verify its live official guidance.+- Do not claim that a palette, DPI value, format, or automated report makes a figure accessible or journal-compliant.+- Do not silently connect missing observations, suppress inconvenient points, upsample images as if detail increased, or tune axes/dual axes to exaggerate a conclusion.+- Keep interactive and static outputs as distinct deliverables. Interactive hover is not a substitute for labels, alt text, keyboard access, an accessible data table, or a static fallback.-This skill should be used when:-- Creating plots or visualizations for scientific manuscripts-- Preparing figures for journal submission (Nature, Science, Cell, PLOS, etc.)-- Ensuring figures are colorblind-friendly and accessible-- Making multi-panel figures with consistent styling-- Exporting figures at correct resolution and format-- Following specific publication guidelines-- Improving existing figures to meet publication standards-- Creating figures that need to work in both color and grayscale+Read `references/publication_guidelines.md` for deceptive-encoding and integrity checks. Read `references/journal_requirements.md` only after the target and phase are known.-## Quick Start Guide+## Workflow-### Basic Publication-Quality Figure+### 1. Define the evidence and destination-```python-import matplotlib.pyplot as plt-import numpy as np+Record:-# Apply publication style (from scripts/style_presets.py)-from style_presets import apply_publication_style-apply_publication_style('default')+- audience and medium: manuscript, web, slide, poster, supplement;+- exact publisher/journal, article type, submission phase, and intended final width;+- variable semantics, units, sample/replicate structure, missing/censored values;+- estimator and uncertainty definition;+- transformations: filtering, aggregation, normalization, smoothing, bins, image processing;+- source-data paths/identifiers and output provenance.-# Create figure with appropriate size (single column = 3.5 inches)-fig, ax = plt.subplots(figsize=(3.5, 2.5))+If requirements are not known, create a provisional general figure and label all publisher choices as pending verification.-# Plot data-x = np.linspace(0, 10, 100)-ax.plot(x, np.sin(x), label='sin(x)')-ax.plot(x, np.cos(x), label='cos(x)')+### 2. Choose an honest encoding-# Proper labeling with units-ax.set_xlabel('Time (seconds)')-ax.set_ylabel('Amplitude (mV)')-ax.legend(frameon=False)+Prefer position on a common scale. Before coding, check:-# Remove unnecessary spines-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)+- **Bars/areas:** normally include zero because length/area is measured from a baseline.+- **Points/lines:** nonzero limits can be valid; show context and disclose breaks.+- **Uncertainty:** name SD, SE, CI, percentile, posterior, or another interval; state `n` and the unit of replication.+- **Raw observations:** show them when feasible; do not let jitter obscure categories/values.+- **Missing data:** distinguish missing, zero, censored, and excluded; use gaps or explicit model/interpolation styling.+- **Area/volume:** scale area/volume, not radius/diameter; avoid decorative 3D.+- **Log axes:** label the base/transform and declare how zero/negative values are handled.+- **Binning/smoothing:** record edges, bandwidth/window, method, and sensitivity.+- **Normalization:** state formula/reference and keep limits consistent across compared panels.+- **Dual axes:** prefer aligned panels; if unavoidable, justify units and do not engineer apparent correlation.+- **Images:** preserve originals, disclose whole-image adjustments, show scale bars, and avoid clipped/erased background.-# Save in publication formats (from scripts/figure_export.py)-from figure_export import save_publication_figure-save_publication_figure(fig, 'figure1', formats=['pdf', 'png'], dpi=300)-```+### 3. Design accessibility in, not after-### Using Pre-configured Styles+- Use color plus marker, line style, hatching, direct label, or panel separation.+- Choose qualitative, sequential, diverging, or cyclic color according to data semantics.+- Audit foreground/background contrast at the rendered size.+- Make missing and out-of-range values explicit.+- Provide alt text, a longer description for complex figures, and underlying data for web delivery.+- Treat WCAG 2.2 as web guidance: 4.5:1 normal text, 3:1 large text, and 3:1 for graphical objects required for understanding; color cannot be the only cue. Applicability and exceptions matter.-Apply journal-specific styles using the matplotlib style files in `assets/`:+See `references/color_palettes.md`. A grayscale screen is useful but is not a complete color-vision or accessibility test.-```python-import matplotlib.pyplot as plt--# Option 1: Use style file directly-plt.style.use('assets/nature.mplstyle')+### 4. Implement with scoped styles-# Option 2: Use style_presets.py helper-from style_presets import configure_for_journal-configure_for_journal('nature', figure_width='single')--# Now create figures - they'll automatically match Nature specifications-fig, ax = plt.subplots()-# ... your plotting code ...-```--### Quick Start with Seaborn--For statistical plots, use seaborn with publication styling:+Use Matplotlib's object-oriented API and temporary style contexts:```python-import seaborn as snsimport matplotlib.pyplot as plt-from style_presets import apply_publication_style--# Apply publication style-apply_publication_style('default')-sns.set_theme(style='ticks', context='paper', font_scale=1.1)-sns.set_palette('colorblind')--# Create statistical comparison figure-fig, ax = plt.subplots(figsize=(3.5, 3))-sns.boxplot(data=df, x='treatment', y='response',- order=['Control', 'Low', 'High'], palette='Set2', ax=ax)-sns.stripplot(data=df, x='treatment', y='response',- order=['Control', 'Low', 'High'],- color='black', alpha=0.3, size=3, ax=ax)-ax.set_ylabel('Response (μM)')-sns.despine()--# Save figure-from figure_export import save_publication_figure-save_publication_figure(fig, 'treatment_comparison', formats=['pdf', 'png'], dpi=300)-```--## Core Principles and Best Practices--### 1. Resolution and File Format-**Critical requirements** (detailed in `references/publication_guidelines.md`):-- **Raster images** (photos, microscopy): 300-600 DPI-- **Line art** (graphs, plots): 600-1200 DPI or vector format-- **Vector formats** (preferred): PDF, EPS, SVG-- **Raster formats**: TIFF, PNG (never JPEG for scientific data)+from style_presets import style_context-**Implementation:**-```python-# Use the figure_export.py script for correct settings-from figure_export import save_publication_figure--# Saves in multiple formats with proper DPI-save_publication_figure(fig, 'myfigure', formats=['pdf', 'png'], dpi=300)--# Or save for specific journal requirements-from figure_export import save_for_journal-save_for_journal(fig, 'figure1', journal='nature', figure_type='combination')-```--### 2. Color Selection - Colorblind Accessibility--**Always use colorblind-friendly palettes** (detailed in `references/color_palettes.md`):--**Recommended: Okabe-Ito palette** (distinguishable by all types of color blindness):-```python-# Option 1: Use assets/color_palettes.py-from color_palettes import OKABE_ITO_LIST, apply_palette-apply_palette('okabe_ito')--# Option 2: Manual specification-okabe_ito = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7', '#000000']-plt.rcParams['axes.prop_cycle'] = plt.cycler(color=okabe_ito)+with style_context("default", palette_name="okabe_ito_on_white"):+ fig, ax = plt.subplots(+ figsize=(89 / 25.4, 60 / 25.4),+ layout="constrained",+ )+ ax.plot(x, y, marker="o", label="Observed")+ ax.set(xlabel="Time (hours)", ylabel="Response (unit)")+ ax.legend()```-**For heatmaps/continuous data:**-- Use perceptually uniform colormaps: `viridis`, `plasma`, `cividis`-- Avoid red-green diverging maps (use `PuOr`, `RdBu`, `BrBG` instead)-- Never use `jet` or `rainbow` colormaps--**Always test figures in grayscale** to ensure interpretability.+`layout="constrained"` supports colorbars, nested GridSpec, subfigures, and `subplot_mosaic`. Do not call `tight_layout()` afterward; it disables constrained layout.-### 3. Typography and Text+For exact physical dimensions, do not use `bbox_inches="tight"` unless the changed page size is intentional.-**Font guidelines** (detailed in `references/publication_guidelines.md`):-- Sans-serif fonts: Arial, Helvetica, Calibri-- Minimum sizes at **final print size**:- - Axis labels: 7-9 pt- - Tick labels: 6-8 pt- - Panel labels: 8-12 pt (bold)-- Sentence case for labels: "Time (hours)" not "TIME (HOURS)"-- Always include units in parentheses+#### Color normalization-**Implementation:**```python-# Set fonts globallyimport matplotlib as mpl-mpl.rcParams['font.family'] = 'sans-serif'-mpl.rcParams['font.sans-serif'] = ['Arial', 'Helvetica']-mpl.rcParams['font.size'] = 8-mpl.rcParams['axes.labelsize'] = 9-mpl.rcParams['xtick.labelsize'] = 7-mpl.rcParams['ytick.labelsize'] = 7-```--### 4. Figure Dimensions--**Journal-specific widths** (detailed in `references/journal_requirements.md`):-- **Nature**: Single 89 mm, Double 183 mm-- **Science**: Single 55 mm, Double 175 mm-- **Cell**: Single 85 mm, Double 178 mm-**Check figure size compliance:**-```python-from figure_export import check_figure_size--fig = plt.figure(figsize=(3.5, 3)) # 89 mm for Nature-check_figure_size(fig, journal='nature')+norm = mpl.colors.TwoSlopeNorm(vmin=-2, vcenter=0, vmax=5)+cmap = mpl.colormaps["RdBu_r"].with_extremes(bad="#777777")+image = ax.imshow(values, norm=norm, cmap=cmap, interpolation="nearest")+fig.colorbar(image, ax=ax, label="Change (unit)")```-### 5. Multi-Panel Figures+Use `LogNorm`, `CenteredNorm`, `SymLogNorm`, `BoundaryNorm`, or `TwoSlopeNorm` only when its mapping matches the scientific meaning.-**Best practices:**-- Label panels with bold letters: **A**, **B**, **C** (uppercase for most journals, lowercase for Nature)-- Maintain consistent styling across all panels-- Align panels along edges where possible-- Use adequate white space between panels--**Example implementation** (see `references/matplotlib_examples.md` for complete code):-```python-from string import ascii_uppercase+#### Seaborn-fig = plt.figure(figsize=(7, 4))-gs = fig.add_gridspec(2, 2, hspace=0.4, wspace=0.4)--ax1 = fig.add_subplot(gs[0, 0])-ax2 = fig.add_subplot(gs[0, 1])-# ... create other panels ...--# Add panel labels-for i, ax in enumerate([ax1, ax2, ...]):- ax.text(-0.15, 1.05, ascii_uppercase[i], transform=ax.transAxes,- fontsize=10, fontweight='bold', va='top')-```+Seaborn 0.13.2 uses the current `errorbar` API:-## Common Tasks--### Task 1: Create a Publication-Ready Line Plot--See `references/matplotlib_examples.md` Example 1 for complete code.--**Key steps:**-1. Apply publication style-2. Set appropriate figure size for target journal-3. Use colorblind-friendly colors-4. Add error bars with correct representation (SEM, SD, or CI)-5. Label axes with units-6. Remove unnecessary spines-7. Save in vector format--**Using seaborn for automatic confidence intervals:**```python-import seaborn as sns-fig, ax = plt.subplots(figsize=(5, 3))-sns.lineplot(data=timeseries, x='time', y='measurement',- hue='treatment', errorbar=('ci', 95),- markers=True, ax=ax)-ax.set_xlabel('Time (hours)')-ax.set_ylabel('Measurement (AU)')-sns.despine()+sns.lineplot(+ data=frame,+ x="time",+ y="response",+ hue="treatment",+ style="treatment",+ markers=True,+ errorbar=("ci", 95),+ n_boot=5000,+ seed=20260723,+ ax=ax,+)```-### Task 2: Create a Multi-Panel Figure--See `references/matplotlib_examples.md` Example 2 for complete code.+Axes-level functions fit custom Matplotlib layouts; figure-level functions create their own figures/facets. Do not customize Seaborn's internal artist lists as if they were stable API.-**Key steps:**-1. Use `GridSpec` for flexible layout-2. Ensure consistent styling across panels-3. Add bold panel labels (A, B, C, etc.)-4. Align related panels-5. Verify all text is readable at final size+#### Plotly-### Task 3: Create a Heatmap with Proper Colormap+- Use `write_html()` for interaction and `write_image()`/`plotly.io.write_images()` for static output.+- Kaleido 1.3.0 requires Chrome/Chromium; it no longer bundles Chrome.+- Current static formats: PNG, JPEG, WebP, SVG, PDF. EPS is Kaleido v0-only.+- Do not pass deprecated `engine=` or use Orca/`plotly.io.kaleido.scope`.+- `width`, `height`, and `scale` control pixels; `scale=3` is not inherently “300 DPI.”+- WebGL traces embed raster content in PDF/SVG.+- Fully offline exports need local external assets when a figure references MathJax/topojson/tiles.-See `references/matplotlib_examples.md` Example 4 for complete code.+### 5. Export explicitly and record provenance-**Key steps:**-1. Use perceptually uniform colormap (`viridis`, `plasma`, `cividis`)-2. Include labeled colorbar-3. For diverging data, use colorblind-safe diverging map (`RdBu_r`, `PuOr`)-4. Set appropriate center value for diverging maps-5. Test appearance in grayscale--**Using seaborn for correlation matrices:**-```python-import seaborn as sns-fig, ax = plt.subplots(figsize=(5, 4))-corr = df.corr()-mask = np.triu(np.ones_like(corr, dtype=bool))-sns.heatmap(corr, mask=mask, annot=True, fmt='.2f',- cmap='RdBu_r', center=0, square=True,- linewidths=1, cbar_kws={'shrink': 0.8}, ax=ax)-```--### Task 4: Prepare Figure for Specific Journal--**Workflow:**-1. Check journal requirements: `references/journal_requirements.md`-2. Configure matplotlib for journal:- ```python- from style_presets import configure_for_journal- configure_for_journal('nature', figure_width='single')- ```-3. Create figure (will auto-size correctly)-4. Export with journal specifications:- ```python- from figure_export import save_for_journal- save_for_journal(fig, 'figure1', journal='nature', figure_type='line_art')- ```--### Task 5: Fix an Existing Figure to Meet Publication Standards--**Checklist approach** (full checklist in `references/publication_guidelines.md`):--1. **Check resolution**: Verify DPI meets journal requirements-2. **Check file format**: Use vector for plots, TIFF/PNG for images-3. **Check colors**: Ensure colorblind-friendly-4. **Check fonts**: Minimum 6-7 pt at final size, sans-serif-5. **Check labels**: All axes labeled with units-6. **Check size**: Matches journal column width-7. **Test grayscale**: Figure interpretable without color-8. **Remove chart junk**: No unnecessary grids, 3D effects, shadows--### Task 6: Create Colorblind-Friendly Visualizations--**Strategy:**-1. Use approved palettes from `assets/color_palettes.py`-2. Add redundant encoding (line styles, markers, patterns)-3. Test with colorblind simulator-4. Ensure grayscale compatibility--**Example:**```python-from color_palettes import apply_palette-import matplotlib.pyplot as plt--apply_palette('okabe_ito')--# Add redundant encoding beyond color-line_styles = ['-', '--', '-.', ':']-markers = ['o', 's', '^', 'v']--for i, (data, label) in enumerate(datasets):- plt.plot(x, data, linestyle=line_styles[i % 4],- marker=markers[i % 4], label=label)-```--## Statistical Rigor--**Always include:**-- Error bars (SD, SEM, or CI - specify which in caption)-- Sample size (n) in figure or caption-- Statistical significance markers (*, **, ***)-- Individual data points when possible (not just summary statistics)--**Example with statistics:**-```python-# Show individual points with summary statistics-ax.scatter(x_jittered, individual_points, alpha=0.4, s=8)-ax.errorbar(x, means, yerr=sems, fmt='o', capsize=3)--# Mark significance-ax.text(1.5, max_y * 1.1, '***', ha='center', fontsize=8)+from figure_export import export_figure++report = export_figure(+ fig,+ "outputs/figure1",+ formats=["pdf", "png"],+ dpi=600,+ bbox_inches=None, # preserve figure page dimensions+ provenance={+ "raw_data": "data/source.csv",+ "transformations": ["predeclared QC filter", "group mean"],+ "uncertainty": "95% bootstrap CI; seed 20260723",+ "missing_data": "retained as gaps",+ },+ write_manifest=True,+)```-## Working with Different Plotting Libraries--### Matplotlib-- Most control over publication details-- Best for complex multi-panel figures-- Use provided style files for consistent formatting-- See `references/matplotlib_examples.md` for extensive examples+The exporter refuses implicit overwrite, writes atomically, keeps vector DPI for embedded rasters, uses TIFF LZW, and can use PDF/PS Type 42 fonts. It does not validate scientific content or publisher acceptance.-### Seaborn+For editable fonts:-Seaborn provides a high-level, dataset-oriented interface for statistical graphics, built on matplotlib. It excels at creating publication-quality statistical visualizations with minimal code while maintaining full compatibility with matplotlib customization.+- PDF/PS Type 42 embeds TrueType fonts.+- `svg.fonttype="none"` keeps text editable/searchable but does not embed fonts; appearance depends on installed fonts.+- `svg.fonttype="path"` preserves glyph appearance as paths but loses editable/searchable text.-**Key advantages for scientific visualization:**-- Automatic statistical estimation and confidence intervals-- Built-in support for multi-panel figures (faceting)-- Colorblind-friendly palettes by default-- Dataset-oriented API using pandas DataFrames-- Semantic mapping of variables to visual properties+Use an opaque explicit background unless transparency is required; blending against another background changes apparent contrast.-#### Quick Start with Publication Style+### 6. Inspect, compare, and review-Always apply matplotlib publication styles first, then configure seaborn:--```python-import seaborn as sns-import matplotlib.pyplot as plt-from style_presets import apply_publication_style+1. Inspect file metadata.+2. Audit palette contrast/grayscale separation.+3. Compare against a dated publisher snapshot.+4. View at final size in the manuscript/web context.+5. Manually review fonts, embedded rasters, clipping, legends, scale bars, image integrity, caption, alt text, and source data.+6. Re-check the live target-journal page immediately before upload.-# Apply publication style-apply_publication_style('default')+## Pinned snapshot-# Configure seaborn for publication-sns.set_theme(style='ticks', context='paper', font_scale=1.1)-sns.set_palette('colorblind') # Use colorblind-safe palette+The examples and smoke tests use direct package pins current on 2026-07-23:-# Create figure-fig, ax = plt.subplots(figsize=(3.5, 2.5))-sns.scatterplot(data=df, x='time', y='response',- hue='treatment', style='condition', ax=ax)-sns.despine() # Remove top and right spines+```bash+uv run --isolated --no-project --python 3.13 \+ --with "matplotlib==3.11.1" \+ --with "seaborn==0.13.2" \+ --with "plotly==6.9.0" \+ --with "kaleido==1.3.0" \+ --with "pillow==12.3.0" \+ --with "pypdf==6.14.2" \+ python your_figure.py```-#### Common Plot Types for Publications+This is a dated direct-dependency snapshot, not a transitive lock. Use the project's uv lock for exact replay; this skill intentionally ships no dependency lock.-**Statistical comparisons:**-```python-# Box plot with individual points for transparency-fig, ax = plt.subplots(figsize=(3.5, 3))-sns.boxplot(data=df, x='treatment', y='response',- order=['Control', 'Low', 'High'], palette='Set2', ax=ax)-sns.stripplot(data=df, x='treatment', y='response',- order=['Control', 'Low', 'High'],- color='black', alpha=0.3, size=3, ax=ax)-ax.set_ylabel('Response (μM)')-sns.despine()-```+## Bundled CLIs-**Distribution analysis:**-```python-# Violin plot with split comparison-fig, ax = plt.subplots(figsize=(4, 3))-sns.violinplot(data=df, x='timepoint', y='expression',- hue='treatment', split=True, inner='quartile', ax=ax)-ax.set_ylabel('Gene Expression (AU)')-sns.despine()-```+All helpers are deterministic, network-free, bounded, reject symlink inputs/destinations where relevant, and refuse overwrite unless `--force` is explicit.-**Correlation matrices:**-```python-# Heatmap with proper colormap and annotations-fig, ax = plt.subplots(figsize=(5, 4))-corr = df.corr()-mask = np.triu(np.ones_like(corr, dtype=bool)) # Show only lower triangle-sns.heatmap(corr, mask=mask, annot=True, fmt='.2f',- cmap='RdBu_r', center=0, square=True,- linewidths=1, cbar_kws={'shrink': 0.8}, ax=ax)-plt.tight_layout()-```+### Inspect raster/vector metadata-**Time series with confidence bands:**-```python-# Line plot with automatic CI calculation-fig, ax = plt.subplots(figsize=(5, 3))-sns.lineplot(data=timeseries, x='time', y='measurement',- hue='treatment', style='replicate',- errorbar=('ci', 95), markers=True, dashes=False, ax=ax)-ax.set_xlabel('Time (hours)')-ax.set_ylabel('Measurement (AU)')-sns.despine()+```bash+uv run --isolated --no-project --python 3.13 \+ --with "pillow==12.3.0" \+ python scripts/image_metadata.py figure.tiff \+ --format tiff --mode RGB --min-dpi 300 --target-width-mm 85 \+ --alpha-policy forbid```-#### Multi-Panel Figures with Seaborn+Supports raster images (Pillow), SVG, PDF (pypdf), and EPS/PS. Reports dimensions, DPI/effective DPI, mode, alpha, ICC presence, compression, page size, and conservative first-page PDF font resources. It does not inspect every embedded raster in a vector container.-**Using FacetGrid for automatic faceting:**-```python-# Create faceted plot-g = sns.relplot(data=df, x='dose', y='response',- hue='treatment', col='cell_line', row='timepoint',- kind='line', height=2.5, aspect=1.2,- errorbar=('ci', 95), markers=True)-g.set_axis_labels('Dose (μM)', 'Response (AU)')-g.set_titles('{row_name} | {col_name}')-sns.despine()--# Save with correct DPI-from figure_export import save_publication_figure-save_publication_figure(g.figure, 'figure_facets',- formats=['pdf', 'png'], dpi=300)-```+### Audit palette contrast and grayscale-**Combining seaborn with matplotlib subplots:**-```python-# Create custom multi-panel layout-fig, axes = plt.subplots(2, 2, figsize=(7, 6))--# Panel A: Scatter with regression-sns.regplot(data=df, x='predictor', y='response', ax=axes[0, 0])-axes[0, 0].text(-0.15, 1.05, 'A', transform=axes[0, 0].transAxes,- fontsize=10, fontweight='bold')--# Panel B: Distribution comparison-sns.violinplot(data=df, x='group', y='value', ax=axes[0, 1])-axes[0, 1].text(-0.15, 1.05, 'B', transform=axes[0, 1].transAxes,- fontsize=10, fontweight='bold')--# Panel C: Heatmap-sns.heatmap(correlation_data, cmap='viridis', ax=axes[1, 0])-axes[1, 0].text(-0.15, 1.05, 'C', transform=axes[1, 0].transAxes,- fontsize=10, fontweight='bold')--# Panel D: Time series-sns.lineplot(data=timeseries, x='time', y='signal',- hue='condition', ax=axes[1, 1])-axes[1, 1].text(-0.15, 1.05, 'D', transform=axes[1, 1].transAxes,- fontsize=10, fontweight='bold')--plt.tight_layout()-sns.despine()+```bash+uv run --isolated --no-project --python 3.13 \+ python scripts/palette_audit.py \+ --palette okabe_ito_on_white \+ --background FFFFFF \+ --role graphical```-#### Color Palettes for Publications--Seaborn includes several colorblind-safe palettes:--```python-# Use built-in colorblind palette (recommended)-sns.set_palette('colorblind')+Reports exact WCAG sRGB contrast plus pairwise CIE L* grayscale screening. The grayscale threshold is a heuristic, not a standard.-# Or specify custom colorblind-safe colors (Okabe-Ito)-okabe_ito = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7', '#000000']-sns.set_palette(okabe_ito)+### Plan/screen publisher export-# For heatmaps and continuous data-sns.heatmap(data, cmap='viridis') # Perceptually uniform-sns.heatmap(corr, cmap='RdBu_r', center=0) # Diverging, centered+```bash+uv run --isolated --no-project --python 3.13 \+ python scripts/export_plan.py \+ --publisher nature \+ --figure-type combination \+ --width single \+ --phase final```-#### Choosing Between Axes-Level and Figure-Level Functions+Add `--input figure.pdf` to screen machine-readable properties. Profiles are official-source snapshots accessed 2026-07-23, not automatic compliance rules.-**Axes-level functions** (e.g., `scatterplot`, `boxplot`, `heatmap`):-- Use when building custom multi-panel layouts-- Accept `ax=` parameter for precise placement-- Better integration with matplotlib subplots-- More control over figure composition+### Preview styles-```python-fig, ax = plt.subplots(figsize=(3.5, 2.5))-sns.scatterplot(data=df, x='x', y='y', hue='group', ax=ax)+```bash+uv run --isolated --no-project --python 3.13 \+ --with "matplotlib==3.11.1" \+ python scripts/style_preview.py \+ --output outputs/style-preview \+ --style default \+ --palette okabe_ito_on_white \+ --formats png,svg```-**Figure-level functions** (e.g., `relplot`, `catplot`, `displot`):-- Use for automatic faceting by categorical variables-- Create complete figures with consistent styling-- Great for exploratory analysis-- Use `height` and `aspect` for sizing+### Inspect/write styles and smoke-test export-```python-g = sns.relplot(data=df, x='x', y='y', col='category', kind='scatter')+```bash+uv run --isolated --no-project --python 3.13 \+ python scripts/style_presets.py --list+uv run --isolated --no-project --python 3.13 \+ python scripts/style_presets.py --show nature+uv run --isolated --no-project --python 3.13 \+ --with "matplotlib==3.11.1" \+ python scripts/figure_export.py --demo outputs/export-smoke --manifest```-#### Statistical Rigor with Seaborn+## Assets-Seaborn automatically computes and displays uncertainty:+- `assets/publication.mplstyle`: general print starting point.+- `assets/nature.mplstyle`: dated flagship Nature visual starting point, not a compliance preset.+- `assets/presentation.mplstyle`: larger projected-display style.+- `assets/color_palettes.py`: importable Okabe-Ito and Paul Tol values with metadata.+- `assets/publisher_profiles.json`: dated, machine-readable planning snapshots.-```python-# Line plot: shows mean ± 95% CI by default-sns.lineplot(data=df, x='time', y='value', hue='treatment',- errorbar=('ci', 95)) # Can change to 'sd', 'se', etc.--# Bar plot: shows mean with bootstrapped CI-sns.barplot(data=df, x='treatment', y='response',- errorbar=('ci', 95), capsize=0.1)+Matplotlib style files omit `#` in hex colors because `#` begins comments in `.mplstyle` parsing.-# Always specify error type in figure caption:-# "Error bars represent 95% confidence intervals"-```+## References-#### Best Practices for Publication-Ready Seaborn Figures--1. **Always set publication theme first:**- ```python- sns.set_theme(style='ticks', context='paper', font_scale=1.1)- ```--2. **Use colorblind-safe palettes:**- ```python- sns.set_palette('colorblind')- ```--3. **Remove unnecessary elements:**- ```python- sns.despine() # Remove top and right spines- ```--4. **Control figure size appropriately:**- ```python- # Axes-level: use matplotlib figsize- fig, ax = plt.subplots(figsize=(3.5, 2.5))-- # Figure-level: use height and aspect- g = sns.relplot(..., height=3, aspect=1.2)- ```--5. **Show individual data points when possible:**- ```python- sns.boxplot(...) # Summary statistics- sns.stripplot(..., alpha=0.3) # Individual points- ```--6. **Include proper labels with units:**- ```python- ax.set_xlabel('Time (hours)')- ax.set_ylabel('Expression (AU)')- ```--7. **Export at correct resolution:**- ```python- from figure_export import save_publication_figure- save_publication_figure(fig, 'figure_name',- formats=['pdf', 'png'], dpi=300)- ```--#### Advanced Seaborn Techniques--**Pairwise relationships for exploratory analysis:**-```python-# Quick overview of all relationships-g = sns.pairplot(data=df, hue='condition',- vars=['gene1', 'gene2', 'gene3'],- corner=True, diag_kind='kde', height=2)-```--**Hierarchical clustering heatmap:**-```python-# Cluster samples and features-g = sns.clustermap(expression_data, method='ward',- metric='euclidean', z_score=0,- cmap='RdBu_r', center=0,- figsize=(10, 8),- row_colors=condition_colors,- cbar_kws={'label': 'Z-score'})-```--**Joint distributions with marginals:**-```python-# Bivariate distribution with context-g = sns.jointplot(data=df, x='gene1', y='gene2',- hue='treatment', kind='scatter',- height=6, ratio=4, marginal_kws={'kde': True})-```--#### Common Seaborn Issues and Solutions--**Issue: Legend outside plot area**-```python-g = sns.relplot(...)-g._legend.set_bbox_to_anchor((0.9, 0.5))-```--**Issue: Overlapping labels**-```python-plt.xticks(rotation=45, ha='right')-plt.tight_layout()-```--**Issue: Text too small at final size**-```python-sns.set_context('paper', font_scale=1.2) # Increase if needed-```--#### Additional Resources--For more detailed seaborn information, see:-- `skills/seaborn/SKILL.md` - Comprehensive seaborn documentation-- `skills/seaborn/references/examples.md` - Practical use cases-- `skills/seaborn/references/function_reference.md` - Complete API reference-- `skills/seaborn/references/objects_interface.md` - Modern declarative API--### Plotly-- Interactive figures for exploration-- Export static images for publication-- Configure for publication quality:-```python-fig.update_layout(- font=dict(family='Arial, sans-serif', size=10),- plot_bgcolor='white',- # ... see matplotlib_examples.md Example 8-)-fig.write_image('figure.png', scale=3) # scale=3 gives ~300 DPI-```+- `references/publication_guidelines.md`: integrity, deceptive encodings, accessibility, static/interactive output.+- `references/color_palettes.md`: palette semantics, exact values, WCAG contrast, grayscale caveats, color management.+- `references/journal_requirements.md`: phase-specific official publisher snapshots.+- `references/matplotlib_examples.md`: current, runnable Matplotlib/Seaborn/Plotly patterns.+- `references/sources.md`: official URLs, dates, versions, and research basis.-## Resources--### References Directory--**Load these as needed for detailed information:**--- **`publication_guidelines.md`**: Comprehensive best practices- - Resolution and file format requirements- - Typography guidelines- - Layout and composition rules- - Statistical rigor requirements- - Complete publication checklist--- **`color_palettes.md`**: Color usage guide- - Colorblind-friendly palette specifications with RGB values- - Sequential and diverging colormap recommendations- - Testing procedures for accessibility- - Domain-specific palettes (genomics, microscopy)--- **`journal_requirements.md`**: Journal-specific specifications- - Technical requirements by publisher- - File format and DPI specifications- - Figure dimension requirements- - Quick reference table--- **`matplotlib_examples.md`**: Practical code examples- - 10 complete working examples- - Line plots, bar plots, heatmaps, multi-panel figures- - Journal-specific figure examples- - Tips for each library (matplotlib, seaborn, plotly)--### Scripts Directory--**Use these helper scripts for automation:**--- **`figure_export.py`**: Export utilities- - `save_publication_figure()`: Save in multiple formats with correct DPI- - `save_for_journal()`: Use journal-specific requirements automatically- - `check_figure_size()`: Verify dimensions meet journal specs- - Run directly: `python scripts/figure_export.py` for examples--- **`style_presets.py`**: Pre-configured styles- - `apply_publication_style()`: Apply preset styles (default, nature, science, cell)- - `set_color_palette()`: Quick palette switching- - `configure_for_journal()`: One-command journal configuration- - Run directly: `python scripts/style_presets.py` to see examples--### Assets Directory--**Use these files in figures:**--- **`color_palettes.py`**: Importable color definitions- - All recommended palettes as Python constants- - `apply_palette()` helper function- - Can be imported directly into notebooks/scripts--- **Matplotlib style files**: Use with `plt.style.use()`- - `publication.mplstyle`: General publication quality- - `nature.mplstyle`: Nature journal specifications- - `presentation.mplstyle`: Larger fonts for posters/slides--## Workflow Summary--**Recommended workflow for creating publication figures:**--1. **Plan**: Determine target journal, figure type, and content-2. **Configure**: Apply appropriate style for journal- ```python- from style_presets import configure_for_journal- configure_for_journal('nature', 'single')- ```-3. **Create**: Build figure with proper labels, colors, statistics-4. **Verify**: Check size, fonts, colors, accessibility- ```python- from figure_export import check_figure_size- check_figure_size(fig, journal='nature')- ```-5. **Export**: Save in required formats- ```python- from figure_export import save_for_journal- save_for_journal(fig, 'figure1', 'nature', 'combination')- ```-6. **Review**: View at final size in manuscript context--## Common Pitfalls to Avoid--1. **Font too small**: Text unreadable when printed at final size-2. **JPEG format**: Never use JPEG for graphs/plots (creates artifacts)-3. **Red-green colors**: ~8% of males cannot distinguish-4. **Low resolution**: Pixelated figures in publication-5. **Missing units**: Always label axes with units-6. **3D effects**: Distorts perception, avoid completely-7. **Chart junk**: Remove unnecessary gridlines, decorations-8. **Truncated axes**: Start bar charts at zero unless scientifically justified-9. **Inconsistent styling**: Different fonts/colors across figures in same manuscript-10. **No error bars**: Always show uncertainty--## Final Checklist--Before submitting figures, verify:--- [ ] Resolution meets journal requirements (300+ DPI)-- [ ] File format is correct (vector for plots, TIFF for images)-- [ ] Figure size matches journal specifications-- [ ] All text readable at final size (≥6 pt)-- [ ] Colors are colorblind-friendly-- [ ] Figure works in grayscale-- [ ] All axes labeled with units-- [ ] Error bars present with definition in caption-- [ ] Panel labels present and consistent-- [ ] No chart junk or 3D effects-- [ ] Fonts consistent across all figures-- [ ] Statistical significance clearly marked-- [ ] Legend is clear and complete--Use this skill to ensure scientific figures meet the highest publication standards while remaining accessible to all readers.+## Final review checklist+- [ ] Raw data/images and transformation code are preserved.+- [ ] Missing values, exclusions, bins, normalization, and uncertainty are explicit.+- [ ] Baselines, scales, limits, and area/volume encodings are honest.+- [ ] Color is redundant and rendered contrast was reviewed.+- [ ] Figure has an accessible description/data alternative where applicable.+- [ ] Physical dimensions, DPI, format, fonts, transparency, and file size were inspected after export.+- [ ] Publisher rules were verified for the exact journal and phase.+- [ ] No automated report is presented as a scientific, accessibility, or compliance certification.diff --git a/skills/scientific-visualization/assets/color_palettes.py b/skills/scientific-visualization/assets/color_palettes.pyindex be4f0e98..137ef9d7 100644--- a/skills/scientific-visualization/assets/color_palettes.py+++ b/skills/scientific-visualization/assets/color_palettes.py@@ -1,197 +1,263 @@-"""-Colorblind-Friendly Color Palettes for Scientific Visualization--This module provides carefully curated color palettes optimized for-scientific publications and accessibility.+"""Curated sRGB palettes for scientific figures.-Usage:- from color_palettes import OKABE_ITO, apply_palette- import matplotlib.pyplot as plt+Sources were checked 2026-07-23. Palette selection alone never establishes+accessibility: audit the rendered foreground/background contrast and add+redundant encodings.- apply_palette('okabe_ito')- plt.plot([1, 2, 3], [1, 4, 9])+Paul Tol values and recommended fixed order:+https://sronpersonalpages.nl/~pault/data/colourschemes.pdf+(SRON/EPS/TN/09-002, issue 3.2, 18 August 2021)"""-# Okabe-Ito Palette (2008)-# The most widely recommended colorblind-friendly palette-OKABE_ITO = {- 'orange': '#E69F00',- 'sky_blue': '#56B4E9',- 'bluish_green': '#009E73',- 'yellow': '#F0E442',- 'blue': '#0072B2',- 'vermillion': '#D55E00',- 'reddish_purple': '#CC79A7',- 'black': '#000000'+from __future__ import annotations++from typing import Final++# Okabe-Ito colors as reproduced in Wong, Nature Methods 8, 441 (2011).+OKABE_ITO: Final = {+ "orange": "#E69F00",+ "sky_blue": "#56B4E9",+ "bluish_green": "#009E73",+ "yellow": "#F0E442",+ "blue": "#0072B2",+ "vermillion": "#D55E00",+ "reddish_purple": "#CC79A7",+ "black": "#000000",}+OKABE_ITO_LIST: Final = [+ "#E69F00",+ "#56B4E9",+ "#009E73",+ "#F0E442",+ "#0072B2",+ "#D55E00",+ "#CC79A7",+ "#000000",+]-OKABE_ITO_LIST = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7', '#000000']--# Wong Palette (Nature Methods)-WONG = ['#000000', '#E69F00', '#56B4E9', '#009E73',- '#F0E442', '#0072B2', '#D55E00', '#CC79A7']--# Paul Tol Palettes (https://personal.sron.nl/~pault/)-TOL_BRIGHT = ['#4477AA', '#EE6677', '#228833', '#CCBB44',- '#66CCEE', '#AA3377', '#BBBBBB']--TOL_MUTED = ['#332288', '#88CCEE', '#44AA99', '#117733',- '#999933', '#DDCC77', '#CC6677', '#882255', '#AA4499']--TOL_LIGHT = ['#77AADD', '#EE8866', '#EEDD88', '#FFAABB',- '#99DDFF', '#44BB99', '#BBCC33', '#AAAA00', '#DDDDDD']--TOL_HIGH_CONTRAST = ['#004488', '#DDAA33', '#BB5566']--# Sequential colormaps (for continuous data)-SEQUENTIAL_COLORMAPS = [- 'viridis', # Default, perceptually uniform- 'plasma', # Perceptually uniform- 'inferno', # Perceptually uniform- 'magma', # Perceptually uniform- 'cividis', # Optimized for colorblind viewers- 'YlOrRd', # Yellow-Orange-Red- 'YlGnBu', # Yellow-Green-Blue- 'Blues', # Single hue- 'Greens', # Single hue- 'Purples', # Single hue+# These five meet 3:1 against white in sRGB. Still use markers/line styles.+OKABE_ITO_ON_WHITE: Final = [+ "#0072B2",+ "#D55E00",+ "#009E73",+ "#CC79A7",+ "#000000",]-# Diverging colormaps (for data with meaningful center)-DIVERGING_COLORMAPS_SAFE = [- 'RdYlBu', # Red-Yellow-Blue (reversed is common)- 'RdBu', # Red-Blue- 'PuOr', # Purple-Orange (excellent for colorblind)- 'BrBG', # Brown-Blue-Green (good for colorblind)- 'PRGn', # Purple-Green (use with caution)- 'PiYG', # Pink-Yellow-Green (use with caution)+# Compatibility alias: Wong presents the same eight colors in another order.+WONG: Final = [+ "#000000",+ "#E69F00",+ "#56B4E9",+ "#009E73",+ "#F0E442",+ "#0072B2",+ "#D55E00",+ "#CC79A7",]-# Diverging colormaps to AVOID (red-green combinations)-DIVERGING_COLORMAPS_AVOID = [- 'RdGn', # Red-Green (problematic!)- 'RdYlGn', # Red-Yellow-Green (problematic!)+# Paul Tol qualitative schemes in the fixed sequence recommended in issue 3.2.+TOL_BRIGHT: Final = [+ "#4477AA",+ "#EE6677",+ "#228833",+ "#CCBB44",+ "#66CCEE",+ "#AA3377",+ "#BBBBBB",+]+TOL_HIGH_CONTRAST: Final = ["#004488", "#DDAA33", "#BB5566"]+TOL_VIBRANT: Final = [+ "#EE7733",+ "#0077BB",+ "#33BBEE",+ "#EE3377",+ "#CC3311",+ "#009988",+ "#BBBBBB",+]+TOL_MUTED: Final = [+ "#CC6677",+ "#332288",+ "#DDCC77",+ "#117733",+ "#88CCEE",+ "#882255",+ "#44AA99",+ "#999933",+ "#AA4499",+]+TOL_MEDIUM_CONTRAST: Final = [+ "#6699CC",+ "#004488",+ "#EECC66",+ "#994455",+ "#997700",+ "#EE99AA",+]+TOL_PALE: Final = [+ "#BBCCEE",+ "#CCEEFF",+ "#CCDDAA",+ "#EEEEBB",+ "#FFCCCC",+ "#DDDDDD",+]+TOL_DARK: Final = [+ "#222255",+ "#225555",+ "#225522",+ "#666633",+ "#663333",+ "#555555",+]+TOL_LIGHT: Final = [+ "#77AADD",+ "#EE8866",+ "#EEDD88",+ "#FFAABB",+ "#99DDFF",+ "#44BB99",+ "#BBCC33",+ "#AAAA00",+ "#DDDDDD",]-# Fluorophore colors (traditional - use with caution)-FLUOROPHORES_TRADITIONAL = {- 'DAPI': '#0000FF', # Blue- 'GFP': '#00FF00', # Green (problematic for colorblind)- 'RFP': '#FF0000', # Red- 'Cy5': '#FF00FF', # Magenta- 'YFP': '#FFFF00', # Yellow+PALETTES: Final = {+ "okabe_ito": OKABE_ITO_LIST,+ "okabe_ito_on_white": OKABE_ITO_ON_WHITE,+ "wong": WONG,+ "tol_bright": TOL_BRIGHT,+ "tol_high_contrast": TOL_HIGH_CONTRAST,+ "tol_vibrant": TOL_VIBRANT,+ "tol_muted": TOL_MUTED,+ "tol_medium_contrast": TOL_MEDIUM_CONTRAST,+ "tol_pale": TOL_PALE,+ "tol_dark": TOL_DARK,+ "tol_light": TOL_LIGHT,}-# Fluorophore colors (colorblind-friendly alternatives)-FLUOROPHORES_ACCESSIBLE = {- 'Channel1': '#0072B2', # Blue- 'Channel2': '#E69F00', # Orange (instead of green)- 'Channel3': '#D55E00', # Vermillion (instead of red)- 'Channel4': '#CC79A7', # Magenta- 'Channel5': '#F0E442', # Yellow+PALETTE_METADATA: Final = {+ "okabe_ito": {+ "kind": "qualitative",+ "source": "https://www.nature.com/articles/nmeth.1618",+ "caveat": "Yellow and other light colors need outlines on white.",+ },+ "okabe_ito_on_white": {+ "kind": "qualitative-subset",+ "source": "derived from Okabe-Ito by WCAG sRGB contrast calculation",+ "caveat": "At most five categories; use redundant encodings.",+ },+ "tol_bright": {+ "kind": "qualitative",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "maximum_categories": 7,+ },+ "tol_high_contrast": {+ "kind": "qualitative",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "maximum_categories": 3,+ "caveat": "Designed to retain separation in grayscale.",+ },+ "tol_vibrant": {+ "kind": "qualitative",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "maximum_categories": 7,+ },+ "tol_muted": {+ "kind": "qualitative",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "maximum_categories": 9,+ },+ "tol_medium_contrast": {+ "kind": "qualitative-pairs",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "maximum_categories": 6,+ },+ "tol_pale": {+ "kind": "text-background",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "caveat": "Not intended for plot lines or categorical maps.",+ },+ "tol_dark": {+ "kind": "text-foreground",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "caveat": "Not intended as a multi-category plot palette.",+ },+ "tol_light": {+ "kind": "labeled-cell-fill",+ "source": "https://sronpersonalpages.nl/~pault/data/colourschemes.pdf",+ "maximum_categories": 9,+ },}-# Genomics/Bioinformatics-DNA_BASES = {- 'A': '#00CC00', # Green- 'C': '#0000CC', # Blue- 'G': '#FFB300', # Orange- 'T': '#CC0000', # Red-}+SEQUENTIAL_COLORMAPS: Final = [+ "viridis",+ "plasma",+ "inferno",+ "magma",+ "cividis",+]-DNA_BASES_ACCESSIBLE = {- 'A': '#009E73', # Bluish Green- 'C': '#0072B2', # Blue- 'G': '#E69F00', # Orange- 'T': '#D55E00', # Vermillion+# Candidates, not blanket accessibility certifications. Center and normalization+# must match the scientific meaning, and rendered contrast still needs review.+DIVERGING_COLORMAP_CANDIDATES: Final = ["RdBu_r", "PuOr", "BrBG"]+DIVERGING_COLORMAPS_SAFE = DIVERGING_COLORMAP_CANDIDATES # Compatibility alias.+DIVERGING_COLORMAPS_AVOID: Final = ["RdYlGn"]++FLUOROPHORES_TRADITIONAL: Final = {+ "DAPI": "#0000FF",+ "GFP": "#00FF00",+ "RFP": "#FF0000",+ "Cy5": "#FF00FF",+ "YFP": "#FFFF00",+}+FLUOROPHORES_ACCESSIBLE: Final = {+ "Channel1": "#0072B2",+ "Channel2": "#E69F00",+ "Channel3": "#D55E00",+ "Channel4": "#CC79A7",+ "Channel5": "#F0E442",+}+DNA_BASES: Final = {+ "A": "#00CC00",+ "C": "#0000CC",+ "G": "#FFB300",+ "T": "#CC0000",+}+DNA_BASES_ACCESSIBLE: Final = {+ "A": "#009E73",+ "C": "#0072B2",+ "G": "#E69F00",+ "T": "#D55E00",}-def apply_palette(palette_name='okabe_ito'):- """- Apply a color palette to matplotlib's default color cycle.-- Parameters- ----------- palette_name : str- Name of the palette to apply. Options:- 'okabe_ito', 'wong', 'tol_bright', 'tol_muted',- 'tol_light', 'tol_high_contrast'+def get_palette(palette_name: str = "okabe_ito_on_white") -> list[str]:+ """Return a copy of a named palette."""+ try:+ return list(PALETTES[palette_name])+ except KeyError as exc:+ available = ", ".join(sorted(PALETTES))+ raise ValueError(+ f"palette {palette_name!r} not found; available: {available}"+ ) from exc- Returns- -------- list- List of colors in the palette- Examples- --------- >>> apply_palette('okabe_ito')- >>> plt.plot([1, 2, 3], [1, 4, 9]) # Uses Okabe-Ito colors- """+def apply_palette(palette_name: str = "okabe_ito_on_white") -> list[str]:+ """Apply a named palette to Matplotlib, importing it only when requested."""try:- import matplotlib.pyplot as plt- except ImportError:- print("matplotlib not installed")- return None-- palettes = {- 'okabe_ito': OKABE_ITO_LIST,- 'wong': WONG,- 'tol_bright': TOL_BRIGHT,- 'tol_muted': TOL_MUTED,- 'tol_light': TOL_LIGHT,- 'tol_high_contrast': TOL_HIGH_CONTRAST,- }-- if palette_name not in palettes:- available = ', '.join(palettes.keys())- raise ValueError(f"Palette '{palette_name}' not found. Available: {available}")-- colors = palettes[palette_name]- plt.rcParams['axes.prop_cycle'] = plt.cycler(color=colors)+ import matplotlib as mpl+ except ImportError as exc:+ raise RuntimeError(+ "Matplotlib is required to apply a palette; "+ "install the pinned snapshot documented in SKILL.md"+ ) from exc+ colors = get_palette(palette_name)+ mpl.rcParams["axes.prop_cycle"] = mpl.cycler(color=colors)return colors-def get_palette(palette_name='okabe_ito'):- """- Get a color palette as a list.-- Parameters- ----------- palette_name : str- Name of the palette-- Returns- -------- list- List of color hex codes- """- palettes = {- 'okabe_ito': OKABE_ITO_LIST,- 'wong': WONG,- 'tol_bright': TOL_BRIGHT,- 'tol_muted': TOL_MUTED,- 'tol_light': TOL_LIGHT,- 'tol_high_contrast': TOL_HIGH_CONTRAST,- }-- if palette_name not in palettes:- available = ', '.join(palettes.keys())- raise ValueError(f"Palette '{palette_name}' not found. Available: {available}")-- return palettes[palette_name]--if __name__ == "__main__":- print("Available colorblind-friendly palettes:")- print(f" - Okabe-Ito: {len(OKABE_ITO_LIST)} colors")- print(f" - Wong: {len(WONG)} colors")- print(f" - Tol Bright: {len(TOL_BRIGHT)} colors")- print(f" - Tol Muted: {len(TOL_MUTED)} colors")- print(f" - Tol Light: {len(TOL_LIGHT)} colors")- print(f" - Tol High Contrast: {len(TOL_HIGH_CONTRAST)} colors")-- print("\nOkabe-Ito palette (most recommended):")- for name, color in OKABE_ITO.items():- print(f" {name:15s}: {color}")+ for palette_name in sorted(PALETTES):+ print(f"{palette_name}: {', '.join(PALETTES[palette_name])}")diff --git a/skills/scientific-visualization/assets/nature.mplstyle b/skills/scientific-visualization/assets/nature.mplstyleindex bd8386d5..b10d1249 100644--- a/skills/scientific-visualization/assets/nature.mplstyle+++ b/skills/scientific-visualization/assets/nature.mplstyle@@ -1,33 +1,32 @@-# Nature journal style+# Flagship Nature visual starting style# Usage: plt.style.use('nature.mplstyle')#-# Optimized for Nature journal specifications:-# - Single column: 89 mm-# - Double column: 183 mm-# - High resolution requirements+# Snapshot source accessed 2026-07-23:+# https://research-figure-guide.nature.com/figures/preparing-figures-our-specifications+# This preset does not establish submission compliance.# Figure propertiesfigure.dpi: 100figure.facecolor: white-figure.constrained_layout.use: True-figure.figsize: 3.5, 2.625 # 89 mm single column, 3:4 aspect+figure.constrained_layout.use: False+figure.figsize: 3.503937, 2.625 # 89 mm single-column starting width# Font properties (Nature prefers smaller fonts)font.size: 7font.family: sans-serif-font.sans-serif: Arial, Helvetica+font.sans-serif: Arial, Helvetica, DejaVu Sans# Axes propertiesaxes.linewidth: 0.5-axes.labelsize: 8-axes.titlesize: 8+axes.labelsize: 7+axes.titlesize: 7axes.labelweight: normalaxes.spines.top: Falseaxes.spines.right: Falseaxes.edgecolor: blackaxes.axisbelow: Trueaxes.grid: False-axes.prop_cycle: cycler('color', ['E69F00', '56B4E9', '009E73', 'F0E442', '0072B2', 'D55E00', 'CC79A7'])+axes.prop_cycle: cycler('color', ['0072B2', 'D55E00', '009E73', 'CC79A7', '000000'])# Tick propertiesxtick.major.size: 2.5@@ -44,7 +43,7 @@ ytick.labelsize: 6ytick.direction: out# Line properties-lines.linewidth: 1.2+lines.linewidth: 1.0lines.markersize: 3lines.markeredgewidth: 0.4@@ -52,12 +51,18 @@ lines.markeredgewidth: 0.4legend.fontsize: 6legend.frameon: False-# Save properties (Nature requirements)-savefig.dpi: 600 # 1000 for line art, 600 for combination+# Save properties; DPI affects embedded raster artists in vector output.+savefig.dpi: 450savefig.format: pdf-savefig.bbox: tight+savefig.bbox: standardsavefig.pad_inches: 0.05+savefig.transparent: Falsesavefig.facecolor: white+savefig.edgecolor: white++pdf.fonttype: 42+ps.fonttype: 42+svg.fonttype: none# Image propertiesimage.cmap: viridisdiff --git a/skills/scientific-visualization/assets/presentation.mplstyle b/skills/scientific-visualization/assets/presentation.mplstyleindex d435fef7..e4930f52 100644--- a/skills/scientific-visualization/assets/presentation.mplstyle+++ b/skills/scientific-visualization/assets/presentation.mplstyle@@ -3,17 +3,18 @@## Larger fonts and thicker lines for presentations,# posters, and projected displays+# Reviewed with Matplotlib 3.11.1 on 2026-07-23.# Figure propertiesfigure.dpi: 100figure.facecolor: white-figure.constrained_layout.use: True+figure.constrained_layout.use: Falsefigure.figsize: 8, 6# Font properties (larger for visibility)font.size: 14font.family: sans-serif-font.sans-serif: Arial, Helvetica, Calibri+font.sans-serif: Arial, Helvetica, Calibri, DejaVu Sans# Axes propertiesaxes.linewidth: 1.5@@ -25,7 +26,7 @@ axes.spines.right: Falseaxes.edgecolor: blackaxes.axisbelow: Trueaxes.grid: False-axes.prop_cycle: cycler('color', ['E69F00', '56B4E9', '009E73', 'F0E442', '0072B2', 'D55E00', 'CC79A7'])+axes.prop_cycle: cycler('color', ['0072B2', 'D55E00', '009E73', 'CC79A7', '000000'])# Tick propertiesxtick.major.size: 6@@ -53,9 +54,15 @@ legend.frameon: False# Save propertiessavefig.dpi: 300savefig.format: png-savefig.bbox: tight+savefig.bbox: standardsavefig.pad_inches: 0.1+savefig.transparent: Falsesavefig.facecolor: white+savefig.edgecolor: white++pdf.fonttype: 42+ps.fonttype: 42+svg.fonttype: none# Image propertiesimage.cmap: viridisdiff --git a/skills/scientific-visualization/assets/publication.mplstyle b/skills/scientific-visualization/assets/publication.mplstyleindex fe224c40..106936e0 100644--- a/skills/scientific-visualization/assets/publication.mplstyle+++ b/skills/scientific-visualization/assets/publication.mplstyle@@ -1,14 +1,14 @@-# Publication-quality matplotlib style+# General scientific-publication starting style# Usage: plt.style.use('publication.mplstyle')#-# This style provides clean, professional formatting suitable-# for most scientific journals+# Reviewed with Matplotlib 3.11.1 on 2026-07-23.+# This is not a journal-compliance profile.# Figure propertiesfigure.dpi: 100figure.facecolor: whitefigure.autolayout: False-figure.constrained_layout.use: True+figure.constrained_layout.use: Falsefigure.figsize: 3.5, 2.5# Font properties@@ -17,9 +17,9 @@ font.family: sans-seriffont.sans-serif: Arial, Helvetica, DejaVu Sans# Axes properties-axes.linewidth: 0.5-axes.labelsize: 9-axes.titlesize: 9+axes.linewidth: 0.6+axes.labelsize: 8+axes.titlesize: 8axes.labelweight: normalaxes.spines.top: Falseaxes.spines.right: False@@ -29,18 +29,20 @@ axes.edgecolor: blackaxes.labelcolor: blackaxes.axisbelow: Trueaxes.grid: False-axes.prop_cycle: cycler('color', ['E69F00', '56B4E9', '009E73', 'F0E442', '0072B2', 'D55E00', 'CC79A7', '000000'])+# Five Okabe-Ito colors that meet 3:1 against white in sRGB.+# Still add markers, line styles, hatching, or direct labels.+axes.prop_cycle: cycler('color', ['0072B2', 'D55E00', '009E73', 'CC79A7', '000000'])# Tick propertiesxtick.major.size: 3xtick.minor.size: 2-xtick.major.width: 0.5+xtick.major.width: 0.6xtick.minor.width: 0.5xtick.labelsize: 7xtick.direction: outytick.major.size: 3ytick.minor.size: 2-ytick.major.width: 0.5+ytick.major.width: 0.6ytick.minor.width: 0.5ytick.labelsize: 7ytick.direction: out@@ -58,10 +60,17 @@ legend.loc: best# Save propertiessavefig.dpi: 300savefig.format: pdf-savefig.bbox: tight+# "tight" changes physical output dimensions; request it explicitly if wanted.+savefig.bbox: standardsavefig.pad_inches: 0.05savefig.transparent: Falsesavefig.facecolor: white+savefig.edgecolor: white++# Editable TrueType text in PDF/PS. SVG text stays as text and is not embedded.+pdf.fonttype: 42+ps.fonttype: 42+svg.fonttype: none# Image propertiesimage.cmap: viridisdiff --git a/skills/scientific-visualization/assets/publisher_profiles.json b/skills/scientific-visualization/assets/publisher_profiles.jsonnew file mode 100644index 00000000..7d99e7eb--- /dev/null+++ b/skills/scientific-visualization/assets/publisher_profiles.json@@ -0,0 +1,269 @@+{+ "schema_version": "1.0",+ "accessed": "2026-07-23",+ "notice": "Dated publisher snapshots for planning only. They do not establish submission compliance; verify the target journal, article type, and submission stage.",+ "profiles": {+ "nature": {+ "label": "Nature (flagship)",+ "scope": "Final submission after acceptance in principle",+ "phase": "final",+ "journal_specific": true,+ "sources": [+ "https://www.nature.com/nature/for-authors/final-submission",+ "https://research-figure-guide.nature.com/figures/preparing-figures-our-specifications"+ ],+ "widths_mm": {+ "single": 89.0,+ "one-and-half-min": 120.0,+ "one-and-half-max": 136.0,+ "full": 183.0+ },+ "max_height_mm": 247.0,+ "formats": {+ "line-art": ["pdf", "eps", "ai", "ps"],+ "photo": ["psd", "tiff", "jpeg"],+ "combination": ["pdf", "eps", "ai", "ps"]+ },+ "raster_dpi": {+ "photo": {"min": 300.0, "max": 600.0},+ "line-art": null,+ "combination": null+ },+ "color_modes": ["RGB", "CMYK"],+ "preferred_color_mode": "RGB",+ "max_file_bytes": null,+ "notes": [+ "Keep line art, text, and labels editable rather than rasterizing or outlining them.",+ "The companion research-figure guide says image exports should be at least 450 dpi; the flagship final-submission page accepts photographic images at 300-600 dpi.",+ "Panel labels are lowercase, bold, upright, 8 pt; other text is 5-7 pt at final size."+ ]+ },+ "science": {+ "label": "Science (AAAS)",+ "scope": "Revised manuscript figures",+ "phase": "revised",+ "journal_specific": true,+ "sources": [+ "https://www.science.org/content/page/instructions-preparing-initial-manuscript",+ "https://www.science.org/content/page/instructions-preparing-revised-manuscript"+ ],+ "widths_mm": {+ "single": 57.0,+ "double": 121.0,+ "full": 184.0+ },+ "max_height_mm": null,+ "formats": {+ "line-art": ["pdf", "eps", "ai"],+ "photo": ["tiff"],+ "combination": ["pdf", "eps"]+ },+ "raster_dpi": {+ "photo": {"min": 300.0},+ "line-art": {"min": 300.0},+ "combination": {"min": 300.0}+ },+ "color_modes": null,+ "preferred_color_mode": null,+ "max_file_bytes": null,+ "notes": [+ "Initial-submission figures may be embedded and should be 300 dpi; revised figures are uploaded separately.",+ "Revised guidance prefers vectors and prohibits upsampling.",+ "Lettering should be about 7 pt after reduction and never smaller than 5 pt."+ ]+ },+ "cell": {+ "label": "Cell Press",+ "scope": "Final production files for most Cell Press journals",+ "phase": "final",+ "journal_specific": false,+ "sources": [+ "https://www.cell.com/information-for-authors/figure-guidelines"+ ],+ "widths_mm": {+ "single": 85.0,+ "one-and-half": 114.0,+ "full": 174.0+ },+ "max_height_mm": 200.0,+ "formats": {+ "line-art": ["pdf", "tiff", "eps"],+ "photo": ["tiff", "pdf", "jpeg"],+ "combination": ["pdf", "tiff", "eps"]+ },+ "raster_dpi": {+ "photo": {"min": 300.0},+ "line-art": {"min": 1000.0},+ "combination": null,+ "black-and-white": {"min": 500.0}+ },+ "color_modes": ["RGB"],+ "preferred_color_mode": "RGB",+ "max_file_bytes": 20000000,+ "notes": [+ "Widths apply to two-column article formats; three-column formats differ.",+ "TIFF and PDF are preferred for most final production files; exceptions include STAR Protocols and Cell Leading Edge.",+ "The 16.5 by 20 cm overall size is a recommendation, not a universal hard limit.",+ "Use Arial, capital panel labels, and about 6-8 pt text at final size."+ ]+ },+ "plos": {+ "label": "PLOS research journals",+ "scope": "Figures after provisional Editorial Accept (PLOS Computational Biology snapshot)",+ "phase": "post-provisional-accept",+ "journal_specific": false,+ "sources": [+ "https://journals.plos.org/ploscompbiol/s/figures"+ ],+ "widths_mm": {+ "minimum": 66.8,+ "text-column": 132.0,+ "full": 190.5+ },+ "width_range_px_at_300_dpi": [789, 2250],+ "max_height_mm": 222.3,+ "max_height_px_at_300_dpi": 2625,+ "formats": {+ "line-art": ["eps", "tiff"],+ "photo": ["tiff"],+ "combination": ["eps", "tiff"]+ },+ "raster_dpi": {+ "photo": {"min": 300.0, "max": 600.0},+ "line-art": {"min": 300.0, "max": 600.0},+ "combination": {"min": 300.0, "max": 600.0}+ },+ "color_modes": ["RGB", "L"],+ "preferred_color_mode": "RGB",+ "max_file_bytes": null,+ "max_file_bytes_exclusive": 10000000,+ "notes": [+ "PLOS waives formatting requirements until provisional Editorial Accept.",+ "Use Arial, Times, or Symbol at 8-12 pt; combine all panels for one figure into one file.",+ "The cited page states TIFF or EPS only and requires RGB 8-bit/channel or grayscale.",+ "The <10 MB rule is screened as <10,000,000 bytes; verify how the submission portal reports MB."+ ]+ },+ "elsevier": {+ "label": "Elsevier general artwork guidance",+ "scope": "Publisher-general defaults; target journals may override them",+ "phase": "final",+ "journal_specific": false,+ "sources": [+ "https://www.elsevier.com/about/policies-and-standards/author/artwork-and-media-instructions/artwork-formats-checklist",+ "https://www.elsevier.com/about/policies-and-standards/author/artwork-and-media-instructions/artwork-sizing"+ ],+ "widths_mm": {+ "minimum": 30.0,+ "single": 90.0,+ "one-and-half": 140.0,+ "full": 190.0+ },+ "max_height_mm": null,+ "formats": {+ "line-art": ["eps", "pdf"],+ "photo": ["tiff", "jpeg"],+ "combination": ["eps", "pdf", "tiff"]+ },+ "raster_dpi": {+ "photo": {"min": 300.0},+ "line-art": {"min": 1000.0},+ "combination": {"min": 500.0}+ },+ "color_modes": ["RGB"],+ "preferred_color_mode": "RGB",+ "max_file_bytes": null,+ "notes": [+ "Elsevier explicitly directs authors to the target journal's Guide for Authors for overrides.",+ "Normal lettering is generally 7 pt at final size; 6 pt is a rule-of-thumb floor for subscripts and superscripts."+ ]+ },+ "ieee": {+ "label": "IEEE journals",+ "scope": "IEEE Author Center journal graphics guidance",+ "phase": "submission",+ "journal_specific": false,+ "sources": [+ "https://journals.ieeeauthorcenter.ieee.org/create-your-ieee-journal-article/create-graphics-for-your-article/resolution-and-size/"+ ],+ "widths_mm": {+ "single": 88.9,+ "full": 182.0+ },+ "max_height_mm": null,+ "formats": {+ "line-art": ["ps", "eps", "pdf"],+ "photo": [],+ "combination": ["ps", "eps", "pdf"]+ },+ "raster_dpi": {+ "photo": {"min_exclusive": 300.0},+ "line-art": {"min_exclusive": 600.0},+ "combination": {"min_exclusive": 300.0}+ },+ "color_modes": null,+ "preferred_color_mode": null,+ "max_file_bytes": null,+ "notes": [+ "The cited page identifies PS, EPS, and PDF as acceptable vector formats.",+ "IEEE states greater than 300 dpi for non-vector color/grayscale and greater than 600 dpi for black-and-white line art."+ ]+ },+ "bmc": {+ "label": "BMC journal example",+ "scope": "BMC Bioinformatics preparing-your-manuscript guidance; verify the target BMC journal",+ "phase": "submission",+ "journal_specific": true,+ "sources": [+ "https://bmcbioinformatics.biomedcentral.com/submission-guidelines/preparing-your-manuscript"+ ],+ "widths_mm": {+ "half": 85.0,+ "full": 170.0+ },+ "max_height_mm": 225.0,+ "formats": {+ "line-art": ["eps", "pdf"],+ "photo": ["tiff", "jpeg", "png", "bmp"],+ "combination": ["eps", "pdf"]+ },+ "raster_dpi": {+ "photo": {"target": 300.0},+ "line-art": {"target": 300.0},+ "combination": {"target": 300.0}+ },+ "color_modes": null,+ "preferred_color_mode": null,+ "max_file_bytes": 10000000,+ "notes": [+ "The source says approximately 300 dpi at final size, not a universal minimum.",+ "Figures are 600 px standard or 1200 px high-resolution on the web.",+ "All fonts must be embedded and lines should be wider than 0.25 pt at final width."+ ]+ },+ "acs": {+ "label": "ACS Publications general graphics page",+ "scope": "General manuscript-graphics sizing; consult the selected ACS journal",+ "phase": "submission",+ "journal_specific": false,+ "sources": [+ "https://pubs.acs.org/page/4authors/submission/graphics_prep.html"+ ],+ "widths_mm": {+ "single": 82.55,+ "full": 177.8+ },+ "max_height_mm": 241.3,+ "formats": null,+ "raster_dpi": null,+ "color_modes": null,+ "preferred_color_mode": null,+ "max_file_bytes": null,+ "notes": [+ "The general page gives 3.25-inch and 7-inch widths but does not establish a current universal digital format/DPI profile.",+ "It recommends lettering no smaller than 5 pt after reduction and lines no thinner than 1 pt.",+ "Use the target ACS journal's current Author Guidelines for binding technical requirements."+ ]+ }+ }+}diff --git a/skills/scientific-visualization/references/color_palettes.md b/skills/scientific-visualization/references/color_palettes.mdindex 293270af..6b1a6c6e 100644--- a/skills/scientific-visualization/references/color_palettes.md+++ b/skills/scientific-visualization/references/color_palettes.md@@ -1,348 +1,227 @@-# Scientific Color Palettes and Guidelines+# Color, Contrast, and Palette Selection-## Overview+Reviewed 2026-07-23. Source IDs resolve in `sources.md`. A named “colorblind-safe” palette is not a guarantee that a rendered figure is accessible: background, line thickness, adjacency, text, category count, display/print conversion, and redundant encoding all matter.-Color choice in scientific visualization is critical for accessibility, clarity, and accurate data representation. This reference provides colorblind-friendly palettes and best practices for color usage.+## Start with data semantics-## Colorblind-Friendly Palettes+- **Qualitative:** unordered categories. Hue can separate groups; lightness should not imply an unintended ranking.+- **Sequential:** ordered low-to-high values. Use monotonic perceived lightness.+- **Diverging:** departure around a meaningful center. Use a neutral midpoint and document the normalization.+- **Cyclic:** periodic values where endpoints meet.-### Okabe-Ito Palette (Recommended for Categories)+Do not use a diverging map merely because values contain positive and negative numbers; the center must have scientific meaning. Do not use a rainbow map as a generic ordered scale. Paul Tol documents false visual transitions, lack of inherent magnitude ordering, and color-vision problems in ordinary rainbow schemes [TOL].-The Okabe-Ito palette is specifically designed to be distinguishable by people with all forms of color blindness.+With Matplotlib, colormap and normalization are separate:```python-# Okabe-Ito colors (RGB values)-okabe_ito = {- 'orange': '#E69F00', # RGB: (230, 159, 0)- 'sky_blue': '#56B4E9', # RGB: (86, 180, 233)- 'bluish_green': '#009E73', # RGB: (0, 158, 115)- 'yellow': '#F0E442', # RGB: (240, 228, 66)- 'blue': '#0072B2', # RGB: (0, 114, 178)- 'vermillion': '#D55E00', # RGB: (213, 94, 0)- 'reddish_purple': '#CC79A7', # RGB: (204, 121, 167)- 'black': '#000000' # RGB: (0, 0, 0)-}-```--**Usage in Matplotlib:**-```python-import matplotlib.pyplot as plt--colors = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7', '#000000']-plt.rcParams['axes.prop_cycle'] = plt.cycler(color=colors)-```--**Usage in Seaborn:**-```python-import seaborn as sns--okabe_ito_palette = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7']-sns.set_palette(okabe_ito_palette)-```--**Usage in Plotly:**-```python-import plotly.graph_objects as go+import matplotlib as mpl-okabe_ito_plotly = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7']-fig = go.Figure()-# Apply to discrete color scale+norm = mpl.colors.TwoSlopeNorm(vmin=-2, vcenter=0, vmax=5)+image = ax.imshow(values, cmap="RdBu_r", norm=norm)+fig.colorbar(image, ax=ax, label="Change (unit)")```-### Wong Palette (Alternative for Categories)+Use `LogNorm` for strictly positive orders of magnitude, `SymLogNorm` for signed data with a disclosed linear zone, `BoundaryNorm` for meaningful classes, and `TwoSlopeNorm` for unequal ranges around a center [MPL-NORM].-Another excellent colorblind-friendly palette by Bang Wong (Nature Methods).+## Okabe-Ito / Wong colors-```python-wong_palette = {- 'black': '#000000',- 'orange': '#E69F00',- 'sky_blue': '#56B4E9',- 'green': '#009E73',- 'yellow': '#F0E442',- 'blue': '#0072B2',- 'vermillion': '#D55E00',- 'purple': '#CC79A7'-}-```--### Paul Tol Palettes--Paul Tol has designed multiple scientifically-optimized palettes for different use cases.+The eight colors commonly reproduced from Wong’s Nature Methods article are [WONG]:-**Bright Palette (up to 7 categories):**```python-tol_bright = ['#4477AA', '#EE6677', '#228833', '#CCBB44',- '#66CCEE', '#AA3377', '#BBBBBB']+OKABE_ITO = [+ "#E69F00", # orange+ "#56B4E9", # sky blue+ "#009E73", # bluish green+ "#F0E442", # yellow+ "#0072B2", # blue+ "#D55E00", # vermillion+ "#CC79A7", # reddish purple+ "#000000", # black+]```-**Muted Palette (up to 9 categories):**-```python-tol_muted = ['#332288', '#88CCEE', '#44AA99', '#117733',- '#999933', '#DDCC77', '#CC6677', '#882255', '#AA4499']-```+Several light colors do not reach 3:1 against white. For thin lines or required graphical objects on white, start with the bundled five-color subset:-**High Contrast (3 categories only):**```python-tol_high_contrast = ['#004488', '#DDAA33', '#BB5566']+OKABE_ITO_ON_WHITE = [+ "#0072B2",+ "#D55E00",+ "#009E73",+ "#CC79A7",+ "#000000",+]```-## Sequential Colormaps (Continuous Data)+This subset is derived by WCAG sRGB contrast calculation; it is not a published palette or a compliance certification. Use marker/line-style redundancy and audit the actual rendering.-Sequential colormaps represent data from low to high values with a single hue.+## Paul Tol qualitative schemes-### Perceptually Uniform Colormaps+Paul Tol’s canonical site moved to `sronpersonalpages.nl` in July 2026. The current technical note remains SRON/EPS/TN/09-002, issue 3.2, dated 2021-08-18 [TOL] [TOL-HOME].-These colormaps have uniform perceptual change across the color scale.+The note states that the bright, high-contrast, vibrant, muted, and medium-contrast qualitative schemes are color-blind safe under its design/testing assumptions. It also states:-**Viridis (default in Matplotlib):**-- Colorblind-friendly-- Prints well in grayscale-- Perceptually uniform-```python-plt.imshow(data, cmap='viridis')-```+- high-contrast is the strongest choice for grayscale/monochrome separation;+- medium-contrast provides three pairs but weaker grayscale separation;+- light is reasonably distinct and intended mainly for labeled cell fills;+- pale/dark are not sufficiently distinct for multi-series lines or maps and are meant for text backgrounds/foregrounds;+- most multi-color qualitative schemes do not remain fully separable in grayscale.-**Cividis:**-- Optimized for colorblind viewers-- Designed specifically for deuteranopia/protanopia-```python-plt.imshow(data, cmap='cividis')-```+Bundled fixed-order values:-**Plasma, Inferno, Magma:**-- Perceptually uniform alternatives to viridis-- Good for different aesthetic preferences```python-plt.imshow(data, cmap='plasma')-```--### When to Use Sequential Maps-- Heatmaps showing intensity-- Geographic elevation data-- Probability distributions-- Any single-variable continuous data (low → high)+TOL_BRIGHT = [+ "#4477AA", "#EE6677", "#228833", "#CCBB44",+ "#66CCEE", "#AA3377", "#BBBBBB",+]-## Diverging Colormaps (Negative to Positive)+TOL_HIGH_CONTRAST = ["#004488", "#DDAA33", "#BB5566"]-Diverging colormaps have a neutral middle color with two contrasting colors at extremes.+TOL_VIBRANT = [+ "#EE7733", "#0077BB", "#33BBEE", "#EE3377",+ "#CC3311", "#009988", "#BBBBBB",+]-### Colorblind-Safe Diverging Maps+TOL_MUTED = [+ "#CC6677", "#332288", "#DDCC77", "#117733", "#88CCEE",+ "#882255", "#44AA99", "#999933", "#AA4499",+]-**RdYlBu (Red-Yellow-Blue):**-```python-plt.imshow(data, cmap='RdYlBu_r') # _r reverses: blue (low) to red (high)+TOL_MEDIUM_CONTRAST = [+ "#6699CC", "#004488", "#EECC66",+ "#994455", "#997700", "#EE99AA",+]```-**PuOr (Purple-Orange):**-- Excellent for colorblind viewers-```python-plt.imshow(data, cmap='PuOr')-```+The maximum intended series counts are 7, 3, 7, 9, and 6 respectively. Do not interpolate qualitative palettes.-**BrBG (Brown-Blue-Green):**-- Good colorblind accessibility-```python-plt.imshow(data, cmap='BrBG')-```+## ColorBrewer-### Avoid These Diverging Maps-- **RdGn (Red-Green)**: Problematic for red-green colorblindness-- **RdYlGn (Red-Yellow-Green)**: Same issue+ColorBrewer 2.0 is an authoritative interactive resource for sequential, diverging, and qualitative cartographic schemes. It allows filtering by “colorblind safe,” “print friendly,” and “photocopy safe,” and exposes the supported number of data classes [COLORBREWER].-### When to Use Diverging Maps-- Correlation matrices-- Change/difference data (positive vs. negative)-- Deviation from a central value-- Temperature anomalies+Use the exact class count shown by ColorBrewer. A scheme marked safe at one class count may not be marked safe at another. ColorBrewer’s flags are design guidance for its intended mapping context, not a WCAG conformance result for arbitrary line widths, backgrounds, or text.-## Special Purpose Palettes+Matplotlib exposes many ColorBrewer-derived maps. Verify the exact map direction and class count rather than relying on its name.-### For Genomics/Bioinformatics+## Perceptually uniform continuous maps-**Sequence type identification:**-```python-# DNA/RNA bases-nucleotide_colors = {- 'A': '#00CC00', # Green- 'C': '#0000CC', # Blue- 'G': '#FFB300', # Orange- 'T': '#CC0000', # Red- 'U': '#CC0000' # Red (RNA)-}-```+Matplotlib recommends selecting maps based on data semantics and discusses lightness behavior in its colormap guide [MPL-CMAP]. Common continuous candidates:-**Gene expression:**-- Use sequential colormaps (viridis, YlOrRd) for expression levels-- Use diverging colormaps (RdBu) for log2 fold change+- `viridis`, `plasma`, `inferno`, `magma`: perceptually uniform sequential families;+- `cividis`: designed with color-vision deficiencies in mind;+- `RdBu_r`, `PuOr`, `BrBG`: possible diverging candidates after checking the center, direction, contrast, and grayscale behavior.-### For Microscopy+No list is universally safe. A map can be perceptually uniform yet still fail to distinguish a narrow feature at the chosen size, or lose detail during RGB-to-CMYK conversion.-**Fluorescence channels:**-```python-# Traditional fluorophore colors (use with caution)-fluorophore_colors = {- 'DAPI': '#0000FF', # Blue - DNA- 'GFP': '#00FF00', # Green (problematic for colorblind)- 'RFP': '#FF0000', # Red- 'Cy5': '#FF00FF' # Magenta-}--# Colorblind-friendly alternatives-fluorophore_alt = {- 'Channel1': '#0072B2', # Blue- 'Channel2': '#E69F00', # Orange (instead of green)- 'Channel3': '#D55E00', # Vermillion- 'Channel4': '#CC79A7' # Magenta-}-```+## WCAG contrast: what to test-## Color Usage Best Practices+WCAG 2.2 is normative for web content [WCAG22]:-### Categorical Data (Qualitative Color Schemes)+- normal text: 4.5:1 against its background (SC 1.4.3 AA);+- large text: 3:1 (SC 1.4.3 AA);+- graphical objects required to understand content: 3:1 against adjacent colors (SC 1.4.11 AA);+- color must not be the only visual means of conveying information (SC 1.4.1 A).-**Do:**-- Use distinct, saturated colors from Okabe-Ito or Wong palette-- Limit to 7-8 categories max in one plot-- Use consistent colors for same categories across figures-- Add patterns/markers when colors alone might be insufficient+W3C’s understanding document uses line and pie charts as examples and explains that required graphical objects are tested against adjacent colors; all data-series colors do not automatically need 3:1 against each other when they do not overlap [WCAG-NONTEXT].-**Don't:**-- Use red/green combinations-- Use rainbow (jet) colormap for categories-- Use similar hues that are hard to distinguish+Run:-### Continuous Data (Sequential/Diverging Schemes)+```bash+uv run --isolated --no-project --python 3.13 \+ python scripts/palette_audit.py \+ --palette okabe_ito_on_white \+ --background FFFFFF \+ --role graphical+```-**Do:**-- Use perceptually uniform colormaps (viridis, plasma, cividis)-- Choose diverging maps when data has meaningful center point-- Include colorbar with labeled ticks-- Test appearance in grayscale+The CLI reports:-**Don't:**-- Use rainbow (jet) colormap - not perceptually uniform-- Use red-green diverging maps-- Omit colorbar on heatmaps+- exact WCAG sRGB contrast against the chosen background;+- pairwise contrast;+- pairwise CIE L* separation after removing hue.-## Testing for Colorblind Accessibility+Its default grayscale threshold (ΔL* 10) is a heuristic. It is not a WCAG threshold and does not simulate every color-vision deficiency, printer, profile, or viewing condition.-### Online Simulators-- **Coblis**: https://www.color-blindness.com/coblis-color-blindness-simulator/-- **Color Oracle**: Free downloadable tool for Windows/Mac/Linux-- **Sim Daltonism**: Mac application+## Redundant encoding-### Types of Color Vision Deficiency-- **Deuteranopia** (~5% of males): Cannot distinguish green-- **Protanopia** (~2% of males): Cannot distinguish red-- **Tritanopia** (<1%): Cannot distinguish blue (rare)+Use at least one non-color cue:-### Python Tools```python-# Using colorspacious to simulate colorblind vision-from colorspacious import cspace_convert--def simulate_deuteranopia(image_rgb):- from colorspacious import cspace_convert- # Convert to colorblind simulation- # (Implementation would require colorspacious library)- pass+colors = ["#0072B2", "#D55E00", "#009E73"]+linestyles = ["-", "--", "-."]+markers = ["o", "s", "^"]++for index, series in enumerate(series_list):+ ax.plot(+ x,+ series,+ color=colors[index],+ linestyle=linestyles[index],+ marker=markers[index],+ markevery=5,+ label=labels[index],+ )```-## Implementation Examples+For bars, use edge contrast, labels, and restrained hatching. For images, consider accessible channel combinations plus separate grayscale panels. Direct labels often outperform distant legends.++## Missing and out-of-range colors++Assign explicit colors to missing and out-of-range values:-### Setting Global Matplotlib Style```python-import matplotlib.pyplot as pltimport matplotlib as mpl-# Set Okabe-Ito as default color cycle-okabe_ito_colors = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7']-mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=okabe_ito_colors)--# Set default colormap to viridis-mpl.rcParams['image.cmap'] = 'viridis'+cmap = mpl.colormaps["viridis"].with_extremes(+ bad="#777777",+ under="#222222",+ over="#FDE725",+)```-### Seaborn with Custom Palette-```python-import seaborn as sns+Label these states in the colorbar/legend. Never let missing values default to the low end of a quantitative scale.-# Set Paul Tol muted palette-tol_muted = ['#332288', '#88CCEE', '#44AA99', '#117733',- '#999933', '#DDCC77', '#CC6677', '#882255', '#AA4499']-sns.set_palette(tol_muted)+## Seaborn and Plotly-# For heatmaps-sns.heatmap(data, cmap='viridis', annot=True)-```+Seaborn 0.13.2 accepts palette names, lists, and dictionaries through `palette=`/`set_palette()`. Apply a Matplotlib style before Seaborn only if the subsequent `sns.set_theme()` call will not overwrite the intended rc settings; pass `rc=` explicitly when needed [SEABORN-PALETTE] [SEABORN-THEME].-### Plotly with Discrete Colors```python-import plotly.express as px--# Use Okabe-Ito for categorical data-okabe_ito_plotly = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7']+import seaborn as sns-fig = px.scatter(df, x='x', y='y', color='category',- color_discrete_sequence=okabe_ito_plotly)+sns.set_theme(style="ticks", context="paper")+sns.set_palette(OKABE_ITO_ON_WHITE)```-## Grayscale Compatibility--All figures should remain interpretable in grayscale. Test by converting to grayscale:+For Plotly, set categorical colors explicitly and add symbols/dashes:```python-# Convert figure to grayscale for testing-fig.savefig('figure_gray.png', dpi=300, colormap='gray')+fig = px.scatter(+ frame,+ x="x",+ y="y",+ color="group",+ symbol="group",+ color_discrete_sequence=OKABE_ITO_ON_WHITE,+)```-**Strategies for grayscale compatibility:**-1. Use different line styles (solid, dashed, dotted)-2. Use different marker shapes (circles, squares, triangles)-3. Add hatching patterns to bars-4. Ensure sufficient luminance contrast between colors--## Color Spaces--### RGB vs CMYK-- **RGB** (Red, Green, Blue): For digital/screen display-- **CMYK** (Cyan, Magenta, Yellow, Black): For print--**Important:** Colors appear different in print vs. screen. When preparing for print:-1. Convert to CMYK color space-2. Check color appearance in CMYK preview-3. Ensure sufficient contrast remains--### Matplotlib Color Spaces-```python-# Save for print (CMYK)-# Note: Direct CMYK support limited; use PDF and let publisher convert-fig.savefig('figure.pdf', dpi=300)--# For RGB (digital)-fig.savefig('figure.png', dpi=300)-```+Check the static export too. Interactive hover does not replace contrast, labels, keyboard operation, alt text, or a static fallback.-## Common Mistakes+## Color management-1. **Using jet/rainbow colormap**: Not perceptually uniform; avoid-2. **Red-green combinations**: ~8% of males cannot distinguish-3. **Too many colors**: More than 7-8 becomes difficult to distinguish-4. **Inconsistent color meaning**: Same color should mean same thing across figures-5. **Missing colorbar**: Always include for continuous data-6. **Low contrast**: Ensure colors differ sufficiently-7. **Relying solely on color**: Add texture, patterns, or markers+- Work in sRGB unless the publisher or calibrated workflow requires another space.+- Preserve ICC profiles in scientific raster images when relevant.+- Preview print conversion when the publisher converts RGB to CMYK.+- Do not alter raw intensity data merely to obtain attractive colors.+- Record channel mappings, color limits, normalization, and any color-space conversion.+- Avoid transparency when blending with an unknown background can change contrast.-## Resources+## Review checklist-- **ColorBrewer**: http://colorbrewer2.org/ - Choose palettes by colorblind-safe option-- **Paul Tol's palettes**: https://personal.sron.nl/~pault/-- **Okabe-Ito palette origin**: "Color Universal Design" (Okabe & Ito, 2008)-- **Matplotlib colormaps**: https://matplotlib.org/stable/tutorials/colors/colormaps.html-- **Seaborn palettes**: https://seaborn.pydata.org/tutorial/color_palettes.html+- [ ] Palette type matches data semantics.+- [ ] Center, limits, normalization, and missing-value color are explicit.+- [ ] Foreground/background contrast was audited at final size.+- [ ] Color is redundant with shape, line style, hatching, labels, or layout.+- [ ] Grayscale was inspected without treating it as a complete accessibility test.+- [ ] Print/profile conversion was reviewed when relevant.+- [ ] Palette and category mapping are consistent across figures.+- [ ] Underlying values and an accessible text/table alternative are available.diff --git a/skills/scientific-visualization/references/journal_requirements.md b/skills/scientific-visualization/references/journal_requirements.mdindex 2256fe14..cf7438c5 100644--- a/skills/scientific-visualization/references/journal_requirements.md+++ b/skills/scientific-visualization/references/journal_requirements.md@@ -1,320 +1,169 @@-# Journal-Specific Figure Requirements--## Overview--Different journals have specific technical requirements for figures. This reference compiles common requirements from major scientific publishers. **Always check the specific journal's author guidelines for the most current requirements.**--## Nature Portfolio (Nature, Nature Methods, etc.)--### Technical Specifications-- **File formats**:- - Vector: PDF, EPS, AI (preferred for graphs)- - Raster: TIFF, PNG (for images)- - Never: PowerPoint, Word, JPEG--- **Resolution**:- - Line art: 1000-1200 DPI- - Combination (line art + images): 600 DPI- - Photographs/microscopy: 300 DPI minimum--- **Color space**: RGB (Nature is digital-first)--- **Dimensions**:- - Single column: 89 mm (3.5 inches)- - 1.5 column: 120 mm (4.7 inches)- - Double column: 183 mm (7.2 inches)- - Maximum height: 247 mm (9.7 inches)--- **Fonts**:- - Arial or Helvetica (or similar sans-serif)- - Minimum 5-7 pt at final size- - Embed all fonts in PDF/EPS--### Nature Specific Guidelines-- Panel labels: a, b, c (lowercase, bold) in top-left corner-- Scale bars required for microscopy images-- Gel images: Include molecular weight markers-- Cropping: Indicate with line breaks-- Statistics: Mark significance; define symbols in legend-- Source data: Required for all graphs--### File Naming-Format: `FirstAuthorLastName_FigureNumber.ext`-Example: `Smith_Fig1.pdf`--## Science (AAAS)--### Technical Specifications-- **File formats**:- - Vector: EPS, PDF (preferred)- - Raster: TIFF- - Acceptable: AI, PSD (Photoshop)--- **Resolution**:- - Line art: 1000 DPI minimum- - Photographs: 300 DPI minimum- - Combination: 600 DPI minimum--- **Color space**: RGB--- **Dimensions**:- - Single column: 5.5 cm (2.17 inches)- - 1.5 column: 12 cm (4.72 inches)- - Full width: 17.5 cm (6.89 inches)- - Maximum height: 23.3 cm (9.17 inches)--- **Fonts**:- - Helvetica (or Arial)- - 6-8 pt minimum at final size- - Consistent across all figures--### Science Specific Guidelines-- Panel labels: (A), (B), (C) in parentheses-- Minimal text within figures (details in caption)-- High contrast for web and print-- Error bars required; define in caption-- Avoid excessive whitespace--### File Naming-Format: `Manuscript#_Fig#.ext`-Example: `abn1234_Fig1.eps`--## Cell Press (Cell, Neuron, Molecular Cell, etc.)--### Technical Specifications-- **File formats**:- - Vector: PDF, EPS (preferred for graphs/diagrams)- - Raster: TIFF (for photographs)--- **Resolution**:- - Line art: 1000 DPI- - Photographs: 300 DPI- - Combination: 600 DPI--- **Color space**: RGB--- **Dimensions**:- - Single column: 85 mm (3.35 inches)- - Double column: 178 mm (7.01 inches)- - Maximum height: 230 mm (9.06 inches)--- **Fonts**:- - Arial or Helvetica only- - 8-12 pt for axis labels- - 6-8 pt for tick labels--### Cell Press Specific Guidelines-- Panel labels: (A), (B), (C) or A, B, C in top-left-- Related panels should match in size-- Scale bars mandatory for microscopy-- Western blots: Include molecular weight markers-- Arrows/arrowheads: 2 pt minimum width-- Line widths: 1-2 pt for data--## PLOS (Public Library of Science)--### Technical Specifications-- **File formats**:- - Vector: EPS, PDF (preferred)- - Raster: TIFF, PNG- - TIFF with LZW compression acceptable--- **Resolution**:- - Minimum 300 DPI at final size (all figure types)- - 600 DPI preferred for line art--- **Color space**: RGB--- **Dimensions**:- - Single column: 8.3 cm (3.27 inches)- - 1.5 column: 11.4 cm (4.49 inches)- - Double column: 17.3 cm (6.81 inches)- - Maximum height: 23.3 cm (9.17 inches)--- **Fonts**:- - Sans-serif preferred (Arial, Helvetica)- - 8-12 pt for labels at final size--### PLOS Specific Guidelines-- Figures should be understandable without caption-- Color required only if adding information-- All figures convertible to grayscale-- Panel labels optional but recommended-- Open access: Figures must be CC-BY licensed-- Source data files encouraged--## ACS (American Chemical Society)--### Technical Specifications-- **File formats**:- - Preferred: TIFF, PDF, EPS- - Application files: AI, CDX (ChemDraw), CDL- - Acceptable: PNG (not for publication)--- **Resolution**:- - Minimum 300 DPI at final size- - 600 DPI for line art and chemical structures- - 1200 DPI for detailed structures--- **Color space**: RGB or CMYK (check specific journal)--- **Dimensions**:- - Single column: 3.25 inches (8.25 cm)- - Double column: 7 inches (17.78 cm)--- **Fonts**:- - Embedded fonts required- - Consistent sizing across figures--### ACS Specific Guidelines-- Chemical structures: Use ChemDraw or equivalent-- Atom labels: 10-12 pt-- Bond thickness: 2 pt-- Panel labels: Lowercase bold (a, b, c)-- High contrast required (many ACS journals grayscale print)--## Elsevier Journals (varies by journal)--### Technical Specifications-- **File formats**:- - Vector: EPS, PDF- - Raster: TIFF, JPEG (only for photographs)--- **Resolution**:- - Line art: 1000 DPI minimum- - Photographs: 300 DPI minimum- - Combination: 600 DPI minimum--- **Color space**: RGB (for online); CMYK (for print journals)--- **Dimensions**: Vary by journal- - Common single column: 90 mm- - Common double column: 190 mm--- **Fonts**:- - Preferred: Arial, Times, Symbol- - Minimum 6 pt at final size--### Elsevier Specific Guidelines-- Check individual journal guidelines (highly variable)-- Some journals charge for color in print-- Panel labels typically (A), (B), (C) or A, B, C-- Graphical abstract often required (separate from figures)--## IEEE (Engineering/Computer Science)--### Technical Specifications-- **File formats**:- - Vector: PDF, EPS (preferred)- - Raster: TIFF, PNG--- **Resolution**:- - Photographs/graphics: 300 DPI minimum at final size- - Line art: 600 DPI minimum--- **Color space**: RGB (online); CMYK (print)--- **Dimensions**:- - Single column: 3.5 inches (8.9 cm)- - Double column: 7.16 inches (18.2 cm)--- **Fonts**:- - Sans-serif preferred- - Minimum 8-10 pt at final size--### IEEE Specific Guidelines-- Figures should be readable in black and white-- Color figures incur no charge (online publication)-- Panel labels: (a), (b), (c) in lowercase-- Captions below figures (not on separate page)-- Use IEEE graphics checker tool before submission--## BMC (BioMed Central) - Open Access--### Technical Specifications-- **File formats**:- - Any standard format accepted- - Preferred: TIFF, PDF, EPS, PNG--- **Resolution**:- - Minimum 600 DPI for line art- - Minimum 300 DPI for photographs--- **Color space**: RGB--- **Dimensions**:- - Flexible, but consider readability- - Maximum width typically 140 mm--- **Fonts**:- - Embedded and readable--### BMC Specific Guidelines-- Open access: CC-BY license required-- Figure files uploaded separately-- Panel labels as appropriate for field-- Source data encouraged-- Accessibility important (colorblind-friendly)--## Common Requirements Across Journals--### Universal Best Practices-1. **Never use JPEG for graphs/plots**: Compression artifacts-2. **Embed all fonts**: In PDF/EPS files-3. **Layer structure**: Flatten images (merge layers in Photoshop)-4. **RGB vs CMYK**: Most journals now RGB (digital-first)-5. **High resolution**: Always better to start high, reduce if needed-6. **Consistency**: Same style across all figures in manuscript-7. **File size**: Balance quality with reasonable file sizes (typically <10 MB per figure)--### Submitting Figures-- **Initial submission**: Lower resolution often acceptable (for review)-- **Revision/acceptance**: High-resolution required-- **Separate files**: Each figure as separate file-- **File naming**: Clear, systematic naming-- **Supporting information**: May have different requirements--## Quick Reference Table--| Publisher | Single Column | Double Column | Min DPI (photos) | Min DPI (line art) | Preferred Format |-|-----------|---------------|---------------|------------------|-------------------|------------------|-| Nature | 89 mm | 183 mm | 300 | 1000 | EPS, PDF |-| Science | 5.5 cm | 17.5 cm | 300 | 1000 | EPS, PDF |-| Cell Press | 85 mm | 178 mm | 300 | 1000 | EPS, PDF |-| PLOS | 8.3 cm | 17.3 cm | 300 | 600 | EPS, TIFF |-| ACS | 3.25 in | 7 in | 300 | 600 | TIFF, EPS |--## Checking Requirements--### Before Submission Checklist-1. Read journal's author guidelines (figure section)-2. Check file format requirements-3. Verify resolution requirements-4. Confirm size specifications (width × height)-5. Check font requirements-6. Verify color space (RGB vs CMYK)-7. Check panel labeling style-8. Review supplementary materials requirements-9. Confirm file naming conventions-10. Check file size limits--### Useful Tools-- **ImageJ/Fiji**: Check/adjust DPI-- **Adobe Acrobat**: Verify embedded fonts, check PDF properties-- **GIMP**: Free alternative to Photoshop for raster editing-- **Inkscape**: Free vector graphics editor--## Resources--- **Journal websites**: Always check "Author Guidelines" or "Instructions for Authors"-- **Publisher resources**: Many provide templates and tools-- **Format conversion**: Use reputable tools; check output quality-- **Help desks**: Contact journal staff if unclear--## Notes--- Requirements change periodically - always verify current guidelines-- Preprint servers (bioRxiv, arXiv) often have different requirements-- Conference proceedings may have separate requirements-- Some journals offer figure preparation services (often paid)-- Supplementary figures may have relaxed requirements compared to main text figures+# Publisher and Journal Figure Snapshots++Accessed 2026-07-23 from current official pages. Requirements are date-sensitive and often depend on the journal, article type, figure type, and submission phase. Verify the live target-journal page before submission. Source IDs resolve in `sources.md`.++The machine-readable subset in `assets/publisher_profiles.json` is for planning and deterministic screening only. `scripts/export_plan.py` never claims compliance.++## Nature (flagship journal)++**Scope:** `Nature` final submission after acceptance in principle, not all Nature Portfolio journals [NATURE-FINAL] [NATURE-FIG].++- Standard widths: 89 mm single column, 183 mm double column; 120-136 mm is possible for one-and-a-half columns.+- Full page depth: 247 mm.+- Panels: lowercase bold upright `a`, `b`, `c`, 8 pt.+- Other text: 5-7 pt at final size; Helvetica or Arial preferred.+- Keep text and line art editable; do not outline or rasterize them.+- Preferred line/graph containers include AI, PostScript, vector EPS, and PDF.+- Preferred raster source: layered PSD or TIFF. The final-submission page states 300-600 dpi for photographs; minimum 300 dpi at maximum use size.+- The newer research-figure guide recommends export images at 450 dpi or above because online proofs top out at 450 dpi. This is a recommendation layered on the final-submission minimum, not a universal 450 dpi rule.+- RGB is recommended; final print conversion may use CMYK.+- Type 42 fonts are requested. The figure guide explicitly gives `matplotlib.rcParams["pdf.fonttype"] = 42`.+- High-quality JPEG can be accepted when it is the only option for a photograph. Therefore, “Nature never accepts JPEG” is false.+- Extended Data has different rules: RGB, maximum 300 ppi, maximum 10 MB, and JPEG preferred with TIFF/EPS alternatives.++Do not apply these flagship rules automatically to Nature Communications, Scientific Reports, npj journals, or Nature Reviews; each has its own page.++## Science (AAAS flagship journal)++**Scope:** `Science`, with separate initial and revised-manuscript stages [SCIENCE-INITIAL] [SCIENCE-REVISED].++### Initial submission++- A single manuscript file with embedded figures is preferred.+- Figures should be 300 dpi for review.+- Printed widths are usually 5.7 cm (one column), 12.1 cm (two columns), or 18.4 cm (three columns).+- Vector creation is preferred.+- Use sans serif, preferably Helvetica. Lettering should be about 7 pt after reduction and no smaller than 5 pt.+- Avoid red/green combinations and similar hues as sole identifiers; add shape/texture where needed.+- Scales should not extend beyond the plotted data merely as empty range.++### Revised manuscript++- Upload each figure separately.+- Preferred vector formats: PDF, EPS, or AI.+- Raster illustrations/diagrams and photographs/microscopy: TIFF.+- Vector/raster combinations: PDF or EPS.+- Line art without a vector original: at least 300 dpi at final size, preferably higher.+- Color and grayscale images: at least 300 dpi at final size.+- Upsampling is not permitted.+- PowerPoint and figures embedded in Word are not accepted at this stage.++The current official page does **not** state the older blanket “1,000 dpi line art / 600 dpi combination” numbers that this skill previously claimed.++Science Advances and other AAAS journals publish separate figure guides; do not reuse the flagship profile without checking.++## Cell Press++**Scope:** general Cell Press figure page; exceptions are explicitly listed for STAR Protocols and `Cell` Leading Edge [CELL-FIG].++### Initial submission and review++- Cell Press accepts a wide range of formats, sizes, and resolutions.+- Figures may be embedded or uploaded separately.+- Individual 1-2 MB files are recommended for reviewer convenience.++### Final production++- Upload each main figure as one separate file containing all its panels; keep titles/legends in the manuscript.+- Recommended overall maximum: 16.5 × 20 cm. This is framed as a recommendation.+- Two-column article widths: 8.5 cm, 11.4 cm, and 17.4 cm.+- Three-column formats: 5.5 cm, 11.4 cm, and 17.4 cm.+- Maximum individual file size: 20 MB.+- TIFF and PDF are preferred for most journals/types. EPS, JPEG, and CDX are accepted. Special cases differ.+- Color/grayscale: at least 300 dpi; black-and-white: at least 500 dpi; line art: at least 1,000 dpi at final size.+- RGB, Arial, capital panel letters, 6-8 pt text, and 0.5-1.5 pt strokes.+- Embed fonts. General production guidance says flatten layers, except specified `Cell` Leading Edge material.+- Do not use red and green together as the only distinction.++Cell Press requires minimal image processing, original unprocessed data on request, and disclosure of processing/stitching. Its current policy prohibits generative AI/AI-assisted alteration of research/data images, including brightness, contrast, or color-balance adjustment performed by such tools.++## PLOS research journals++**Scope:** current PLOS Computational Biology page, consistent with the sampled PLOS research-journal figure pages [PLOS-FIG]. Verify the selected PLOS journal.++- Formatting requirements are waived until provisional Editorial Accept.+- Final figure format: TIFF or EPS.+- Width: 789-2250 px at 300 dpi, equivalent to 6.68-19.05 cm.+- Text-column alignment recommendation: no wider than 13.2 cm.+- Maximum height: 2625 px at 300 dpi, equivalent to 22.23 cm.+- Resolution: 300-600 dpi at final dimensions. The page warns that above 600 may trigger resizing and below 300 will degrade output.+- Maximum file size: less than 10 MB.+- Text: Arial, Times, or Symbol, 8-12 pt.+- Color mode: RGB 8-bit/channel or grayscale.+- Put all panels of one figure in one single-page file.+- Captions remain in the manuscript; filenames are `Fig1.tif`, `Fig2.eps`, and so on.+- Do not increase pixel count and present that as improved resolution.++For manuscripts submitted on or after 2026-04-01, original uncropped, minimally adjusted blot/gel images must be supplied before acceptance. Adjustments must not alter scientific information and must be applied consistently.++## Elsevier++**Scope:** publisher-general artwork instructions. Elsevier explicitly says journal-specific Guides for Authors can override them [ELSEVIER-FORMAT] [ELSEVIER-SIZE].++- Recommended containers: TIFF for halftones/bitmaps, EPS for vector-based images (including embedded images), and PDF for vector/text material.+- JPEG and Microsoft Office files are accepted in the general checklist; use the journal page to decide suitability.+- RGB is preferred unless the journal says otherwise.+- General target widths: 90 mm single, 140 mm one-and-a-half, 190 mm full; 30 mm is the listed minimal size.+- General raster targets at final size: 300 dpi halftone, 500 dpi combination, 1,000 dpi line art.+- General lettering rule of thumb: 7 pt normal text, not smaller than 6 pt for sub/superscripts.++These are publisher defaults, not universal Elsevier-journal hard limits.++## IEEE journals++**Scope:** IEEE Author Center journal graphics page, modified 2025-02-25 [IEEE-SIZE].++- Acceptable vector formats listed on the page: PS, EPS, PDF.+- Non-vector color/grayscale graphics: **greater than** 300 dpi.+- Black-and-white line art: **greater than** 600 dpi.+- One-column width: 3.5 in / 88.9 mm.+- Two-column width: 7.16 in / 182 mm.+- IEEE warns that increasing resolution after creation does not improve quality.++Conference and magazine instructions can differ; use their separate Author Center pages.++## BMC++**Scope:** BMC Bioinformatics, used as a current BMC-journal example rather than a guaranteed BMC-wide profile [BMC-BIOINFO].++- Web widths: 600 px standard, 1200 px high resolution.+- PDF widths: 85 mm half page, 170 mm full page.+- Maximum figure-plus-legend height: 225 mm.+- Approximately 300 dpi at final size.+- Fonts embedded; lines wider than 0.25 pt at final width.+- Accepted formats include EPS, PDF, Word, PowerPoint, TIFF, JPEG, PNG, BMP, and CDX; JPEG is described as less suitable for graphical images.+- One composite file per multi-panel figure; individual file maximum 10 MB.++BMC journals are migrating onto Springer Nature Link and may publish updated journal-specific instructions. Verify the selected journal rather than assuming this profile.++## ACS Publications++**Scope:** the current general “Preparing Manuscript Graphics” page. It does not provide a complete modern universal digital-export profile [ACS-GRAPHICS].++- Listed maximum dimensions: 3.25 in single column, 7 in double column, 9.5 in length.+- Lettering should be no smaller than 5 pt after reduction; Helvetica/Arial are suggested.+- Lines should be no thinner than 1 pt on that general page.+- The page mentions using the best available resolution and 600+ dpi printing, but it does not establish a universal per-figure digital file-format/DPI rule for all ACS journals.++Use the selected ACS journal’s current Author Guidelines for formats, color, resolution, and TOC/abstract graphics. The planner intentionally does not invent missing ACS-wide requirements.++## Rules that are not universal++Do not present these as cross-publisher laws:++- “JPEG is never accepted.” Several publishers accept it for photographs or specific workflows.+- “All line art must be 1,000/1,200 dpi.” Vector output is often preferred, and current Science revised guidance says at least 300 dpi when vector is unavailable.+- “All publishers require grayscale compatibility.” Accessibility guidance varies; use redundant encoding regardless.+- “All figures must be RGB.” RGB is often preferred, but Nature accepts RGB or CMYK for final print artwork and target-journal rules can differ.+- “All figures need error bars/significance stars.” The right display depends on the estimand, data, and analysis.+- “A matching DPI/width means compliant.” Technical metadata is only one part of submission review.++## Submission-stage workflow++1. Identify exact journal, article type, figure type, and current stage.+2. Save the live official page URL and access date.+3. Create a plan with `scripts/export_plan.py`.+4. Export with explicit dimensions and settings.+5. Inspect the delivered file with `scripts/image_metadata.py`.+6. Review fonts, embedded rasters, image integrity, accessibility, caption, and source data manually.+7. Re-check official instructions immediately before upload.diff --git a/skills/scientific-visualization/references/matplotlib_examples.md b/skills/scientific-visualization/references/matplotlib_examples.mdindex 637cdd23..a752a880 100644--- a/skills/scientific-visualization/references/matplotlib_examples.md+++ b/skills/scientific-visualization/references/matplotlib_examples.md@@ -1,620 +1,336 @@-# Publication-Ready Matplotlib Examples+# Current Matplotlib, Seaborn, and Plotly Patterns-## Overview+Verified 2026-07-23 against Matplotlib 3.11.1, Seaborn 0.13.2, Plotly 6.9.0, Kaleido 1.3.0, Pillow 12.3.0, and pypdf 6.14.2. Source IDs resolve in `sources.md`.-This reference provides practical code examples for creating publication-ready scientific figures using Matplotlib, Seaborn, and Plotly. All examples follow best practices from `publication_guidelines.md` and use colorblind-friendly palettes from `color_palettes.md`.+Run examples from the skill directory with pinned direct dependencies:-## Setup and Configuration+```bash+uv run --isolated --no-project --python 3.13 \+ --with "matplotlib==3.11.1" \+ --with "seaborn==0.13.2" \+ --with "plotly==6.9.0" \+ --with "kaleido==1.3.0" \+ --with "pillow==12.3.0" \+ --with "pypdf==6.14.2" \+ python your_figure.py+```-### Publication-Quality Matplotlib Configuration+These pins are a dated direct-dependency snapshot, not a lock of all transitive artifacts. Keep a project lock when exact environment replay is required.-```python-import matplotlib.pyplot as plt-import matplotlib as mpl-import numpy as np+## Rendering and hardcopy backends-# Set publication quality parameters-mpl.rcParams['figure.dpi'] = 300-mpl.rcParams['savefig.dpi'] = 300-mpl.rcParams['font.size'] = 8-mpl.rcParams['font.family'] = 'sans-serif'-mpl.rcParams['font.sans-serif'] = ['Arial', 'Helvetica']-mpl.rcParams['axes.labelsize'] = 9-mpl.rcParams['axes.titlesize'] = 9-mpl.rcParams['xtick.labelsize'] = 7-mpl.rcParams['ytick.labelsize'] = 7-mpl.rcParams['legend.fontsize'] = 7-mpl.rcParams['axes.linewidth'] = 0.5-mpl.rcParams['xtick.major.width'] = 0.5-mpl.rcParams['ytick.major.width'] = 0.5-mpl.rcParams['lines.linewidth'] = 1.5--# Use colorblind-friendly colors (Okabe-Ito palette)-okabe_ito = ['#E69F00', '#56B4E9', '#009E73', '#F0E442',- '#0072B2', '#D55E00', '#CC79A7', '#000000']-mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=okabe_ito)--# Use perceptually uniform colormap-mpl.rcParams['image.cmap'] = 'viridis'-```+Matplotlib separates interactive display backends from hardcopy renderers. The current built-ins include PDF (`pdf`), PS (`ps`, `eps`), SVG (`svg`), PGF (`pgf`, `pdf` through TeX), and optional Cairo (`png`, `ps`, `pdf`, `svg`); Agg is the common raster renderer [MPL-BACKENDS]. JPEG, TIFF, and WebP saving uses Pillow through the raster path.-### Helper Function for Saving+Available output depends on the active backend/build:```python-def save_publication_figure(fig, filename, formats=['pdf', 'png'], dpi=300):- """- Save figure in multiple formats for publication.-- Parameters:- ------------ fig : matplotlib.figure.Figure- Figure to save- filename : str- Base filename (without extension)- formats : list- List of file formats to save ['pdf', 'png', 'eps', 'svg']- dpi : int- Resolution for raster formats- """- for fmt in formats:- output_file = f"{filename}.{fmt}"- fig.savefig(output_file, dpi=dpi, bbox_inches='tight',- facecolor='white', edgecolor='none',- transparent=False, format=fmt)- print(f"Saved: {output_file}")+supported = fig.canvas.get_supported_filetypes()+print(supported)```-## Example 1: Line Plot with Error Bars+`Figure.savefig(..., backend="cairo")` or `backend="pgf"` can select another renderer, but Matplotlib documents the default as normally sufficient [MPL-SAVE]. PGF requires a working TeX setup; Cairo requires pycairo or cairocffi. Inspect output because a vector container can still contain rasterized artists.++## Scoped style and exact dimensions++Prefer temporary style contexts to global state:```pythonimport matplotlib.pyplot as plt-import numpy as np-# Generate sample data-x = np.linspace(0, 10, 50)-y1 = 2 * x + 1 + np.random.normal(0, 1, 50)-y2 = 1.5 * x + 2 + np.random.normal(0, 1.2, 50)+from style_presets import style_context-# Calculate means and standard errors for binned data-bins = np.linspace(0, 10, 11)-y1_mean = [y1[(x >= bins[i]) & (x < bins[i+1])].mean() for i in range(len(bins)-1)]-y1_sem = [y1[(x >= bins[i]) & (x < bins[i+1])].std() /- np.sqrt(len(y1[(x >= bins[i]) & (x < bins[i+1])]))- for i in range(len(bins)-1)]-x_binned = (bins[:-1] + bins[1:]) / 2+with style_context("default", palette_name="okabe_ito_on_white"):+ fig, ax = plt.subplots(+ figsize=(89 / 25.4, 60 / 25.4),+ layout="constrained",+ )+ ax.plot([0, 1, 2], [1, 3, 2], marker="o", label="Observed")+ ax.set(xlabel="Time (hours)", ylabel="Response (unit)")+ ax.legend()+```-# Create figure with appropriate size (single column width = 3.5 inches)-fig, ax = plt.subplots(figsize=(3.5, 2.5))+`layout="constrained"` handles labels, legends, nested layouts, and colorbars more flexibly than `tight_layout`; calling `tight_layout()` turns constrained layout off [MPL-LAYOUT].-# Plot with error bars-ax.errorbar(x_binned, y1_mean, yerr=y1_sem,- marker='o', markersize=4, capsize=3, capthick=0.5,- label='Condition A', linewidth=1.5)+If exact page dimensions matter, do not export with `bbox_inches="tight"`; it recalculates the bounding box and changes the physical output size [MPL-SAVE].-# Add labels with units-ax.set_xlabel('Time (hours)')-ax.set_ylabel('Fluorescence intensity (a.u.)')+You can also use the bundled parseable style:-# Add legend-ax.legend(frameon=False, loc='upper left')+```python+from pathlib import Path+import matplotlib.pyplot as plt-# Remove top and right spines-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)+skill_root = Path("skills/scientific-visualization")+with plt.style.context(skill_root / "assets" / "publication.mplstyle"):+ fig, ax = plt.subplots(layout="constrained")+```-# Tight layout-fig.tight_layout()+## Preserve raw observations and define uncertainty-# Save-save_publication_figure(fig, 'line_plot_with_errors')-plt.show()+```python+import numpy as np+import matplotlib.pyplot as plt++rng = np.random.default_rng(20260723)+groups = {+ "Control": rng.normal(0.0, 1.0, 24),+ "Treatment": rng.normal(0.7, 1.1, 24),+}++fig, ax = plt.subplots(figsize=(3.5, 2.8), layout="constrained")+for position, (label, values) in enumerate(groups.items()):+ jitter = rng.uniform(-0.08, 0.08, len(values))+ ax.scatter(+ position + jitter,+ values,+ alpha=0.65,+ label=label,+ )+ mean = values.mean()+ sem = values.std(ddof=1) / np.sqrt(len(values))+ ax.errorbar(position, mean, yerr=sem, color="black", capsize=3)++ax.set(+ xticks=range(len(groups)),+ xticklabels=list(groups),+ ylabel="Response (unit)",+)```-## Example 2: Multi-Panel Figure+Caption the error bars as mean ± one SEM and state `n=24` independent observations per group. If independence is false, use an analysis and interval that respects the design.++## Missing data and no silent interpolation```python-import matplotlib.pyplot as pltimport numpy as np-from string import ascii_uppercase--# Create figure with multiple panels (double column width = 7 inches)-fig = plt.figure(figsize=(7, 4))--# Define grid for panels-gs = fig.add_gridspec(2, 3, hspace=0.4, wspace=0.4,- left=0.08, right=0.98, top=0.95, bottom=0.08)--# Panel A: Line plot-ax_a = fig.add_subplot(gs[0, :2])-x = np.linspace(0, 10, 100)-for i, offset in enumerate([0, 0.5, 1.0]):- ax_a.plot(x, np.sin(x) + offset, label=f'Dataset {i+1}')-ax_a.set_xlabel('Time (s)')-ax_a.set_ylabel('Amplitude (V)')-ax_a.legend(frameon=False, fontsize=6)-ax_a.spines['top'].set_visible(False)-ax_a.spines['right'].set_visible(False)--# Panel B: Bar plot-ax_b = fig.add_subplot(gs[0, 2])-categories = ['Control', 'Treatment\nA', 'Treatment\nB']-values = [100, 125, 140]-errors = [5, 8, 6]-ax_b.bar(categories, values, yerr=errors, capsize=3,- color=['#0072B2', '#E69F00', '#009E73'], alpha=0.8)-ax_b.set_ylabel('Response (%)')-ax_b.spines['top'].set_visible(False)-ax_b.spines['right'].set_visible(False)-ax_b.set_ylim(0, 160)--# Panel C: Scatter plot-ax_c = fig.add_subplot(gs[1, 0])-x = np.random.randn(100)-y = 2*x + np.random.randn(100)-ax_c.scatter(x, y, s=10, alpha=0.6, color='#0072B2')-ax_c.set_xlabel('Variable X')-ax_c.set_ylabel('Variable Y')-ax_c.spines['top'].set_visible(False)-ax_c.spines['right'].set_visible(False)--# Panel D: Heatmap-ax_d = fig.add_subplot(gs[1, 1:])-data = np.random.randn(10, 20)-im = ax_d.imshow(data, cmap='viridis', aspect='auto')-ax_d.set_xlabel('Sample number')-ax_d.set_ylabel('Feature')-cbar = plt.colorbar(im, ax=ax_d, fraction=0.046, pad=0.04)-cbar.set_label('Intensity (a.u.)', rotation=270, labelpad=12)--# Add panel labels-panels = [ax_a, ax_b, ax_c, ax_d]-for i, ax in enumerate(panels):- ax.text(-0.15, 1.05, ascii_uppercase[i], transform=ax.transAxes,- fontsize=10, fontweight='bold', va='top')--save_publication_figure(fig, 'multi_panel_figure')-plt.show()++time = np.arange(8)+signal = np.array([1.0, 1.4, np.nan, np.nan, 2.1, 2.0, 2.4, 2.7])++fig, ax = plt.subplots(layout="constrained")+ax.plot(time, signal, marker="o", label="Observed") # gaps remain gaps+ax.scatter([2, 3], [0.9, 0.9], marker="x", color="0.35", label="Missing")+ax.set(xlabel="Time (days)", ylabel="Signal (unit)")+ax.legend()```-## Example 3: Box Plot with Individual Points+If a model estimates the missing interval, plot the model separately with its uncertainty and identify it as modeled, not observed.++## Log axes and explicit nonpositive policy```python-import matplotlib.pyplot as pltimport numpy as np+import matplotlib.pyplot as plt++concentration = np.array([0.1, 1.0, 10.0, 100.0])+response = np.array([0.4, 0.9, 2.1, 4.3])-# Generate sample data-np.random.seed(42)-data = [np.random.normal(100, 15, 30),- np.random.normal(120, 20, 30),- np.random.normal(140, 18, 30),- np.random.normal(110, 22, 30)]--fig, ax = plt.subplots(figsize=(3.5, 3))--# Create box plot-bp = ax.boxplot(data, widths=0.5, patch_artist=True,- showfliers=False, # We'll add points manually- boxprops=dict(facecolor='lightgray', edgecolor='black', linewidth=0.8),- medianprops=dict(color='black', linewidth=1.5),- whiskerprops=dict(linewidth=0.8),- capprops=dict(linewidth=0.8))--# Overlay individual points-colors = ['#0072B2', '#E69F00', '#009E73', '#D55E00']-for i, (d, color) in enumerate(zip(data, colors)):- # Add jitter to x positions- x = np.random.normal(i+1, 0.04, size=len(d))- ax.scatter(x, d, alpha=0.4, s=8, color=color)--# Customize-ax.set_xticklabels(['Control', 'Treatment A', 'Treatment B', 'Treatment C'])-ax.set_ylabel('Cell count')-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)-ax.set_ylim(50, 200)--fig.tight_layout()-save_publication_figure(fig, 'boxplot_with_points')-plt.show()+fig, ax = plt.subplots(layout="constrained")+ax.plot(concentration, response, marker="o")+ax.set_xscale("log", base=10)+ax.set(xlabel="Concentration (µM; log10 axis)", ylabel="Response (unit)")```-## Example 4: Heatmap with Colorbar+Do not silently omit zeros or negatives. State the measurement-domain rule or use another representation.++## Centered heatmap and missing-value color```python+import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np-# Generate correlation matrix-np.random.seed(42)-n = 10-A = np.random.randn(n, n)-corr_matrix = np.corrcoef(A)--# Create figure-fig, ax = plt.subplots(figsize=(4, 3.5))--# Plot heatmap-im = ax.imshow(corr_matrix, cmap='RdBu_r', vmin=-1, vmax=1, aspect='auto')--# Add colorbar-cbar = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)-cbar.set_label('Correlation coefficient', rotation=270, labelpad=15)--# Set ticks and labels-gene_names = [f'Gene{i+1}' for i in range(n)]-ax.set_xticks(np.arange(n))-ax.set_yticks(np.arange(n))-ax.set_xticklabels(gene_names, rotation=45, ha='right')-ax.set_yticklabels(gene_names)--# Add grid-ax.set_xticks(np.arange(n)-.5, minor=True)-ax.set_yticks(np.arange(n)-.5, minor=True)-ax.grid(which='minor', color='white', linestyle='-', linewidth=0.5)--fig.tight_layout()-save_publication_figure(fig, 'correlation_heatmap')-plt.show()+values = np.array([+ [-2.0, -0.5, 0.1],+ [-1.1, np.nan, 1.8],+ [-0.2, 0.7, 3.0],+])+norm = mpl.colors.TwoSlopeNorm(vmin=-2, vcenter=0, vmax=3)+cmap = mpl.colormaps["RdBu_r"].with_extremes(bad="#777777")++fig, ax = plt.subplots(layout="constrained")+image = ax.imshow(values, norm=norm, cmap=cmap, interpolation="nearest")+colorbar = fig.colorbar(image, ax=ax)+colorbar.set_label("Change from baseline (unit)")+ax.set(xlabel="Sample", ylabel="Feature")```-## Example 5: Seaborn Violin Plot+`TwoSlopeNorm` gives each side of the center a different linear mapping. Use `CenteredNorm` when symmetric treatment around a center is appropriate, `LogNorm` for strictly positive orders of magnitude, and `BoundaryNorm` for declared classes [MPL-NORM].++## Multi-panel layout```pythonimport matplotlib.pyplot as plt-import seaborn as sns-import pandas as pd-import numpy as np-# Generate sample data-np.random.seed(42)-data = pd.DataFrame({- 'condition': np.repeat(['Control', 'Drug A', 'Drug B'], 50),- 'value': np.concatenate([- np.random.normal(100, 15, 50),- np.random.normal(120, 20, 50),- np.random.normal(140, 18, 50)- ])-})--# Set style-sns.set_style('ticks')-sns.set_palette(['#0072B2', '#E69F00', '#009E73'])--fig, ax = plt.subplots(figsize=(3.5, 3))--# Create violin plot-sns.violinplot(data=data, x='condition', y='value', ax=ax,- inner='box', linewidth=0.8)--# Add strip plot-sns.stripplot(data=data, x='condition', y='value', ax=ax,- size=2, alpha=0.3, color='black')--# Customize-ax.set_xlabel('')-ax.set_ylabel('Expression level (AU)')-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)--fig.tight_layout()-save_publication_figure(fig, 'violin_plot')-plt.show()+fig = plt.figure(figsize=(7.0, 4.0), layout="constrained")+subfigures = fig.subfigures(1, 2, width_ratios=[2, 1])+left_axes = subfigures[0].subplots(2, 1, sharex=True)+right_ax = subfigures[1].subplots()++for label, ax in zip("ABC", [*left_axes, right_ax]):+ ax.text(+ -0.12,+ 1.05,+ label,+ transform=ax.transAxes,+ fontweight="bold",+ va="top",+ )```-## Example 6: Scientific Scatter with Regression+`GridSpec`, subgrids, `subplot_mosaic`, and subfigures all work with constrained layout [MPL-LAYOUT] [MPL-GRIDSPEC].++## Selective rasterization in vector output++Dense point clouds can make PDF/SVG huge. Rasterize only the dense artist:```python-import matplotlib.pyplot as plt-import numpy as np-from scipy import stats--# Generate data with correlation-np.random.seed(42)-x = np.random.randn(100)-y = 2.5 * x + np.random.randn(100) * 0.8--# Calculate regression-slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)--# Create figure-fig, ax = plt.subplots(figsize=(3.5, 3.5))--# Scatter plot-ax.scatter(x, y, s=15, alpha=0.6, color='#0072B2', edgecolors='none')--# Regression line-x_line = np.array([x.min(), x.max()])-y_line = slope * x_line + intercept-ax.plot(x_line, y_line, 'r-', linewidth=1.5, label=f'y = {slope:.2f}x + {intercept:.2f}')--# Add statistics text-stats_text = f'$R^2$ = {r_value**2:.3f}\n$p$ < 0.001' if p_value < 0.001 else f'$R^2$ = {r_value**2:.3f}\n$p$ = {p_value:.3f}'-ax.text(0.05, 0.95, stats_text, transform=ax.transAxes,- verticalalignment='top', fontsize=7,- bbox=dict(boxstyle='round', facecolor='white', alpha=0.8, edgecolor='gray', linewidth=0.5))--# Customize-ax.set_xlabel('Predictor variable')-ax.set_ylabel('Response variable')-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)--fig.tight_layout()-save_publication_figure(fig, 'scatter_regression')-plt.show()+fig, ax = plt.subplots(layout="constrained")+ax.scatter(x, y, s=2, alpha=0.25, rasterized=True)+ax.set(xlabel="Predictor (unit)", ylabel="Outcome (unit)")++from figure_export import export_figure++report = export_figure(+ fig,+ "outputs/figure1",+ formats=["pdf", "png"],+ dpi=600, # controls PNG and rasterized artists embedded in PDF+ provenance={+ "raw_data": "data/observations.csv",+ "transformations": ["rows filtered by predeclared QC flag"],+ "uncertainty": "none displayed",+ "missing_data": "retained as gaps",+ },+ write_manifest=True,+)```-## Example 7: Time Series with Shaded Error+The exporter:++- refuses implicit overwrite;+- preserves page dimensions by default;+- passes DPI to vector backends for embedded raster artists;+- writes TIFF with LZW compression;+- can keep TrueType text editable in PDF/PS;+- can write an explicit provenance manifest.++It does not inspect data truth or certify submission compliance.++## Raster image export and inspection```python-import matplotlib.pyplot as plt-import numpy as np+report = export_figure(+ fig,+ "outputs/microscopy_panel",+ formats=["tiff"],+ dpi=300,+ facecolor="white",+ overwrite=False,+)+```-# Generate time series data-np.random.seed(42)-time = np.linspace(0, 24, 100)-n_replicates = 5--# Simulate multiple replicates-data = np.array([10 * np.exp(-time/10) + np.random.normal(0, 0.5, 100)- for _ in range(n_replicates)])--# Calculate mean and SEM-mean = data.mean(axis=0)-sem = data.std(axis=0) / np.sqrt(n_replicates)--# Create figure-fig, ax = plt.subplots(figsize=(4, 2.5))--# Plot mean line-ax.plot(time, mean, linewidth=1.5, color='#0072B2', label='Mean ± SEM')--# Add shaded error region-ax.fill_between(time, mean - sem, mean + sem,- alpha=0.3, color='#0072B2', linewidth=0)--# Customize-ax.set_xlabel('Time (hours)')-ax.set_ylabel('Concentration (μM)')-ax.legend(frameon=False, loc='upper right')-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)-ax.set_xlim(0, 24)-ax.set_ylim(0, 12)--fig.tight_layout()-save_publication_figure(fig, 'timeseries_shaded')-plt.show()+Then inspect:++```bash+uv run --isolated --no-project --python 3.13 \+ --with "pillow==12.3.0" \+ python scripts/image_metadata.py outputs/microscopy_panel.tiff \+ --format tiff --mode RGB --min-dpi 300 --target-width-mm 85 \+ --alpha-policy forbid```-## Example 8: Plotly Interactive Figure+Effective DPI is pixel width divided by final width in inches. Changing only the TIFF DPI tag does not create detail.++## Seaborn 0.13.2++Seaborn remains built on Matplotlib. Use axes-level functions for custom multi-panel layouts and figure-level functions for automatic faceting [SEABORN-FAQ].```python-import plotly.graph_objects as go-import numpy as np+import seaborn as sns+import matplotlib.pyplot as plt-# Generate data-np.random.seed(42)-x = np.random.randn(100)-y = 2*x + np.random.randn(100)-colors = np.random.choice(['Group A', 'Group B'], 100)--# Okabe-Ito colors for Plotly-okabe_ito_plotly = ['#E69F00', '#56B4E9']--# Create figure-fig = go.Figure()--for group, color in zip(['Group A', 'Group B'], okabe_ito_plotly):- mask = colors == group- fig.add_trace(go.Scatter(- x=x[mask], y=y[mask],- mode='markers',- name=group,- marker=dict(size=6, color=color, opacity=0.6)- ))--# Update layout for publication quality-fig.update_layout(- width=500,- height=400,- font=dict(family='Arial, sans-serif', size=10),- plot_bgcolor='white',- xaxis=dict(- title='Variable X',- showgrid=False,- showline=True,- linewidth=1,- linecolor='black',- mirror=False- ),- yaxis=dict(- title='Variable Y',- showgrid=False,- showline=True,- linewidth=1,- linecolor='black',- mirror=False- ),- legend=dict(- x=0.02,- y=0.98,- bgcolor='rgba(255,255,255,0.8)',- bordercolor='gray',- borderwidth=0.5+from color_palettes import OKABE_ITO_ON_WHITE+from style_presets import style_context++sns.set_theme(style="ticks", context="paper", palette=OKABE_ITO_ON_WHITE)+with style_context("default", palette_name="okabe_ito_on_white"):+ fig, ax = plt.subplots(figsize=(3.5, 2.8), layout="constrained")+ sns.lineplot(+ data=frame,+ x="time",+ y="response",+ hue="treatment",+ style="treatment",+ markers=True,+ errorbar=("ci", 95),+ n_boot=5000,+ seed=20260723,+ ax=ax,)+ ax.set(xlabel="Time (hours)", ylabel="Response (unit)")+```++Current `errorbar` choices include `"sd"`, `"se"`, `"pi"`, `"ci"`, tuples, callables, or `None`. The old `ci=` interface is not the current general API [SEABORN-ERROR].++For categorical axes whose numeric/datetime values must retain their real spacing, use supported functions with `native_scale=True`. Do not assume every categorical plot uses native coordinates by default.++## Plotly 6.9 and Kaleido 1.3++Interactive HTML:++```python+fig.write_html(+ "outputs/exploration.html",+ include_plotlyjs=True, # self-contained, larger file+ full_html=True,)+```-# Save as static image (requires kaleido)-fig.write_image('plotly_scatter.png', width=500, height=400, scale=3) # scale=3 gives ~300 DPI-fig.write_html('plotly_scatter.html') # Interactive version+Static image:-fig.show()+```python+fig.write_image(+ "outputs/figure.svg",+ width=700,+ height=450,+ scale=1,+)```-## Example 9: Grouped Bar Plot with Significance+Batch export is faster with Kaleido v1:```python-import matplotlib.pyplot as plt-import numpy as np+import plotly.io as pio-# Data-categories = ['WT', 'Mutant A', 'Mutant B']-control_means = [100, 85, 70]-control_sem = [5, 6, 5]-treatment_means = [100, 120, 140]-treatment_sem = [6, 8, 9]--x = np.arange(len(categories))-width = 0.35--fig, ax = plt.subplots(figsize=(3.5, 3))--# Create bars-bars1 = ax.bar(x - width/2, control_means, width, yerr=control_sem,- capsize=3, label='Control', color='#0072B2', alpha=0.8)-bars2 = ax.bar(x + width/2, treatment_means, width, yerr=treatment_sem,- capsize=3, label='Treatment', color='#E69F00', alpha=0.8)--# Add significance markers-def add_significance_bar(ax, x1, x2, y, h, text):- """Add significance bar between two bars"""- ax.plot([x1, x1, x2, x2], [y, y+h, y+h, y], linewidth=0.8, c='black')- ax.text((x1+x2)/2, y+h, text, ha='center', va='bottom', fontsize=7)--# Mark significant differences-add_significance_bar(ax, x[1]-width/2, x[1]+width/2, 135, 3, '***')-add_significance_bar(ax, x[2]-width/2, x[2]+width/2, 155, 3, '***')--# Customize-ax.set_ylabel('Activity (% of WT control)')-ax.set_xticks(x)-ax.set_xticklabels(categories)-ax.legend(frameon=False, loc='upper left')-ax.spines['top'].set_visible(False)-ax.spines['right'].set_visible(False)-ax.set_ylim(0, 180)--# Add note about significance-ax.text(0.98, 0.02, '*** p < 0.001', transform=ax.transAxes,- ha='right', va='bottom', fontsize=6)--fig.tight_layout()-save_publication_figure(fig, 'grouped_bar_significance')-plt.show()+pio.write_images(+ fig=[figure_a, figure_b],+ file=["outputs/a.pdf", "outputs/b.pdf"],+)```-## Example 10: Publication-Ready Figure for Nature+Current facts [PLOTLY-STATIC] [KALEIDO]:-```python-import matplotlib.pyplot as plt-import numpy as np-from string import ascii_lowercase--# Nature specifications: 89mm single column-inch_per_mm = 0.0393701-width_mm = 89-height_mm = 110-figsize = (width_mm * inch_per_mm, height_mm * inch_per_mm)--fig = plt.figure(figsize=figsize)-gs = fig.add_gridspec(3, 2, hspace=0.5, wspace=0.4,- left=0.12, right=0.95, top=0.96, bottom=0.08)--# Panel a: Time course-ax_a = fig.add_subplot(gs[0, :])-time = np.linspace(0, 48, 100)-for i, label in enumerate(['Control', 'Treatment']):- y = (1 + i*0.5) * np.exp(-time/20) * (1 + 0.3*np.sin(time/5))- ax_a.plot(time, y, linewidth=1.2, label=label)-ax_a.set_xlabel('Time (h)', fontsize=7)-ax_a.set_ylabel('Growth (OD$_{600}$)', fontsize=7)-ax_a.legend(frameon=False, fontsize=6)-ax_a.tick_params(labelsize=6)-ax_a.spines['top'].set_visible(False)-ax_a.spines['right'].set_visible(False)--# Panel b: Bar plot-ax_b = fig.add_subplot(gs[1, 0])-categories = ['A', 'B', 'C']-values = [1.0, 1.5, 2.2]-errors = [0.1, 0.15, 0.2]-ax_b.bar(categories, values, yerr=errors, capsize=2, width=0.6,- color='#0072B2', alpha=0.8)-ax_b.set_ylabel('Fold change', fontsize=7)-ax_b.tick_params(labelsize=6)-ax_b.spines['top'].set_visible(False)-ax_b.spines['right'].set_visible(False)--# Panel c: Heatmap-ax_c = fig.add_subplot(gs[1, 1])-data = np.random.randn(8, 6)-im = ax_c.imshow(data, cmap='viridis', aspect='auto')-ax_c.set_xlabel('Sample', fontsize=7)-ax_c.set_ylabel('Gene', fontsize=7)-ax_c.tick_params(labelsize=6)--# Panel d: Scatter-ax_d = fig.add_subplot(gs[2, :])-x = np.random.randn(50)-y = 2*x + np.random.randn(50)*0.5-ax_d.scatter(x, y, s=8, alpha=0.6, color='#E69F00')-ax_d.set_xlabel('Expression gene X', fontsize=7)-ax_d.set_ylabel('Expression gene Y', fontsize=7)-ax_d.tick_params(labelsize=6)-ax_d.spines['top'].set_visible(False)-ax_d.spines['right'].set_visible(False)--# Add lowercase panel labels (Nature style)-for i, ax in enumerate([ax_a, ax_b, ax_c, ax_d]):- ax.text(-0.2, 1.1, f'{ascii_lowercase[i]}', transform=ax.transAxes,- fontsize=9, fontweight='bold', va='top')--# Save in Nature-preferred format-fig.savefig('nature_figure.pdf', dpi=1000, bbox_inches='tight',- facecolor='white', edgecolor='none')-fig.savefig('nature_figure.png', dpi=300, bbox_inches='tight',- facecolor='white', edgecolor='none')--plt.show()+- Kaleido v1 requires a compatible Chrome/Chromium installation; Chrome is no longer bundled.+- Plotly `write_image` supports PNG, JPEG, WebP, SVG, and PDF.+- EPS was supported only by Kaleido versions earlier than 1.0.+- `engine=` and Orca are deprecated; do not use them in new code.+- `plotly.io.kaleido.scope` is deprecated; use `plotly.io.defaults`.+- Width/height are logical pixels and `scale` multiplies output pixels; `scale=3` is **not inherently “300 DPI.”**+- WebGL traces embed raster content inside vector exports.+- Fully offline MathJax/topojson use requires local resources; do not assume a network-independent export when a figure references external assets.++An interactive HTML file does not replace a static fallback, caption, alt text, keyboard review, or accessible data table.++## Font and transparency checks++Matplotlib 3.11.1 defaults PDF/PS to Type 3 and SVG text to paths. The bundled presets instead use PDF/PS Type 42 and leave SVG text as text [MPL-STYLE]. Verify the actual PDF:++```bash+uv run --isolated --no-project --python 3.13 \+ --with "pypdf==6.14.2" \+ python scripts/image_metadata.py outputs/figure1.pdf```-## Tips for Each Library--### Matplotlib-- Use `fig.tight_layout()` or `constrained_layout=True` to prevent overlapping-- Set DPI to 300-600 for publication-- Use vector formats (PDF, EPS) for line plots-- Embed fonts in PDF/EPS files--### Seaborn-- Built on matplotlib, so all matplotlib customizations work-- Use `sns.set_style('ticks')` or `'whitegrid'` for clean looks-- `sns.despine()` removes top and right spines-- Set custom palette with `sns.set_palette()`--### Plotly-- Great for interactive exploratory analysis-- Export static images with `fig.write_image()` (requires kaleido package)-- Use `scale` parameter to control DPI (scale=3 ≈ 300 DPI)-- Update layout extensively for publication quality--## Common Workflow--1. **Explore with default settings**-2. **Apply publication configuration** (see Setup section)-3. **Create plot with appropriate size** (check journal requirements)-4. **Customize colors** (use colorblind-friendly palettes)-5. **Adjust fonts and line widths** (readable at final size)-6. **Remove chart junk** (top/right spines, excessive grid)-7. **Add clear labels with units**-8. **Test in grayscale**-9. **Save in multiple formats** (PDF for vector, PNG for raster)-10. **Verify in final context** (import into manuscript to check size)--## Resources--- Matplotlib documentation: https://matplotlib.org/-- Seaborn gallery: https://seaborn.pydata.org/examples/index.html-- Plotly documentation: https://plotly.com/python/-- Nature Methods Points of View: Data visualization column archive+SVG text is not an embedded font; its appearance depends on the renderer’s installed fonts. If portability matters more than editable/searchable text, use paths and retain an editable source separately.++Use opaque white submission output unless transparency is explicitly required. Transparent artists blend with the destination and can change apparent contrast [MPL-SAVE].diff --git a/skills/scientific-visualization/references/publication_guidelines.md b/skills/scientific-visualization/references/publication_guidelines.mdindex d61f5911..dea3863b 100644--- a/skills/scientific-visualization/references/publication_guidelines.md+++ b/skills/scientific-visualization/references/publication_guidelines.md@@ -1,205 +1,196 @@-# Publication-Ready Figure Guidelines--## Core Principles--Scientific figures must be clear, accurate, and accessible. Publication-ready figures follow these fundamental principles:--1. **Clarity**: Information should be immediately understandable-2. **Accuracy**: Data representation must be truthful and unmanipulated-3. **Accessibility**: Figures should be interpretable by all readers, including those with visual impairments-4. **Professional**: Clean, polished appearance suitable for peer-reviewed journals--## Resolution and File Format--### Resolution Requirements-- **Raster images (photos, microscopy)**: 300-600 DPI at final print size-- **Line art and graphs**: 600-1200 DPI (or vector format)-- **Combined figures**: 300-600 DPI--### File Formats-- **Vector formats (preferred for graphs/plots)**: PDF, EPS, SVG- - Infinitely scalable without quality loss- - Smaller file sizes for line art- - Best for: plots, diagrams, schematics--- **Raster formats**: TIFF, PNG (never JPEG for scientific data)- - Use for: photographs, microscopy, images with continuous tone- - TIFF: Lossless, widely accepted- - PNG: Lossless, good for web and supplementary materials- - **Never use JPEG**: Lossy compression introduces artifacts--### Size Specifications-- **Single column**: 85-90 mm (3.35-3.54 inches) width-- **1.5 column**: 114-120 mm (4.49-4.72 inches) width-- **Double column**: 174-180 mm (6.85-7.08 inches) width-- **Maximum height**: Usually 230-240 mm (9-9.5 inches)--## Typography--### Font Guidelines-- **Font family**: Sans-serif fonts (Arial, Helvetica, Calibri) for most journals- - Some journals prefer specific fonts (check guidelines)- - Consistency across all figures in manuscript--- **Font sizes at final print size**:- - Axis labels: 7-9 pt minimum- - Tick labels: 6-8 pt minimum- - Legends: 6-8 pt- - Panel labels (A, B, C): 8-12 pt, bold- - Title: Generally avoided in multi-panel figures--- **Font weight**: Regular weight for most text; bold for panel labels only--### Text Best Practices-- Use sentence case for axis labels ("Time (hours)" not "TIME (HOURS)")-- Include units in parentheses-- Avoid abbreviations unless space-constrained (define in caption)-- No text smaller than 5-6 pt at final size--## Color Usage--### Color Selection Principles-1. **Colorblind-friendly**: ~8% of males have color vision deficiency- - Avoid red/green combinations- - Use blue/orange, blue/yellow, or add texture/pattern- - Test with colorblindness simulators--2. **Purposeful color**: Color should convey meaning, not just aesthetics- - Use color to distinguish categories or highlight key data- - Maintain consistency across figures (same treatment = same color)--3. **Print considerations**:- - Colors may appear different in print vs. screen- - Use CMYK color space for print, RGB for digital- - Ensure sufficient contrast (especially for grayscale conversion)--### Recommended Color Palettes-- **Qualitative (categories)**: ColorBrewer, Okabe-Ito palette-- **Sequential (low to high)**: Viridis, Cividis, Blues, Oranges-- **Diverging (negative to positive)**: RdBu, PuOr, BrBG (ensure colorblind-safe)--### Grayscale Compatibility-- All figures should be interpretable in grayscale-- Use different line styles (solid, dashed, dotted) and markers-- Add patterns/hatching to bars and areas--## Layout and Composition--### Multi-Panel Figures-- **Panel labels**: Use bold uppercase letters (A, B, C) in top-left corner-- **Spacing**: Adequate white space between panels-- **Alignment**: Align panels along edges or axes where possible-- **Sizing**: Related panels should have consistent sizes-- **Arrangement**: Logical flow (left-to-right, top-to-bottom)--### Plot Elements--#### Axes-- **Axis lines**: 0.5-1 pt thickness-- **Tick marks**: Point inward or outward consistently-- **Tick frequency**: Enough to read values, not cluttered (typically 4-7 major ticks)-- **Axis labels**: Required on all plots; state units-- **Axis ranges**: Start from zero for bar charts (unless scientifically inappropriate)--#### Lines and Markers-- **Line width**: 1-2 pt for data lines; 0.5-1 pt for reference lines-- **Marker size**: 3-6 pt, larger than line width-- **Marker types**: Differentiate when multiple series (circles, squares, triangles)-- **Error bars**: 0.5-1 pt width; include caps if appropriate--#### Legends-- **Position**: Inside plot area if space permits, outside otherwise-- **Frame**: Optional; if used, thin line (0.5 pt)-- **Order**: Match order of data appearance (top to bottom or left to right)-- **Content**: Concise descriptions; full details in caption--### White Space and Margins-- Remove unnecessary white space around plots-- Maintain consistent margins-- `tight_layout()` or `constrained_layout=True` in matplotlib--## Data Representation Best Practices--### Statistical Rigor-- **Error bars**: Always show uncertainty (SD, SEM, CI) and state which in caption-- **Sample size**: Indicate n in figure or caption-- **Significance**: Mark statistical significance clearly (*, **, ***)-- **Replicates**: Show individual data points when possible, not just summary statistics--### Appropriate Chart Types-- **Bar plots**: Comparing discrete categories; always start y-axis at zero-- **Line plots**: Time series or continuous relationships-- **Scatter plots**: Correlation between variables; add regression line if appropriate-- **Box plots**: Distribution comparisons; show outliers-- **Heatmaps**: Matrix data, correlations, expression patterns-- **Violin plots**: Distribution shape comparison (better than box plots for bimodal data)--### Avoiding Distortion-- **No 3D effects**: Distorts perception of values-- **No unnecessary decorations**: No gradients, shadows, or chart junk-- **Consistent scales**: Use same scale for comparable panels-- **No truncated axes**: Unless clearly indicated and scientifically justified-- **Linear vs. log scales**: Choose appropriate scale; always label clearly+# Publication Figure Principles++Reviewed 2026-07-23. These are general scientific-communication principles, not publisher requirements. Date-sensitive rules belong in `journal_requirements.md`. Source IDs resolve in `sources.md`.++## Preserve evidence before styling++Keep three layers separate:++1. **Raw/source data**: immutable originals, acquisition metadata, exclusions, and missing-value codes.+2. **Transformation record**: executable code or a machine-readable log of filtering, normalization, aggregation, statistical estimation, image processing, and random seeds.+3. **Presentation output**: the figure and an export manifest recording dimensions, format, package versions, and source references.++Do not overwrite raw images or tabular data. Keep native-resolution images. Upsampling changes pixel count, not information; PLOS, Science, Nature, Cell Press, Elsevier, and IEEE explicitly warn against treating it as improved quality [PLOS-FIG] [SCIENCE-REVISED] [NATURE-FINAL] [CELL-FIG] [ELSEVIER-SIZE] [IEEE-SIZE].++For experimental/observational images:++- Apply necessary brightness, contrast, and color adjustments consistently to the whole image unless a disclosed scientific method requires otherwise.+- Never selectively erase, obscure, clone, or enhance features.+- Preserve background and nonspecific signal.+- Mark and explain splices, omitted lanes, composites, or stitched fields.+- Retain originals and the exact processing steps. Some publishers request originals during review or production [CELL-FIG] [PLOS-FIG].++The bundled exporter can write a provenance manifest, but it cannot confirm that supplied provenance is complete.++## Avoid visual deception++### Baselines and context++- **Bars and filled areas encode length/area from a baseline**: normally show the zero baseline. If a nonzero reference is scientifically meaningful, make that reference explicit and avoid implying absolute magnitude.+- **Points and lines encode position**: a nonzero axis limit can be valid, but show enough context, disclose breaks, and avoid choosing limits solely to magnify a small effect.+- Use common limits for panels intended for direct comparison. If limits differ, make the difference unmistakable.+- Do not extend axes far beyond observed data merely to suppress visible variation; Science explicitly advises that scales not extend beyond plotted data [SCIENCE-INITIAL].++### Uncertainty and raw observations++- Do not add error bars mechanically. Show uncertainty when an estimate is displayed, and show spread when the distribution is the question.+- Name the interval precisely: SD, SE, percentile interval, parametric CI, bootstrap CI, posterior interval, or another definition.+- State sample size, unit of replication, estimator, interval level, and dependence/repeated-measure handling.+- Use deterministic seeds for bootstrap displays. Seaborn 0.13.2 supports `errorbar="sd"`, `"se"`, `"pi"`, `"ci"`, tuples such as `("ci", 95)`, or a callable; bootstrap results vary unless `seed` is set [SEABORN-ERROR].+- Show raw observations when feasible. Do not jitter points so far that their category or value becomes ambiguous.+- Significance stars are not uncertainty. Add them only for a reported analysis, identify the test and multiplicity handling, and provide exact values where practical.++### Missing, excluded, and censored data++- Keep missing values distinct from zero, below-detection-limit values, and excluded observations.+- Do not silently connect across missing time points. Use a gap, explicit interpolation style, or a model curve whose status is stated.+- Give missing values a dedicated legend entry or neutral `bad` colormap color.+- Record exclusions and their rationale outside the plotting code as well as in the caption/methods.++### Area, volume, and 3D encodings++- Prefer position on a common scale.+- If area represents magnitude, scale **area**, not radius. If volume represents magnitude, scale volume, not diameter.+- Avoid perspective 3D bars, pies, and surfaces for simple comparisons; occlusion and perspective distort values.+- If a true 3D scientific structure is necessary, add orthogonal views, scale/orientation cues, and accessible alternatives.++### Logarithms and other transforms++- Label the transformed scale and base. Say whether values, axes, or model outputs were transformed.+- A logarithmic axis requires a declared policy for zero and negative values; never silently discard them.+- Interpret equal distances as ratios, not additive differences.+- For signed data around zero, consider `SymLogNorm`/a symmetric log axis with a disclosed linear region; for unequal ranges around a meaningful center, consider `TwoSlopeNorm` [MPL-NORM].+- Power-law or arbitrary transforms need strong justification and conspicuous disclosure. Matplotlib itself notes that viewers are less familiar with power normalization [MPL-NORM].++### Binning, smoothing, and aggregation++- Record bin edges, inclusion convention, bandwidth/window, smoothing method, and whether choices were made before seeing the result.+- Show sensitivity to reasonable bin or bandwidth choices when conclusions depend on them.+- Do not use interpolation or smoothing to imply observations between measured points.+- Preserve and, where practical, expose underlying observations.++### Normalization and color limits++- State the formula and reference: per-capita, percent of baseline, z-score axis, library-size factor, min-max range, or other transformation.+- Fit normalization parameters on the appropriate data partition; avoid information leakage.+- Use the same normalization and color limits across directly compared panels unless the difference is explicit.+- A diverging map needs a scientifically meaningful center. `Normalize`, `LogNorm`, `CenteredNorm`, `SymLogNorm`, `TwoSlopeNorm`, and `BoundaryNorm` encode different assumptions [MPL-NORM].+- Always label colorbars with units and transformed scale.++### Dual axes++Prefer aligned panels or normalized/common-unit displays. Dual y-axes can make unrelated series appear correlated because each range can be tuned independently. If unavoidable:++- justify the shared x-domain and distinct units;+- label each axis and series directly;+- avoid matching colors as the only association cue;+- choose limits independently of the desired visual relationship;+- provide the underlying data.++### Image contrast and channels++- Inspect histograms and clipped-pixel counts before and after adjustment.+- Apply comparable processing to images being compared.+- State channel assignment, lookup table, projection, denoising, deconvolution, thresholding, and contrast limits.+- Use scale bars based on calibration, not magnification text.+- Do not rely on red/green channel identity alone; use accessible channel combinations, outlines, labels, or separate grayscale panels.++## Encoding and color++Match the palette to data:++- **Qualitative** for unordered categories; do not imply order.+- **Sequential** for ordered magnitude.+- **Diverging** only when a meaningful midpoint exists.+- **Cyclic** for periodic variables such as direction or phase.++Use hue consistently across a manuscript. Avoid rainbow maps for ordered data unless there is a documented scientific reason and the map has been evaluated for perceptual artifacts. Paul Tol explains why ordinary rainbow schemes create false transitions and fail for some color-vision conditions [TOL].++Color is not a sufficient encoding:++- combine it with marker shape, line style, hatching, direct labels, or panel separation;+- audit contrast against the actual background;+- inspect grayscale, but do not treat grayscale conversion as a complete color-vision simulation;+- keep legends ordered like the data or direct-label series.++See `color_palettes.md` and run `scripts/palette_audit.py`.## Accessibility-### Colorblind Considerations-- Test with online simulators (e.g., Coblis, Color Oracle)-- Use patterns/textures in addition to color-- Provide alternative representations in supplementary materials if needed--### Visual Impairment-- High contrast between elements-- Thick enough lines (minimum 0.5 pt)-- Clear, uncluttered layouts--### Data Availability-- Include data tables in supplementary materials-- Provide source data files for graphs-- Consider interactive figures for online supplementary materials--## Common Mistakes to Avoid--1. **Font too small**: Text unreadable at final print size-2. **Low resolution**: Pixelated or blurry images-3. **Chart junk**: Unnecessary grid lines, 3D effects, decorations-4. **Poor color choices**: Red/green combinations, low contrast-5. **Missing elements**: No axis labels, no units, no error bars-6. **Inconsistent styling**: Different fonts/sizes within figure or between figures-7. **Data distortion**: Truncated axes, inappropriate scales, 3D effects-8. **JPEG compression**: Artifacts around text and lines-9. **Too much information**: Cramming too many data series into one plot-10. **Inaccessible legends**: Legends outside the figure boundary after export--## Figure Checklist--Before submission, verify:--- [ ] Resolution meets journal requirements (300+ DPI for raster)-- [ ] File format is acceptable (vector for plots, TIFF/PNG for images)-- [ ] Figure dimensions match journal specifications-- [ ] All text is readable at final size (minimum 6-7 pt)-- [ ] Fonts are consistent and embedded (for PDF/EPS)-- [ ] Colors are colorblind-friendly-- [ ] Figure is interpretable in grayscale-- [ ] All axes are labeled with units-- [ ] Error bars or uncertainty indicators are present-- [ ] Statistical significance is marked if applicable-- [ ] Panel labels are present and consistent (A, B, C)-- [ ] Legend is clear and complete-- [ ] No chart junk or unnecessary elements-- [ ] File naming follows journal conventions-- [ ] Figure caption is comprehensive-- [ ] Source data is available--## Journal-Specific Considerations--Always consult the specific journal's author guidelines. Common variations include:--- **Nature journals**: RGB, 300 DPI minimum, specific size requirements-- **Science**: EPS or high-res TIFF, specific font requirements-- **Cell Press**: PDF or EPS preferred, Arial or Helvetica fonts-- **PLOS**: TIFF or EPS, specific color space requirements-- **ACS journals**: Application files (AI, EPS) or high-res TIFF--See `journal_requirements.md` for detailed specifications from major publishers.+WCAG 2.2 is a web-content standard, not a journal-print specification. It provides useful targets for figures delivered on the web [WCAG22]:++- SC 1.4.1 (Level A): color is not the only visual means of conveying information.+- SC 1.4.3 (Level AA): normal text has at least 4.5:1 contrast; large text has at least 3:1, with stated exceptions.+- SC 1.4.11 (Level AA): graphical objects required to understand content have at least 3:1 contrast against adjacent colors, with an essential-presentation exception.+- SC 1.1.1 (Level A): non-text content has an equivalent text alternative.+- SC 1.4.5 (Level AA): use actual text rather than images of text when the technology can provide it, subject to exceptions.++For web/interactive figures also provide:++- a concise alt text naming chart type, variables, main pattern, and important exception;+- a long description or nearby narrative for complex figures;+- the underlying data in an accessible table/download;+- keyboard-operable interactions, visible focus, and non-hover access to values;+- a static fallback that preserves the scientific message.++Passing a palette ratio audit does not prove WCAG conformance; applicability depends on rendered context and alternatives.++## Layout, typography, and annotation++- Design at final physical size. Judge labels, symbols, and line weights at that size.+- Use one legible font family and a restrained size hierarchy.+- Include units in axis/colorbar labels. Define abbreviations.+- Keep panel labels consistent and outside dense data regions.+- Use layout engines intentionally: `layout="constrained"` handles nested grids and colorbars; calling `tight_layout()` disables constrained layout [MPL-LAYOUT].+- Check all labels and legends after export. `bbox_inches="tight"` can alter physical page dimensions, so do not use it when exact page width is required [MPL-SAVE].+- Keep decorative ink subordinate to data, uncertainty, and annotations. Gridlines can help value lookup when light and sparse; removing them is not a universal rule.++## Static, vector, raster, and interactive output++### Vector++PDF/SVG/EPS are useful for text and line art, but a vector container may include rasterized artists. DPI still controls those raster elements [MPL-SAVE]. Dense scatter plots can be selectively rasterized to control file size while preserving vector text/axes.++For Matplotlib:++```python+import matplotlib as mpl++mpl.rcParams["pdf.fonttype"] = 42+mpl.rcParams["ps.fonttype"] = 42+mpl.rcParams["svg.fonttype"] = "none"+```++PDF/PS Type 42 embeds TrueType fonts. `svg.fonttype="none"` leaves text as text and therefore depends on font availability; `svg.fonttype="path"` trades editability/searchability for appearance portability [MPL-STYLE]. Inspect the delivered file rather than assuming font behavior.++### Raster++Required pixel width is:++```text+pixels = final width (inches) × target pixels per inch+```++Embedded DPI metadata alone does not add detail. TIFF/PNG are lossless choices; JPEG can be accepted by some publishers for photographs but is a poor choice for line art or text because it is lossy. Preserve original bit depth and color profile when scientifically important.++Transparency can reveal an unintended background or change apparent contrast. Matplotlib's `transparent=True` makes axes patches transparent and, unless explicitly overridden, the figure patch too [MPL-SAVE]. Prefer an explicit opaque background for submission unless the destination requires transparency.++### Plotly++- `write_html()` preserves interaction and is self-contained by default; that embeds Plotly.js and creates a large file. `include_plotlyjs` and `full_html=False` change portability and embedding behavior [PLOTLY-HTML].+- Static `write_image()` uses Kaleido and supports PNG, JPEG, WebP, SVG, and PDF. Width/height are logical pixels; `scale` changes physical output pixel count, not a journal DPI declaration [PLOTLY-STATIC].+- WebGL traces are partly rasterized inside SVG/PDF output [PLOTLY-STATIC].+- Export a static, captioned fallback and accessible data alongside interactive output.++## Final scientific review++- [ ] Raw data and native images are preserved.+- [ ] Transformations, exclusions, normalization, bins, and random seeds are recorded.+- [ ] Missing/censored values are explicit.+- [ ] Baselines, limits, log scales, and breaks are scientifically justified.+- [ ] Uncertainty and sample size are defined.+- [ ] Area/volume and color normalization encode magnitude correctly.+- [ ] Color is redundant; foreground/background contrast was reviewed.+- [ ] Alt text/long description and underlying data are available for web delivery.+- [ ] Physical size, raster pixels, fonts, transparency, and file format were inspected after export.+- [ ] The current target-journal instructions were checked for the correct submission phase.diff --git a/skills/scientific-visualization/references/sources.md b/skills/scientific-visualization/references/sources.mdnew file mode 100644index 00000000..2025cc8e--- /dev/null+++ b/skills/scientific-visualization/references/sources.md@@ -0,0 +1,76 @@+# Sources and Version Snapshot++Research refreshed 2026-07-23 with `parallel-cli search` and `parallel-cli extract`. API and publisher requirements below use current official project, standards-body, publisher, or journal sources only. “Accessed” is 2026-07-23 unless another date is stated.++## Tested direct package snapshot++- **Matplotlib 3.11.1**, released 2026-07-18; Python >=3.11 [MPL-PYPI].+- **Seaborn 0.13.2**, released 2024-01-25; Python >=3.8 [SEABORN-PYPI].+- **Plotly 6.9.0**, released 2026-07-09; Python >=3.8 [PLOTLY-PYPI].+- **Kaleido 1.3.0**, released 2026-05-04 [KALEIDO-PYPI].+- **Pillow 12.3.0**, released 2026-07-01; Python >=3.10 [PIL-PYPI].+- **pypdf 6.14.2**, released 2026-06-23; Python >=3.9 [PYPDF-PYPI].++These are pinned direct-dependency snapshots used for smoke tests, not a transitive lock.++## Matplotlib++- **[MPL-PYPI]** [matplotlib on PyPI](https://pypi.org/project/matplotlib/) — current package version and release history; page dated 2026-07-18.+- **[MPL-RELEASE]** [Matplotlib release notes](https://matplotlib.org/stable/release/release_notes.html) — 3.11 release/API changes.+- **[MPL-SAVE]** [`matplotlib.figure.Figure.savefig`](https://matplotlib.org/stable/api/_as_gen/matplotlib.figure.Figure.savefig.html) — 3.11.1 signature; format inference, DPI, metadata, bounding boxes, transparency, backends, Pillow kwargs; built 2026-07-18.+- **[MPL-BACKENDS]** [Backends](https://matplotlib.org/stable/users/explain/figure/backends.html) — interactive versus static renderers; PDF/PS/SVG/PGF/Cairo formats.+- **[MPL-STYLE]** [Customizing Matplotlib with style sheets and rcParams](https://matplotlib.org/stable/users/explain/customizing.html) — `rc_context`, style composition, save settings, PDF/PS/SVG font types; built 2026-07-18.+- **[MPL-LAYOUT]** [Constrained layout guide](https://matplotlib.org/stable/users/explain/axes/constrainedlayout_guide.html) — `layout="constrained"`, colorbars, subfigures, GridSpec, interaction with `tight_layout`.+- **[MPL-GRIDSPEC]** [`matplotlib.gridspec`](https://matplotlib.org/stable/api/gridspec_api.html) — current grid layout API.+- **[MPL-NORM]** [Colormap normalization](https://matplotlib.org/stable/users/explain/colors/colormapnorms.html) — `Normalize`, `LogNorm`, `CenteredNorm`, `SymLogNorm`, `PowerNorm`, `BoundaryNorm`, `TwoSlopeNorm`; built 2026-07-18.+- **[MPL-CMAP]** [Choosing colormaps](https://matplotlib.org/stable/users/explain/colors/colormaps.html) — data classes and perceived lightness.++## Seaborn++- **[SEABORN-PYPI]** [seaborn on PyPI](https://pypi.org/project/seaborn/) — 0.13.2 package metadata and release history.+- **[SEABORN-ERROR]** [Statistical estimation and error bars](https://seaborn.pydata.org/tutorial/error_bars.html) — current `errorbar` methods, callable intervals, bootstrapping, `seed`, and `n_boot`.+- **[SEABORN-FAQ]** [Frequently asked questions](https://seaborn.pydata.org/faq.html) — axes-level versus figure-level functions, Matplotlib object-oriented integration, DPI/SVG notes.+- **[SEABORN-PALETTE]** [Choosing color palettes](https://seaborn.pydata.org/tutorial/color_palettes.html) — qualitative, sequential, and diverging palette APIs.+- **[SEABORN-THEME]** [`seaborn.set_theme`](https://seaborn.pydata.org/generated/seaborn.set_theme.html) — style, context, palette, font, scale, and rc parameters.++## Plotly and Kaleido++- **[PLOTLY-PYPI]** [plotly on PyPI](https://pypi.org/project/plotly/) — 6.9.0 package metadata; released 2026-07-09.+- **[PLOTLY-STATIC]** [Static image export in Python](https://plotly.com/python/static-image-export/) — Kaleido/Chrome setup, formats, `write_image`, `write_images`, dimensions/scale, WebGL rasterization, offline assets, defaults, EPS/Orca/engine deprecations; page dated 2026.+- **[PLOTLY-HTML]** [Interactive HTML export](https://plotly.com/python/interactive-html-export/) — `write_html`, `to_html`, `include_plotlyjs`, `full_html`; page dated 2026.+- **[PLOTLY-CHANGES]** [Static image generation changes in Plotly.py 6.1](https://plotly.com/python/static-image-generation-changes/) — Kaleido v1 migration and deprecations.+- **[KALEIDO]** [Plotly Kaleido repository](https://github.com/plotly/Kaleido) — Chrome requirement, v1 migration, direct APIs, and offline/page behavior.+- **[KALEIDO-PYPI]** [kaleido on PyPI](https://pypi.org/project/kaleido/) — 1.3.0 package metadata; released 2026-05-04.++## Accessibility and color++- **[WCAG22]** [Web Content Accessibility Guidelines (WCAG) 2.2](https://www.w3.org/TR/WCAG22/) — W3C Recommendation; normative SC 1.1.1, 1.4.1, 1.4.3, 1.4.5, and 1.4.11.+- **[WCAG-NONTEXT]** [Understanding SC 1.4.11: Non-text Contrast](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html) — informative chart/graph examples and testing principles; not itself normative.+- **[WCAG-COLOR]** [Understanding SC 1.4.1: Use of Color](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html) — informative non-color cue guidance.+- **[COLORBREWER]** [ColorBrewer 2.0](https://colorbrewer2.org/) — Cynthia Brewer, Mark Harrower, and Penn State; scheme type, data-class count, colorblind/print/photocopy filters, and exports.+- **[TOL-HOME]** [Paul Tol’s Notes](https://sronpersonalpages.nl/~pault/) — canonical site; page states the move from SRON on 2026-07-07.+- **[TOL]** [Paul Tol, “Colour Schemes”](https://sronpersonalpages.nl/~pault/data/colourschemes.pdf) — SRON/EPS/TN/09-002, issue 3.2, 2021-08-18; exact sRGB palettes, intended uses, color-vision checks, and grayscale analysis.+- **[WONG]** [Bang Wong, “Color blindness”](https://www.nature.com/articles/nmeth.1618) — Nature Methods 8, 441 (2011); source commonly used for the eight-color palette.++## Publishers and journals++All rules were accessed 2026-07-23. Pages without a displayed update date are labeled by access date rather than assigning an invented publication date.++- **[NATURE-FINAL]** [`Nature` final submission](https://www.nature.com/nature/for-authors/final-submission) — flagship final files, dimensions, fonts, formats, raster resolution, RGB/CMYK, Extended Data distinctions.+- **[NATURE-FIG]** [`Nature` research figure specifications](https://research-figure-guide.nature.com/figures/preparing-figures-our-specifications) — graphs, accessibility, RGB, 300/450 dpi discussion, editable Type 42 text, export.+- **[SCIENCE-INITIAL]** [`Science` initial manuscript instructions](https://www.science.org/content/page/instructions-preparing-initial-manuscript) — initial figure embedding, 300 dpi, widths, fonts, color/contrast, source data.+- **[SCIENCE-REVISED]** [`Science` revised manuscript instructions](https://www.science.org/content/page/instructions-preparing-revised-manuscript) — separate files, formats, minimum resolution, dimensions, no upsampling.+- **[CELL-FIG]** [Cell Press figure guidelines](https://www.cell.com/information-for-authors/figure-guidelines) — initial versus final stages, formats, widths, file size, DPI, RGB, fonts, image integrity, AI-assisted image policy.+- **[PLOS-FIG]** [PLOS Computational Biology figures](https://journals.plos.org/ploscompbiol/s/figures) — provisional-accept waiver, TIFF/EPS, dimensions, 300-600 dpi, RGB/grayscale, file size, image integrity, 2026-04-01 blot/gel requirement.+- **[ELSEVIER-FORMAT]** [Elsevier artwork formats checklist](https://www.elsevier.com/about/policies-and-standards/author/artwork-and-media-instructions/artwork-formats-checklist) — general formats, RGB preference, separate files, journal override.+- **[ELSEVIER-SIZE]** [Elsevier artwork sizing](https://www.elsevier.com/about/policies-and-standards/author/artwork-and-media-instructions/artwork-sizing) — general widths, 300/500/1,000 dpi, typography, and explicit journal variability.+- **[IEEE-SIZE]** [IEEE Resolution and Size](https://journals.ieeeauthorcenter.ieee.org/create-your-ieee-journal-article/create-graphics-for-your-article/resolution-and-size/) — modified 2025-02-25; PS/EPS/PDF, >300/>600 dpi, 88.9/182 mm.+- **[BMC-BIOINFO]** [BMC Bioinformatics: preparing your manuscript](https://bmcbioinformatics.biomedcentral.com/submission-guidelines/preparing-your-manuscript) — journal-specific formats, 85/170 mm, approximately 300 dpi, 10 MB, embedded fonts.+- **[ACS-GRAPHICS]** [ACS Preparing Manuscript Graphics](https://pubs.acs.org/page/4authors/submission/graphics_prep.html) — general dimensions and typography; no page update date displayed.++## Optional inspection backends++- **[PIL-PYPI]** [Pillow on PyPI](https://pypi.org/project/Pillow/) — 12.3.0 package metadata; released 2026-07-01.+- **[PYPDF-PYPI]** [pypdf on PyPI](https://pypi.org/project/pypdf/) — 6.14.2 package metadata; released 2026-06-23.++No Parallel JSON research artifacts are stored in this skill.diff --git a/skills/scientific-visualization/scripts/_common.py b/skills/scientific-visualization/scripts/_common.pynew file mode 100644index 00000000..6c8355a4--- /dev/null+++ b/skills/scientific-visualization/scripts/_common.py@@ -0,0 +1,136 @@+#!/usr/bin/env python3+"""Shared, network-free safety helpers for visualization command-line tools."""++from __future__ import annotations++import json+import os+import stat+import tempfile+from pathlib import Path+from typing import Any++MAX_INPUT_BYTES = 200 * 1024 * 1024+MAX_REPORT_BYTES = 4 * 1024 * 1024+++class CliError(ValueError):+ """An expected command-line validation error."""+++def checked_input_file(+ value: str | os.PathLike[str],+ *,+ max_bytes: int = MAX_INPUT_BYTES,+) -> Path:+ """Return a bounded regular input file, rejecting symlinks."""+ path = Path(value).expanduser()+ if path.is_symlink():+ raise CliError(f"input must not be a symlink: {path}")+ try:+ info = path.stat()+ except OSError as exc:+ raise CliError(f"cannot access input file {path}: {exc}") from exc+ if not stat.S_ISREG(info.st_mode):+ raise CliError(f"input is not a regular file: {path}")+ if info.st_size > max_bytes:+ raise CliError(+ f"input is {info.st_size} bytes; limit is {max_bytes} bytes"+ )+ return path.resolve()+++def checked_output_file(+ value: str | os.PathLike[str],+ *,+ force: bool = False,+ mkdir: bool = False,+) -> Path:+ """Validate an explicit output path without following a destination symlink."""+ path = Path(value).expanduser()+ if path.name in {"", ".", ".."}:+ raise CliError("output must name a file")+ if path.is_symlink():+ raise CliError(f"output must not be a symlink: {path}")++ parent = path.parent+ if mkdir:+ parent.mkdir(parents=True, exist_ok=True)+ if not parent.exists() or not parent.is_dir():+ raise CliError(f"output parent directory does not exist: {parent}")+ if parent.is_symlink():+ raise CliError(f"output parent must not be a symlink: {parent}")++ if path.exists():+ if not path.is_file():+ raise CliError(f"output exists and is not a regular file: {path}")+ if not force:+ raise CliError(f"refusing to overwrite existing output: {path}")+ return parent.resolve() / path.name+++def atomic_write_bytes(path: Path, payload: bytes, *, force: bool = False) -> None:+ """Write bytes through a same-directory temporary file."""+ destination = checked_output_file(path, force=force)+ descriptor, temporary_name = tempfile.mkstemp(+ prefix=f".{destination.name}.",+ suffix=".tmp",+ dir=destination.parent,+ )+ temporary = Path(temporary_name)+ try:+ with os.fdopen(descriptor, "wb") as handle:+ handle.write(payload)+ handle.flush()+ os.fsync(handle.fileno())+ os.chmod(temporary, 0o600)+ if destination.exists() and not force:+ raise CliError(f"refusing to overwrite existing output: {destination}")+ os.replace(temporary, destination)+ finally:+ try:+ temporary.unlink()+ except FileNotFoundError:+ pass+++def emit_json(+ document: dict[str, Any],+ *,+ output: str | os.PathLike[str] | None = None,+ force: bool = False,+) -> None:+ """Print deterministic JSON or write it safely to an explicit file."""+ payload = (+ json.dumps(document, indent=2, sort_keys=True, ensure_ascii=False) + "\n"+ ).encode("utf-8")+ if len(payload) > MAX_REPORT_BYTES:+ raise CliError(+ f"report is {len(payload)} bytes; limit is {MAX_REPORT_BYTES} bytes"+ )+ if output is None:+ print(payload.decode("utf-8"), end="")+ return+ atomic_write_bytes(Path(output), payload, force=force)+++def positive_int(value: str) -> int:+ """Argparse converter for strictly positive integers."""+ try:+ parsed = int(value)+ except ValueError as exc:+ raise CliError(f"expected an integer, got {value!r}") from exc+ if parsed <= 0:+ raise CliError("value must be greater than zero")+ return parsed+++def positive_float(value: str) -> float:+ """Argparse converter for finite, strictly positive floats."""+ try:+ parsed = float(value)+ except ValueError as exc:+ raise CliError(f"expected a number, got {value!r}") from exc+ if not (parsed > 0 and parsed < float("inf")):+ raise CliError("value must be finite and greater than zero")+ return parseddiff --git a/skills/scientific-visualization/scripts/export_plan.py b/skills/scientific-visualization/scripts/export_plan.pynew file mode 100644index 00000000..24a6f18c--- /dev/null+++ b/skills/scientific-visualization/scripts/export_plan.py@@ -0,0 +1,493 @@+#!/usr/bin/env python3+"""Build and screen against dated publication-export planning snapshots."""++from __future__ import annotations++import argparse+import json+import math+import sys+from pathlib import Path+from typing import Any++from _common import CliError, emit_json, positive_float++SCHEMA_VERSION = "1.0"+PROFILE_PATH = Path(__file__).resolve().parents[1] / "assets" / "publisher_profiles.json"+++def load_profiles() -> dict[str, Any]:+ try:+ document = json.loads(PROFILE_PATH.read_text(encoding="utf-8"))+ except (OSError, json.JSONDecodeError) as exc:+ raise CliError(f"cannot load publisher profile asset: {exc}") from exc+ if not isinstance(document.get("profiles"), dict):+ raise CliError("publisher profile asset does not contain profiles")+ return document+++def build_plan(+ publisher: str,+ *,+ figure_type: str,+ width: str | None = None,+ phase: str | None = None,+) -> dict[str, Any]:+ """Build an explicit plan without exporting or claiming compliance."""+ document = load_profiles()+ try:+ profile = document["profiles"][publisher]+ except KeyError as exc:+ raise CliError(+ f"unknown publisher profile {publisher!r}; "+ f"available: {', '.join(sorted(document['profiles']))}"+ ) from exc+ widths = profile.get("widths_mm") or {}+ if width is not None and width not in widths:+ raise CliError(+ f"unknown width {width!r} for {publisher}; "+ f"available: {', '.join(sorted(widths))}"+ )+ format_map = profile.get("formats") or {}+ dpi_map = profile.get("raster_dpi") or {}+ available_types = sorted(set(format_map) | set(dpi_map))+ if figure_type not in available_types and available_types:+ raise CliError(+ f"unknown figure type {figure_type!r} for {publisher}; "+ f"available: {', '.join(available_types)}"+ )++ profile_phase = profile.get("phase")+ phase_matches = phase is None or phase == profile_phase+ selected_width = float(widths[width]) if width is not None else None+ return {+ "schema_version": SCHEMA_VERSION,+ "publisher": publisher,+ "label": profile["label"],+ "scope": profile["scope"],+ "profile_accessed": document["accessed"],+ "profile_phase": profile_phase,+ "requested_phase": phase,+ "phase_matches_snapshot": phase_matches,+ "journal_specific": profile.get("journal_specific"),+ "figure_type": figure_type,+ "width": {+ "name": width,+ "millimeters": selected_width,+ "available": widths,+ },+ "max_height_mm": profile.get("max_height_mm"),+ "formats": format_map.get(figure_type) if format_map else None,+ "raster_dpi": dpi_map.get(figure_type) if dpi_map else None,+ "color_modes": profile.get("color_modes"),+ "preferred_color_mode": profile.get("preferred_color_mode"),+ "max_file_bytes": profile.get("max_file_bytes"),+ "max_file_bytes_exclusive": profile.get("max_file_bytes_exclusive"),+ "width_range_px_at_300_dpi": profile.get("width_range_px_at_300_dpi"),+ "max_height_px_at_300_dpi": profile.get("max_height_px_at_300_dpi"),+ "sources": profile["sources"],+ "notes": profile.get("notes", []),+ "notice": document["notice"],+ }+++def _finding(+ name: str,+ status: str,+ *,+ actual: Any,+ expected: Any,+ detail: str,+) -> dict[str, Any]:+ return {+ "name": name,+ "status": status,+ "actual": actual,+ "expected": expected,+ "detail": detail,+ }+++def _normalized_mode(mode: str | None) -> str | None:+ if mode is None:+ return None+ if mode in {"RGB", "RGBA"}:+ return "RGB"+ if mode in {"L", "LA", "1", "I", "I;16", "F"}:+ return "L"+ return mode+++def _effective_dimensions(+ metadata: dict[str, Any], target_width_mm: float | None+) -> tuple[float | None, float | None, float | None]:+ """Return effective DPI, width mm, and height mm."""+ width_px = metadata.get("width_px")+ height_px = metadata.get("height_px")+ if target_width_mm is not None and width_px:+ dpi = float(width_px) / (target_width_mm / 25.4)+ height_mm = (+ float(height_px) / float(width_px) * target_width_mm+ if height_px+ else None+ )+ return dpi, target_width_mm, height_mm+ width_mm = metadata.get("width_mm") or metadata.get("width_mm_from_metadata")+ height_mm = metadata.get("height_mm") or metadata.get("height_mm_from_metadata")+ dpi_values = [+ float(value)+ for value in (metadata.get("dpi_x"), metadata.get("dpi_y"))+ if value+ ]+ return (+ min(dpi_values) if dpi_values else None,+ float(width_mm) if width_mm is not None else None,+ float(height_mm) if height_mm is not None else None,+ )+++def validate_against_plan(+ plan: dict[str, Any],+ input_path: str | Path,+ *,+ width_tolerance_mm: float = 1.0,+) -> dict[str, Any]:+ """Screen inspectable file properties against one explicit plan."""+ try:+ from image_metadata import inspect_file+ except ImportError as exc:+ raise CliError(f"cannot import image_metadata.py: {exc}") from exc+ inspected = inspect_file(input_path)+ metadata = inspected["metadata"]+ findings: list[dict[str, Any]] = []++ formats = plan.get("formats")+ actual_format = str(metadata.get("format", "")).lower()+ if formats is None:+ findings.append(+ _finding(+ "format",+ "unknown",+ actual=actual_format,+ expected=None,+ detail="The profile does not state a universal format for this type.",+ )+ )+ elif not formats:+ findings.append(+ _finding(+ "format",+ "unknown",+ actual=actual_format,+ expected=[],+ detail="No raster format was stated on the cited profile page.",+ )+ )+ else:+ findings.append(+ _finding(+ "format",+ "pass" if actual_format in formats else "fail",+ actual=actual_format,+ expected=formats,+ detail="Compared with formats explicitly recorded in the snapshot.",+ )+ )++ target_width = plan["width"]["millimeters"]+ effective_dpi, effective_width, effective_height = _effective_dimensions(+ metadata, target_width+ )+ dpi_rule = plan.get("raster_dpi")+ if metadata.get("kind") == "raster" and dpi_rule:+ if effective_dpi is None:+ dpi_status = "unknown"+ elif (+ dpi_rule.get("min") is not None+ and effective_dpi < float(dpi_rule["min"])+ and not math.isclose(+ effective_dpi,+ float(dpi_rule["min"]),+ rel_tol=1e-12,+ abs_tol=1e-9,+ )+ ):+ dpi_status = "fail"+ elif (+ dpi_rule.get("min_exclusive") is not None+ and effective_dpi <= float(dpi_rule["min_exclusive"])+ ):+ dpi_status = "fail"+ elif (+ dpi_rule.get("max") is not None+ and effective_dpi > float(dpi_rule["max"])+ and not math.isclose(+ effective_dpi,+ float(dpi_rule["max"]),+ rel_tol=1e-12,+ abs_tol=1e-9,+ )+ ):+ dpi_status = "fail"+ else:+ dpi_status = "pass"+ findings.append(+ _finding(+ "effective_raster_dpi",+ dpi_status,+ actual=effective_dpi,+ expected=dpi_rule,+ detail=(+ "Calculated at the selected final width when supplied; "+ "otherwise uses embedded DPI metadata. Upsampling is not "+ "evidence of added detail."+ ),+ )+ )+ elif dpi_rule and metadata.get("kind") != "raster":+ findings.append(+ _finding(+ "effective_raster_dpi",+ "unknown",+ actual=None,+ expected=dpi_rule,+ detail=(+ "Vector-container DPI is not meaningful and embedded raster "+ "objects were not individually inspected."+ ),+ )+ )+ else:+ findings.append(+ _finding(+ "effective_raster_dpi",+ "unknown",+ actual=effective_dpi,+ expected=dpi_rule,+ detail="No binding DPI rule was recorded for this figure type.",+ )+ )++ if target_width is not None:+ if effective_width is None:+ width_status = "unknown"+ else:+ width_status = (+ "pass"+ if abs(effective_width - target_width) <= width_tolerance_mm+ else "fail"+ )+ findings.append(+ _finding(+ "final_width_mm",+ width_status,+ actual=effective_width,+ expected={+ "target": target_width,+ "tolerance": width_tolerance_mm,+ },+ detail=(+ "Raster inputs are evaluated at the requested target width; "+ "vector containers use their page dimensions."+ ),+ )+ )++ max_height = plan.get("max_height_mm")+ if max_height is not None:+ findings.append(+ _finding(+ "final_height_mm",+ (+ "unknown"+ if effective_height is None+ else (+ "pass"+ if effective_height <= float(max_height)+ else "fail"+ )+ ),+ actual=effective_height,+ expected={"max": max_height},+ detail="Height includes the exported page/canvas, not a caption.",+ )+ )++ pixel_range = plan.get("width_range_px_at_300_dpi")+ if pixel_range and metadata.get("width_px") is not None:+ actual_width_px = int(metadata["width_px"])+ findings.append(+ _finding(+ "pixel_width_snapshot",+ (+ "pass"+ if int(pixel_range[0]) <= actual_width_px <= int(pixel_range[1])+ else "fail"+ ),+ actual=actual_width_px,+ expected={"min": pixel_range[0], "max": pixel_range[1]},+ detail="This publisher expresses its width range at 300 dpi.",+ )+ )++ max_bytes = plan.get("max_file_bytes")+ max_bytes_exclusive = plan.get("max_file_bytes_exclusive")+ if max_bytes is not None or max_bytes_exclusive is not None:+ actual_bytes = inspected["input"]["size_bytes"]+ if max_bytes_exclusive is not None:+ size_status = (+ "pass" if actual_bytes < int(max_bytes_exclusive) else "fail"+ )+ expected_size = {"less_than": max_bytes_exclusive}+ else:+ size_status = "pass" if actual_bytes <= int(max_bytes) else "fail"+ expected_size = {"max": max_bytes}+ findings.append(+ _finding(+ "file_size",+ size_status,+ actual=actual_bytes,+ expected=expected_size,+ detail="Compared with the dated snapshot's file-size limit.",+ )+ )++ allowed_modes = plan.get("color_modes")+ actual_mode = metadata.get("mode")+ if allowed_modes:+ normalized_mode = _normalized_mode(actual_mode)+ findings.append(+ _finding(+ "color_mode",+ (+ "unknown"+ if normalized_mode is None+ else (+ "pass"+ if normalized_mode in allowed_modes+ else "fail"+ )+ ),+ actual=actual_mode,+ expected=allowed_modes,+ detail=(+ "Raster mode is screened coarsely. ICC profiles and print "+ "conversion require separate color-management review."+ ),+ )+ )++ if metadata.get("has_alpha") is True:+ findings.append(+ _finding(+ "transparency",+ "review",+ actual=True,+ expected="verify against target journal/background",+ detail=(+ "The raster has alpha/transparency. Blending can change "+ "contrast, and no universal publisher rule is assumed."+ ),+ )+ )++ counts = {+ status: sum(item["status"] == status for item in findings)+ for status in ("pass", "fail", "review", "unknown")+ }+ return {+ "schema_version": SCHEMA_VERSION,+ "plan": plan,+ "inspection": inspected,+ "findings": findings,+ "screening_summary": counts,+ "notice": (+ "This screen compares only machine-readable properties with a dated "+ "profile. It cannot certify journal acceptance, font appearance, "+ "embedded-raster quality, accessibility, or scientific integrity."+ ),+ }+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Plan publication export from dated official-source snapshots and "+ "optionally screen a local file. No network access is used."+ )+ )+ parser.add_argument("--list", action="store_true", help="list profiles")+ parser.add_argument("--publisher", help="publisher/profile key")+ parser.add_argument(+ "--figure-type",+ default="combination",+ help="figure type in the selected profile (default: combination)",+ )+ parser.add_argument("--width", help="named final width from the profile")+ parser.add_argument(+ "--phase", help="your submission phase, for an explicit scope check"+ )+ parser.add_argument("--input", help="optional local file to screen")+ parser.add_argument(+ "--width-tolerance-mm",+ type=positive_float,+ default=1.0,+ help="vector/page width tolerance (default: 1.0)",+ )+ parser.add_argument("--output", help="optional JSON output path")+ parser.add_argument("--force", action="store_true")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ try:+ args = parser.parse_args(argv)+ document = load_profiles()+ if args.list:+ emit_json(+ {+ "schema_version": SCHEMA_VERSION,+ "accessed": document["accessed"],+ "profiles": {+ name: {+ "label": profile["label"],+ "scope": profile["scope"],+ "phase": profile["phase"],+ "sources": profile["sources"],+ }+ for name, profile in sorted(document["profiles"].items())+ },+ "notice": document["notice"],+ },+ output=args.output,+ force=args.force,+ )+ return 0+ if not args.publisher:+ parser.error("--publisher is required unless --list is used")+ plan = build_plan(+ args.publisher,+ figure_type=args.figure_type,+ width=args.width,+ phase=args.phase,+ )+ result = (+ validate_against_plan(+ plan,+ args.input,+ width_tolerance_mm=args.width_tolerance_mm,+ )+ if args.input+ else plan+ )+ emit_json(result, output=args.output, force=args.force)+ if args.input:+ return 1 if result["screening_summary"]["fail"] else 0+ return 0+ except CliError as exc:+ parser.exit(2, f"error: {exc}\n")+++if __name__ == "__main__":+ sys.exit(main())diff --git a/skills/scientific-visualization/scripts/figure_export.py b/skills/scientific-visualization/scripts/figure_export.pyindex 0a643ff2..8035a2c2 100644--- a/skills/scientific-visualization/scripts/figure_export.py+++ b/skills/scientific-visualization/scripts/figure_export.py@@ -1,343 +1,642 @@#!/usr/bin/env python3-"""-Figure Export Utilities for Publication-Ready Scientific Figures+"""Safe Matplotlib export helpers with explicit, auditable settings.-This module provides utilities to export matplotlib figures in publication-ready-formats with appropriate settings for various journals.+This module does not certify journal compliance. Publisher profiles are dated+planning snapshots and must be confirmed against the target journal."""-import matplotlib.pyplot as plt-from pathlib import Path-from typing import List, Optional, Union-+from __future__ import annotations-def save_publication_figure(- fig: plt.Figure,- filename: Union[str, Path],- formats: List[str] = ['pdf', 'png'],- dpi: int = 300,- transparent: bool = False,- bbox_inches: str = 'tight',- pad_inches: float = 0.1,- facecolor: str = 'white',- **kwargs-) -> List[Path]:- """- Save a matplotlib figure in multiple formats with publication-quality settings.-- Parameters- ----------- fig : matplotlib.figure.Figure- The figure to save- filename : str or Path- Base filename (without extension)- formats : list of str, default ['pdf', 'png']- List of file formats to save. Options: 'pdf', 'png', 'eps', 'svg', 'tiff'- dpi : int, default 300- Resolution for raster formats (png, tiff). 300 DPI is minimum for most journals- transparent : bool, default False- If True, save with transparent background- bbox_inches : str, default 'tight'- Bounding box specification. 'tight' removes excess whitespace- pad_inches : float, default 0.1- Padding around the figure when bbox_inches='tight'- facecolor : str, default 'white'- Background color (ignored if transparent=True)- **kwargs- Additional keyword arguments passed to fig.savefig()-- Returns- -------- list of Path- List of paths to saved files-- Examples- --------- >>> fig, ax = plt.subplots()- >>> ax.plot([1, 2, 3], [1, 4, 9])- >>> save_publication_figure(fig, 'my_plot', formats=['pdf', 'png'], dpi=600)- ['my_plot.pdf', 'my_plot.png']- """- filename = Path(filename)- base_name = filename.stem- output_dir = filename.parent if filename.parent.exists() else Path.cwd()-- saved_files = []-- for fmt in formats:- output_file = output_dir / f"{base_name}.{fmt}"-- # Set format-specific parameters- save_kwargs = {- 'dpi': dpi,- 'bbox_inches': bbox_inches,- 'pad_inches': pad_inches,- 'facecolor': facecolor if not transparent else 'none',- 'edgecolor': 'none',- 'transparent': transparent,- 'format': fmt,+import argparse+import importlib.metadata+import json+import math+import os+import sys+import tempfile+from pathlib import Path+from typing import Any, Iterable++from _common import CliError, checked_output_file, emit_json, positive_float++SCHEMA_VERSION = "1.0"+SUPPORTED_FORMATS = {+ "pdf",+ "svg",+ "eps",+ "ps",+ "png",+ "tiff",+ "jpeg",+ "webp",+}+FORMAT_ALIASES = {"jpg": "jpeg", "tif": "tiff"}+VECTOR_FORMATS = {"pdf", "svg", "eps", "ps"}+RASTER_FORMATS = {"png", "tiff", "jpeg", "webp"}+METADATA_FORMATS = {"pdf", "svg", "png", "eps", "ps"}+MAX_FORMATS = 8+++def _normalize_formats(formats: Iterable[str]) -> list[str]:+ normalized: list[str] = []+ for item in formats:+ for value in item.split(","):+ raw_name = value.strip().lower().lstrip(".")+ name = FORMAT_ALIASES.get(raw_name, raw_name)+ if not name:+ continue+ if name not in SUPPORTED_FORMATS:+ raise CliError(+ f"unsupported format {name!r}; "+ f"available: {', '.join(sorted(SUPPORTED_FORMATS))}"+ )+ if name not in normalized:+ normalized.append(name)+ if not normalized:+ raise CliError("at least one output format is required")+ if len(normalized) > MAX_FORMATS:+ raise CliError(f"at most {MAX_FORMATS} output formats are allowed")+ return normalized+++def _base_output_path(value: str | os.PathLike[str]) -> Path:+ path = Path(value).expanduser()+ if path.suffix.lower().lstrip(".") in SUPPORTED_FORMATS | set(FORMAT_ALIASES):+ path = path.with_suffix("")+ if not path.name:+ raise CliError("output must include a base filename")+ return path+++def _font_rc(font_mode: str) -> dict[str, Any]:+ if font_mode == "current":+ return {}+ if font_mode == "truetype":+ return {+ "pdf.fonttype": 42,+ "ps.fonttype": 42,+ # Editable SVG text; unlike PDF Type 42, this is not embedded.+ "svg.fonttype": "none",+ }+ if font_mode == "paths":+ return {+ "pdf.fonttype": 3,+ "ps.fonttype": 3,+ "svg.fonttype": "path",}+ raise CliError("font_mode must be 'truetype', 'paths', or 'current'")- # Update with user-provided kwargs- save_kwargs.update(kwargs)- # Adjust DPI for vector formats (DPI less relevant)- if fmt in ['pdf', 'eps', 'svg']:- save_kwargs['dpi'] = min(dpi, 300) # Lower DPI for embedded rasters in vector+def _package_version(distribution: str) -> str | None:+ try:+ return importlib.metadata.version(distribution)+ except importlib.metadata.PackageNotFoundError:+ return None- try:- fig.savefig(output_file, **save_kwargs)- saved_files.append(output_file)- print(f"✓ Saved: {output_file}")- except Exception as e:- print(f"✗ Failed to save {output_file}: {e}")- return saved_files+def _atomic_savefig(+ fig: Any,+ destination: Path,+ *,+ format_name: str,+ save_kwargs: dict[str, Any],+ overwrite: bool,+) -> None:+ destination = checked_output_file(destination, force=overwrite)+ descriptor, temporary_name = tempfile.mkstemp(+ prefix=f".{destination.stem}.",+ suffix=f".{format_name}",+ dir=destination.parent,+ )+ os.close(descriptor)+ temporary = Path(temporary_name)+ try:+ # Matplotlib expects to create the target on some backends.+ temporary.unlink()+ fig.savefig(temporary, format=format_name, **save_kwargs)+ if not temporary.exists() or temporary.stat().st_size == 0:+ raise CliError(f"Matplotlib produced no data for {destination}")+ if destination.exists() and not overwrite:+ raise CliError(f"refusing to overwrite existing output: {destination}")+ os.replace(temporary, destination)+ except CliError:+ raise+ except Exception as exc:+ raise CliError(+ f"failed to export {destination.name} as {format_name}: {exc}"+ ) from exc+ finally:+ try:+ temporary.unlink()+ except FileNotFoundError:+ pass-def save_for_journal(- fig: plt.Figure,- filename: Union[str, Path],- journal: str,- figure_type: str = 'combination'-) -> List[Path]:- """- Save figure with journal-specific requirements.-- Parameters- ----------- fig : matplotlib.figure.Figure- The figure to save- filename : str or Path- Base filename (without extension)- journal : str- Journal name. Options: 'nature', 'science', 'cell', 'plos', 'acs', 'ieee'- figure_type : str, default 'combination'- Type of figure. Options: 'line_art', 'photo', 'combination'-- Returns- -------- list of Path- List of paths to saved files-- Examples- --------- >>> fig, ax = plt.subplots()- >>> ax.plot([1, 2, 3], [1, 4, 9])- >>> save_for_journal(fig, 'figure1', journal='nature', figure_type='line_art')+def _validate_provenance(provenance: dict[str, Any] | None) -> dict[str, Any]:+ if provenance is None:+ return {}+ if not isinstance(provenance, dict):+ raise CliError("provenance must be a dictionary")+ try:+ encoded = json.dumps(provenance, sort_keys=True).encode("utf-8")+ except (TypeError, ValueError) as exc:+ raise CliError(f"provenance must be JSON serializable: {exc}") from exc+ if len(encoded) > 1_000_000:+ raise CliError("provenance metadata is limited to 1,000,000 bytes")+ return provenance+++def export_figure(+ fig: Any,+ filename: str | os.PathLike[str],+ *,+ formats: Iterable[str] = ("pdf", "png"),+ dpi: float = 300,+ transparent: bool = False,+ bbox_inches: str | None = None,+ pad_inches: float = 0.1,+ facecolor: str = "white",+ edgecolor: str = "none",+ font_mode: str = "truetype",+ overwrite: bool = False,+ mkdir: bool = False,+ metadata: dict[str, Any] | None = None,+ provenance: dict[str, Any] | None = None,+ write_manifest: bool = False,+ savefig_kwargs: dict[str, Any] | None = None,+) -> dict[str, Any]:+ """Export a Matplotlib figure atomically with explicit format settings.++ ``dpi`` is passed to vector backends too because it controls rasterized+ artists and embedded raster images. ``bbox_inches=None`` preserves the+ figure's physical page dimensions; use ``"tight"`` only when that change is+ intentional."""- journal = journal.lower()-- # Define journal-specific requirements- journal_specs = {- 'nature': {- 'line_art': {'formats': ['pdf', 'eps'], 'dpi': 1000},- 'photo': {'formats': ['tiff'], 'dpi': 300},- 'combination': {'formats': ['pdf'], 'dpi': 600},- },- 'science': {- 'line_art': {'formats': ['eps', 'pdf'], 'dpi': 1000},- 'photo': {'formats': ['tiff'], 'dpi': 300},- 'combination': {'formats': ['eps'], 'dpi': 600},- },- 'cell': {- 'line_art': {'formats': ['pdf', 'eps'], 'dpi': 1000},- 'photo': {'formats': ['tiff'], 'dpi': 300},- 'combination': {'formats': ['pdf'], 'dpi': 600},- },- 'plos': {- 'line_art': {'formats': ['pdf', 'eps'], 'dpi': 600},- 'photo': {'formats': ['tiff', 'png'], 'dpi': 300},- 'combination': {'formats': ['tiff'], 'dpi': 300},- },- 'acs': {- 'line_art': {'formats': ['tiff', 'pdf'], 'dpi': 600},- 'photo': {'formats': ['tiff'], 'dpi': 300},- 'combination': {'formats': ['tiff'], 'dpi': 600},+ if not (float(dpi) > 0 and math.isfinite(float(dpi))):+ raise CliError("dpi must be finite and greater than zero")+ if not (float(pad_inches) >= 0 and math.isfinite(float(pad_inches))):+ raise CliError("pad_inches must be finite and non-negative")+ normalized_formats = _normalize_formats(formats)+ base = _base_output_path(filename)+ parent = base.parent+ if mkdir:+ parent.mkdir(parents=True, exist_ok=True)+ if not parent.exists() or not parent.is_dir():+ raise CliError(f"output parent directory does not exist: {parent}")+ if parent.is_symlink():+ raise CliError(f"output parent must not be a symlink: {parent}")+ for format_name in normalized_formats:+ checked_output_file(+ parent / f"{base.name}.{format_name}",+ force=overwrite,+ )+ if write_manifest:+ checked_output_file(+ parent / f"{base.name}.export.json",+ force=overwrite,+ )++ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required for figure export; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc++ provenance_document = _validate_provenance(provenance)+ user_metadata = dict(metadata or {})+ creator = user_metadata.setdefault(+ "Creator",+ f"scientific-visualization figure_export.py; Matplotlib {mpl.__version__}",+ )+ if not isinstance(creator, str):+ raise CliError("metadata Creator must be a string")+ extra_kwargs = dict(savefig_kwargs or {})+ if "format" in extra_kwargs:+ raise CliError("savefig_kwargs must not override format")+ protected = {+ "dpi",+ "transparent",+ "bbox_inches",+ "pad_inches",+ "facecolor",+ "edgecolor",+ "metadata",+ }+ overlap = protected.intersection(extra_kwargs)+ if overlap:+ raise CliError(+ "savefig_kwargs must not override explicit settings: "+ + ", ".join(sorted(overlap))+ )++ figure_size = [float(value) for value in fig.get_size_inches()]+ outputs: list[dict[str, Any]] = []+ warnings_list: list[str] = []+ export_rc = _font_rc(font_mode)+ if bbox_inches is None:+ # Prevent a user's global savefig.bbox="tight" from changing page size.+ export_rc["savefig.bbox"] = None+ with mpl.rc_context(export_rc):+ for format_name in normalized_formats:+ destination = parent / f"{base.name}.{format_name}"+ checked_output_file(destination, force=overwrite)+ if format_name in {"eps", "ps"}:+ format_metadata = {"Creator": user_metadata["Creator"]}+ elif format_name in METADATA_FORMATS:+ format_metadata = user_metadata+ else:+ format_metadata = None+ if user_metadata and format_name not in METADATA_FORMATS:+ warnings_list.append(+ f"{format_name}: Matplotlib does not support savefig metadata "+ "for this format; metadata remains in the optional manifest."+ )+ save_kwargs: dict[str, Any] = {+ "dpi": float(dpi),+ "transparent": transparent,+ "bbox_inches": bbox_inches,+ "pad_inches": float(pad_inches),+ "facecolor": "none" if transparent else facecolor,+ "edgecolor": "none" if transparent else edgecolor,+ **extra_kwargs,+ }+ if format_metadata is not None:+ save_kwargs["metadata"] = format_metadata+ if format_name == "tiff":+ save_kwargs["pil_kwargs"] = {"compression": "tiff_lzw"}++ _atomic_savefig(+ fig,+ destination,+ format_name=format_name,+ save_kwargs=save_kwargs,+ overwrite=overwrite,+ )+ outputs.append(+ {+ "path": str(destination.resolve()),+ "format": format_name,+ "kind": (+ "vector-container"+ if format_name in VECTOR_FORMATS+ else "raster"+ ),+ "size_bytes": destination.stat().st_size,+ }+ )++ report: dict[str, Any] = {+ "schema_version": SCHEMA_VERSION,+ "outputs": outputs,+ "settings": {+ "formats": normalized_formats,+ "dpi": float(dpi),+ "transparent": transparent,+ "bbox_inches": bbox_inches,+ "pad_inches": float(pad_inches),+ "facecolor": facecolor,+ "font_mode": font_mode,+ "figure_size_inches": figure_size,},- 'ieee': {- 'line_art': {'formats': ['pdf', 'eps'], 'dpi': 600},- 'photo': {'formats': ['tiff'], 'dpi': 300},- 'combination': {'formats': ['pdf'], 'dpi': 300},+ "versions": {+ "matplotlib": mpl.__version__,+ "pillow": _package_version("Pillow"),},+ "provenance": provenance_document,+ "warnings": sorted(set(warnings_list)),+ "notice": (+ "Export completed with explicit settings. This report does not "+ "establish visual, accessibility, scientific-integrity, or "+ "journal-submission compliance."+ ),}- if journal not in journal_specs:- available = ', '.join(journal_specs.keys())- raise ValueError(f"Journal '{journal}' not recognized. Available: {available}")-- if figure_type not in journal_specs[journal]:- available = ', '.join(journal_specs[journal].keys())- raise ValueError(f"Figure type '{figure_type}' not valid. Available: {available}")+ if write_manifest:+ manifest_path = parent / f"{base.name}.export.json"+ emit_json(report, output=manifest_path, force=overwrite)+ report["manifest"] = str(manifest_path.resolve())+ return report- specs = journal_specs[journal][figure_type]- print(f"Saving for {journal.upper()} ({figure_type}):")- print(f" Formats: {', '.join(specs['formats'])}")- print(f" DPI: {specs['dpi']}")-- return save_publication_figure(- fig=fig,- filename=filename,- formats=specs['formats'],- dpi=specs['dpi']+def save_publication_figure(+ fig: Any,+ filename: str | os.PathLike[str],+ formats: Iterable[str] = ("pdf", "png"),+ dpi: float = 300,+ transparent: bool = False,+ bbox_inches: str | None = None,+ pad_inches: float = 0.1,+ facecolor: str = "white",+ *,+ overwrite: bool = False,+ provenance: dict[str, Any] | None = None,+ write_manifest: bool = False,+ **kwargs: Any,+) -> list[Path]:+ """Compatibility wrapper returning saved paths.++ Unlike the previous implementation, this function refuses implicit+ overwrite, does not silently swallow export errors, does not cap vector+ DPI, and preserves figure dimensions unless ``bbox_inches="tight"`` is+ requested.+ """+ report = export_figure(+ fig,+ filename,+ formats=formats,+ dpi=dpi,+ transparent=transparent,+ bbox_inches=bbox_inches,+ pad_inches=pad_inches,+ facecolor=facecolor,+ overwrite=overwrite,+ provenance=provenance,+ write_manifest=write_manifest,+ savefig_kwargs=kwargs,)+ return [Path(item["path"]) for item in report["outputs"]]-def check_figure_size(fig: plt.Figure, journal: str = 'nature') -> dict:- """- Check if figure dimensions are appropriate for journal requirements.-- Parameters- ----------- fig : matplotlib.figure.Figure- The figure to check- journal : str, default 'nature'- Journal name-- Returns- -------- dict- Dictionary with figure dimensions and compliance status-- Examples- --------- >>> fig = plt.figure(figsize=(3.5, 3))- >>> info = check_figure_size(fig, journal='nature')- >>> print(info)- """- journal = journal.lower()-- # Get figure dimensions in inches- width_inches, height_inches = fig.get_size_inches()+def _load_profiles() -> dict[str, Any]:+ profile_path = Path(__file__).resolve().parents[1] / "assets" / "publisher_profiles.json"+ try:+ document = json.loads(profile_path.read_text(encoding="utf-8"))+ except (OSError, json.JSONDecodeError) as exc:+ raise CliError(f"cannot load publisher profiles: {exc}") from exc+ if not isinstance(document.get("profiles"), dict):+ raise CliError("publisher profile asset is malformed")+ return document+++def check_figure_size(+ fig: Any,+ journal: str = "nature",+ *,+ tolerance_mm: float = 1.0,+) -> dict[str, Any]:+ """Compare figure dimensions with a dated profile without claiming compliance."""+ if not (tolerance_mm >= 0 and math.isfinite(tolerance_mm)):+ raise CliError("tolerance_mm must be finite and non-negative")+ document = _load_profiles()+ try:+ profile = document["profiles"][journal]+ except KeyError as exc:+ raise CliError(+ f"unknown profile {journal!r}; "+ f"available: {', '.join(sorted(document['profiles']))}"+ ) from exc+ width_inches, height_inches = (float(value) for value in fig.get_size_inches())width_mm = width_inches * 25.4height_mm = height_inches * 25.4-- # Journal specifications (widths in mm)- specs = {- 'nature': {'single': 89, 'double': 183, 'max_height': 247},- 'science': {'single': 55, 'double': 175, 'max_height': 233},- 'cell': {'single': 85, 'double': 178, 'max_height': 230},- 'plos': {'single': 83, 'double': 173, 'max_height': 233},- 'acs': {'single': 82.5, 'double': 178, 'max_height': 247},+ candidates = {+ name: float(value)+ for name, value in (profile.get("widths_mm") or {}).items()}-- if journal not in specs:- journal_spec = specs['nature']- print(f"Warning: Journal '{journal}' not found, using Nature specifications")- else:- journal_spec = specs[journal]-- # Determine column type- column_type = None- width_ok = False-- tolerance = 5 # mm tolerance- if abs(width_mm - journal_spec['single']) < tolerance:- column_type = 'single'- width_ok = True- elif abs(width_mm - journal_spec['double']) < tolerance:- column_type = 'double'- width_ok = True-- height_ok = height_mm <= journal_spec['max_height']-- result = {- 'width_inches': width_inches,- 'height_inches': height_inches,- 'width_mm': width_mm,- 'height_mm': height_mm,- 'journal': journal,- 'column_type': column_type,- 'width_ok': width_ok,- 'height_ok': height_ok,- 'compliant': width_ok and height_ok,- 'recommendations': {- 'single_column_mm': journal_spec['single'],- 'double_column_mm': journal_spec['double'],- 'max_height_mm': journal_spec['max_height'],- }+ nearest_name = None+ nearest_width = None+ difference = None+ if candidates:+ nearest_name, nearest_width = min(+ candidates.items(), key=lambda item: abs(width_mm - item[1])+ )+ difference = abs(width_mm - nearest_width)+ max_height = profile.get("max_height_mm")+ return {+ "schema_version": SCHEMA_VERSION,+ "profile": journal,+ "profile_label": profile["label"],+ "profile_scope": profile["scope"],+ "profile_accessed": document["accessed"],+ "figure": {+ "width_inches": width_inches,+ "height_inches": height_inches,+ "width_mm": width_mm,+ "height_mm": height_mm,+ },+ "nearest_profile_width": {+ "name": nearest_name,+ "width_mm": nearest_width,+ "difference_mm": difference,+ "matches_tolerance": (+ difference is not None and difference <= tolerance_mm+ ),+ },+ "height_screen": {+ "maximum_mm": max_height,+ "within_snapshot": (+ None if max_height is None else height_mm <= float(max_height)+ ),+ },+ "notice": (+ "A dimensional match to this dated snapshot is not a journal "+ "compliance determination."+ ),}- # Print report- print(f"\n{'='*60}")- print(f"Figure Size Check for {journal.upper()}")- print(f"{'='*60}")- print(f"Current size: {width_mm:.1f} × {height_mm:.1f} mm")- print(f" ({width_inches:.2f} × {height_inches:.2f} inches)")- print(f"\n{journal.upper()} specifications:")- print(f" Single column: {journal_spec['single']} mm")- print(f" Double column: {journal_spec['double']} mm")- print(f" Max height: {journal_spec['max_height']} mm")- print(f"\nCompliance:")- print(f" Width: {'✓ OK' if width_ok else '✗ Non-standard'} ({column_type or 'custom'})")- print(f" Height: {'✓ OK' if height_ok else '✗ Too tall'}")- print(f" Overall: {'✓ COMPLIANT' if result['compliant'] else '✗ NEEDS ADJUSTMENT'}")- print(f"{'='*60}\n")-- return result---def verify_font_embedding(pdf_path: Union[str, Path]) -> bool:- """- Check if fonts are embedded in a PDF file.-- Note: This requires PyPDF2 or a similar library to be installed.-- Parameters- ----------- pdf_path : str or Path- Path to PDF file- Returns- -------- bool- True if fonts are embedded, False otherwise+def save_for_journal(+ fig: Any,+ filename: str | os.PathLike[str],+ journal: str,+ figure_type: str = "combination",+ *,+ confirm_profile: bool = False,+ format_name: str | None = None,+ dpi: float | None = None,+ overwrite: bool = False,+) -> list[Path]:+ """Use a dated profile only after explicit caller confirmation.++ This compatibility helper intentionally requires ``confirm_profile=True``.+ Prefer ``export_plan.py`` and ``export_figure`` for an auditable workflow."""+ if not confirm_profile:+ raise CliError(+ "publisher rules are date-sensitive; run export_plan.py, verify the "+ "target journal, then pass confirm_profile=True if this snapshot is "+ "appropriate"+ )+ document = _load_profiles()try:- from PyPDF2 import PdfReader- except ImportError:- print("Warning: PyPDF2 not installed. Cannot verify font embedding.")- print("Install with: pip install PyPDF2")- return None+ profile = document["profiles"][journal]+ except KeyError as exc:+ raise CliError(f"unknown publisher profile: {journal!r}") from exc+ profile_formats = profile.get("formats") or {}+ allowed = profile_formats.get(figure_type) or []+ selected = (+ _normalize_formats([format_name])[0]+ if format_name is not None+ else next((name for name in allowed if name in SUPPORTED_FORMATS), None)+ )+ if selected is None:+ raise CliError(+ f"profile {journal!r} has no directly exportable {figure_type!r} "+ "format; choose settings manually"+ )+ if selected not in allowed:+ raise CliError(+ f"format {selected!r} is not recorded for {journal} "+ f"{figure_type}; recorded formats: {', '.join(allowed)}"+ )+ if dpi is None:+ raise CliError(+ "provide an explicit dpi after reviewing embedded raster content; "+ "the compatibility helper does not choose one automatically"+ )+ dpi_profile = (profile.get("raster_dpi") or {}).get(figure_type) or {}+ if dpi_profile.get("min") is not None and dpi < float(dpi_profile["min"]):+ raise CliError(f"dpi is below the profile snapshot minimum: {dpi_profile}")+ if (+ dpi_profile.get("min_exclusive") is not None+ and dpi <= float(dpi_profile["min_exclusive"])+ ):+ raise CliError(+ f"dpi does not exceed the profile snapshot threshold: {dpi_profile}"+ )+ if dpi_profile.get("max") is not None and dpi > float(dpi_profile["max"]):+ raise CliError(f"dpi is above the profile snapshot maximum: {dpi_profile}")+ return save_publication_figure(+ fig,+ filename,+ formats=[selected],+ dpi=dpi,+ overwrite=overwrite,+ provenance={+ "publisher_profile": journal,+ "profile_accessed": document["accessed"],+ "profile_scope": profile["scope"],+ "caller_confirmed_snapshot": True,+ },+ write_manifest=True,+ )- pdf_path = Path(pdf_path)+def verify_font_embedding(pdf_path: str | os.PathLike[str]) -> dict[str, Any]:+ """Return a conservative PDF font-resource report using the metadata tool."""try:- reader = PdfReader(pdf_path)- # This is a simplified check; full verification is complex- print(f"PDF has {len(reader.pages)} page(s)")- print("Note: Full font embedding verification requires detailed PDF inspection.")- return True- except Exception as e:- print(f"Error reading PDF: {e}")- return False+ from image_metadata import inspect_file+ except ImportError as exc:+ raise CliError(f"cannot import image_metadata.py: {exc}") from exc+ report = inspect_file(pdf_path)+ if report["metadata"]["format"] != "PDF":+ raise CliError("font embedding inspection requires a PDF input")+ return {+ "path": report["input"]["path"],+ "font_resources": report["metadata"].get("font_resources"),+ "notice": (+ "Inspection covers declared first-page font resources only; "+ "publisher preflight may apply additional checks."+ ),+ }-if __name__ == "__main__":- # Example usage- import numpy as np-- # Create example figure- fig, ax = plt.subplots(figsize=(3.5, 2.5))- x = np.linspace(0, 10, 100)- ax.plot(x, np.sin(x), label='sin(x)')- ax.plot(x, np.cos(x), label='cos(x)')- ax.set_xlabel('x')- ax.set_ylabel('y')+def _demo_figure() -> Any:+ try:+ import matplotlib.pyplot as plt+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required for --demo; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc+ x_values = [index / 20.0 for index in range(121)]+ first = [math.sin(value) for value in x_values]+ second = [math.cos(value) for value in x_values]+ fig, ax = plt.subplots(figsize=(3.5, 2.5), layout="constrained")+ ax.plot(x_values, first, label="sin(x)", marker="o", markevery=20)+ ax.plot(+ x_values,+ second,+ label="cos(x)",+ linestyle="--",+ marker="s",+ markevery=20,+ )+ ax.axhline(0, color="0.35", linewidth=0.7)+ ax.set(xlabel="Angle (radians)", ylabel="Amplitude (unitless)")ax.legend()- ax.spines['top'].set_visible(False)- ax.spines['right'].set_visible(False)+ return fig+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Render a deterministic Matplotlib export smoke-test. Programmatic "+ "helpers in this module support explicit, safe figure export."+ )+ )+ parser.add_argument(+ "--demo",+ metavar="BASE_PATH",+ help="render a deterministic demo to this base path",+ )+ parser.add_argument(+ "--formats",+ default="pdf,png",+ help="comma-separated formats (default: pdf,png)",+ )+ parser.add_argument("--dpi", type=positive_float, default=300.0)+ parser.add_argument(+ "--font-mode",+ choices=("truetype", "paths", "current"),+ default="truetype",+ )+ parser.add_argument("--transparent", action="store_true")+ parser.add_argument(+ "--tight",+ action="store_true",+ help="use bbox_inches='tight' (changes physical page dimensions)",+ )+ parser.add_argument("--manifest", action="store_true")+ parser.add_argument("--force", action="store_true")+ parser.add_argument(+ "--style",+ choices=("default", "nature", "science", "cell", "minimal", "presentation"),+ default="default",+ )+ parser.add_argument("--palette", default="okabe_ito_on_white")+ return parser- # Check size- check_figure_size(fig, journal='nature')- # Save in multiple formats- print("\nSaving figure...")- save_publication_figure(fig, 'example_figure', formats=['pdf', 'png'], dpi=300)+def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ try:+ args = parser.parse_args(argv)+ if not args.demo:+ parser.error("--demo BASE_PATH is required")+ try:+ from style_presets import style_context+ except ImportError as exc:+ raise CliError(f"cannot import style_presets.py: {exc}") from exc+ with style_context(args.style, palette_name=args.palette):+ fig = _demo_figure()+ try:+ report = export_figure(+ fig,+ args.demo,+ formats=args.formats.split(","),+ dpi=args.dpi,+ transparent=args.transparent,+ bbox_inches="tight" if args.tight else None,+ font_mode=args.font_mode,+ overwrite=args.force,+ provenance={+ "purpose": "deterministic export smoke test",+ "raw_data": "analytical sin/cos values generated in code",+ "transformations": ["none"],+ },+ write_manifest=args.manifest,+ )+ finally:+ import matplotlib.pyplot as plt++ plt.close(fig)+ emit_json(report)+ return 0+ except CliError as exc:+ parser.exit(2, f"error: {exc}\n")- # Save with journal-specific requirements- print("\nSaving for Nature...")- save_for_journal(fig, 'example_figure_nature', journal='nature', figure_type='line_art')- plt.close(fig)+if __name__ == "__main__":+ sys.exit(main())diff --git a/skills/scientific-visualization/scripts/image_metadata.py b/skills/scientific-visualization/scripts/image_metadata.pynew file mode 100644index 00000000..5541e518--- /dev/null+++ b/skills/scientific-visualization/scripts/image_metadata.py@@ -0,0 +1,731 @@+#!/usr/bin/env python3+"""Inspect figure/image metadata and screen it against explicit constraints.++The tool is network-free. Pillow is loaded only for raster inputs and pypdf is+loaded only for PDF inputs. A successful screen is not a journal-compliance+claim.+"""++from __future__ import annotations++import argparse+import math+import re+import sys+import warnings+import xml.etree.ElementTree as ET+from pathlib import Path+from typing import Any, Iterable++from _common import (+ MAX_INPUT_BYTES,+ CliError,+ checked_input_file,+ emit_json,+ positive_float,+ positive_int,+)++SCHEMA_VERSION = "1.0"+DEFAULT_MAX_PIXELS = 100_000_000+MAX_XML_ELEMENTS = 100_000+MAX_SVG_BYTES = 20 * 1024 * 1024+FORMAT_ALIASES = {+ "jpg": "JPEG",+ "jpeg": "JPEG",+ "tif": "TIFF",+ "tiff": "TIFF",+ "svg": "SVG",+ "pdf": "PDF",+ "eps": "EPS",+ "ps": "PS",+ "png": "PNG",+ "webp": "WEBP",+}+VECTOR_SUFFIXES = {".svg", ".pdf", ".eps", ".ps"}+LENGTH_RE = re.compile(+ r"^\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)"+ r"\s*(px|pt|pc|in|cm|mm)?\s*$"+)+++def _finite_number(value: Any) -> float | None:+ """Convert a scalar or rational-like value to a finite float."""+ try:+ number = float(value)+ except (TypeError, ValueError, OverflowError):+ return None+ return number if math.isfinite(number) else None+++def _dpi_pair(value: Any) -> tuple[float | None, float | None]:+ """Normalize common Pillow DPI metadata representations."""+ if isinstance(value, (tuple, list)) and len(value) >= 2:+ return _finite_number(value[0]), _finite_number(value[1])+ scalar = _finite_number(value)+ return scalar, scalar+++def _length_to_inches(value: str | None) -> tuple[float | None, str | None]:+ """Parse an SVG/CSS absolute length using the CSS 96 px/in convention."""+ if value is None:+ return None, None+ match = LENGTH_RE.match(value)+ if not match:+ return None, None+ number = float(match.group(1))+ unit = (match.group(2) or "px").lower()+ factors = {+ "px": 1.0 / 96.0,+ "pt": 1.0 / 72.0,+ "pc": 1.0 / 6.0,+ "in": 1.0,+ "cm": 1.0 / 2.54,+ "mm": 1.0 / 25.4,+ }+ return number * factors[unit], unit+++def _base_metadata(path: Path) -> dict[str, Any]:+ info = path.stat()+ return {+ "path": str(path),+ "name": path.name,+ "suffix": path.suffix.lower(),+ "size_bytes": info.st_size,+ }+++def inspect_raster(path: Path, *, max_pixels: int) -> dict[str, Any]:+ """Inspect a raster image with Pillow without decoding all pixel data."""+ try:+ from PIL import Image+ except ImportError as exc:+ raise CliError(+ "Pillow is required for raster metadata; "+ "run with --with 'pillow==12.3.0'"+ ) from exc++ previous_limit = Image.MAX_IMAGE_PIXELS+ Image.MAX_IMAGE_PIXELS = max_pixels+ try:+ with warnings.catch_warnings():+ warnings.simplefilter("error", Image.DecompressionBombWarning)+ with Image.open(path) as image:+ width, height = image.size+ if width * height > max_pixels:+ raise CliError(+ f"image has {width * height} pixels; "+ f"limit is {max_pixels}"+ )+ dpi_x, dpi_y = _dpi_pair(image.info.get("dpi"))+ icc = image.info.get("icc_profile")+ metadata = {+ "format": (image.format or path.suffix.lstrip(".")).upper(),+ "kind": "raster",+ "width_px": int(width),+ "height_px": int(height),+ "pixel_count": int(width * height),+ "mode": image.mode,+ "bands": list(image.getbands()),+ "has_alpha": "A" in image.getbands()+ or "transparency" in image.info,+ "frames": int(getattr(image, "n_frames", 1)),+ "dpi_x": dpi_x,+ "dpi_y": dpi_y,+ "width_mm_from_metadata": (+ width / dpi_x * 25.4 if dpi_x and dpi_x > 0 else None+ ),+ "height_mm_from_metadata": (+ height / dpi_y * 25.4 if dpi_y and dpi_y > 0 else None+ ),+ "icc_profile_present": bool(icc),+ "icc_profile_bytes": len(icc) if isinstance(icc, bytes) else 0,+ "compression": image.info.get("compression"),+ "exif_present": bool(image.info.get("exif")),+ }+ image.verify()+ return metadata+ except Image.DecompressionBombWarning as exc:+ raise CliError(f"image exceeds safe pixel limits: {exc}") from exc+ except Image.DecompressionBombError as exc:+ raise CliError(f"image exceeds safe pixel limits: {exc}") from exc+ except OSError as exc:+ raise CliError(f"Pillow could not inspect {path}: {exc}") from exc+ finally:+ Image.MAX_IMAGE_PIXELS = previous_limit+++def inspect_svg(path: Path) -> dict[str, Any]:+ """Inspect SVG dimensions and resource types with bounded XML parsing."""+ if path.stat().st_size > MAX_SVG_BYTES:+ raise CliError(+ f"SVG is {path.stat().st_size} bytes; limit is {MAX_SVG_BYTES} bytes"+ )+ try:+ payload = path.read_bytes()+ if re.search(br"<!ENTITY\b", payload, flags=re.IGNORECASE) or re.search(+ br"<!DOCTYPE[^>]*\[", payload, flags=re.IGNORECASE | re.DOTALL+ ):+ raise CliError("SVG internal DTD/entity declarations are not allowed")+ root = ET.fromstring(payload)+ except (ET.ParseError, OSError) as exc:+ raise CliError(f"invalid SVG/XML input: {exc}") from exc++ width_raw = root.get("width")+ height_raw = root.get("height")+ width_in, width_unit = _length_to_inches(width_raw)+ height_in, height_unit = _length_to_inches(height_raw)+ viewbox_raw = root.get("viewBox")+ viewbox: list[float] | None = None+ if viewbox_raw:+ try:+ values = [float(item) for item in re.split(r"[\s,]+", viewbox_raw.strip())]+ except ValueError:+ values = []+ if len(values) == 4 and all(math.isfinite(item) for item in values):+ viewbox = values++ element_count = 0+ text_count = 0+ image_count = 0+ external_images = 0+ for element in root.iter():+ element_count += 1+ if element_count > MAX_XML_ELEMENTS:+ raise CliError(+ f"SVG has more than {MAX_XML_ELEMENTS} elements; refusing input"+ )+ local_name = element.tag.rsplit("}", 1)[-1].lower()+ if local_name == "text":+ text_count += 1+ elif local_name == "image":+ image_count += 1+ href = (+ element.get("href")+ or element.get("{http://www.w3.org/1999/xlink}href")+ or ""+ )+ if href and not href.startswith("data:"):+ external_images += 1++ return {+ "format": "SVG",+ "kind": "vector",+ "width_raw": width_raw,+ "height_raw": height_raw,+ "width_unit": width_unit,+ "height_unit": height_unit,+ "width_mm": width_in * 25.4 if width_in is not None else None,+ "height_mm": height_in * 25.4 if height_in is not None else None,+ "viewbox": viewbox,+ "dpi_x": None,+ "dpi_y": None,+ "mode": None,+ "element_count": element_count,+ "text_element_count": text_count,+ "image_element_count": image_count,+ "external_image_count": external_images,+ "note": (+ "SVG is resolution-independent except for embedded raster images; "+ "external image resources were counted but not fetched."+ ),+ }+++def _pdf_font_report(page: Any) -> dict[str, Any]:+ """Return a conservative first-page PDF font resource report."""+ try:+ resources = page.get("/Resources")+ resources = resources.get_object() if resources else None+ fonts = resources.get("/Font") if resources else None+ fonts = fonts.get_object() if fonts else None+ except Exception:+ fonts = None+ if not fonts:+ return {+ "resource_count": 0,+ "embedded_count": 0,+ "unembedded_count": 0,+ "unknown_count": 0,+ "all_embedded": None,+ "fonts": [],+ }++ def embedded_status(font: Any) -> bool | None:+ try:+ subtype = str(font.get("/Subtype", ""))+ if subtype == "/Type3":+ # Type 3 glyph programs live in the PDF CharProcs dictionary.+ return True+ candidates = [font]+ descendants = font.get("/DescendantFonts")+ if descendants:+ candidates.extend(+ reference.get_object() for reference in descendants+ )+ saw_descriptor = False+ for candidate in candidates:+ descriptor = candidate.get("/FontDescriptor")+ descriptor = descriptor.get_object() if descriptor else None+ if descriptor:+ saw_descriptor = True+ if any(+ descriptor.get(key)+ for key in ("/FontFile", "/FontFile2", "/FontFile3")+ ):+ return True+ return False if saw_descriptor else None+ except Exception:+ return None++ records: list[dict[str, Any]] = []+ for resource_name, reference in fonts.items():+ try:+ font = reference.get_object()+ embedded = embedded_status(font)+ records.append(+ {+ "resource": str(resource_name),+ "base_font": str(font.get("/BaseFont", "")),+ "subtype": str(font.get("/Subtype", "")),+ "embedded": embedded,+ }+ )+ except Exception:+ records.append(+ {+ "resource": str(resource_name),+ "base_font": "",+ "subtype": "",+ "embedded": None,+ }+ )+ definite = [item["embedded"] for item in records if item["embedded"] is not None]+ unknown_count = sum(item["embedded"] is None for item in records)+ return {+ "resource_count": len(records),+ "embedded_count": sum(item is True for item in definite),+ "unembedded_count": sum(item is False for item in definite),+ "unknown_count": unknown_count,+ "all_embedded": (+ None+ if not definite or unknown_count+ else all(item is True for item in definite)+ ),+ "fonts": records,+ "scope": "first page resource dictionary only",+ }+++def inspect_pdf(path: Path) -> dict[str, Any]:+ """Inspect PDF page dimensions and first-page font resources with pypdf."""+ try:+ from pypdf import PdfReader+ except ImportError as exc:+ raise CliError(+ "pypdf is required for PDF metadata; "+ "run with --with 'pypdf==6.14.2'"+ ) from exc+ try:+ reader = PdfReader(path, strict=False)+ if reader.is_encrypted:+ try:+ unlocked = reader.decrypt("")+ except Exception:+ unlocked = 0+ if not unlocked:+ return {+ "format": "PDF",+ "kind": "vector-container",+ "encrypted": True,+ "page_count": None,+ "width_mm": None,+ "height_mm": None,+ "dpi_x": None,+ "dpi_y": None,+ "mode": None,+ "font_resources": None,+ "note": "Encrypted PDF could not be inspected without a password.",+ }+ page_count = len(reader.pages)+ if page_count < 1:+ raise CliError("PDF has no pages")+ page = reader.pages[0]+ width_pt = float(page.mediabox.width)+ height_pt = float(page.mediabox.height)+ return {+ "format": "PDF",+ "kind": "vector-container",+ "encrypted": bool(reader.is_encrypted),+ "page_count": page_count,+ "first_page_width_pt": width_pt,+ "first_page_height_pt": height_pt,+ "width_mm": width_pt / 72.0 * 25.4,+ "height_mm": height_pt / 72.0 * 25.4,+ "dpi_x": None,+ "dpi_y": None,+ "mode": None,+ "font_resources": _pdf_font_report(page),+ "note": (+ "PDF page size and font resources do not establish the "+ "resolution of embedded raster images."+ ),+ }+ except CliError:+ raise+ except Exception as exc:+ raise CliError(f"pypdf could not inspect {path}: {exc}") from exc+++def inspect_eps(path: Path) -> dict[str, Any]:+ """Inspect an EPS/PS bounding box without invoking PostScript."""+ size = path.stat().st_size+ with path.open("rb") as handle:+ prefix = handle.read(min(size, 128 * 1024))+ suffix = b""+ if size > len(prefix):+ handle.seek(max(0, size - 128 * 1024))+ suffix = handle.read(128 * 1024)+ text = (prefix + b"\n" + suffix).decode("latin-1", errors="replace")+ matches = re.findall(+ r"^%%(?:HiRes)?BoundingBox:\s+"+ r"([+-]?\d+(?:\.\d+)?)\s+([+-]?\d+(?:\.\d+)?)\s+"+ r"([+-]?\d+(?:\.\d+)?)\s+([+-]?\d+(?:\.\d+)?)\s*$",+ text,+ flags=re.MULTILINE,+ )+ bbox = [float(value) for value in matches[-1]] if matches else None+ width_pt = bbox[2] - bbox[0] if bbox else None+ height_pt = bbox[3] - bbox[1] if bbox else None+ return {+ "format": "EPS" if path.suffix.lower() == ".eps" else "PS",+ "kind": "vector-container",+ "bounding_box_pt": bbox,+ "width_mm": width_pt / 72.0 * 25.4 if width_pt is not None else None,+ "height_mm": (+ height_pt / 72.0 * 25.4 if height_pt is not None else None+ ),+ "dpi_x": None,+ "dpi_y": None,+ "mode": None,+ "note": "PostScript was not executed; only DSC bounding-box text was read.",+ }+++def inspect_file(+ value: str | Path,+ *,+ max_bytes: int = MAX_INPUT_BYTES,+ max_pixels: int = DEFAULT_MAX_PIXELS,+) -> dict[str, Any]:+ """Inspect a supported local figure/image and return structured metadata."""+ path = checked_input_file(value, max_bytes=max_bytes)+ suffix = path.suffix.lower()+ if suffix == ".svg":+ metadata = inspect_svg(path)+ elif suffix == ".pdf":+ metadata = inspect_pdf(path)+ elif suffix in {".eps", ".ps"}:+ metadata = inspect_eps(path)+ else:+ metadata = inspect_raster(path, max_pixels=max_pixels)+ return {+ "schema_version": SCHEMA_VERSION,+ "input": _base_metadata(path),+ "metadata": metadata,+ }+++def _normalize_formats(values: Iterable[str]) -> list[str]:+ normalized: list[str] = []+ for value in values:+ for item in value.split(","):+ name = item.strip().lower().lstrip(".")+ if not name:+ continue+ normalized.append(FORMAT_ALIASES.get(name, name.upper()))+ return sorted(set(normalized))+++def _check(+ name: str,+ status: str,+ *,+ actual: Any,+ expected: Any,+ detail: str,+) -> dict[str, Any]:+ return {+ "name": name,+ "status": status,+ "actual": actual,+ "expected": expected,+ "detail": detail,+ }+++def screen_metadata(+ report: dict[str, Any],+ *,+ expected_formats: Iterable[str] = (),+ expected_modes: Iterable[str] = (),+ min_dpi: float | None = None,+ max_dpi: float | None = None,+ min_width_px: int | None = None,+ max_width_px: int | None = None,+ min_height_px: int | None = None,+ max_height_px: int | None = None,+ max_file_bytes: int | None = None,+ target_width_mm: float | None = None,+ alpha_policy: str | None = None,+) -> dict[str, Any]:+ """Apply only caller-supplied constraints to an inspection report."""+ metadata = report["metadata"]+ checks: list[dict[str, Any]] = []++ formats = _normalize_formats(expected_formats)+ if formats:+ actual = str(metadata.get("format", "")).upper()+ checks.append(+ _check(+ "format",+ "pass" if actual in formats else "fail",+ actual=actual,+ expected=formats,+ detail="File format is compared with the explicit allow-list.",+ )+ )++ modes = sorted(+ {+ item.strip().upper()+ for value in expected_modes+ for item in value.split(",")+ if item.strip()+ }+ )+ if modes:+ actual_mode = metadata.get("mode")+ status = (+ "unknown"+ if actual_mode is None+ else ("pass" if str(actual_mode).upper() in modes else "fail")+ )+ checks.append(+ _check(+ "mode",+ status,+ actual=actual_mode,+ expected=modes,+ detail="Vector containers generally do not expose one image mode.",+ )+ )++ width_px = metadata.get("width_px")+ height_px = metadata.get("height_px")+ for name, actual, minimum, maximum in (+ ("width_px", width_px, min_width_px, max_width_px),+ ("height_px", height_px, min_height_px, max_height_px),+ ):+ if minimum is None and maximum is None:+ continue+ if actual is None:+ status = "unknown"+ elif minimum is not None and actual < minimum:+ status = "fail"+ elif maximum is not None and actual > maximum:+ status = "fail"+ else:+ status = "pass"+ checks.append(+ _check(+ name,+ status,+ actual=actual,+ expected={"min": minimum, "max": maximum},+ detail="Pixel dimensions are available for raster inputs.",+ )+ )++ dpi_x = metadata.get("dpi_x")+ dpi_y = metadata.get("dpi_y")+ effective_dpi = None+ if target_width_mm is not None and width_px is not None:+ effective_dpi = width_px / (target_width_mm / 25.4)+ actual_dpi = effective_dpi+ dpi_basis = "effective at target width"+ if actual_dpi is None and dpi_x and dpi_y:+ actual_dpi = min(float(dpi_x), float(dpi_y))+ dpi_basis = "embedded metadata"+ if min_dpi is not None or max_dpi is not None:+ if actual_dpi is None:+ status = "unknown"+ elif (+ min_dpi is not None+ and actual_dpi < min_dpi+ and not math.isclose(actual_dpi, min_dpi, rel_tol=1e-12, abs_tol=1e-9)+ ):+ status = "fail"+ elif (+ max_dpi is not None+ and actual_dpi > max_dpi+ and not math.isclose(actual_dpi, max_dpi, rel_tol=1e-12, abs_tol=1e-9)+ ):+ status = "fail"+ else:+ status = "pass"+ checks.append(+ _check(+ "dpi",+ status,+ actual=actual_dpi,+ expected={"min": min_dpi, "max": max_dpi},+ detail=(+ f"DPI basis: {dpi_basis}. Metadata DPI is not a quality "+ "measure; effective DPI depends on final physical size."+ ),+ )+ )++ if max_file_bytes is not None:+ actual_bytes = report["input"]["size_bytes"]+ checks.append(+ _check(+ "file_size",+ "pass" if actual_bytes <= max_file_bytes else "fail",+ actual=actual_bytes,+ expected={"max": max_file_bytes},+ detail="File size is measured from the local input.",+ )+ )++ if alpha_policy is not None:+ if alpha_policy not in {"allow", "forbid", "require"}:+ raise CliError("alpha_policy must be allow, forbid, or require")+ has_alpha = metadata.get("has_alpha")+ if has_alpha is None:+ alpha_status = "unknown"+ elif alpha_policy == "allow":+ alpha_status = "pass"+ elif alpha_policy == "forbid":+ alpha_status = "pass" if not has_alpha else "fail"+ else:+ alpha_status = "pass" if has_alpha else "fail"+ checks.append(+ _check(+ "alpha",+ alpha_status,+ actual=has_alpha,+ expected=alpha_policy,+ detail=(+ "Alpha is detected for raster modes/transparency metadata. "+ "Vector transparency requires rendered-content inspection."+ ),+ )+ )++ counts = {+ status: sum(item["status"] == status for item in checks)+ for status in ("pass", "fail", "warning", "unknown")+ }+ screened = dict(report)+ screened["checks"] = checks+ screened["screening_summary"] = counts+ screened["notice"] = (+ "This deterministic metadata screen checks only the supplied constraints. "+ "It does not verify visual quality, scientific integrity, accessibility, "+ "embedded-raster resolution in vector files, or journal compliance."+ )+ return screened+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Inspect local raster, SVG, PDF, EPS, or PS metadata and optionally "+ "screen it against explicit constraints. No network access is used."+ )+ )+ parser.add_argument("input", help="local figure/image file (symlinks rejected)")+ parser.add_argument(+ "--format",+ dest="formats",+ action="append",+ default=[],+ help="allowed format; repeat or use comma-separated values",+ )+ parser.add_argument(+ "--mode",+ dest="modes",+ action="append",+ default=[],+ help="allowed raster mode (for example RGB,L); repeat as needed",+ )+ parser.add_argument("--min-dpi", type=positive_float)+ parser.add_argument("--max-dpi", type=positive_float)+ parser.add_argument(+ "--target-width-mm",+ type=positive_float,+ help="calculate effective raster DPI at this final width",+ )+ parser.add_argument("--min-width-px", type=positive_int)+ parser.add_argument("--max-width-px", type=positive_int)+ parser.add_argument("--min-height-px", type=positive_int)+ parser.add_argument("--max-height-px", type=positive_int)+ parser.add_argument("--max-file-bytes", type=positive_int)+ parser.add_argument(+ "--alpha-policy",+ choices=("allow", "forbid", "require"),+ help="optional raster alpha/transparency screen",+ )+ parser.add_argument(+ "--max-input-bytes",+ type=positive_int,+ default=MAX_INPUT_BYTES,+ help=f"input byte limit (default: {MAX_INPUT_BYTES})",+ )+ parser.add_argument(+ "--max-pixels",+ type=positive_int,+ default=DEFAULT_MAX_PIXELS,+ help=f"raster pixel limit (default: {DEFAULT_MAX_PIXELS})",+ )+ parser.add_argument("--output", help="optional JSON report path")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing JSON report"+ )+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ try:+ args = parser.parse_args(argv)+ report = inspect_file(+ args.input,+ max_bytes=args.max_input_bytes,+ max_pixels=args.max_pixels,+ )+ report = screen_metadata(+ report,+ expected_formats=args.formats,+ expected_modes=args.modes,+ min_dpi=args.min_dpi,+ max_dpi=args.max_dpi,+ min_width_px=args.min_width_px,+ max_width_px=args.max_width_px,+ min_height_px=args.min_height_px,+ max_height_px=args.max_height_px,+ max_file_bytes=args.max_file_bytes,+ target_width_mm=args.target_width_mm,+ alpha_policy=args.alpha_policy,+ )+ emit_json(report, output=args.output, force=args.force)+ return 1 if report["screening_summary"]["fail"] else 0+ except CliError as exc:+ parser.exit(2, f"error: {exc}\n")+++if __name__ == "__main__":+ sys.exit(main())diff --git a/skills/scientific-visualization/scripts/palette_audit.py b/skills/scientific-visualization/scripts/palette_audit.pynew file mode 100644index 00000000..e12e158d--- /dev/null+++ b/skills/scientific-visualization/scripts/palette_audit.py@@ -0,0 +1,327 @@+#!/usr/bin/env python3+"""Audit palette contrast and heuristic grayscale distinguishability.++WCAG contrast calculations are exact for the supplied sRGB values. Whether a+specific WCAG success criterion applies depends on how a color is used. The+grayscale delta-L* threshold is a screening heuristic, not a standard.+"""++from __future__ import annotations++import argparse+import importlib.util+import itertools+import math+import sys+from pathlib import Path+from types import ModuleType+from typing import Any, Iterable++from _common import CliError, emit_json, positive_float++SCHEMA_VERSION = "1.0"+DEFAULT_BACKGROUND = "#FFFFFF"+DEFAULT_GRAYSCALE_DELTA = 10.0+ROLE_THRESHOLDS = {+ "graphical": 3.0,+ "large-text": 3.0,+ "normal-text": 4.5,+ "enhanced-text": 7.0,+}+++def parse_hex_color(value: str) -> tuple[int, int, int]:+ """Parse an opaque six-digit sRGB color."""+ normalized = value.strip().lstrip("#")+ if len(normalized) != 6:+ raise CliError(f"expected six-digit hex color, got {value!r}")+ try:+ channels = tuple(int(normalized[index : index + 2], 16) for index in (0, 2, 4))+ except ValueError as exc:+ raise CliError(f"invalid hex color: {value!r}") from exc+ return channels # type: ignore[return-value]+++def canonical_hex(rgb: tuple[int, int, int]) -> str:+ return "#" + "".join(f"{channel:02X}" for channel in rgb)+++def _linear_channel(channel: int) -> float:+ encoded = channel / 255.0+ return encoded / 12.92 if encoded <= 0.04045 else ((encoded + 0.055) / 1.055) ** 2.4+++def relative_luminance(rgb: tuple[int, int, int]) -> float:+ """Return WCAG relative luminance for an sRGB color."""+ red, green, blue = (_linear_channel(channel) for channel in rgb)+ return 0.2126 * red + 0.7152 * green + 0.0722 * blue+++def contrast_ratio(+ first: tuple[int, int, int], second: tuple[int, int, int]+) -> float:+ """Return WCAG contrast ratio for two opaque sRGB colors."""+ first_luminance = relative_luminance(first)+ second_luminance = relative_luminance(second)+ lighter = max(first_luminance, second_luminance)+ darker = min(first_luminance, second_luminance)+ return (lighter + 0.05) / (darker + 0.05)+++def cie_lstar(rgb: tuple[int, int, int]) -> float:+ """Return CIE L* from sRGB/D65, used for grayscale screening."""+ luminance = relative_luminance(rgb)+ delta = 6.0 / 29.0+ if luminance > delta**3:+ transformed = luminance ** (1.0 / 3.0)+ else:+ transformed = luminance / (3.0 * delta**2) + 4.0 / 29.0+ return 116.0 * transformed - 16.0+++def _load_palette_asset() -> ModuleType:+ asset = Path(__file__).resolve().parents[1] / "assets" / "color_palettes.py"+ spec = importlib.util.spec_from_file_location(+ "_scientific_visualization_color_palettes", asset+ )+ if spec is None or spec.loader is None:+ raise CliError(f"cannot load bundled palette asset: {asset}")+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++def bundled_palettes() -> dict[str, list[str]]:+ """Load bundled palettes without importing Matplotlib."""+ module = _load_palette_asset()+ palettes = getattr(module, "PALETTES", None)+ if not isinstance(palettes, dict):+ raise CliError("bundled color_palettes.py does not define PALETTES")+ normalized: dict[str, list[str]] = {}+ for name, colors in palettes.items():+ if isinstance(name, str) and isinstance(colors, (list, tuple)):+ normalized[name] = [canonical_hex(parse_hex_color(item)) for item in colors]+ return normalized+++def audit_palette(+ colors: Iterable[str],+ *,+ background: str = DEFAULT_BACKGROUND,+ role: str = "graphical",+ contrast_threshold: float | None = None,+ grayscale_min_delta: float = DEFAULT_GRAYSCALE_DELTA,+ name: str | None = None,+) -> dict[str, Any]:+ """Return deterministic palette contrast and grayscale screening results."""+ if role not in ROLE_THRESHOLDS and contrast_threshold is None:+ raise CliError(+ f"unknown role {role!r}; provide an explicit contrast threshold"+ )+ threshold = (+ float(contrast_threshold)+ if contrast_threshold is not None+ else ROLE_THRESHOLDS[role]+ )+ if not math.isfinite(threshold) or threshold <= 1:+ raise CliError("contrast threshold must be finite and greater than 1")+ if not math.isfinite(grayscale_min_delta) or grayscale_min_delta <= 0:+ raise CliError("grayscale delta threshold must be positive and finite")++ canonical = [canonical_hex(parse_hex_color(color)) for color in colors]+ if not canonical:+ raise CliError("at least one color is required")+ if len(canonical) > 32:+ raise CliError("palette is limited to 32 colors")+ background_rgb = parse_hex_color(background)+ background_hex = canonical_hex(background_rgb)++ color_records: list[dict[str, Any]] = []+ parsed: list[tuple[int, int, int]] = []+ for index, color in enumerate(canonical):+ rgb = parse_hex_color(color)+ parsed.append(rgb)+ ratio = contrast_ratio(rgb, background_rgb)+ color_records.append(+ {+ "index": index,+ "hex": color,+ "rgb": list(rgb),+ "relative_luminance": round(relative_luminance(rgb), 6),+ "cie_lstar": round(cie_lstar(rgb), 3),+ "contrast_against_background": round(ratio, 3),+ "background_screen": "pass" if ratio >= threshold else "review",+ }+ )++ pair_records: list[dict[str, Any]] = []+ for first, second in itertools.combinations(range(len(parsed)), 2):+ first_rgb = parsed[first]+ second_rgb = parsed[second]+ delta = abs(cie_lstar(first_rgb) - cie_lstar(second_rgb))+ pair_records.append(+ {+ "first_index": first,+ "second_index": second,+ "first_hex": canonical[first],+ "second_hex": canonical[second],+ "contrast_ratio": round(contrast_ratio(first_rgb, second_rgb), 3),+ "grayscale_delta_lstar": round(delta, 3),+ "grayscale_screen": (+ "pass" if delta >= grayscale_min_delta else "review"+ ),+ }+ )++ background_reviews = sum(+ item["background_screen"] == "review" for item in color_records+ )+ grayscale_reviews = sum(+ item["grayscale_screen"] == "review" for item in pair_records+ )+ return {+ "schema_version": SCHEMA_VERSION,+ "palette": {+ "name": name,+ "colors": color_records,+ "color_count": len(color_records),+ },+ "background": background_hex,+ "contrast_screen": {+ "role": role,+ "threshold": threshold,+ "review_count": background_reviews,+ "basis": (+ "WCAG 2.2 contrast mathematics. Applicability depends on use; "+ "for example, SC 1.4.11 covers graphical objects required to "+ "understand web content."+ ),+ },+ "grayscale_screen": {+ "minimum_delta_lstar": grayscale_min_delta,+ "review_count": grayscale_reviews,+ "pair_count": len(pair_records),+ "basis": (+ "Heuristic CIE L* separation after removing hue. It does not "+ "predict all viewing, printing, or color-vision conditions."+ ),+ },+ "pairs": pair_records,+ "notice": (+ "No palette can establish accessibility by itself. Preserve labels "+ "and add redundant encodings such as markers, line styles, direct "+ "labels, or patterns; evaluate the rendered figure in context."+ ),+ }+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Audit sRGB palette contrast against a background and screen "+ "pairwise grayscale distinguishability. No network is used."+ )+ )+ source = parser.add_mutually_exclusive_group(required=False)+ source.add_argument(+ "--palette", help="bundled palette name (use --list-palettes)"+ )+ source.add_argument(+ "--color",+ action="append",+ default=[],+ help="six-digit hex color; repeat for each palette color",+ )+ parser.add_argument(+ "--list-palettes",+ action="store_true",+ help="print bundled palette names and exit",+ )+ parser.add_argument("--background", default=DEFAULT_BACKGROUND)+ parser.add_argument(+ "--role",+ choices=sorted(ROLE_THRESHOLDS),+ default="graphical",+ help="select the WCAG contrast screening threshold",+ )+ parser.add_argument(+ "--contrast-threshold",+ type=positive_float,+ help="override the role threshold",+ )+ parser.add_argument(+ "--grayscale-min-delta",+ type=positive_float,+ default=DEFAULT_GRAYSCALE_DELTA,+ help=(+ "heuristic minimum CIE L* separation "+ f"(default: {DEFAULT_GRAYSCALE_DELTA})"+ ),+ )+ parser.add_argument(+ "--fail-on",+ choices=("none", "background", "grayscale", "any"),+ default="none",+ help="return exit 1 for selected review findings (default: none)",+ )+ parser.add_argument("--output", help="optional JSON report path")+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing JSON report"+ )+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ try:+ args = parser.parse_args(argv)+ palettes = bundled_palettes()+ if args.list_palettes:+ emit_json(+ {+ "schema_version": SCHEMA_VERSION,+ "palettes": {+ name: {"count": len(colors), "colors": colors}+ for name, colors in sorted(palettes.items())+ },+ },+ output=args.output,+ force=args.force,+ )+ return 0+ if args.palette:+ if args.palette not in palettes:+ raise CliError(+ f"unknown palette {args.palette!r}; "+ f"available: {', '.join(sorted(palettes))}"+ )+ colors = palettes[args.palette]+ palette_name = args.palette+ else:+ colors = args.color+ palette_name = "custom"+ if not colors:+ raise CliError("provide --palette or at least one --color")++ report = audit_palette(+ colors,+ background=args.background,+ role=args.role,+ contrast_threshold=args.contrast_threshold,+ grayscale_min_delta=args.grayscale_min_delta,+ name=palette_name,+ )+ emit_json(report, output=args.output, force=args.force)+ background_review = report["contrast_screen"]["review_count"] > 0+ grayscale_review = report["grayscale_screen"]["review_count"] > 0+ should_fail = (+ (args.fail_on in {"background", "any"} and background_review)+ or (args.fail_on in {"grayscale", "any"} and grayscale_review)+ )+ return 1 if should_fail else 0+ except CliError as exc:+ parser.exit(2, f"error: {exc}\n")+++if __name__ == "__main__":+ sys.exit(main())diff --git a/skills/scientific-visualization/scripts/style_presets.py b/skills/scientific-visualization/scripts/style_presets.pyindex f6b15465..d5b50fa3 100644--- a/skills/scientific-visualization/scripts/style_presets.py+++ b/skills/scientific-visualization/scripts/style_presets.py@@ -1,416 +1,501 @@#!/usr/bin/env python3-"""-Matplotlib Style Presets for Publication-Ready Scientific Figures+"""Scoped Matplotlib style presets for scientific figures.-This module provides pre-configured matplotlib styles optimized for-different journals and use cases.+Presets are visual starting points, not journal-compliance profiles. Matplotlib+is imported lazily so listing and help work in a standard-library environment."""-import matplotlib.pyplot as plt-import matplotlib as mpl-from typing import Optional, Dict, Any---# Okabe-Ito colorblind-friendly palette-OKABE_ITO_COLORS = [- '#E69F00', # Orange- '#56B4E9', # Sky Blue- '#009E73', # Bluish Green- '#F0E442', # Yellow- '#0072B2', # Blue- '#D55E00', # Vermillion- '#CC79A7', # Reddish Purple- '#000000' # Black-]--# Paul Tol palettes-TOL_BRIGHT = ['#4477AA', '#EE6677', '#228833', '#CCBB44', '#66CCEE', '#AA3377', '#BBBBBB']-TOL_MUTED = ['#332288', '#88CCEE', '#44AA99', '#117733', '#999933', '#DDCC77', '#CC6677', '#882255', '#AA4499']-TOL_HIGH_CONTRAST = ['#004488', '#DDAA33', '#BB5566']--# Wong palette-WONG_COLORS = ['#000000', '#E69F00', '#56B4E9', '#009E73', '#F0E442', '#0072B2', '#D55E00', '#CC79A7']---def get_base_style() -> Dict[str, Any]:- """- Get base publication-quality style settings.-- Returns- -------- dict- Dictionary of matplotlib rcParams- """+from __future__ import annotations++import argparse+import importlib.util+import json+import sys+from contextlib import contextmanager+from pathlib import Path+from types import ModuleType+from typing import Any, Iterator++from _common import CliError, atomic_write_bytes, checked_output_file++ASSET_ROOT = Path(__file__).resolve().parents[1] / "assets"+PROFILE_PATH = ASSET_ROOT / "publisher_profiles.json"+SCHEMA_VERSION = "1.0"++BASE_STYLE: dict[str, Any] = {+ "figure.dpi": 100,+ "figure.facecolor": "white",+ "figure.autolayout": False,+ "figure.constrained_layout.use": False,+ "font.size": 8,+ "font.family": "sans-serif",+ "font.sans-serif": ["Arial", "Helvetica", "DejaVu Sans"],+ "axes.linewidth": 0.6,+ "axes.labelsize": 8,+ "axes.titlesize": 8,+ "axes.labelweight": "normal",+ "axes.spines.top": False,+ "axes.spines.right": False,+ "axes.edgecolor": "black",+ "axes.labelcolor": "black",+ "axes.axisbelow": True,+ "axes.grid": False,+ "xtick.major.size": 3,+ "xtick.minor.size": 2,+ "xtick.major.width": 0.6,+ "xtick.minor.width": 0.5,+ "xtick.labelsize": 7,+ "xtick.direction": "out",+ "ytick.major.size": 3,+ "ytick.minor.size": 2,+ "ytick.major.width": 0.6,+ "ytick.minor.width": 0.5,+ "ytick.labelsize": 7,+ "ytick.direction": "out",+ "lines.linewidth": 1.4,+ "lines.markersize": 4,+ "lines.markeredgewidth": 0.6,+ "patch.linewidth": 0.6,+ "legend.fontsize": 7,+ "legend.frameon": False,+ "savefig.dpi": 300,+ "savefig.format": "pdf",+ # "tight" changes the exported physical page dimensions. Opt in explicitly.+ "savefig.bbox": "standard",+ "savefig.pad_inches": 0.05,+ "savefig.transparent": False,+ "savefig.facecolor": "white",+ "savefig.edgecolor": "white",+ "image.cmap": "viridis",+ # Type 42 keeps TrueType fonts editable/embedded in PDF/PS.+ "pdf.fonttype": 42,+ "ps.fonttype": 42,+ # SVG text remains text but depends on font availability at render time.+ "svg.fonttype": "none",+}++STYLE_OVERRIDES: dict[str, dict[str, Any]] = {+ "default": {},+ "nature": {+ "font.size": 7,+ "axes.labelsize": 7,+ "axes.titlesize": 7,+ "xtick.labelsize": 6,+ "ytick.labelsize": 6,+ "legend.fontsize": 6,+ "lines.linewidth": 1.0,+ "savefig.dpi": 450,+ },+ "science": {+ "font.size": 7,+ "axes.labelsize": 7,+ "axes.titlesize": 7,+ "xtick.labelsize": 6,+ "ytick.labelsize": 6,+ "legend.fontsize": 6,+ "savefig.dpi": 300,+ },+ "cell": {+ "font.size": 7,+ "axes.labelsize": 8,+ "axes.titlesize": 8,+ "xtick.labelsize": 7,+ "ytick.labelsize": 7,+ "legend.fontsize": 7,+ "savefig.dpi": 300,+ },+ "minimal": {+ "axes.linewidth": 0.8,+ "xtick.major.width": 0.8,+ "ytick.major.width": 0.8,+ "lines.linewidth": 1.8,+ },+ "presentation": {+ "font.size": 14,+ "axes.labelsize": 16,+ "axes.titlesize": 18,+ "xtick.labelsize": 12,+ "ytick.labelsize": 12,+ "legend.fontsize": 12,+ "axes.linewidth": 1.4,+ "lines.linewidth": 2.4,+ "lines.markersize": 8,+ "savefig.format": "png",+ },+}++STYLE_NOTICES = {+ "nature": (+ "Starting point based on flagship Nature final-artwork guidance "+ "accessed 2026-07-23; verify article stage and current instructions."+ ),+ "science": (+ "Starting point based on Science revised-manuscript guidance accessed "+ "2026-07-23; it is not a submission-compliance claim."+ ),+ "cell": (+ "Starting point based on general Cell Press production guidance accessed "+ "2026-07-23; journal and article-type exceptions exist."+ ),+}+++def _load_palette_asset() -> ModuleType:+ asset = ASSET_ROOT / "color_palettes.py"+ spec = importlib.util.spec_from_file_location(+ "_scientific_visualization_color_palettes", asset+ )+ if spec is None or spec.loader is None:+ raise CliError(f"cannot load bundled palette asset: {asset}")+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module+++def available_palettes() -> dict[str, list[str]]:+ palettes = getattr(_load_palette_asset(), "PALETTES", None)+ if not isinstance(palettes, dict):+ raise CliError("bundled palette asset does not define PALETTES")+ return {name: list(colors) for name, colors in palettes.items()}+++def get_style(+ style_name: str = "default",+ *,+ palette_name: str = "okabe_ito_on_white",+) -> dict[str, Any]:+ """Return a validated plain rcParams dictionary without global mutation."""+ if style_name not in STYLE_OVERRIDES:+ raise CliError(+ f"unknown style {style_name!r}; "+ f"available: {', '.join(sorted(STYLE_OVERRIDES))}"+ )+ palettes = available_palettes()+ if palette_name not in palettes:+ raise CliError(+ f"unknown palette {palette_name!r}; "+ f"available: {', '.join(sorted(palettes))}"+ )+ style = dict(BASE_STYLE)+ style.update(STYLE_OVERRIDES[style_name])+ # Stored separately because a Matplotlib Cycler is not JSON serializable.+ style["_palette_colors"] = palettes[palette_name]+ return style+++def _matplotlib_style(style: dict[str, Any]) -> dict[str, Any]:+ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required to apply styles; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc+ prepared = dict(style)+ colors = prepared.pop("_palette_colors")+ prepared["axes.prop_cycle"] = mpl.cycler(color=colors)+ # rcParams validation catches stale or misspelled keys.+ validated = mpl.RcParams()+ validated.update(prepared)+ return dict(validated)+++def apply_publication_style(+ style_name: str = "default",+ *,+ palette_name: str = "okabe_ito_on_white",+ reset: bool = False,+) -> dict[str, Any]:+ """Apply a validated style globally and return the settings used."""+ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required to apply styles; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc+ if reset:+ mpl.rcdefaults()+ style = _matplotlib_style(get_style(style_name, palette_name=palette_name))+ mpl.rcParams.update(style)return {- # Figure- 'figure.dpi': 100, # Display DPI (changed on save)- 'figure.facecolor': 'white',- 'figure.autolayout': False,- 'figure.constrained_layout.use': True,-- # Font- 'font.size': 8,- 'font.family': 'sans-serif',- 'font.sans-serif': ['Arial', 'Helvetica', 'DejaVu Sans'],-- # Axes- 'axes.linewidth': 0.5,- 'axes.labelsize': 9,- 'axes.titlesize': 9,- 'axes.labelweight': 'normal',- 'axes.spines.top': False,- 'axes.spines.right': False,- 'axes.spines.left': True,- 'axes.spines.bottom': True,- 'axes.edgecolor': 'black',- 'axes.labelcolor': 'black',- 'axes.axisbelow': True,- 'axes.prop_cycle': mpl.cycler(color=OKABE_ITO_COLORS),-- # Grid- 'axes.grid': False,-- # Ticks- 'xtick.major.size': 3,- 'xtick.minor.size': 2,- 'xtick.major.width': 0.5,- 'xtick.minor.width': 0.5,- 'xtick.labelsize': 7,- 'xtick.direction': 'out',- 'ytick.major.size': 3,- 'ytick.minor.size': 2,- 'ytick.major.width': 0.5,- 'ytick.minor.width': 0.5,- 'ytick.labelsize': 7,- 'ytick.direction': 'out',-- # Lines- 'lines.linewidth': 1.5,- 'lines.markersize': 4,- 'lines.markeredgewidth': 0.5,-- # Legend- 'legend.fontsize': 7,- 'legend.frameon': False,- 'legend.loc': 'best',-- # Savefig- 'savefig.dpi': 300,- 'savefig.format': 'pdf',- 'savefig.bbox': 'tight',- 'savefig.pad_inches': 0.05,- 'savefig.transparent': False,- 'savefig.facecolor': 'white',-- # Image- 'image.cmap': 'viridis',- 'image.aspect': 'auto',+ "style": style_name,+ "palette": palette_name,+ "notice": STYLE_NOTICES.get(+ style_name,+ "General-purpose visual preset; review the rendered figure in context.",+ ),}-def apply_publication_style(style_name: str = 'default') -> None:- """- Apply a pre-configured publication style.-- Parameters- ----------- style_name : str, default 'default'- Name of the style to apply. Options:- - 'default': General publication style- - 'nature': Nature journal style- - 'science': Science journal style- - 'cell': Cell Press style- - 'minimal': Minimal clean style- - 'presentation': Larger fonts for presentations-- Examples- --------- >>> apply_publication_style('nature')- >>> fig, ax = plt.subplots()- >>> ax.plot([1, 2, 3], [1, 4, 9])- """- base_style = get_base_style()-- # Style-specific modifications- if style_name == 'nature':- base_style.update({- 'font.size': 7,- 'axes.labelsize': 8,- 'axes.titlesize': 8,- 'xtick.labelsize': 6,- 'ytick.labelsize': 6,- 'legend.fontsize': 6,- 'savefig.dpi': 600,- })-- elif style_name == 'science':- base_style.update({- 'font.size': 7,- 'axes.labelsize': 8,- 'xtick.labelsize': 6,- 'ytick.labelsize': 6,- 'legend.fontsize': 6,- 'savefig.dpi': 600,- })-- elif style_name == 'cell':- base_style.update({- 'font.size': 8,- 'axes.labelsize': 9,- 'xtick.labelsize': 7,- 'ytick.labelsize': 7,- 'legend.fontsize': 7,- 'savefig.dpi': 600,- })-- elif style_name == 'minimal':- base_style.update({- 'axes.linewidth': 0.8,- 'xtick.major.width': 0.8,- 'ytick.major.width': 0.8,- 'lines.linewidth': 2,- })-- elif style_name == 'presentation':- base_style.update({- 'font.size': 14,- 'axes.labelsize': 16,- 'axes.titlesize': 18,- 'xtick.labelsize': 12,- 'ytick.labelsize': 12,- 'legend.fontsize': 12,- 'axes.linewidth': 1.5,- 'lines.linewidth': 2.5,- 'lines.markersize': 8,- })-- elif style_name != 'default':- print(f"Warning: Style '{style_name}' not recognized. Using 'default'.")-- # Apply the style- plt.rcParams.update(base_style)- print(f"✓ Applied '{style_name}' publication style")---def set_color_palette(palette_name: str = 'okabe_ito') -> None:- """- Set a colorblind-friendly color palette.-- Parameters- ----------- palette_name : str, default 'okabe_ito'- Name of the palette. Options:- - 'okabe_ito': Okabe-Ito palette (8 colors)- - 'wong': Wong palette (8 colors)- - 'tol_bright': Paul Tol bright palette (7 colors)- - 'tol_muted': Paul Tol muted palette (9 colors)- - 'tol_high_contrast': Paul Tol high contrast (3 colors)-- Examples- --------- >>> set_color_palette('tol_muted')- >>> fig, ax = plt.subplots()- >>> for i in range(5):- ... ax.plot([1, 2, 3], [i, i+1, i+2])- """- palettes = {- 'okabe_ito': OKABE_ITO_COLORS,- 'wong': WONG_COLORS,- 'tol_bright': TOL_BRIGHT,- 'tol_muted': TOL_MUTED,- 'tol_high_contrast': TOL_HIGH_CONTRAST,- }-+@contextmanager+def style_context(+ style_name: str = "default",+ *,+ palette_name: str = "okabe_ito_on_white",+) -> Iterator[dict[str, Any]]:+ """Temporarily apply a preset without leaking global rcParams changes."""+ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required to use a style context; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc+ style = _matplotlib_style(get_style(style_name, palette_name=palette_name))+ with mpl.rc_context(style):+ yield {+ "style": style_name,+ "palette": palette_name,+ "notice": STYLE_NOTICES.get(style_name),+ }+++def set_color_palette(palette_name: str = "okabe_ito_on_white") -> list[str]:+ """Set only the Matplotlib color cycle, preserving other rcParams."""+ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required to set a palette; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc+ palettes = available_palettes()if palette_name not in palettes:- available = ', '.join(palettes.keys())- print(f"Warning: Palette '{palette_name}' not found. Available: {available}")- palette_name = 'okabe_ito'-+ raise CliError(+ f"unknown palette {palette_name!r}; "+ f"available: {', '.join(sorted(palettes))}"+ )colors = palettes[palette_name]- plt.rcParams['axes.prop_cycle'] = plt.cycler(color=colors)- print(f"✓ Applied '{palette_name}' color palette ({len(colors)} colors)")---def configure_for_journal(journal: str, figure_width: str = 'single') -> None:- """- Configure matplotlib for a specific journal.-- Parameters- ----------- journal : str- Journal name: 'nature', 'science', 'cell', 'plos', 'acs', 'ieee'- figure_width : str, default 'single'- Figure width: 'single' or 'double' column-- Examples- --------- >>> configure_for_journal('nature', figure_width='single')- >>> fig, ax = plt.subplots() # Will have correct size for Nature- """- journal = journal.lower()-- # Journal specifications- journal_configs = {- 'nature': {- 'single_width': 89, # mm- 'double_width': 183,- 'style': 'nature',- },- 'science': {- 'single_width': 55,- 'double_width': 175,- 'style': 'science',- },- 'cell': {- 'single_width': 85,- 'double_width': 178,- 'style': 'cell',- },- 'plos': {- 'single_width': 83,- 'double_width': 173,- 'style': 'default',- },- 'acs': {- 'single_width': 82.5,- 'double_width': 178,- 'style': 'default',- },- 'ieee': {- 'single_width': 89,- 'double_width': 182,- 'style': 'default',- },+ mpl.rcParams["axes.prop_cycle"] = mpl.cycler(color=colors)+ return colors+++def load_publisher_profiles() -> dict[str, Any]:+ """Load the bundled dated publisher snapshots."""+ try:+ document = json.loads(PROFILE_PATH.read_text(encoding="utf-8"))+ except (OSError, json.JSONDecodeError) as exc:+ raise CliError(f"cannot load publisher profiles: {exc}") from exc+ profiles = document.get("profiles")+ if not isinstance(profiles, dict):+ raise CliError("publisher profile asset is malformed")+ return document+++def figure_size_for_profile(+ publisher: str,+ width: str,+ *,+ aspect: float = 0.75,+) -> tuple[float, float]:+ """Return width/height in inches from a dated profile snapshot."""+ if not (math_is_finite_positive(aspect)):+ raise CliError("aspect must be finite and greater than zero")+ document = load_publisher_profiles()+ try:+ profile = document["profiles"][publisher]+ except KeyError as exc:+ raise CliError(+ f"unknown publisher {publisher!r}; "+ f"available: {', '.join(sorted(document['profiles']))}"+ ) from exc+ widths = profile.get("widths_mm") or {}+ if width not in widths:+ raise CliError(+ f"width {width!r} unavailable for {publisher}; "+ f"available: {', '.join(sorted(widths))}"+ )+ width_inches = float(widths[width]) / 25.4+ return width_inches, width_inches * aspect+++def math_is_finite_positive(value: float) -> bool:+ return value > 0 and value < float("inf")+++def configure_for_journal(+ journal: str,+ figure_width: str = "single",+ *,+ aspect: float = 0.75,+ palette_name: str = "okabe_ito_on_white",+) -> dict[str, Any]:+ """Compatibility helper using a dated profile, without claiming compliance."""+ aliases = {+ "plos": "default",+ "acs": "default",+ "ieee": "default",+ "bmc": "default",+ "elsevier": "default",}-- if journal not in journal_configs:- available = ', '.join(journal_configs.keys())- raise ValueError(f"Journal '{journal}' not recognized. Available: {available}")-- config = journal_configs[journal]-- # Apply style- apply_publication_style(config['style'])-- # Set default figure size- width_mm = config['single_width'] if figure_width == 'single' else config['double_width']- width_inches = width_mm / 25.4- plt.rcParams['figure.figsize'] = (width_inches, width_inches * 0.75) # 4:3 aspect ratio-- print(f"✓ Configured for {journal.upper()} ({figure_width} column: {width_mm} mm)")---def create_style_template(output_file: str = 'publication.mplstyle') -> None:- """- Create a matplotlib style file that can be used with plt.style.use().-- Parameters- ----------- output_file : str, default 'publication.mplstyle'- Output filename for the style file-- Examples- --------- >>> create_style_template('my_style.mplstyle')- >>> plt.style.use('my_style.mplstyle')- """- style = get_base_style()-- with open(output_file, 'w') as f:- f.write("# Publication-quality matplotlib style\n")- f.write("# Usage: plt.style.use('publication.mplstyle')\n\n")-- for key, value in style.items():- if isinstance(value, mpl.cycler):- # Handle cycler specially- colors = [c['color'] for c in value]- f.write(f"axes.prop_cycle : cycler('color', {colors})\n")- else:- f.write(f"{key} : {value}\n")-- print(f"✓ Created style template: {output_file}")- print(f" Use with: plt.style.use('{output_file}')")---def show_color_palettes() -> None:- """- Display available color palettes for visual inspection.- """- palettes = {- 'Okabe-Ito': OKABE_ITO_COLORS,- 'Wong': WONG_COLORS,- 'Tol Bright': TOL_BRIGHT,- 'Tol Muted': TOL_MUTED,- 'Tol High Contrast': TOL_HIGH_CONTRAST,+ style_name = journal if journal in {"nature", "science", "cell"} else aliases.get(journal)+ if style_name is None:+ raise CliError(f"unknown journal/publisher profile: {journal!r}")+ notice = apply_publication_style(style_name, palette_name=palette_name)+ width_aliases = {+ "nature": {"double": "full"},+ "cell": {"double": "full"},+ "plos": {"single": "text-column", "double": "full"},+ "acs": {"double": "full"},+ "ieee": {"double": "full"},+ "bmc": {"single": "half", "double": "full"},+ "elsevier": {"double": "full"},+ }+ profile_width = width_aliases.get(journal, {}).get(+ figure_width, figure_width+ )+ width_inches, height_inches = figure_size_for_profile(+ journal, profile_width, aspect=aspect+ )+ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError("Matplotlib is required to configure figure size") from exc+ mpl.rcParams["figure.figsize"] = (width_inches, height_inches)+ return {+ **notice,+ "publisher_profile": journal,+ "profile_width": profile_width,+ "figsize_inches": [width_inches, height_inches],+ "profile_accessed": load_publisher_profiles()["accessed"],+ "notice": (+ "Configured from a dated planning snapshot. This does not establish "+ "journal compliance; verify current target-journal instructions."+ ),}- fig, axes = plt.subplots(len(palettes), 1, figsize=(8, len(palettes) * 0.5))-- for ax, (name, colors) in zip(axes, palettes.items()):- ax.set_xlim(0, len(colors))- ax.set_ylim(0, 1)- ax.set_yticks([])- ax.set_xticks([])- ax.set_ylabel(name, fontsize=10)-- for i, color in enumerate(colors):- ax.add_patch(plt.Rectangle((i, 0), 1, 1, facecolor=color, edgecolor='black', linewidth=0.5))- # Add hex code- ax.text(i + 0.5, 0.5, color, ha='center', va='center',- fontsize=7, color='white' if i >= len(colors) - 1 else 'black')- fig.suptitle('Colorblind-Friendly Palettes', fontsize=12, fontweight='bold')- plt.tight_layout()- plt.show()+def _mplstyle_value(key: str, value: Any) -> str:+ if isinstance(value, bool):+ return "True" if value else "False"+ if isinstance(value, (list, tuple)):+ return ", ".join(str(item) for item in value)+ return str(value)+++def create_style_template(+ output_file: str | Path,+ *,+ style_name: str = "default",+ palette_name: str = "okabe_ito_on_white",+ force: bool = False,+) -> Path:+ """Write a parseable mplstyle file, refusing implicit overwrite."""+ output = checked_output_file(output_file, force=force)+ style = get_style(style_name, palette_name=palette_name)+ colors = [color.lstrip("#") for color in style.pop("_palette_colors")]+ lines = [+ "# Scientific visualization style preset",+ f"# style: {style_name}; palette: {palette_name}",+ "# Generated from skill version 1.1; verify target-journal rules.",+ "",+ ]+ for key, value in style.items():+ lines.append(f"{key}: {_mplstyle_value(key, value)}")+ lines.append(+ "axes.prop_cycle: cycler('color', "+ + repr(colors).replace('"', "'")+ + ")"+ )+ payload = ("\n".join(lines) + "\n").encode("utf-8")+ atomic_write_bytes(output, payload, force=force)+ return outputdef reset_to_default() -> None:- """- Reset matplotlib to default settings.- """+ """Reset Matplotlib's global rcParams."""+ try:+ import matplotlib as mpl+ except ImportError as exc:+ raise CliError("Matplotlib is required to reset rcParams") from excmpl.rcdefaults()- print("✓ Reset to matplotlib defaults")+++def _serializable_style(style_name: str, palette_name: str) -> dict[str, Any]:+ style = get_style(style_name, palette_name=palette_name)+ colors = style.pop("_palette_colors")+ return {+ "schema_version": SCHEMA_VERSION,+ "style": style_name,+ "palette": palette_name,+ "palette_colors": colors,+ "rcparams": style,+ "notice": STYLE_NOTICES.get(+ style_name,+ "General-purpose visual preset; verify the rendered output.",+ ),+ }+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Inspect or write deterministic Matplotlib style presets. "+ "Presets are not journal-compliance certifications."+ )+ )+ parser.add_argument(+ "--list", action="store_true", help="list available styles and palettes"+ )+ parser.add_argument(+ "--show", metavar="STYLE", help="print one style as JSON"+ )+ parser.add_argument(+ "--write", nargs=2, metavar=("STYLE", "PATH"), help="write an mplstyle file"+ )+ parser.add_argument(+ "--palette", default="okabe_ito_on_white", help="palette name"+ )+ parser.add_argument(+ "--force", action="store_true", help="overwrite an existing style file"+ )+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ try:+ args = parser.parse_args(argv)+ if args.list:+ print(+ json.dumps(+ {+ "schema_version": SCHEMA_VERSION,+ "styles": sorted(STYLE_OVERRIDES),+ "palettes": {+ name: colors+ for name, colors in sorted(available_palettes().items())+ },+ },+ indent=2,+ sort_keys=True,+ )+ )+ return 0+ if args.show:+ print(+ json.dumps(+ _serializable_style(args.show, args.palette),+ indent=2,+ sort_keys=True,+ )+ )+ return 0+ if args.write:+ style_name, output = args.write+ written = create_style_template(+ output,+ style_name=style_name,+ palette_name=args.palette,+ force=args.force,+ )+ print(written)+ return 0+ parser.error("choose --list, --show STYLE, or --write STYLE PATH")+ except CliError as exc:+ parser.exit(2, f"error: {exc}\n")if __name__ == "__main__":- print("Matplotlib Style Presets for Scientific Figures")- print("=" * 50)-- # Show available styles- print("\nAvailable publication styles:")- print(" - default")- print(" - nature")- print(" - science")- print(" - cell")- print(" - minimal")- print(" - presentation")-- print("\nAvailable color palettes:")- print(" - okabe_ito (recommended)")- print(" - wong")- print(" - tol_bright")- print(" - tol_muted")- print(" - tol_high_contrast")-- print("\nExample usage:")- print(" from style_presets import apply_publication_style, set_color_palette")- print(" apply_publication_style('nature')")- print(" set_color_palette('okabe_ito')")-- # Create example figure- print("\nGenerating example figure with 'default' style...")- apply_publication_style('default')-- fig, ax = plt.subplots(figsize=(3.5, 2.5))- for i in range(5):- ax.plot([1, 2, 3, 4], [i, i+1, i+0.5, i+2], marker='o', label=f'Series {i+1}')- ax.set_xlabel('Time (hours)')- ax.set_ylabel('Response (AU)')- ax.legend()- fig.suptitle('Example with Publication Style')- plt.tight_layout()- plt.show()-- # Show color palettes- print("\nDisplaying color palettes...")- show_color_palettes()+ sys.exit(main())diff --git a/skills/scientific-visualization/scripts/style_preview.py b/skills/scientific-visualization/scripts/style_preview.pynew file mode 100644index 00000000..7b68a529--- /dev/null+++ b/skills/scientific-visualization/scripts/style_preview.py@@ -0,0 +1,232 @@+#!/usr/bin/env python3+"""Generate a deterministic preview for a bundled scientific figure style."""++from __future__ import annotations++import argparse+import math+import sys+from typing import Any++from _common import CliError, emit_json, positive_float+++def build_preview(style_name: str, palette_name: str) -> tuple[Any, dict[str, Any]]:+ """Create a deterministic multi-panel preview and palette audit."""+ try:+ import matplotlib as mpl+ import matplotlib.pyplot as plt+ except ImportError as exc:+ raise CliError(+ "Matplotlib is required for previews; "+ "run with --with 'matplotlib==3.11.1'"+ ) from exc+ from palette_audit import audit_palette+ from style_presets import available_palettes, style_context++ palettes = available_palettes()+ if palette_name not in palettes:+ raise CliError(+ f"unknown palette {palette_name!r}; "+ f"available: {', '.join(sorted(palettes))}"+ )+ colors = palettes[palette_name]+ if len(colors) < 3:+ raise CliError("preview palette must contain at least three colors")+ audit = audit_palette(+ colors,+ background="#FFFFFF",+ role="graphical",+ name=palette_name,+ )++ with style_context(style_name, palette_name=palette_name):+ fig, axes = plt.subplots(+ 2,+ 2,+ figsize=(7.0, 5.2),+ layout="constrained",+ )+ x_values = [index / 10.0 for index in range(41)]+ line_styles = ["-", "--", "-.", ":"]+ markers = ["o", "s", "^", "D"]+ for index in range(4):+ values = [+ math.sin(value + index * 0.45) + index * 0.35+ for value in x_values+ ]+ axes[0, 0].plot(+ x_values,+ values,+ color=colors[index % len(colors)],+ linestyle=line_styles[index],+ marker=markers[index],+ markevery=8,+ label=f"Series {index + 1}",+ )+ axes[0, 0].axhline(0, color="0.35", linewidth=0.7)+ axes[0, 0].set(+ xlabel="Time (hours)",+ ylabel="Response (a.u.)",+ title="Color plus redundant encoding",+ )+ axes[0, 0].legend(ncols=2)++ categories = ["Control", "Low", "High"]+ values = [0.0, 1.4, -0.8]+ hatches = ["", "///", "xx"]+ bars = axes[0, 1].bar(+ categories,+ values,+ color=[colors[index % len(colors)] for index in range(3)],+ edgecolor="black",+ linewidth=0.7,+ )+ for bar, hatch in zip(bars, hatches):+ bar.set_hatch(hatch)+ axes[0, 1].axhline(0, color="black", linewidth=0.7)+ axes[0, 1].set(+ ylabel="Change from baseline (unit)",+ title="Signed bars with visible zero",+ )++ matrix = [+ [-2.0, -1.2, -0.4, 0.2, 1.0],+ [-1.5, -0.8, float("nan"), 0.8, 1.7],+ [-1.0, -0.2, 0.0, 1.1, 2.0],+ ]+ colormap = mpl.colormaps["RdBu_r"].with_extremes(bad="#777777")+ image = axes[1, 0].imshow(+ matrix,+ cmap=colormap,+ norm=mpl.colors.TwoSlopeNorm(vmin=-2, vcenter=0, vmax=2),+ aspect="auto",+ interpolation="nearest",+ )+ axes[1, 0].set(+ xlabel="Sample",+ ylabel="Feature",+ title="Centered normalization; gray = missing",+ )+ colorbar = fig.colorbar(image, ax=axes[1, 0])+ colorbar.set_label("Effect (unit)")++ groups = [+ [1.0, 1.2, 0.9, 1.1, 1.4, 0.8],+ [1.4, 1.8, 1.6, 1.5, 2.0, 1.7],+ [0.7, 0.9, 1.0, 0.6, 0.8, 1.1],+ ]+ for index, group in enumerate(groups):+ offsets = [-0.08, -0.05, -0.02, 0.02, 0.05, 0.08]+ axes[1, 1].scatter(+ [index + offset for offset in offsets],+ group,+ color=colors[index % len(colors)],+ edgecolor="black",+ linewidth=0.4,+ marker=markers[index],+ zorder=2,+ )+ ordered = sorted(group)+ median = (ordered[2] + ordered[3]) / 2+ axes[1, 1].plot(+ [index - 0.15, index + 0.15],+ [median, median],+ color="black",+ linewidth=1.2,+ )+ axes[1, 1].set(+ xticks=range(3),+ xticklabels=categories,+ ylabel="Observed value (unit)",+ title="Raw observations with median",+ )++ for label, ax in zip("ABCD", axes.flat):+ ax.text(+ -0.12,+ 1.06,+ label,+ transform=ax.transAxes,+ fontweight="bold",+ va="top",+ )+ fig.suptitle(f"Style preview: {style_name} / {palette_name}")+ return fig, audit+++def build_parser() -> argparse.ArgumentParser:+ parser = argparse.ArgumentParser(+ description=(+ "Render a deterministic accessible-style preview with line, bar, "+ "heatmap, missing-data, and raw-observation examples."+ )+ )+ parser.add_argument("--output", required=True, help="output base path")+ parser.add_argument(+ "--style",+ choices=("default", "nature", "science", "cell", "minimal", "presentation"),+ default="default",+ )+ parser.add_argument("--palette", default="okabe_ito_on_white")+ parser.add_argument(+ "--formats", default="png,svg", help="comma-separated formats"+ )+ parser.add_argument("--dpi", type=positive_float, default=300.0)+ parser.add_argument("--manifest", action="store_true")+ parser.add_argument("--force", action="store_true")+ return parser+++def main(argv: list[str] | None = None) -> int:+ parser = build_parser()+ try:+ args = parser.parse_args(argv)+ from figure_export import export_figure++ fig, audit = build_preview(args.style, args.palette)+ try:+ report = export_figure(+ fig,+ args.output,+ formats=args.formats.split(","),+ dpi=args.dpi,+ overwrite=args.force,+ provenance={+ "purpose": "deterministic bundled-style preview",+ "style": args.style,+ "palette": args.palette,+ "data": "synthetic values defined in style_preview.py",+ "transformations": [+ "analytical sine offsets",+ "explicit TwoSlopeNorm(-2, 0, 2)",+ "median of displayed raw observations",+ ],+ "palette_screen": {+ "background_review_count": audit["contrast_screen"][+ "review_count"+ ],+ "grayscale_review_count": audit["grayscale_screen"][+ "review_count"+ ],+ },+ },+ write_manifest=args.manifest,+ )+ finally:+ import matplotlib.pyplot as plt++ plt.close(fig)+ report["palette_audit"] = {+ "contrast_screen": audit["contrast_screen"],+ "grayscale_screen": audit["grayscale_screen"],+ "notice": audit["notice"],+ }+ emit_json(report)+ return 0+ except CliError as exc:+ parser.exit(2, f"error: {exc}\n")+++if __name__ == "__main__":+ sys.exit(main())diff --git a/skills/scientific-visualization/tests/test_scripts.py b/skills/scientific-visualization/tests/test_scripts.pynew file mode 100644index 00000000..f9158cff--- /dev/null+++ b/skills/scientific-visualization/tests/test_scripts.py@@ -0,0 +1,345 @@+"""Synthetic, network-free tests for scientific-visualization helpers."""++from __future__ import annotations++import json+import importlib.util+import stat+import sys+import tempfile+import unittest+from pathlib import Path++SKILL_ROOT = Path(__file__).resolve().parents[1]+SCRIPTS = SKILL_ROOT / "scripts"+ASSETS = SKILL_ROOT / "assets"+sys.path.insert(0, str(SCRIPTS))+sys.path.insert(0, str(ASSETS))++import _common # noqa: E402+import color_palettes # noqa: E402+import export_plan # noqa: E402+import figure_export # noqa: E402+import image_metadata # noqa: E402+import palette_audit # noqa: E402+import style_presets # noqa: E402+++class CommonSafetyTests(unittest.TestCase):+ def test_json_output_is_private_and_refuses_overwrite(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ output = Path(directory) / "report.json"+ _common.emit_json({"ok": True}, output=output)+ self.assertEqual(json.loads(output.read_text()), {"ok": True})+ self.assertEqual(stat.S_IMODE(output.stat().st_mode), 0o600)+ with self.assertRaises(_common.CliError):+ _common.emit_json({"ok": False}, output=output)++ def test_input_and_output_symlinks_are_rejected(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory)+ real = root / "real.txt"+ real.write_text("data", encoding="utf-8")+ link = root / "link.txt"+ try:+ link.symlink_to(real)+ except OSError:+ self.skipTest("symlinks unavailable")+ with self.assertRaises(_common.CliError):+ _common.checked_input_file(link)+ with self.assertRaises(_common.CliError):+ _common.checked_output_file(link, force=True)+++class PaletteTests(unittest.TestCase):+ def test_wcag_contrast_reference_values(self) -> None:+ black = palette_audit.parse_hex_color("#000000")+ white = palette_audit.parse_hex_color("#FFFFFF")+ self.assertAlmostEqual(palette_audit.contrast_ratio(black, white), 21.0)+ self.assertAlmostEqual(palette_audit.cie_lstar(black), 0.0)+ self.assertAlmostEqual(palette_audit.cie_lstar(white), 100.0)++ def test_on_white_subset_meets_graphical_threshold(self) -> None:+ report = palette_audit.audit_palette(+ color_palettes.OKABE_ITO_ON_WHITE,+ background="#FFFFFF",+ role="graphical",+ )+ self.assertEqual(report["contrast_screen"]["review_count"], 0)+ self.assertEqual(report["palette"]["color_count"], 5)++ def test_tol_palettes_match_current_issue(self) -> None:+ self.assertEqual(+ color_palettes.TOL_HIGH_CONTRAST,+ ["#004488", "#DDAA33", "#BB5566"],+ )+ self.assertEqual(len(color_palettes.TOL_MUTED), 9)+ self.assertEqual(color_palettes.TOL_MUTED[0], "#CC6677")++ def test_grayscale_screen_is_explicitly_heuristic(self) -> None:+ report = palette_audit.audit_palette(+ ["#FF0000", "#00FF00"],+ grayscale_min_delta=90,+ )+ self.assertEqual(report["grayscale_screen"]["review_count"], 1)+ self.assertIn("Heuristic", report["grayscale_screen"]["basis"])+++class MetadataTests(unittest.TestCase):+ def test_svg_dimensions_and_external_resource_count(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ svg = Path(directory) / "figure.svg"+ svg.write_text(+ '<svg xmlns="http://www.w3.org/2000/svg" '+ 'width="96px" height="1in" viewBox="0 0 96 96">'+ '<text x="1" y="12">Label</text>'+ '<image href="external.png" x="0" y="0" width="2" height="2"/>'+ "</svg>",+ encoding="utf-8",+ )+ report = image_metadata.inspect_file(svg)+ metadata = report["metadata"]+ self.assertEqual(metadata["format"], "SVG")+ self.assertAlmostEqual(metadata["width_mm"], 25.4)+ self.assertAlmostEqual(metadata["height_mm"], 25.4)+ self.assertEqual(metadata["text_element_count"], 1)+ self.assertEqual(metadata["external_image_count"], 1)++ def test_eps_bounding_box(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ eps = Path(directory) / "figure.eps"+ eps.write_text(+ "%!PS-Adobe-3.0 EPSF-3.0\n"+ "%%BoundingBox: 0 0 144 72\n"+ "%%EOF\n",+ encoding="latin-1",+ )+ report = image_metadata.inspect_file(eps)+ self.assertAlmostEqual(report["metadata"]["width_mm"], 50.8)+ self.assertAlmostEqual(report["metadata"]["height_mm"], 25.4)++ def test_svg_dtd_is_rejected(self) -> None:+ with tempfile.TemporaryDirectory() as directory:+ svg = Path(directory) / "unsafe.svg"+ svg.write_text(+ '<!DOCTYPE svg [<!ENTITY x "unsafe">]>'+ '<svg xmlns="http://www.w3.org/2000/svg">&x;</svg>',+ encoding="utf-8",+ )+ with self.assertRaises(_common.CliError):+ image_metadata.inspect_file(svg)++ def test_raster_dimensions_dpi_mode_and_screen(self) -> None:+ try:+ from PIL import Image+ except ImportError:+ self.skipTest("Pillow not installed")+ with tempfile.TemporaryDirectory() as directory:+ image_path = Path(directory) / "synthetic.tiff"+ Image.new("RGB", (300, 150), "white").save(+ image_path,+ dpi=(300, 300),+ compression="tiff_lzw",+ )+ inspected = image_metadata.inspect_file(image_path)+ screened = image_metadata.screen_metadata(+ inspected,+ expected_formats=["tiff"],+ expected_modes=["RGB"],+ min_dpi=300,+ target_width_mm=25.4,+ alpha_policy="forbid",+ )+ self.assertEqual(inspected["metadata"]["mode"], "RGB")+ self.assertEqual(inspected["metadata"]["width_px"], 300)+ self.assertEqual(screened["screening_summary"]["fail"], 0)++ def test_effective_dpi_tolerates_binary_float_roundoff(self) -> None:+ report = {+ "schema_version": "1.0",+ "input": {"size_bytes": 1},+ "metadata": {+ "format": "PNG",+ "kind": "raster",+ "width_px": 2100,+ "height_px": 1500,+ "dpi_x": None,+ "dpi_y": None,+ "mode": "RGB",+ },+ }+ screened = image_metadata.screen_metadata(+ report,+ min_dpi=300,+ target_width_mm=177.8,+ )+ self.assertEqual(screened["screening_summary"]["fail"], 0)+++class PublisherProfileTests(unittest.TestCase):+ def test_profiles_capture_phase_and_current_dimensions(self) -> None:+ nature = export_plan.build_plan(+ "nature",+ figure_type="combination",+ width="single",+ phase="final",+ )+ science = export_plan.build_plan(+ "science",+ figure_type="line-art",+ width="full",+ phase="revised",+ )+ cell = export_plan.build_plan(+ "cell",+ figure_type="line-art",+ width="full",+ phase="final",+ )+ self.assertEqual(nature["width"]["millimeters"], 89.0)+ self.assertEqual(science["width"]["millimeters"], 184.0)+ self.assertEqual(cell["width"]["millimeters"], 174.0)+ self.assertTrue(nature["phase_matches_snapshot"])+ self.assertIn("do not establish", nature["notice"])++ def test_phase_mismatch_is_not_silenced(self) -> None:+ plan = export_plan.build_plan(+ "plos",+ figure_type="photo",+ phase="initial",+ )+ self.assertFalse(plan["phase_matches_snapshot"])+ self.assertEqual(plan["profile_phase"], "post-provisional-accept")++ def test_acs_profile_does_not_invent_formats_or_dpi(self) -> None:+ plan = export_plan.build_plan(+ "acs",+ figure_type="combination",+ width="single",+ )+ self.assertIsNone(plan["formats"])+ self.assertIsNone(plan["raster_dpi"])++ def test_plos_synthetic_tiff_matches_recorded_properties(self) -> None:+ try:+ from PIL import Image+ except ImportError:+ self.skipTest("Pillow not installed")+ plan = export_plan.build_plan(+ "plos",+ figure_type="photo",+ width="full",+ phase="post-provisional-accept",+ )+ with tempfile.TemporaryDirectory() as directory:+ path = Path(directory) / "Fig1.tiff"+ Image.new("RGB", (2250, 1000), "white").save(+ path,+ dpi=(300, 300),+ compression="tiff_lzw",+ )+ result = export_plan.validate_against_plan(plan, path)+ self.assertEqual(result["screening_summary"]["fail"], 0)+++class StyleTests(unittest.TestCase):+ def test_plain_style_has_dimension_preserving_export_default(self) -> None:+ style = style_presets.get_style("default")+ self.assertEqual(style["savefig.bbox"], "standard")+ self.assertEqual(style["pdf.fonttype"], 42)+ self.assertEqual(len(style["_palette_colors"]), 5)++ def test_all_mplstyle_assets_parse_when_matplotlib_available(self) -> None:+ try:+ import matplotlib as mpl+ except ImportError:+ self.skipTest("Matplotlib not installed")+ for path in sorted(ASSETS.glob("*.mplstyle")):+ with self.subTest(path=path.name):+ parsed = mpl.rc_params_from_file(+ path,+ fail_on_error=True,+ use_default_template=False,+ )+ self.assertIn("axes.prop_cycle", parsed)++ def test_generated_style_parses_when_matplotlib_available(self) -> None:+ try:+ import matplotlib as mpl+ except ImportError:+ self.skipTest("Matplotlib not installed")+ with tempfile.TemporaryDirectory() as directory:+ output = Path(directory) / "generated.mplstyle"+ style_presets.create_style_template(output)+ parsed = mpl.rc_params_from_file(+ output,+ fail_on_error=True,+ use_default_template=False,+ )+ self.assertEqual(parsed["pdf.fonttype"], 42)+++class ExportTests(unittest.TestCase):+ def test_profile_export_requires_explicit_confirmation(self) -> None:+ with self.assertRaises(_common.CliError):+ figure_export.save_for_journal(+ object(),+ "unused",+ "nature",+ )+ with self.assertRaises(_common.CliError):+ figure_export.save_for_journal(+ object(),+ "unused",+ "nature",+ confirm_profile=True,+ )++ def test_atomic_export_and_manifest(self) -> None:+ try:+ import matplotlib++ matplotlib.use("Agg")+ import matplotlib as mpl+ import matplotlib.pyplot as plt+ from PIL import Image+ except ImportError:+ self.skipTest("Matplotlib not installed")+ with tempfile.TemporaryDirectory() as directory:+ base = Path(directory) / "figure"+ fig, ax = plt.subplots(figsize=(2, 1))+ ax.plot([0, 1], [0, 1])+ try:+ with mpl.rc_context({"savefig.bbox": "tight"}):+ report = figure_export.export_figure(+ fig,+ base,+ formats=["png", "pdf"],+ dpi=150,+ provenance={"raw_data": "synthetic"},+ write_manifest=True,+ )+ self.assertEqual(len(report["outputs"]), 2)+ self.assertTrue((Path(directory) / "figure.export.json").exists())+ with Image.open(Path(directory) / "figure.png") as image:+ self.assertEqual(image.size, (300, 150))+ if importlib.util.find_spec("pypdf") is not None:+ font_report = figure_export.verify_font_embedding(+ Path(directory) / "figure.pdf"+ )+ resources = font_report["font_resources"]+ self.assertGreater(resources["resource_count"], 0)+ self.assertTrue(resources["all_embedded"])+ with self.assertRaises(_common.CliError):+ figure_export.export_figure(+ fig,+ base,+ formats=["png"],+ dpi=150,+ )+ finally:+ plt.close(fig)+++if __name__ == "__main__":+ unittest.main()