Skip to content

Add a python script to plot the results#15

Merged
EiffL merged 4 commits intomainfrom
dev-plot-results
Feb 10, 2026
Merged

Add a python script to plot the results#15
EiffL merged 4 commits intomainfrom
dev-plot-results

Conversation

@Emmaaycoberry
Copy link
Contributor

Add a python script that can be used as follow:
python shine/plot_shine_results.py --output my_output/

Features:
✅ Reads observation.npz and posterior.nc from the specified directory
✅ Saves all plots to the same directory
✅ Generates visualizations:

  1. Observation visual (image, PSF, noise map)
  2. Posterior distributions
  3. Corner plot with 68% and 95% confidence intervals
  4. Shear analysis (if g1/g2 present)
  5. Summary statistics table (printed, not saved)
  6. Trace plots (if MCMC chains present)
  7. Correlation matrix

✅ Clean terminal output with progress indicators
✅ Proper error handling if files are missing
✅ Auto-installs the corner package if needed

corner_plot correlation_matrix observation_visual posterior_distributions shear_analysis trace_plots

@claude
Copy link

claude bot commented Feb 10, 2026

Code Review

I've reviewed this PR and found 3 issues that need to be addressed:

1. Missing Type Hints (CLAUDE.md violation)

Location: All functions in shine/plot_shine_results.py

The functions lack type hints, which violates CLAUDE.md Section 4.1:

  • Type hints: Full PEP 484 compliance required

For example, setup_plot_style() at line 28 should be:

def setup_plot_style() -> None:
    """Set up matplotlib style for all plots."""
    plt.style.use('seaborn-v0_8-darkgrid')
    plt.rcParams['figure.figsize'] = (12, 8)

This applies to all 10 functions in the file.

2. Docstrings Not in Google-Style Format (CLAUDE.md violation)

Location: All functions in shine/plot_shine_results.py

The docstrings don't follow Google-style format as required by CLAUDE.md Section 4.1:

  • Docstrings: Google-style

Current format (example from load_data at lines 36-43):

def load_data(output_dir):
    """Load observation and posterior data from the output directory.
    
    Args:
        output_dir: Path to directory containing observation.npz and posterior.nc
        
    Returns:
        tuple: (obs_data, idata, posterior)
    """

Should be:

def load_data(output_dir: Path) -> tuple[dict, az.InferenceData, xr.Dataset]:
    """Load observation and posterior data from the output directory.
    
    Args:
        output_dir (Path): Path to directory containing observation.npz and posterior.nc
        
    Returns:
        tuple: A tuple containing:
            - obs_data (dict): Loaded observation data from observation.npz
            - idata (az.InferenceData): ArviZ InferenceData object
            - posterior (xr.Dataset): Posterior dataset
            
    Raises:
        FileNotFoundError: If observation.npz or posterior.nc not found
    """

Google-style requires type annotations in the Args section and more detailed Returns sections.

3. Security Issue - Automatic Package Installation

Location: Lines 202-205 and 266-269 in shine/plot_shine_results.py

The code automatically installs the corner package without user consent:

