2016-12-06 42 views
0

PyMC3トレースプロット関数(here参照)で生成されたサブプロットをファイルにプロットするのは非常に簡単です。PyMC3 traceplotサブプロットを画像ファイルに保存

この関数は、サブプロットのnumpy.ndarray(2d)を生成します。

イメージファイルを保存するために、これらのサブプロットをmatplotlib.figureに移動またはコピーする必要があります。私が見つけることができるすべては、Figureのサブプロットを最初にに生成する方法を示しています。

最小の例として、私はサンプルPyMC3コードをHereから持ち上げ、サブプロットを処理するために数行追加しました。

from pymc3 import * 
import theano.tensor as tt 
from theano import as_op 
from numpy import arange, array, empty 

### Added these three lines relative to source ####################### 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 

__all__ = ['disasters_data', 'switchpoint', 'early_mean', 'late_mean', 'rate', 'disasters'] 

# Time series of recorded coal mining disasters in the UK from 1851 to 1962 
disasters_data = array([4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6, 
         3, 3, 5, 4, 5, 3, 1, 4, 4, 1, 5, 5, 3, 4, 2, 5, 
         2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0, 
         1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1, 
         0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2, 
         3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4, 
         0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1]) 
years = len(disasters_data) 

@as_op(itypes=[tt.lscalar, tt.dscalar, tt.dscalar], otypes=[tt.dvector]) 
def rateFunc(switchpoint, early_mean, late_mean): 
    out = empty(years) 
    out[:switchpoint] = early_mean 
    out[switchpoint:] = late_mean 
    return out 


with Model() as model: 

    # Prior for distribution of switchpoint location 
    switchpoint = DiscreteUniform('switchpoint', lower=0, upper=years) 
    # Priors for pre- and post-switch mean number of disasters 
    early_mean = Exponential('early_mean', lam=1.) 
    late_mean = Exponential('late_mean', lam=1.) 

    # Allocate appropriate Poisson rates to years before and after current switchpoint location 
    rate = rateFunc(switchpoint, early_mean, late_mean) 

    # Data likelihood 
    disasters = Poisson('disasters', rate, observed=disasters_data) 

    # Initial values for stochastic nodes 
    start = {'early_mean': 2., 'late_mean': 3.} 

    # Use slice sampler for means 
    step1 = Slice([early_mean, late_mean]) 
    # Use Metropolis for switchpoint, since it accomodates discrete variables 
    step2 = Metropolis([switchpoint]) 

    # njobs>1 works only with most recent (mid August 2014) Thenao version: 
    # https://github.com/Theano/Theano/pull/2021 
    tr = sample(1000, tune=500, start=start, step=[step1, step2], njobs=1) 

    ### gnashing of teeth starts here ################################ 
    fig, axarr = plt.subplots(3,2) 

    # This gives a KeyError 
    # axarr = traceplot(tr, axarr) 

    # This finishes without error 
    trarr = traceplot(tr) 

    # doesn't work 
    # axarr[0, 0] = trarr[0, 0] 

    fig.savefig("disaster.png") 

私は無駄に、サブプロット()とadd_subplot()線に沿っていくつかのバリエーションを試してみた - すべてのエラーが空のサブプロットは、最初の図のために作成しなければならないという事実に向かって指して、に割り当てられていません既存のサブプロット。

異なる例は、(

### Mysterious code to be explained in Chapter 3. 

から始まる、道の約80%ダウン、hereを参照してください)完全に実用性を回避し、手動でサブプロットを構築し、ので、多分これには良い答えがありませんか? pymc3.traceplotの出力は実際には使用できないサブプロットの孤立したndarrayですか?

答えて

2

type(trarr[0,0])を印刷して結果を掲載できますか?

まず、matplotlib軸オブジェクトは図の一部であり、図の中にしか存在できません。したがって、単に軸を取って別の図形にすることはできません。しかし、あなたのケースではかもしれない、そのfig.add_axes(trarr[0,0])にもかかわらず動作します。私はそれを疑うが、あなたはまだ試してみることができる。

traceplot()には、キーワード引数があります。

ax:軸 Matplotlib軸。デフォルトはNoneです。

いくつかのサブプロットを1つの軸オブジェクトとして指定するにはどうすればよいでしょうか。単一軸を作成するか、独自の作成したサブプロット軸配列axarrを配置するか、その一部だけを配置してください。

編集、ちょうどその誰もがコメントで小さな行を監督:
bug reportでの回答によると、traceplot(tr, ax = axarr)は確かにうまく動作することが報告されています。

+0

ご意見ありがとうございます。 'type(trarr [0,0])'はです。私は 'fig.add_axes(...)'の提案を試してみました。私も、プロットされたnumpl.ndarrayのサブプロットをtraceplot関数に渡すことで遊んでいましたが、それはエラーの全く別のスレッドを生成します[ここでのバグ報告](https://github.com/pymc-devs/pymc3/問題/ 1577)。 – GoneAsync

+0

EDIT: 'traceplot(tr、ax = axarr)'の引数にkeyworkを使うのを覚えていればうまく動作します。 – GoneAsync