<commit_before># -*- encoding: utf-8 -*-
"""The main function."""

import argparse
import codecs
import locale
import os
import shutil
import sys
import time
from operator import attrgetter
from unicodedata import normalize

import numpy as np

from mosfit import __author__, __contributors__, __version__
from mosfit.fitter import Fitter
from mosfit.printer import Printer
from mosfit.utils import get_mosfit_hash, is_master, open_atomic, speak


class SortingHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
    """Sort argparse arguments by argument name."""

    def add_arguments(self, actions):
        """Add sorting action based on `option_strings`."""
        actions = sorted(actions, key=attrgetter('option_strings'))
        super(SortingHelpFormatter, self).add_arguments(actions)


def get_parser(only=None, printer=None):
    """Retrieve MOSFiT's `argparse.ArgumentParser` object."""
    prt = Printer() if printer is None else printer

    parser = argparse.ArgumentParser(
        prog='mosfit',
        description='Fit astrophysical transients.',
        formatter_class=SortingHelpFormatter,
        add_help=only is None)

    parser.add_argument(
        '--language',
        dest='language',
        type=str,
        const='select',
        default='en',
        nargs='?',
        help=("Language for output text."))

    if only == 'language':
        return parser

    parser.add_argument(
        '--events',
        '-e',
        dest='events',
        default=[],
        nargs='+',
        help=prt.message('parser_events'))

    parser.add_argument(
        '--models',
        '-m',
        dest='models',
        default=[],
        nargs='?',
        help=prt.message('parser_models'))

    parser.add_argument(
        '--parameter-paths',
        '-P',
        dest='parameter_paths',
        default=['parameters.json'],
        nargs='+',
        help=prt.message('parser_parameter_paths'))

    parser.add_argument(
        '--walker-paths',
        '-w',
        dest='walker_paths',
        default=[],
        nargs='+',
        help=prt.message('parser_walker_paths'))

    parser.add_argument(
        '--max-time',
        dest='max_time',
        type=float,
        default=1000.,
        help=prt.message('parser_max_time'))

    parser.add_argument(
        '--limiting-magnitude',
        '-l',
        dest='limiting_magnitude',
        default=None,
        nargs='+',
        help=prt.message('parser_limiting_magnitude'))

    parser.add_argument(
        '--band-list',
        '--extra-bands',
        dest='band_list',
        default=[],
        nargs='+',
        help=prt.message('parser_band_list'))

    parser.add_argument(
        '--band-systems',
        '--extra-systems',
        dest='band_systems',
        default=[],
        nargs='+',
        help=prt.message('parser_band_systems'))

    parser.add_argument(
        '--band-instruments',
        '--extra-instruments',
        dest='band_instruments',
        default=[],
        nargs='+',
        help=prt.message('parser_band_instruments'))

    parser.add_argument(
        '--band-bandsets',
        '--extra-bandsets',
        dest='band_bandsets',
        default=[],
        nargs='+',
        help=prt.message('parser_band_bandsets'))

    parser.add_argument(
        '--band-sampling-points',
        dest='band_sampling_points',
        type=int,
        default=17,
        help=prt.message('parser_band_sampling_points'))

    parser.add_argument(
        '--exclude-bands',
        dest='exclude_bands',
        default=[],
        nargs='+',
        help=prt.message('parser_exclude_bands'))

    parser.add_argument(
        '--exclude-instruments',
        dest='exclude_instruments',
        default=[],
        nargs='+',
        help=prt.message('parser_exclude_instruments'))

    parser.add_argument(
        '--exclude-systems',
        dest='exclude_systems',
        default=[],
        nargs='+',
        help=prt.message('parser_exclude_systems'))

    parser.add_argument(
        '--exclude-sources',
        dest='exclude_sources',
        default=[],
        nargs='+',
        help=prt.message('parser_exclude_sources'))

    parser.add_argument(
        '--exclude-kinds',
        dest='exclude_kinds',
        default=[],
        nargs='+',
        help=prt.message('parser_exclude_kinds'))

    parser.add_argument(
        '--fix-parameters',
        '-F',
        dest='user_fixed_parameters',
        default=[],
        nargs='+',
        help=prt.message('parser_user_fixed_parameters'))

    parser.add_argument(
        '--iterations',
        '-i',
        dest='iterations',
        type=int,
        const=0,
        default=-1,
        nargs='?',
        help=prt.message('parser_iterations'))

    parser.add_argument(
        '--smooth-times',
        '--plot-points',
        '-S',
        dest='smooth_times',
        type=int,
        const=0,
        default=20,
        nargs='?',
        action='store',
        help=prt.message('parser_smooth_times'))

    parser.add_argument(
        '--extrapolate-time',
        '-E',
        dest='extrapolate_time',
        type=float,
        default=0.0,
        nargs='*',
        help=prt.message('parser_extrapolate_time'))

    parser.add_argument(
        '--limit-fitting-mjds',
        '-L',
        dest='limit_fitting_mjds',
        type=float,
        default=False,
        nargs=2,
        help=prt.message('parser_limit_fitting_mjds'))

    parser.add_argument(
        '--suffix',
        '-s',
        dest='suffix',
        default='',
        help=prt.message('parser_suffix'))

    parser.add_argument(
        '--num-walkers',
        '-N',
        dest='num_walkers',
        type=int,
        default=None,
        help=prt.message('parser_num_walkers'))

    parser.add_argument(
        '--num-temps',
        '-T',
        dest='num_temps',
        type=int,
        default=1,
        help=prt.message('parser_num_temps'))

    parser.add_argument(
        '--no-fracking',
        dest='fracking',
        default=True,
        action='store_false',
        help=prt.message('parser_fracking'))

    parser.add_argument(
        '--no-write',
        dest='write',
        default=True,
        action='store_false',
        help=prt.message('parser_write'))

    parser.add_argument(
        '--quiet',
        dest='quiet',
        default=False,
        action='store_true',
        help=prt.message('parser_quiet'))

    parser.add_argument(
        '--cuda',
        dest='cuda',
        default=False,
        action='store_true',
        help=prt.message('parser_cuda'))

    parser.add_argument(
        '--no-copy-at-launch',
        dest='copy',
        default=True,
        action='store_false',
        help=prt.message('parser_copy'))

    parser.add_argument(
        '--force-copy-at-launch',
        dest='force_copy',
        default=False,
        action='store_true',
        help=prt.message('parser_force_copy'))

    parser.add_argument(
        '--offline',
        dest='offline',
        default=False,
        action='store_true',
        help=prt.message('parser_offline'))

    parser.add_argument(
        '--frack-step',
        '-f',
        dest='frack_step',
        type=int,
        help=prt.message('parser_frack_step'))

    parser.add_argument(
        '--burn',
        '-b',
        dest='burn',
        type=int,
        help=prt.message('parser_burn'))

    parser.add_argument(
        '--post-burn',
        '-p',
        dest='post_burn',
        type=int,
        help=prt.message('parser_post_burn'))

    parser.add_argument(
        '--upload',
        '-u',
        dest='upload',
        default=False,
        action='store_true',
        help=prt.message('parser_upload'))

    parser.add_argument(
        '--run-until-converged',
        '-R',
        dest='run_until_converged',
        type=float,
        default=False,
        const=True,
        nargs='?',
        help=prt.message('parser_run_until_converged'))

    parser.add_argument(
        '--run-until-uncorrelated',
        '-U',
        dest='run_until_uncorrelated',
        type=int,
        default=None,
        const=5,
        nargs='?',
        help=prt.message('parser_run_until_uncorrelated'))

    parser.add_argument(
        '--maximum-walltime',
        '-W',
        dest='maximum_walltime',
        type=float,
        default=False,
        help=prt.message('parser_maximum_walltime'))

    parser.add_argument(
        '--maximum-memory',
        '-M',
        dest='maximum_memory',
        type=float,
        default=np.inf,
        help=prt.message('parser_maximum_memory'))

    parser.add_argument(
        '--draw-above-likelihood',
        '-d',
        dest='draw_above_likelihood',
        type=float,
        default=False,
        const=True,
        nargs='?',
        help=prt.message('parser_draw_above_likelihood'))

    parser.add_argument(
        '--gibbs',
        '-g',
        dest='gibbs',
        default=False,
        action='store_true',
        help=prt.message('parser_gibbs'))

    parser.add_argument(
        '--save-full-chain',
        '-c',
        dest='save_full_chain',
        default=False,
        action='store_true',
        help=prt.message('parser_save_full_chain'))

    parser.add_argument(
        '--print-trees',
        dest='print_trees',
        default=False,
        action='store_true',
        help=prt.message('parser_print_trees'))

    parser.add_argument(
        '--set-upload-token',
        dest='set_upload_token',
        const=True,
        default=False,
        nargs='?',
        help=prt.message('parser_set_upload_token'))

    parser.add_argument(
        '--ignore-upload-quality',
        dest='check_upload_quality',
        default=True,
        action='store_false',
        help=prt.message('parser_check_upload_quality'))

    parser.add_argument(
        '--test',
        dest='test',
        default=False,
        action='store_true',
        help=prt.message('parser_test'))

    parser.add_argument(
        '--variance-for-each',
        dest='variance_for_each',
        default=[],
        nargs='+',
        help=prt.message('parser_variance_for_each'))

    parser.add_argument(
        '--speak',
        dest='speak',
        const='en',
        default=False,
        nargs='?',
        help=prt.message('parser_speak'))

    parser.add_argument(
        '--version',
        dest='version',
        default=False,
        action='store_true',
        help=prt.message('parser_version'))

    parser.add_argument(
        '--extra-outputs',
        '-x',
        dest='extra_outputs',
        default=[],
        nargs='+',
        help=prt.message('parser_extra_outputs'))

    parser.add_argument(
        '--catalogs',
        '-C',
        dest='catalogs',
        default=[],
        nargs='+',
        help=prt.message('parser_catalogs'))

    parser.add_argument(
        '--open-in-browser',
        '-O',
        dest='open_in_browser',
        default=False,
        action='store_true',
        help=prt.message('parser_open_in_browser'))

    parser.add_argument(
        '--exit-on-prompt',
        dest='exit_on_prompt',
        default=False,
        action='store_true',
        help=prt.message('parser_exit_on_prompt'))

    parser.add_argument(
        '--download-recommended-data',
        dest='download_recommended_data',
        default=False,
        action='store_true',
        help=prt.message('parser_download_recommended_data'))

    parser.add_argument(
        '--local-data-only',
        dest='local_data_only',
        default=False,
        action='store_true',
        help=prt.message('parser_local_data_only'))

    parser.add_argument(
        '--method',
        '-D',
        dest='method',
        type=str,
        const='select',
        default='ensembler',
        nargs='?',
        help=("Method for computing posteriors. Current options are "
              "`ensembler` and `nester`."))

    return parser


