from phycas import randomtree
from phycas.utilities.PhycasCommand import *
from phycas.commands.SumPImpl import ParamSummarizer
import copy

class SumP(PhycasCommand):
    def __init__(self):
        args = (
                   ("skip",   1, "Number of lines from the input list of trees to skip (first line stores starting parameter values, so this value should normally be at least 1)", IntArgValidate(min=0)),
                   ("file",    "", "Name of file containing sampled parameter values", FileExistsValidate),
                   ("cpo_cutoff", 0.10, "Identify sites with lowest CPO values. If cpo_cutoff=0.1, for example, then the lowest 10% of sites will be identified", ProbArgValidate()),
                   ("cpofile", "", "Name of file containing sampled site likelihoods for calculation of Conditional Predictive Ordinates (CPO)", FileExistsValidate),
                )
        o = PhycasCommandOutputOptions()

        o.__dict__["_help_order"] = ["log","cpoplot"]

        logf_spec = TextOutputSpec(prefix='sumpoutput', help_str="The file specified by this setting saves the console output generated by sump(). If set to None, console output will not be saved to a file.")
        o.__dict__["log"] = logf_spec

        cpoplot_spec = ROutputSpec(prefix='cpoplot', help_str="The file specified by this setting saves the R commands to produce a plot of log(CPO) values across sites grouped according to partition. If set to None, no R file will be generated.")
        o.__dict__["cpoplot"] = cpoplot_spec

        cpoinfo_spec = TextOutputSpec(prefix='cpoinfo', help_str="The file specified by this setting saves log(CPO) values and partition information for each site in tab-delimited format. If set to None, no file will be generated.")
        o.__dict__["cpoinfo"] = cpoinfo_spec

        PhycasCommand.__init__(self, args, "sump", "The sump command is used to summarize a sample of parameter values produced by an MCMC simulation.", o)

        # The data members added below should be hidden from the user because they are for use by phycas developers.
        # The roundabout way of introducing these data members is necessary because PhycasCommand.__setattr__ tries
        # to prevent users from adding new data members (to prevent accidental misspellings from causing problems)
        self.__dict__["cposmooth"] = 20.0   # ("cposmooth", 20.0, "Standard deviation to use in smoothing CPO plot", FloatArgValidate(min=1.0))

    def hidden():
        """
        Overrides the PhycasCommand.hidden method to keep SumP's name from being displayed
        in the list of classes displayed when users type help. Delete this function, or
        change its return value to False, when it is ready to be advertised.
        """
        return False

    hidden = staticmethod(hidden)

    def __call__(self, **kwargs):
        self.set(**kwargs)
        c = copy.deepcopy(self)
        param_summarizer = ParamSummarizer(c)
        marginal_likelihood = param_summarizer.run()
        return marginal_likelihood

from phycas import randomtree
from phycas.utilities.PhycasCommand import *
from phycas.commands.SumPImpl import ParamSummarizer
import copy

class SumP(PhycasCommand):
    def __init__(self):
        args = (
                   ("skip",   1, "Number of lines from the input list of trees to skip (first line stores starting parameter values, so this value should normally be at least 1)", IntArgValidate(min=0)),
                   ("file",    "", "Name of file containing sampled parameter values", FileExistsValidate),
                   ("cpo_cutoff", 0.10, "Identify sites with lowest CPO values. If cpo_cutoff=0.1, for example, then the lowest 10% of sites will be identified", ProbArgValidate()),
                   ("cpofile", "", "Name of file containing sampled site likelihoods for calculation of Conditional Predictive Ordinates (CPO)", FileExistsValidate),
                )
        o = PhycasCommandOutputOptions()

        o.__dict__["_help_order"] = ["log","cpoplot"]

        logf_spec = TextOutputSpec(prefix='sumpoutput', help_str="The file specified by this setting saves the console output generated by sump(). If set to None, console output will not be saved to a file.")
        o.__dict__["log"] = logf_spec

        cpoplot_spec = ROutputSpec(prefix='cpoplot', help_str="The file specified by this setting saves the R commands to produce a plot of log(CPO) values across sites grouped according to partition. If set to None, no R file will be generated.")
        o.__dict__["cpoplot"] = cpoplot_spec

        cpoinfo_spec = TextOutputSpec(prefix='cpoinfo', help_str="The file specified by this setting saves log(CPO) values and partition information for each site in tab-delimited format. If set to None, no file will be generated.")
        o.__dict__["cpoinfo"] = cpoinfo_spec

        PhycasCommand.__init__(self, args, "sump", "The sump command is used to summarize a sample of parameter values produced by an MCMC simulation.", o)

        # The data members added below should be hidden from the user because they are for use by phycas developers.
        # The roundabout way of introducing these data members is necessary because PhycasCommand.__setattr__ tries
        # to prevent users from adding new data members (to prevent accidental misspellings from causing problems)
        self.__dict__["cposmooth"] = 20.0   # ("cposmooth", 20.0, "Standard deviation to use in smoothing CPO plot", FloatArgValidate(min=1.0))

    def hidden():
        """
        Overrides the PhycasCommand.hidden method to keep SumP's name from being displayed
        in the list of classes displayed when users type help. Delete this function, or
        change its return value to False, when it is ready to be advertised.
        """
        return False

    hidden = staticmethod(hidden)

    def __call__(self, **kwargs):
        self.set(**kwargs)
        c = copy.deepcopy(self)
        param_summarizer = ParamSummarizer(c)
        marginal_likelihood = param_summarizer.run()
        return marginal_likelihood

# The data members added below should be hidden from the user because they are for use by phycas developers. # The roundabout way of introducing these data members is necessary because PhycasCommand.__setattr__ tries # to prevent users from adding new data members (to prevent accidental misspellings from causing problems) # ("cposmooth", 20.0, "Standard deviation to use in smoothing CPO plot", FloatArgValidate(min=1.0)) Overrides the PhycasCommand.hidden method to keep SumP's name from being displayed         in the list of classes displayed when users type help. Delete this function, or         change its return value to False, when it is ready to be advertised.