|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Escrevendo em python3 e usando python2.6: |
| 5 | +from __future__ import print_function, unicode_literals, absolute_import, division |
| 6 | + |
| 7 | +from numpy import exp, arange, degrees, array, sum,std, sqrt, median, polyfit, polyval, abs, log2 |
| 8 | +import scipy.interpolate as intp |
| 9 | +import scipy.optimize as opt |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +from collections import deque |
| 12 | + |
| 13 | +# phase functions: |
| 14 | +func_kaa = lambda p, x : p[0]*exp(-x/(float(p[1]) + 1e-20)) + p[2] + p[3]*x |
| 15 | +func_sch = lambda p, x: p[1] - p[0]/(1e0 + x + 1e-20) + p[2]*x |
| 16 | + |
| 17 | + |
| 18 | +# Hapke Model |
| 19 | +def Hapke(self,phase_range,**args): |
| 20 | + |
| 21 | + import hapke_model_v2 as hapke |
| 22 | + |
| 23 | + h = hapke(phase_range,**args) |
| 24 | + |
| 25 | + # Make a phase curve with Hapke Model |
| 26 | + r = iRefl(h.g,h.args) |
| 27 | + ph = degrees(h.g) |
| 28 | + |
| 29 | + return r, ph |
| 30 | + |
| 31 | + |
| 32 | +# Sum of square deviations |
| 33 | +def ssd(guess, obs, ph, actual): |
| 34 | + |
| 35 | + res = lambda p, obs, ph: abs(obs - func_kaa(p, ph)) |
| 36 | + sol,cov,infodict,mesg,ier = opt.leastsq(res, guess, args=(obs, ph), full_output=True) |
| 37 | + |
| 38 | + return sol, (sol - actual)**2 |
| 39 | + |
| 40 | +####################################################################################### |
| 41 | +####################### Class: Best Number of Point Finder ############################ |
| 42 | +####################################################################################### |
| 43 | + |
| 44 | + |
| 45 | +class BestNPoints: |
| 46 | + |
| 47 | + def __init__(self): |
| 48 | + pass |
| 49 | + |
| 50 | + def MakeCurve(self,r,ph,**arg): |
| 51 | + |
| 52 | + self.I = r |
| 53 | + self.ph = ph |
| 54 | + |
| 55 | + # Fit |
| 56 | + self.spline = intp.UnivariateSpline(self.ph,self.I, k=5, s=0) |
| 57 | + |
| 58 | + # Linear part of the phase curve |
| 59 | + self.linear = polyfit(arange(12e0,35e0,1e0),self.spline.__call__(arange(12e0,35e0,1e0)),deg=1) |
| 60 | + |
| 61 | + def RandomSet(self,loc,N,SNR): |
| 62 | + from random import uniform, sample, gauss |
| 63 | + |
| 64 | + spline = self.spline |
| 65 | + |
| 66 | + phase_rand = sample(arange(loc[0],loc[1],loc[2]),N) |
| 67 | + #phase_rand = array(loc) |
| 68 | + |
| 69 | + refl_rand = spline.__call__(phase_rand) |
| 70 | + |
| 71 | + #se = sqrt(2)/float(SNR) |
| 72 | + |
| 73 | + # Reflectances in intensity |
| 74 | + observations = array([gauss(each,each/float(SNR)) for each in refl_rand]) |
| 75 | + |
| 76 | + return observations, phase_rand |
| 77 | + |
| 78 | + def Compare(self,**arg): |
| 79 | + |
| 80 | + I = self.I |
| 81 | + ph = self.ph |
| 82 | + |
| 83 | + y = arg["y"] |
| 84 | + |
| 85 | + if arg.has_key("par_abs"): par = arg["par_abs"] |
| 86 | + if arg.has_key("compare"): comp = arg["compare"] |
| 87 | + |
| 88 | + |
| 89 | + plt.plot(ph,I,label="Actual curve") |
| 90 | + |
| 91 | + if arg.has_key("x"): |
| 92 | + plt.plot(arg["x"],y,'bo',label="simulated points") |
| 93 | + plt.plot(ph, func_kaa(comp, ph),label="ajusted phase function (simulated points)") |
| 94 | + |
| 95 | + plt.legend(loc=0) |
| 96 | + |
| 97 | + #ax = plt.gca() |
| 98 | + #ax.set_ylim(ax.get_ylim()[::-1]) |
| 99 | + plt.show() |
| 100 | + |
| 101 | + def SeeCurve(self): |
| 102 | + |
| 103 | + angles = self.ph |
| 104 | + I = self.I |
| 105 | + |
| 106 | + plt.plot(angles,I,"b*",label="points") |
| 107 | + plt.plot(angles,self.spline.__call__(angles),label="spline") |
| 108 | + plt.plot(angles,polyval(self.linear,angles),label="linear part") |
| 109 | + |
| 110 | + plt.legend(loc=0) |
| 111 | + |
| 112 | + #ax = plt.gca() |
| 113 | + #ax.set_ylim(ax.get_ylim()[::-1]) |
| 114 | + |
| 115 | + plt.show() |
| 116 | + |
| 117 | + def Dist(self,*entry): |
| 118 | + |
| 119 | + # last inputs must be the labels |
| 120 | + |
| 121 | + n = len(entry) |
| 122 | + |
| 123 | + plt.figure(figsize=(10,8),dpi=60) |
| 124 | + plt.title("N = 15 | SNR = 50") |
| 125 | + |
| 126 | + for i in reversed(range(int(n/2))): |
| 127 | + plt.hist(entry[i], bins=2*log2(entry[i].size + 1), label=entry[n - i - 1 ], \ |
| 128 | + histtype='step',linewidth=2, normed=True, range=(0,1)) |
| 129 | + |
| 130 | + plt.legend(loc=0) |
| 131 | + plt.ylabel("$f$") |
| 132 | + plt.xlim(0,1.5) |
| 133 | + plt.xlabel("residuo") |
| 134 | + |
| 135 | + plt.show() |
| 136 | + |
| 137 | +# END |
0 commit comments