try:
import corner
except ImportError:
print("Installing corner package...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "corner"])
import corner
# Prepare data: stack all parameters as columns

This is problematic because:

  • It modifies the user's Python environment as a side effect of running a visualization script
  • Users don't get a chance to confirm or opt out
  • It could execute arbitrary code from PyPI in compromised environments
  • It violates the principle of least surprise

Recommended fix: Either:

  1. Declare corner as an optional dependency in pyproject.toml under [project.optional-dependencies] (e.g., plotting = ["corner>=2.0.0"])
  2. Fail gracefully with a clear error message: raise ImportError("corner package is required. Install it with: pip install corner")

This same pattern appears twice in the file (in both plot_corner and plot_shear_analysis functions).


Summary: Please address these 3 issues before merging. The first two are code standards compliance issues from CLAUDE.md, and the third is a security/best-practices concern.

@EiffL
Copy link
Member

EiffL commented Feb 10, 2026

@claude can you address the issues found during the review?

@claude
Copy link

claude bot commented Feb 10, 2026

Claude finished @EiffL's task in 2m 16s —— View job


Code Review Issues Fixed ✅

All 3 issues identified in the code review have been addressed and pushed to the dev-plot-results branch.

Changes Made

1. Added Type Hints (PEP 484 Compliance)

  • Added type hints to all 10 functions in shine/plot_shine_results.py
  • Imported required types: Any, Dict, List, Tuple from typing
  • All function signatures now include parameter types and return types
  • Example: def load_data(output_dir: Path) -> Tuple[Dict[str, Any], az.InferenceData, xr.Dataset]:

2. Converted Docstrings to Google-Style Format

  • All docstrings now follow Google-style with proper formatting
  • Added detailed Args sections with type annotations in parentheses
  • Added comprehensive Returns sections with detailed descriptions
  • Added Raises sections where applicable (e.g., FileNotFoundError, ImportError)
  • Improved clarity and completeness of documentation

3. Removed Automatic Package Installation (Security Issue)

  • Replaced automatic pip install corner with proper error handling
  • Both plot_corner() and plot_shear_analysis() now raise ImportError with clear installation instructions
  • Error message: "The 'corner' package is required for corner plots. Install it with: pip install corner"
  • Users maintain control over their Python environment

Commit Details

Commit: dbdeaff
Branch: dev-plot-results

The changes ensure full compliance with CLAUDE.md requirements and follow security best practices.


- Add full PEP 484 type hints to all functions
- Convert all docstrings to Google-style format with detailed Args, Returns, and Raises sections
- Remove automatic package installation security issue: replaced with ImportError messages
- User must now manually install 'corner' package if needed

Addresses review feedback on PR #15.

Co-authored-by: Francois Lanusse <EiffL@users.noreply.github.com>
@EiffL
Copy link
Member

EiffL commented Feb 10, 2026

@claude corner is a great package, very useful, can you add it as a dependency of the package? we will certainly use it more that once

@claude
Copy link

claude bot commented Feb 10, 2026

Claude finished @EiffL's task in 58s —— View job


Adding corner as a Package Dependency ✅

Successfully added corner as a project dependency.

Tasks

  • Read current pyproject.toml structure
  • Add corner to dependencies
  • Commit and push changes
  • Update comment with results

Changes Made

Added corner>=2.0.0 to the main dependencies in pyproject.toml:34:

dependencies = [
    "jax>=0.4.0",
    "numpyro>=0.13.0",
    "galsim>=2.5.0",
    "jax-galsim>=2026.2.0",
    "pydantic>=2.0.0",
    "pyyaml>=6.0",
    "arviz>=0.17.0",
    "matplotlib>=3.7.0",
    "corner>=2.0.0",  # Added for corner plots and visualization
]

The corner package will now be automatically installed with SHINE, making it available for the plotting script and any future visualization needs.

Commit Details

--- | Branch: dev-plot-results

Added corner>=2.0.0 to project dependencies in pyproject.toml for
visualization and plotting functionality.

Co-authored-by: Francois Lanusse <EiffL@users.noreply.github.com>
@EiffL
Copy link
Member

EiffL commented Feb 10, 2026

@claude hum, this is a plotting script, it shouldn't be in the package itself. Can you add a scripts folder where such utility scripts can be stored?

@claude
Copy link

claude bot commented Feb 10, 2026

Claude finished @EiffL's task in 2m 43s —— View job


Moving Plotting Script to Scripts Folder ✅

Successfully moved shine/plot_shine_results.py to a dedicated scripts/ folder for utility scripts.

Tasks

  • Create scripts/ directory
  • Move shine/plot_shine_results.py to scripts/plot_shine_results.py
  • Commit and push changes
  • Update comment with results

Changes Made

The plotting script has been relocated from the package to a utility scripts directory:

Old location: shine/plot_shine_results.py
New location: scripts/plot_shine_results.py

Git has preserved the file's history by detecting this as a rename operation (98% similarity).

Usage

The script can now be run using:

python scripts/plot_shine_results.py --output my_output/

Commit Details


| Branch: dev-plot-results

Utility scripts should be kept separate from the package itself.
Created scripts/ directory for utility scripts.

Co-authored-by: Francois Lanusse <EiffL@users.noreply.github.com>
Copy link
Member

@EiffL EiffL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@EiffL EiffL merged commit 4b1f5b1 into main Feb 10, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants