proteusPy.hexbin_plot

hexbin_plotter.py Purpose: Create hexbin plots with customizable parameters. Usage: python hexbin_plotter.py [arguments] Author: Eric G. Suchanek, PhD. Last revision: 2025-03-26

  1"""
  2hexbin_plotter.py
  3Purpose: Create hexbin plots with customizable parameters.
  4Usage: python hexbin_plotter.py [arguments]
  5Author: Eric G. Suchanek, PhD.
  6Last revision: 2025-03-26
  7"""
  8
  9# pylint: disable=c0103
 10
 11import argparse
 12import logging
 13import sys
 14
 15import proteusPy as pp
 16
 17
 18def setup_logging(log_file: str, log_level: str = "INFO") -> logging.Logger:
 19    """
 20    Set up logging configuration.
 21
 22    :param log_file: Output log file name
 23    :param log_level: Logging level (default: "INFO")
 24    :return: Configured logger instance
 25    """
 26    pp.configure_master_logger(log_file)
 27    pp.set_logger_level_for_module("proteusPy", getattr(logging, log_level.upper()))
 28    return pp.create_logger(__name__)
 29
 30
 31def create_hexbin_plot(
 32    plot_type: str = "df",
 33    column1: str = "chi2",
 34    column2: str = "chi4",
 35    width: int = 1024,
 36    height: int = 1024,
 37    gridsize: int = 100,
 38    scaling: str = "sqrt",
 39    percentile: float = None,
 40    verbose: bool = False,
 41    title: str = None,
 42) -> None:
 43    """
 44    Create a hexbin plot with specified parameters.
 45
 46    :param plot_type: Type of hexbin plot ('leftright' or 'df')
 47    :type plot_type: str
 48    :param column1: First column name for plotting
 49    :type column1: str
 50    :param column2: Second column name for plotting
 51    :type column2: str
 52    :param width: Plot width in pixels (default: 1024)
 53    :type width: int
 54    :param height: Plot height in pixels (default: 1024)
 55    :type height: int
 56    :param gridsize: Number of hexagons in grid (default: 100)
 57    :type gridsize: int
 58    :param scaling: Scaling method ('sqrt', 'linear', etc.) (default: 'sqrt')
 59    :type scaling: str
 60    :param percentile: Cutoff value for data loading (default: None)
 61    :type percentile: float
 62    :param verbose: Enable verbose output (default: False)
 63    :type verbose: bool
 64    """
 65    # Load PDB data
 66    if percentile > 0:
 67        PDB = pp.DisulfideLoader(verbose=verbose, percentile=percentile)
 68    else:
 69        PDB = pp.Load_PDB_SS(verbose=verbose, subset=False)
 70
 71    # Create plot based on type
 72    if plot_type.lower() == "leftright":
 73        PDB.plot_3d_hexbin_leftright(
 74            scaling=scaling,
 75            width=width,
 76            height=height,
 77            gridsize=gridsize,
 78            column1=column1,
 79            column2=column2,
 80            title=title,
 81        )
 82    elif plot_type.lower() == "df":
 83        pp.DisulfideVisualization.plot_3d_hexbin_df(
 84            df=PDB.TorsionDF,
 85            column1=column1,
 86            column2=column2,
 87            width=width,
 88            height=height,
 89            gridsize=gridsize,
 90            scaling=scaling,
 91            title=title,
 92        )
 93    else:
 94        raise ValueError(f"Unsupported plot type: {plot_type}. Use 'leftright' or 'df'")
 95
 96
 97def main():
 98    """Main function to parse arguments and create hexbin plot."""
 99    parser = argparse.ArgumentParser(description="Create hexbin plots with proteusPy")
100    parser.add_argument(
101        "plot_type", choices=["leftright", "df"], help="Type of hexbin plot to create"
102    )
103    parser.add_argument(
104        "column1",
105        choices=["chi1", "chi2", "chi3", "chi4", "chi5", "energy", "rho"],
106        help="First column name for plotting",
107    )
108    parser.add_argument(
109        "column2",
110        choices=["chi1", "chi2", "chi3", "chi4", "chi5", "energy", "rho"],
111        help="Second column name for plotting",
112    )
113    parser.add_argument(
114        "--width", type=int, default=1024, help="Plot width in pixels (default: 1024)"
115    )
116    parser.add_argument(
117        "--height", type=int, default=1024, help="Plot height in pixels (default: 1024)"
118    )
119    parser.add_argument(
120        "--gridsize",
121        type=int,
122        default=80,
123        help="Number of hexagons in grid (default: 100)",
124    )
125    parser.add_argument(
126        "--scaling",
127        default="sqrt",
128        help="Scaling method (default: sqrt)",
129        choices=["sqrt", "linear", "log", "power"],
130    )
131    parser.add_argument(
132        "--percentile",
133        type=float,
134        default=-1.0,
135        help="Percentile cutoff to build the dataset (default: -1.0)",
136    )
137    parser.add_argument(
138        "--log-file",
139        default="hexbin_plot.log",
140        help="Output log file name (default: hexbin_plot.log)",
141    )
142    parser.add_argument(
143        "--log-level",
144        default="INFO",
145        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
146        help="Logging level (default: INFO)",
147    )
148    parser.add_argument(
149        "--verbose", action="store_true", help="Enable verbose output (default: False)"
150    )
151
152    args = parser.parse_args()
153
154    # Setup environment and logging
155    pp.set_pyvista_theme("auto")
156    logger = setup_logging(args.log_file, args.log_level)
157
158    try:
159        create_hexbin_plot(
160            plot_type=args.plot_type,
161            column1=args.column1,
162            column2=args.column2,
163            width=args.width,
164            height=args.height,
165            gridsize=args.gridsize,
166            scaling=args.scaling,
167            percentile=args.percentile,
168            verbose=args.verbose,
169        )
170        logger.info("Plot created successfully")
171    except ValueError as e:
172        logger.error("Error creating plot: %s", e)
173        sys.exit(1)
174
175
176if __name__ == "__main__":
177    main()
178    sys.exit(0)
def setup_logging(log_file: str, log_level: str = 'INFO') -> logging.Logger:
19def setup_logging(log_file: str, log_level: str = "INFO") -> logging.Logger:
20    """
21    Set up logging configuration.
22
23    :param log_file: Output log file name
24    :param log_level: Logging level (default: "INFO")
25    :return: Configured logger instance
26    """
27    pp.configure_master_logger(log_file)
28    pp.set_logger_level_for_module("proteusPy", getattr(logging, log_level.upper()))
29    return pp.create_logger(__name__)

Set up logging configuration.

Parameters
  • log_file: Output log file name
  • log_level: Logging level (default: "INFO")
Returns

Configured logger instance

def create_hexbin_plot( plot_type: str = 'df', column1: str = 'chi2', column2: str = 'chi4', width: int = 1024, height: int = 1024, gridsize: int = 100, scaling: str = 'sqrt', percentile: float = None, verbose: bool = False, title: str = None) -> None:
32def create_hexbin_plot(
33    plot_type: str = "df",
34    column1: str = "chi2",
35    column2: str = "chi4",
36    width: int = 1024,
37    height: int = 1024,
38    gridsize: int = 100,
39    scaling: str = "sqrt",
40    percentile: float = None,
41    verbose: bool = False,
42    title: str = None,
43) -> None:
44    """
45    Create a hexbin plot with specified parameters.
46
47    :param plot_type: Type of hexbin plot ('leftright' or 'df')
48    :type plot_type: str
49    :param column1: First column name for plotting
50    :type column1: str
51    :param column2: Second column name for plotting
52    :type column2: str
53    :param width: Plot width in pixels (default: 1024)
54    :type width: int
55    :param height: Plot height in pixels (default: 1024)
56    :type height: int
57    :param gridsize: Number of hexagons in grid (default: 100)
58    :type gridsize: int
59    :param scaling: Scaling method ('sqrt', 'linear', etc.) (default: 'sqrt')
60    :type scaling: str
61    :param percentile: Cutoff value for data loading (default: None)
62    :type percentile: float
63    :param verbose: Enable verbose output (default: False)
64    :type verbose: bool
65    """
66    # Load PDB data
67    if percentile > 0:
68        PDB = pp.DisulfideLoader(verbose=verbose, percentile=percentile)
69    else:
70        PDB = pp.Load_PDB_SS(verbose=verbose, subset=False)
71
72    # Create plot based on type
73    if plot_type.lower() == "leftright":
74        PDB.plot_3d_hexbin_leftright(
75            scaling=scaling,
76            width=width,
77            height=height,
78            gridsize=gridsize,
79            column1=column1,
80            column2=column2,
81            title=title,
82        )
83    elif plot_type.lower() == "df":
84        pp.DisulfideVisualization.plot_3d_hexbin_df(
85            df=PDB.TorsionDF,
86            column1=column1,
87            column2=column2,
88            width=width,
89            height=height,
90            gridsize=gridsize,
91            scaling=scaling,
92            title=title,
93        )
94    else:
95        raise ValueError(f"Unsupported plot type: {plot_type}. Use 'leftright' or 'df'")

