Module meteor.scripts.compute_iterative_tv_map

source code for meteor.phaseboost

Functions

def main(command_line_arguments: list[str] | None = None) ‑> None
Expand source code
def main(command_line_arguments: list[str] | None = None) -> None:
    parser = IterativeTvArgParser(
        description=(
            "Compute an difference map, where the phases of the derivative structure are estimated "
            "using the assumption that the resulting map should have a low total variation. Phases "
            "are estimated using a crystallographic analog of the Gerchberg-Saxton algorithm, with "
            "TV denoising as the real-space constraint.\n\n K-weighting can optionally be used to "
            "weight the algorithm input. \n\n In the terminology adopted, this script computes a "
            "`derivative` minus a `native` map, modifying the derivative phases. Native phases,"
            "typically from a model of the `native` data, are computed from a CIF/PDB model you "
            "must provide."
        )
    )
    args = parser.parse_args(command_line_arguments)
    parser.check_output_filepaths(args)
    mapset = parser.load_difference_maps(args)

    log.info("Launching iterative TV phase retrieval", tv_weights_to_scan=args.tv_weights_to_scan)
    log.info("This will take time, typically minutes...")
    denoiser = IterativeTvDenoiser(
        tv_weights_to_scan=args.tv_weights_to_scan,
        convergence_tolerance=args.convergence_tolerance,
        max_iterations=args.max_iterations,
        verbose=True,
    )
    mapset.derivative, it_tv_metadata = denoiser(derivative=mapset.derivative, native=mapset.native)
    log.info("Convergence.")

    diffmap, kparameter_metadata = kweight_diffmap_according_to_mode(
        kweight_mode=args.kweight_mode, kweight_parameter=args.kweight_parameter, mapset=mapset
    )

    log.info("Final real-space TV denoising pass...", method="golden-section search")
    log.info("This may take some time (up to a few minutes)...")
    final_map, final_tv_metadata = tv_denoise_difference_map(diffmap, full_output=True)

    log.info(
        "Optimal TV weight found",
        weight=f"{final_tv_metadata.optimal_parameter_value:.2e}",
        final_negentropy=round(final_tv_metadata.optimal_negentropy, 4),
    )

    log.info("Writing output.", file=str(args.mtzout))
    final_map.write_mtz(args.mtzout)

    log.info("Writing metadata.", file=str(args.metadataout))
    combined_metadata = IterativeDiffmapMetadata(
        kparameter_metadata=kparameter_metadata,
        iterative_tv_iterations=it_tv_metadata,
        final_tv_pass=final_tv_metadata,
    )

    if combined_metadata.final_tv_pass.optimal_negentropy <= 0.0:
        log.warning(
            NEGATIVE_NEGENTROPY_WARNING_MESSAGE,
            final_negentropy=combined_metadata.final_tv_pass.optimal_negentropy,
        )

    with args.metadataout.open("w") as f:
        f.write(combined_metadata.model_dump_json(round_trip=True, indent=4))

Classes

class IterativeTvArgParser (*args: Any, **kwargs: Any)
Expand source code
class IterativeTvArgParser(DiffmapArgParser):
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)

        it_tv_group = self.add_argument_group("iterative TV settings")
        it_tv_group.add_argument(
            "-x",
            "--tv-weights-to-scan",
            nargs="+",
            type=float,
            default=DEFAULT_TV_WEIGHTS_TO_SCAN_AT_EACH_ITERATION,
            help=(
                "Choose what TV weights to evaluate at every iteration. Can be a single float."
                f"Default: {DEFAULT_TV_WEIGHTS_TO_SCAN_AT_EACH_ITERATION}."
            ),
        )
        it_tv_group.add_argument(
            "--convergence-tolerance",
            type=float,
            default=ITERATIVE_TV_CONVERGENCE_TOLERANCE,
            help=(
                "If the average phase change drops below this value at each iteration, stop."
                f"Default: {ITERATIVE_TV_CONVERGENCE_TOLERANCE}."
            ),
        )
        it_tv_group.add_argument(
            "--max-iterations",
            type=float,
            default=ITERATIVE_TV_MAX_ITERATIONS,
            help=(
                "If the number of iterations exceeds this value, stop."
                f"Default: {ITERATIVE_TV_MAX_ITERATIONS}."
            ),
        )

Object for parsing command line strings into Python objects.

Keyword Arguments: - prog – The name of the program (default: os.path.basename(sys.argv[0])) - usage – A usage message (default: auto-generated from arguments) - description – A description of what the program does - epilog – Text following the argument descriptions - parents – Parsers whose arguments should be copied into this one - formatter_class – HelpFormatter class for printing help messages - prefix_chars – Characters that prefix optional arguments - fromfile_prefix_chars – Characters that prefix files containing additional arguments - argument_default – The default value for all arguments - conflict_handler – String indicating how to handle conflicts - add_help – Add a -h/-help option - allow_abbrev – Allow long options to be abbreviated unambiguously - exit_on_error – Determines whether or not ArgumentParser exits with error info when an error occurs

Ancestors

  • DiffmapArgParser
  • argparse.ArgumentParser
  • argparse._AttributeHolder
  • argparse._ActionsContainer