예제 #1
0
def plotit(fobs,
           sigma,
           fcalc,
           alpha,
           beta,
           epsilon,
           centric,
           out,
           limit=5.0,
           steps=1000,
           plot_title="Outlier plot"):

  fobs_a    = flex.double( [fobs] )
  fcalc_a   = flex.double( [fcalc] )
  epsilon_a = flex.double( [epsilon] )
  alpha_a   = flex.double( [alpha] )
  beta_a    = flex.double( [beta] )
  centric_a = flex.bool  ( [centric] )

  p_calc = scaling.likelihood_ratio_outlier_test(
    fobs_a,
    None,
    fcalc_a,
    epsilon_a,
    centric_a,
    alpha_a,
    beta_a)
  print >> out
  print >> out,"#Input parameters: "
  print >> out,"#Title        : ", plot_title
  print >> out,"#F-calc       : ", fcalc
  print >> out,"#F-obs        : ", fobs
  print >> out,"#epsilon      : ", epsilon
  print >> out,"#alpha        : ", alpha
  print >> out,"#beta         : ", beta
  print >> out,"#centric      : ", centric
  mode = p_calc.posterior_mode()[0]

  snd_der = math.sqrt(1.0/ math.fabs( p_calc.posterior_mode_snd_der()[0] )  )
  print >> out,"#A Gaussian approximation of the likelihood function"
  print >> out,"#could be constructed as follows with: "
  print >> out,"# exp[-(fobs-mode)**2/(2*stdev**2)] /(sqrt(2 pi) stdev)"
  print >> out,"#with"
  print >> out,"#mode         = ", mode
  print >> out,"#stdev        = ", snd_der
  print >> out
  print >> out,"#The log likelihood values for the mode and "
  print >> out,"#observed values are"
  print >> out,"#Log[P(fobs)] : ",  p_calc.log_likelihood()[0]
  print >> out,"#Log[P(mode)] : ",  p_calc.posterior_mode_log_likelihood()[0]
  print >> out,"#Their difference is:"
  print >> out,"#delta        : ",  p_calc.log_likelihood()[0]-p_calc.posterior_mode_log_likelihood()[0]
  print >> out,"#"
  mean_fobs = p_calc.mean_fobs()
  print >> out,"#mean f_obs   : ", mean_fobs[0], "   (first moment)"


  low_limit = mode-snd_der*limit
  if low_limit<0:
    low_limit=0
  high_limit = mode+limit*snd_der

  if fobs < low_limit:
    low_limit = fobs-2.0*snd_der
    if low_limit<0:
      low_limit=0
  if fobs > high_limit:
    high_limit = fobs+2.0*snd_der

  fobs_a = flex.double( range(steps) )*(
    high_limit-low_limit)/float(steps)+low_limit

  fcalc_a   = flex.double( [fcalc]*steps )
  epsilon_a = flex.double( [epsilon]*steps )
  alpha_a   = flex.double( [alpha]*steps )
  beta_a    = flex.double( [beta]*steps )
  centric_a = flex.bool  ( [centric]*steps )

  p_calc = scaling.likelihood_ratio_outlier_test(
    fobs_a,
    None,
    fcalc_a,
    epsilon_a,
    centric_a,
    alpha_a,
    beta_a)

  ll = p_calc.log_likelihood()    #-p_calc.posterior_mode_log_likelihood()
  ll = flex.exp( ll )
  if (sigma is None) or (sigma <=0 ):
    sigma=fobs/30.0

  obs_gauss = (fobs_a - fobs)/float(sigma)
  obs_gauss = flex.exp( -obs_gauss*obs_gauss/2.0 ) /(
    math.sqrt(2.0*math.pi*sigma*sigma))

  max_ll = flex.max( ll )*1.10
  truncate_mask = flex.bool( obs_gauss >= max_ll )
  obs_gauss = obs_gauss.set_selected( truncate_mask, max_ll )


  ccp4_loggraph_plot = data_plots.plot_data(
    plot_title=plot_title,
    x_label = 'Fobs',
    y_label = 'P(Fobs)',
    x_data = fobs_a,
    y_data = ll,
    y_legend = 'P(Fobs|Fcalc,alpha,beta)',
    comments = 'Fobs=%5.2f, sigma=%5.2f, Fcalc=%5.2f'%(fobs,sigma,fcalc) )
  ccp4_loggraph_plot.add_data(
    y_data = obs_gauss,
    y_legend = "P(Fobs|<Fobs>,sigma)"
    )
  data_plots.plot_data_loggraph(ccp4_loggraph_plot,out)