Create a hexbin plot with specified parameters.

Parameters
  • plot_type: Type of hexbin plot ('leftright' or 'df')
  • column1: First column name for plotting
  • column2: Second column name for plotting
  • width: Plot width in pixels (default: 1024)
  • height: Plot height in pixels (default: 1024)
  • gridsize: Number of hexagons in grid (default: 100)
  • scaling: Scaling method ('sqrt', 'linear', etc.) (default: 'sqrt')
  • percentile: Cutoff value for data loading (default: None)
  • verbose: Enable verbose output (default: False)
def main():
 98def main():
 99    """Main function to parse arguments and create hexbin plot."""
100    parser = argparse.ArgumentParser(description="Create hexbin plots with proteusPy")
101    parser.add_argument(
102        "plot_type", choices=["leftright", "df"], help="Type of hexbin plot to create"
103    )
104    parser.add_argument(
105        "column1",
106        choices=["chi1", "chi2", "chi3", "chi4", "chi5", "energy", "rho"],
107        help="First column name for plotting",
108    )
109    parser.add_argument(
110        "column2",
111        choices=["chi1", "chi2", "chi3", "chi4", "chi5", "energy", "rho"],
112        help="Second column name for plotting",
113    )
114    parser.add_argument(
115        "--width", type=int, default=1024, help="Plot width in pixels (default: 1024)"
116    )
117    parser.add_argument(
118        "--height", type=int, default=1024, help="Plot height in pixels (default: 1024)"
119    )
120    parser.add_argument(
121        "--gridsize",
122        type=int,
123        default=80,
124        help="Number of hexagons in grid (default: 100)",
125    )
126    parser.add_argument(
127        "--scaling",
128        default="sqrt",
129        help="Scaling method (default: sqrt)",
130        choices=["sqrt", "linear", "log", "power"],
131    )
132    parser.add_argument(
133        "--percentile",
134        type=float,
135        default=-1.0,
136        help="Percentile cutoff to build the dataset (default: -1.0)",
137    )
138    parser.add_argument(
139        "--log-file",
140        default="hexbin_plot.log",
141        help="Output log file name (default: hexbin_plot.log)",
142    )
143    parser.add_argument(
144        "--log-level",
145        default="INFO",
146        choices=["DEBUG", "INFO", "WARNING", "ERROR"],
147        help="Logging level (default: INFO)",
148    )
149    parser.add_argument(
150        "--verbose", action="store_true", help="Enable verbose output (default: False)"
151    )
152
153    args = parser.parse_args()
154
155    # Setup environment and logging
156    pp.set_pyvista_theme("auto")
157    logger = setup_logging(args.log_file, args.log_level)
158
159    try:
160        create_hexbin_plot(
161            plot_type=args.plot_type,
162            column1=args.column1,
163            column2=args.column2,
164            width=args.width,
165            height=args.height,
166            gridsize=args.gridsize,
167            scaling=args.scaling,
168            percentile=args.percentile,
169            verbose=args.verbose,
170        )
171        logger.info("Plot created successfully")
172    except ValueError as e:
173        logger.error("Error creating plot: %s", e)
174        sys.exit(1)

Main function to parse arguments and create hexbin plot.