def main():
    """Run MOSFiT."""
    prt = Printer(<commit_msg>MAINT: merge<commit_after># -*- encoding: utf-8 -*-
"""The main function."""

import argparse
import codecs
import locale
import os
import shutil
import sys
import time
from operator import attrgetter
from unicodedata import normalize

import numpy as np

from mosfit import __author__, __contributors__, __version__
from mosfit.fitter import Fitter
from mosfit.printer import Printer
from mosfit.utils import get_mosfit_hash, is_master, open_atomic, speak


class SortingHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
    """Sort argparse arguments by argument name."""

    def add_arguments(self, actions):
        """Add sorting action based on `option_strings`."""
        actions = sorted(actions, key=attrgetter('option_strings'))
        super(SortingHelpFormatter, self).add_arguments(actions)


def get_parser(only=None, printer=None):
    """Retrieve MOSFiT's `argparse.ArgumentParser` object."""
    prt = Printer() if printer is None else printer

    parser = argparse.ArgumentParser(
        prog='mosfit',
        description='Fit astrophysical transients.',
        formatter_class=SortingHelpFormatter,
        add_help=only is None)

    parser.add_argument(
        '--language',
        dest='language',
        type=str,
        const='select',
        default='en',
        nargs='?',
        help=("Language for output text."))

    if only == 'language':
        return parser

    parser.add_argument(
        '--events',
        '-e',
        dest='events',
        default=[],
        nargs='+',
        help=prt.text('parser_events'))

    parser.add_argument(
        '--models',
        '-m',
        dest='models',
        default=[],
        nargs='?',
        help=prt.text('parser_models'))

    parser.add_argument(
        '--parameter-paths',
        '-P',
        dest='parameter_paths',
        default=['parameters.json'],
        nargs='+',
        help=prt.text('parser_parameter_paths'))

    parser.add_argument(
        '--walker-paths',
        '-w',
        dest='walker_paths',
        default=[],
        nargs='+',
        help=prt.text('parser_walker_paths'))

    parser.add_argument(
        '--max-time',
        dest='max_time',
        type=float,
        default=1000.,
        help=prt.text('parser_max_time'))

    parser.add_argument(
        '--limiting-magnitude',
        '-l',
        dest='limiting_magnitude',
        default=None,
        nargs='+',
        help=prt.text('parser_limiting_magnitude'))

    parser.add_argument(
        '--band-list',
        '--extra-bands',
        dest='band_list',
        default=[],
        nargs='+',
        help=prt.text('parser_band_list'))

    parser.add_argument(
        '--band-systems',
        '--extra-systems',
        dest='band_systems',
        default=[],
        nargs='+',
        help=prt.text('parser_band_systems'))

    parser.add_argument(
        '--band-instruments',
        '--extra-instruments',
        dest='band_instruments',
        default=[],
        nargs='+',
        help=prt.text('parser_band_instruments'))

    parser.add_argument(
        '--band-bandsets',
        '--extra-bandsets',
        dest='band_bandsets',
        default=[],
        nargs='+',
        help=prt.text('parser_band_bandsets'))

    parser.add_argument(
        '--band-sampling-points',
        dest='band_sampling_points',
        type=int,
        default=17,
        help=prt.text('parser_band_sampling_points'))

    parser.add_argument(
        '--exclude-bands',
        dest='exclude_bands',
        default=[],
        nargs='+',
        help=prt.text('parser_exclude_bands'))

    parser.add_argument(
        '--exclude-instruments',
        dest='exclude_instruments',
        default=[],
        nargs='+',
        help=prt.text('parser_exclude_instruments'))

    parser.add_argument(
        '--exclude-systems',
        dest='exclude_systems',
        default=[],
        nargs='+',
        help=prt.text('parser_exclude_systems'))

    parser.add_argument(
        '--exclude-sources',
        dest='exclude_sources',
        default=[],
        nargs='+',
        help=prt.text('parser_exclude_sources'))

    parser.add_argument(
        '--exclude-kinds',
        dest='exclude_kinds',
        default=[],
        nargs='+',
        help=prt.text('parser_exclude_kinds'))

    parser.add_argument(
        '--fix-parameters',
        '-F',
        dest='user_fixed_parameters',
        default=[],
        nargs='+',
        help=prt.text('parser_user_fixed_parameters'))

    parser.add_argument(
        '--iterations',
        '-i',
        dest='iterations',
        type=int,
        const=0,
        default=-1,
        nargs='?',
        help=prt.text('parser_iterations'))

    parser.add_argument(
        '--smooth-times',
        '--plot-points',
        '-S',
        dest='smooth_times',
        type=int,
        const=0,
        default=20,
        nargs='?',
        action='store',
        help=prt.text('parser_smooth_times'))

    parser.add_argument(
        '--extrapolate-time',
        '-E',
        dest='extrapolate_time',
        type=float,
        default=0.0,
        nargs='*',
        help=prt.text('parser_extrapolate_time'))

    parser.add_argument(
        '--limit-fitting-mjds',
        '-L',
        dest='limit_fitting_mjds',
        type=float,
        default=False,
        nargs=2,
        help=prt.text('parser_limit_fitting_mjds'))

    parser.add_argument(
        '--suffix',
        '-s',
        dest='suffix',
        default='',
        help=prt.text('parser_suffix'))

    parser.add_argument(
        '--num-walkers',
        '-N',
        dest='num_walkers',
        type=int,
        default=None,
        help=prt.text('parser_num_walkers'))

    parser.add_argument(
        '--num-temps',
        '-T',
        dest='num_temps',
        type=int,
        default=1,
        help=prt.text('parser_num_temps'))

    parser.add_argument(
        '--no-fracking',
        dest='fracking',
        default=True,
        action='store_false',
        help=prt.text('parser_fracking'))

    parser.add_argument(
        '--no-write',
        dest='write',
        default=True,
        action='store_false',
        help=prt.text('parser_write'))

    parser.add_argument(
        '--quiet',
        dest='quiet',
        default=False,
        action='store_true',
        help=prt.text('parser_quiet'))

    parser.add_argument(
        '--cuda',
        dest='cuda',
        default=False,
        action='store_true',
        help=prt.text('parser_cuda'))

    parser.add_argument(
        '--no-copy-at-launch',
        dest='copy',
        default=True,
        action='store_false',
        help=prt.text('parser_copy'))

    parser.add_argument(
        '--force-copy-at-launch',
        dest='force_copy',
        default=False,
        action='store_true',
        help=prt.text('parser_force_copy'))

    parser.add_argument(
        '--offline',
        dest='offline',
        default=False,
        action='store_true',
        help=prt.text('parser_offline'))

    parser.add_argument(
        '--frack-step',
        '-f',
        dest='frack_step',
        type=int,
        help=prt.text('parser_frack_step'))

    parser.add_argument(
        '--burn',
        '-b',
        dest='burn',
        type=int,
        help=prt.text('parser_burn'))

    parser.add_argument(
        '--post-burn',
        '-p',
        dest='post_burn',
        type=int,
        help=prt.text('parser_post_burn'))

    parser.add_argument(
        '--upload',
        '-u',
        dest='upload',
        default=False,
        action='store_true',
        help=prt.text('parser_upload'))

    parser.add_argument(
        '--run-until-converged',
        '-R',
        dest='run_until_converged',
        type=float,
        default=False,
        const=True,
        nargs='?',
        help=prt.text('parser_run_until_converged'))

    parser.add_argument(
        '--run-until-uncorrelated',
        '-U',
        dest='run_until_uncorrelated',
        type=int,
        default=None,
        const=5,
        nargs='?',
        help=prt.text('parser_run_until_uncorrelated'))

    parser.add_argument(
        '--maximum-walltime',
        '-W',
        dest='maximum_walltime',
        type=float,
        default=False,
        help=prt.text('parser_maximum_walltime'))

    parser.add_argument(
        '--maximum-memory',
        '-M',
        dest='maximum_memory',
        type=float,
        default=np.inf,
        help=prt.text('parser_maximum_memory'))

    parser.add_argument(
        '--draw-above-likelihood',
        '-d',
        dest='draw_above_likelihood',
        type=float,
        default=False,
        const=True,
        nargs='?',
        help=prt.text('parser_draw_above_likelihood'))

    parser.add_argument(
        '--gibbs',
        '-g',
        dest='gibbs',
        default=False,
        action='store_true',
        help=prt.text('parser_gibbs'))

    parser.add_argument(
        '--save-full-chain',
        '-c',
        dest='save_full_chain',
        default=False,
        action='store_true',
        help=prt.text('parser_save_full_chain'))

    parser.add_argument(
        '--print-trees',
        dest='print_trees',
        default=False,
        action='store_true',
        help=prt.text('parser_print_trees'))

    parser.add_argument(
        '--set-upload-token',
        dest='set_upload_token',
        const=True,
        default=False,
        nargs='?',
        help=prt.text('parser_set_upload_token'))

    parser.add_argument(
        '--ignore-upload-quality',
        dest='check_upload_quality',
        default=True,
        action='store_false',
        help=prt.text('parser_check_upload_quality'))

    parser.add_argument(
        '--test',
        dest='test',
        default=False,
        action='store_true',
        help=prt.text('parser_test'))

    parser.add_argument(
        '--variance-for-each',
        dest='variance_for_each',
        default=[],
        nargs='+',
        help=prt.text('parser_variance_for_each'))

    parser.add_argument(
        '--speak',
        dest='speak',
        const='en',
        default=False,
        nargs='?',
        help=prt.text('parser_speak'))

    parser.add_argument(
        '--version',
        dest='version',
        default=False,
        action='store_true',
        help=prt.text('parser_version'))

    parser.add_argument(
        '--extra-outputs',
        '-x',
        dest='extra_outputs',
        default=[],
        nargs='+',
        help=prt.text('parser_extra_outputs'))

    parser.add_argument(
        '--catalogs',
        '-C',
        dest='catalogs',
        default=[],
        nargs='+',
        help=prt.text('parser_catalogs'))

    parser.add_argument(
        '--open-in-browser',
        '-O',
        dest='open_in_browser',
        default=False,
        action='store_true',
        help=prt.text('parser_open_in_browser'))

    parser.add_argument(
        '--exit-on-prompt',
        dest='exit_on_prompt',
        default=False,
        action='store_true',
        help=prt.text('parser_exit_on_prompt'))

    parser.add_argument(
        '--download-recommended-data',
        dest='download_recommended_data',
        default=False,
        action='store_true',
        help=prt.text('parser_download_recommended_data'))

    parser.add_argument(
        '--local-data-only',
        dest='local_data_only',
        default=False,
        action='store_true',
        help=prt.text('parser_local_data_only'))

    parser.add_argument(
        '--method',
        '-D',
        dest='method',
        type=str,
        const='select',
        default='ensembler',
        nargs='?',
        help=("Method for computing posteriors. Current options are "
              "`ensembler` and `nester`."))

    return parser


def main():
    """Run MOSFiT."""
    prt = Printer(
{'fraction_non_alphanumeric': 0.14844529431326903, 'fraction_numerical': 0.0014133688061190555, 'mean_word_length': 2.043648785425101, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1821403', 'n_tokens_mistral': 7624, 'n_tokens_neox': 7320, 'n_words': 1030}