Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Help on class understanding in pymc code Date: Mon, 14 Dec 2015 09:56:39 +0100 Organization: None Lines: 281 Message-ID: References: <8914575b-92c4-4b0e-adbf-ccb63c5edde5@googlegroups.com> <9cb4a680-0c6a-445f-b94d-2d2a06daef91@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de EUyeGzRkWGo4bTq6P7x5uQdSZ+SWbZJGv8R5U6PCdSWA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '---------': 0.05; 'elements.': 0.05; 'initialize': 0.05; 'skip:p 60': 0.05; 'attributes': 0.07; 'constructor': 0.07; 'line:': 0.07; 'puts': 0.07; 'skip:/ 10': 0.07; 'skip:\\ 10': 0.07; 'subject:code': 0.07; 'variable,': 0.07; "'no": 0.09; 'already.': 0.09; 'considered.': 0.09; 'definition,': 0.09; 'derived': 0.09; 'imports': 0.09; 'inherited': 0.09; 'instance.': 0.09; 'int.': 0.09; 'item,': 0.09; 'key)': 0.09; 'keyed': 0.09; 'mentions': 0.09; 'models.': 0.09; 'namespace': 0.09; 'otherwise)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.10; 'python': 0.10; 'appropriate': 0.14; 'value.': 0.15; 'variables': 0.15; '(data': 0.16; '__init__.py': 0.16; 'attributes:': 0.16; 'conditional': 0.16; 'container,': 0.16; 'containers': 0.16; 'descriptors': 0.16; 'dictionary,': 0.16; 'docstring': 0.16; 'fits': 0.16; 'inheritance': 0.16; 'metaclass': 0.16; 'name=none,': 0.16; 'namespace.': 0.16; 'naming': 0.16; 'nodes': 0.16; 'parameter:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'run:': 0.16; 'set,': 0.16; 'skip:[ 50': 0.16; 'subclasses.': 0.16; 'subject:class': 0.16; 'tuple,': 0.16; 'variable.': 0.16; 'weight,': 0.16; 'wrote:': 0.16; 'element': 0.18; 'module,': 0.18; 'input': 0.18; 'variable': 0.18; 'thanks.': 0.18; '>>>': 0.20; '2015': 0.20; 'class,': 0.22; 'variables.': 0.22; 'code,': 0.23; 'defined': 0.23; 'fit': 0.23; 'references': 0.23; 'sets': 0.23; 'import': 0.24; 'url:edu': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'rest': 0.26; 'skip:m 30': 0.27; 'equivalent': 0.27; 'executing': 0.27; 'object,': 0.27; 'package.': 0.27; "skip:' 10": 0.28; 'values': 0.28; 'initial': 0.28; 'resolution': 0.28; '13,': 0.29; 'dictionary': 0.29; 'factor': 0.29; 'initialized': 0.29; 'node': 0.29; 'seed': 0.29; 'subject:skip:u 10': 0.29; 'weak': 0.29; 'objects': 0.29; 'random': 0.29; 'classes': 0.30; 'too.': 0.30; 'code': 0.30; 'class.': 0.30; 'probably': 0.31; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'december': 0.32; 'says': 0.32; 'run': 0.33; 'useful': 0.33; 'class': 0.33; 'int': 0.33; 'values.': 0.33; "skip:' 20": 0.34; 'previous': 0.34; 'skip:d 20': 0.34; 'list': 0.34; 'so,': 0.35; 'exist': 0.35; 'false': 0.35; 'instance': 0.35; 'nothing.': 0.35; 'replaced': 0.35; 'robert': 0.35; 'star': 0.35; 'skip:p 30': 0.35; 'step': 0.36; 'but': 0.36; 'list,': 0.36; 'between': 0.65; 'therefore': 0.67; 'link:': 0.69; 'status:': 0.72; 'aaa': 0.84; 'order:': 0.84; 'otten': 0.84; 'potentials': 0.84; 'stochastic': 0.84; 'monte': 0.91; 'technically': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8403.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100408 Robert wrote: > On Sunday, December 13, 2015 at 8:10:25 PM UTC-5, Peter Otten wrote: >> Robert wrote: >> >> > Hi, >> > >> > I follow code example at link: >> > >> > https://users.obs.carnegiescience.edu/cburns/ipynbs/PyMC.html >> > >> > >> > There is the following code line: >> > >> > sampler = >> > pymc.MCMC([alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true]) >> > >> > >> > I want to know the detail of pymc.MCMC, then I get help content of it >> > with: >> > >> > ///////////// >> > help(pymc.MCMC) >> > Help on class MCMC in module pymc.MCMC: >> > >> > class MCMC(pymc.Model.Sampler) >> > | This class fits probability models using Markov Chain Monte Carlo. >> > | Each stochastic variable is assigned a StepMethod object, which >> > | makes it take a single MCMC step conditional on the rest of the >> > | model. These step methods are called in turn. >> > | >> > | >>> A = MCMC(input, db, verbose=0) >> > | >> > \\\\\\\\\\\\\\\\\\ >> > >> > >> > help('pymc.Model.Sampler') >> > no Python documentation found for 'pymc.Model.Sampler' >> > >> > >> > help('pymc.Model') >> > Help on class Model in pymc: >> > >> > pymc.Model = class Model(pymc.Container.ObjectContainer) >> > | The base class for all objects that fit probability models. Model >> > | is initialized with: >> > | >> > | >>> A = Model(input, verbose=0) >> > | >> > | :Parameters: >> > | - input : module, list, tuple, dictionary, set, object or >> > | nothing. >> > | Model definition, in terms of Stochastics, Deterministics, >> > | Potentials and Containers. If nothing, all nodes are >> > | collected from the base namespace. >> > | >> > | Attributes: >> > | - deterministics >> > | - stochastics (with observed=False) >> > | - data (stochastic variables with observed=True) >> > | - variables >> > | - potentials >> > | - containers >> > | - nodes >> > | - all_objects >> > | - status: Not useful for the Model base class, but may be used by >> > | subclasses. >> > | >> > | The following attributes only exist after the appropriate method is >> > | called: >> > | - moral_neighbors: The edges of the moralized graph. A >> > | dictionary, keyed by stochastic variable, >> > | whose values are sets of stochastic variables. Edges exist >> > | between the key variable and all variables in the value. >> > | Created by method _moralize. >> > | - extended_children: The extended children of self's stochastic >> > | variables. See the docstring of >> > | extend_children. This is a dictionary keyed by stochastic >> > | variable. >> > | - generations: A list of sets of stochastic variables. The >> > | members of each element only have parents in >> > | previous elements. Created by method find_generations. >> > | >> > | Methods: >> > | - sample_model_likelihood(iter): Generate and return iter >> > | samples of p(data and potentials|model). >> > | Can be used to generate Bayes' factors. >> > | >> > | :SeeAlso: Sampler, MAP, NormalApproximation, weight, Container, >> > | :graph. >> > | >> > | Method resolution order: >> > | Model >> > | pymc.Container.ObjectContainer >> > | pymc.six.NewBase >> > | pymc.Node.ContainerBase >> > | __builtin__.object >> > | >> > | Methods defined here: >> > | >> > | __init__(self, input=None, name=None, verbose=-1) >> > | Initialize a Model instance. >> > | >> > | :Parameters: >> > | - input : module, list, tuple, dictionary, set, object or >> > | nothing. >> > | Model definition, in terms of Stochastics, >> > | Deterministics, Potentials and Containers. If nothing, >> > | all nodes are collected from the base namespace. >> > | >> > | draw_from_prior(self) >> > | Sets all variables to random values drawn from joint 'prior', >> > | meaning contributions of data and potentials to the joint >> > | distribution are not considered. >> > | >> > | get_node(self, node_name) >> > | Retrieve node with passed name >> > | >> > | seed(self) >> > | Seed new initial values for the stochastics. >> > | >> > | ---------------------------------------------------------------------- >> > | Data descriptors defined here: >> > | >> > | generations >> > | >> > | ---------------------------------------------------------------------- >> > | Data and other attributes defined here: >> > | >> > | __slotnames__ = [] >> > | >> > | register = False >> > | >> > | ---------------------------------------------------------------------- >> > | Methods inherited from pymc.Container.ObjectContainer: >> > | >> > | replace(self, item, new_container, key) >> > | >> > | ---------------------------------------------------------------------- >> > | Data descriptors inherited from pymc.Container.ObjectContainer: >> > | >> > | value >> > | A copy of self, with all variables replaced by their values. >> > | >> > | ---------------------------------------------------------------------- >> > | Methods inherited from pymc.Node.ContainerBase: >> > | >> > | assimilate(self, new_container) >> > | >> > | ---------------------------------------------------------------------- >> > | Data descriptors inherited from pymc.Node.ContainerBase: >> > | >> > | __dict__ >> > | dictionary for instance variables (if defined) >> > | >> > | __weakref__ >> > | list of weak references to the object (if defined) >> > | >> > | logp >> > | The summed log-probability of all stochastic variables (data >> > | or otherwise) and factor potentials in self. >> > | >> > | ---------------------------------------------------------------------- >> > | Data and other attributes inherited from pymc.Node.ContainerBase: >> > | >> > | change_methods = [] >> > | >> > | containing_classes = [] >> > --------- >> > >> > >> > Now, I have puzzles on the class constructor input parameter: >> > [alpha,betax,betay,eps,model,tau,z_obs,x_true,y_true] >> > >> > 1. 'class MCMC(pymc.Model.Sampler)' says its inheritance is from >> > 'pymc.Model.Sampler' >> > >> > 2. When I try to get help on 'pymc.Model.Sampler', it says: >> > 'no Python documentation found for 'pymc.Model.Sampler' >> > >> > 3. When I continue to catch help on 'pymc.Model.Sampler', I don't see >> > content mentions 'Sampler'. This complete help message is shown above. >> > >> > So, what is 'pymc.Model.Sampler'? >> >> Unfortunately there is a module pymc.Model and a class pymc.Model.Model, >> and in pymc.__init__.py there is code that overwrites the module with the >> class. Therefore when you write >> >> pymc.Model >> >> you get >> >> pymc.Model.Model >> >> as you can see when you type >> >> >>> import pymc >> >>> pymc.Model >> >> >> To get around this bad naming use >> >> >>> from pymc.Model import Sampler >> >>> help(Sampler) > > Thanks. Your answer does solve the problem, but I cannot follow your > words. When you run below code, what is 'pymc.Model'? > > >>>> import pymc >>>> pymc.Model > > > > When I run: import pymc This imports the pymc package. Technically this is achieved by executing pymc/__init__.py In __init__.py there is a line from Model import * This line puts all names in pymc.Model.__all__ into the current namespace and is roughly equivalent to import Model as _tmp Model = _tmp.Model Sampler = _tmp.Sampler del _tmp so that after the star import pymc.Model is the class pymc.Model.Model. > aaa=pymc.Model Now aaa is (a name for) that class, too. > type(aaa) The type() of a class is its "metaclass". The relationship metaclass --> class is the same as class --> instance i. e. a Python class is an instance of its metaclass like 42 is an instance of int. > Out[160]: pymc.Node.ContainerMeta By default all classes derived from object are of type "type". So that's a custom metaclass. You probably don't care about that at this time in your career as a pythonista. > type(pymc.Model) > Out[161]: pymc.Node.ContainerMeta > > I see that it is not ''. Because pymc.Model is pymc.Model.Model already. The equivalent for int would be to type >>> type(int) and expecting it to return int.