def example():
    x_obs = (flex.double(range(100)) + 1.0) / 101.0
    y_ideal = flex.sin(x_obs * 6.0 * 3.1415) + flex.exp(x_obs)
    y_obs = y_ideal + (flex.random_double(size=x_obs.size()) - 0.5) * 0.5
    w_obs = flex.double(x_obs.size(), 1)
    print "Trying to determine the best number of terms "
    print " via cross validation techniques"
    print
    n_terms = chebyshev_lsq_fit.cross_validate_to_determine_number_of_terms(
        x_obs, y_obs, w_obs, min_terms=5, max_terms=20, n_goes=20, n_free=20)
    print "Fitting with", n_terms, "terms"
    print
    fit = chebyshev_lsq_fit.chebyshev_lsq_fit(n_terms, x_obs, y_obs)
    print "Least Squares residual: %7.6f" % (fit.f)
    print "  R2-value            : %7.6f" % (fit.f / flex.sum(y_obs * y_obs))
    print
    fit_funct = chebyshev_polynome(n_terms, fit.low_limit, fit.high_limit,
                                   fit.coefs)

    y_fitted = fit_funct.f(x_obs)
    abs_deviation = flex.max(flex.abs((y_ideal - y_fitted)))
    print "Maximum deviation between fitted and error free data:"
    print "    %4.3f" % (abs_deviation)
    abs_deviation = flex.mean(flex.abs((y_ideal - y_fitted)))
    print "Mean deviation between fitted and error free data:"
    print "    %4.3f" % (abs_deviation)
    print
    abs_deviation = flex.max(flex.abs((y_obs - y_fitted)))
    print "Maximum deviation between fitted and observed data:"
    print "    %4.3f" % (abs_deviation)
    abs_deviation = flex.mean(flex.abs((y_obs - y_fitted)))
    print "Mean deviation between fitted and observed data:"
    print "    %4.3f" % (abs_deviation)
    print
    print "Showing 10 points"
    print "   x    y_obs y_ideal y_fit"
    for ii in range(10):
        print "%6.3f %6.3f %6.3f %6.3f" \
              %(x_obs[ii*9], y_obs[ii*9], y_ideal[ii*9], y_fitted[ii*9])

    try:
        from iotbx import data_plots
    except ImportError:
        pass
    else:
        print "Preparing output for loggraph in a file called"
        print "   chebyshev.loggraph"
        chebyshev_plot = data_plots.plot_data(plot_title='Chebyshev fitting',
                                              x_label='x values',
                                              y_label='y values',
                                              x_data=x_obs,
                                              y_data=y_obs,
                                              y_legend='Observed y values',
                                              comments='Chebyshev fit')
        chebyshev_plot.add_data(y_data=y_ideal, y_legend='Error free y values')
        chebyshev_plot.add_data(y_data=y_fitted,
                                y_legend='Fitted chebyshev approximation')
        output_logfile = open('chebyshev.loggraph', 'w')
        f = StringIO()
        data_plots.plot_data_loggraph(chebyshev_plot, f)
        output_logfile.write(f.getvalue())
def example():
  x_obs = (flex.double(range(100))+1.0)/101.0
  y_ideal = flex.sin(x_obs*6.0*3.1415) + flex.exp(x_obs)
  y_obs = y_ideal + (flex.random_double(size=x_obs.size())-0.5)*0.5
  w_obs = flex.double(x_obs.size(),1)
  print "Trying to determine the best number of terms "
  print " via cross validation techniques"
  print
  n_terms = chebyshev_lsq_fit.cross_validate_to_determine_number_of_terms(
    x_obs,y_obs,w_obs,
    min_terms=5 ,max_terms=20,
    n_goes=20,n_free=20)
  print "Fitting with", n_terms, "terms"
  print
  fit = chebyshev_lsq_fit.chebyshev_lsq_fit(n_terms,x_obs,y_obs)
  print "Least Squares residual: %7.6f" %(fit.f)
  print "  R2-value            : %7.6f" %(fit.f/flex.sum(y_obs*y_obs))
  print
  fit_funct = chebyshev_polynome(
    n_terms, fit.low_limit, fit.high_limit, fit.coefs)

  y_fitted = fit_funct.f(x_obs)
  abs_deviation = flex.max(
    flex.abs( (y_ideal- y_fitted) ) )
  print "Maximum deviation between fitted and error free data:"
  print "    %4.3f" %(abs_deviation)
  abs_deviation = flex.mean(
    flex.abs( (y_ideal- y_fitted) ) )
  print "Mean deviation between fitted and error free data:"
  print "    %4.3f" %(abs_deviation)
  print
  abs_deviation = flex.max(
    flex.abs( (y_obs- y_fitted) ) )
  print "Maximum deviation between fitted and observed data:"
  print "    %4.3f" %(abs_deviation)
  abs_deviation = flex.mean(
    flex.abs( (y_obs- y_fitted) ) )
  print "Mean deviation between fitted and observed data:"
  print "    %4.3f" %(abs_deviation)
  print
  print "Showing 10 points"
  print "   x    y_obs y_ideal y_fit"
  for ii in range(10):
    print "%6.3f %6.3f %6.3f %6.3f" \
          %(x_obs[ii*9], y_obs[ii*9], y_ideal[ii*9], y_fitted[ii*9])

  try:
    from iotbx import data_plots
  except ImportError:
    pass
  else:
    print "Preparing output for loggraph in a file called"
    print "   chebyshev.loggraph"
    chebyshev_plot = data_plots.plot_data(plot_title='Chebyshev fitting',
                                          x_label = 'x values',
                                          y_label = 'y values',
                                          x_data = x_obs,
                                          y_data = y_obs,
                                          y_legend = 'Observed y values',
                                          comments = 'Chebyshev fit')
    chebyshev_plot.add_data(y_data=y_ideal,
                            y_legend='Error free y values')
    chebyshev_plot.add_data(y_data=y_fitted,
                            y_legend='Fitted chebyshev approximation')
    output_logfile=open('chebyshev.loggraph','w')
    f = StringIO()
    data_plots.plot_data_loggraph(chebyshev_plot,f)
    output_logfile.write(f.getvalue())