File f_models.py changed (mode: 100644) (index 374c3af..e5853e8) |
... |
... |
class SNorm: |
186 |
186 |
|
|
187 |
187 |
|
|
188 |
188 |
def add_sample(f, sample, space='R'): |
def add_sample(f, sample, space='R'): |
|
189 |
|
""" |
|
190 |
|
Adds coordinates from input sample |
|
191 |
|
""" |
|
192 |
|
newdata = f._parse_sample(sample, space) |
|
193 |
|
f._data = np.vstack((f._data, newdata)) |
|
194 |
|
|
|
195 |
|
|
|
196 |
|
#č tohle už není "drobná pomucka" |
|
197 |
|
#č to je jedná z funkcí, která běží 30% času |
|
198 |
|
def new_sample(f, sample=None, space='R'): |
|
199 |
|
""" |
|
200 |
|
Returns new f_model object with the same distribution and with coordinates from 'sample' taken |
|
201 |
|
""" |
|
202 |
|
f_copy = copy.copy(f) |
|
203 |
|
if sample is None: |
|
204 |
|
f_copy._data = np.empty((0, f_copy._data.shape[1]), dtype=float) |
|
205 |
|
else: |
|
206 |
|
f_copy._data = np.atleast_2d(f._parse_sample(sample, space)) |
|
207 |
|
return f_copy |
|
208 |
|
|
|
209 |
|
|
|
210 |
|
def _parse_sample(f, input_sample, space='R'): |
189 |
211 |
# does sample is exactly me? |
# does sample is exactly me? |
190 |
|
if f.__repr__() == sample.__repr__(): |
|
191 |
|
newdata = sample._data |
|
|
212 |
|
if f.__repr__() == input_sample.__repr__(): |
|
213 |
|
newdata = input_sample._data |
192 |
214 |
|
|
193 |
|
# new piece of code "alpha"-related |
|
194 |
215 |
else: |
else: |
|
216 |
|
try: # does sample is another f_model object? |
|
217 |
|
sample = getattr(input_sample, space) |
|
218 |
|
except AttributeError: |
|
219 |
|
# no, it is just coordinates array |
|
220 |
|
sample = input_sample |
|
221 |
|
|
|
222 |
|
# new piece of code "alpha"-related |
195 |
223 |
if space in ('aR', 'aRn', 'aGK', 'aG', 'aP', 'aU'): |
if space in ('aR', 'aRn', 'aGK', 'aG', 'aP', 'aU'): |
196 |
|
# does sample is another f_model object? |
|
197 |
|
try: |
|
198 |
|
sample = getattr(sample, space) / f.alpha |
|
199 |
|
except AttributeError: |
|
200 |
|
sample = sample / f.alpha |
|
|
224 |
|
sample = sample / f.alpha |
201 |
225 |
space = space[1:] |
space = space[1:] |
202 |
226 |
|
|
203 |
227 |
if space in ('R', 'Rn', 'GK', 'G'): |
if space in ('R', 'Rn', 'GK', 'G'): |
204 |
|
# does sample is another f_model object? |
|
205 |
|
try: |
|
206 |
|
sample_R = getattr(sample, space) |
|
207 |
|
except AttributeError: |
|
208 |
|
# no |
|
209 |
|
sample_R = sample |
|
|
228 |
|
sample_R = sample |
210 |
229 |
sample_P = stats.norm.cdf(sample_R) |
sample_P = stats.norm.cdf(sample_R) |
211 |
230 |
|
|
212 |
231 |
pdfs_R = stats.norm.pdf(sample_R) |
pdfs_R = stats.norm.pdf(sample_R) |
|
... |
... |
class SNorm: |
217 |
236 |
newdata = np.hstack((sample_R, sample_P, pdf_R)) |
newdata = np.hstack((sample_R, sample_P, pdf_R)) |
218 |
237 |
|
|
219 |
238 |
elif space in ('P', 'U'): |
elif space in ('P', 'U'): |
220 |
|
try: |
|
221 |
|
sample_P = getattr(sample, space) |
|
222 |
|
except AttributeError: |
|
223 |
|
sample_P = sample |
|
|
239 |
|
sample_P = sample |
224 |
240 |
sample_R = stats.norm.ppf(sample_P) |
sample_R = stats.norm.ppf(sample_P) |
225 |
241 |
|
|
226 |
242 |
pdfs_R = stats.norm.pdf(sample_R) |
pdfs_R = stats.norm.pdf(sample_R) |
|
... |
... |
class SNorm: |
229 |
245 |
newdata = np.hstack((sample_R, sample_P, pdf_R.reshape(len(pdf_R), 1))) |
newdata = np.hstack((sample_R, sample_P, pdf_R.reshape(len(pdf_R), 1))) |
230 |
246 |
else: |
else: |
231 |
247 |
newdata = np.hstack((sample_R, sample_P, pdf_R)) |
newdata = np.hstack((sample_R, sample_P, pdf_R)) |
|
248 |
|
|
|
249 |
|
else: |
|
250 |
|
raise ValueError('SNorm: unknown space %s' % space) |
232 |
251 |
|
|
233 |
|
f._data = np.vstack((f._data, newdata)) |
|
234 |
|
|
|
235 |
|
|
|
236 |
|
# drobná pomucka |
|
237 |
|
def new_sample(f, sample=None, space='R'): |
|
238 |
|
f_copy = f() |
|
239 |
|
if sample is not None: |
|
240 |
|
f_copy.add_sample(sample, space) |
|
241 |
|
return f_copy |
|
|
252 |
|
return newdata |
|
253 |
|
|
|
254 |
|
|
242 |
255 |
|
|
243 |
256 |
|
|
244 |
257 |
def pdf(f, space='R'): |
def pdf(f, space='R'): |
|
... |
... |
class UnCorD: # nic moc nazev, ale je přece lepší nez CommonJointDistribution |
386 |
399 |
sl = slice(i*nvar, (i+1)*nvar) |
sl = slice(i*nvar, (i+1)*nvar) |
387 |
400 |
return f._data[:,sl] |
return f._data[:,sl] |
388 |
401 |
|
|
|
402 |
|
|
389 |
403 |
def add_sample(f, sample, space='R'): |
def add_sample(f, sample, space='R'): |
|
404 |
|
""" |
|
405 |
|
Adds coordinates from input sample |
|
406 |
|
""" |
|
407 |
|
newdata = f._parse_sample(sample, space) |
|
408 |
|
f._data = np.vstack((f._data, newdata)) |
|
409 |
|
|
|
410 |
|
|
|
411 |
|
#č tohle už není "drobná pomucka" |
|
412 |
|
#č to je jedná z funkcí, která běží 30% času |
|
413 |
|
def new_sample(f, sample=None, space='R'): |
|
414 |
|
""" |
|
415 |
|
Returns new f_model object with the same distribution and with coordinates from 'sample' taken |
|
416 |
|
""" |
|
417 |
|
f_copy = copy.copy(f) |
|
418 |
|
if sample is None: |
|
419 |
|
f_copy._data = np.empty((0, f_copy._data.shape[1]), dtype=float) |
|
420 |
|
else: |
|
421 |
|
f_copy._data = np.atleast_2d(f._parse_sample(sample, space)) |
|
422 |
|
return f_copy |
|
423 |
|
|
|
424 |
|
|
|
425 |
|
def _parse_sample(f, input_sample, space='R'): |
390 |
426 |
# isinstance, ne? |
# isinstance, ne? |
391 |
|
if f.__class__.__name__ == sample.__class__.__name__: |
|
392 |
|
if f.marginals == sample.marginals: |
|
393 |
|
f._data = np.vstack((f._data, sample._data)) |
|
394 |
|
return f |
|
|
427 |
|
if f.__class__.__name__ == input_sample.__class__.__name__: |
|
428 |
|
if f.marginals == input_sample.marginals: |
|
429 |
|
if (space in ('R', 'Rn', 'P', 'GK', 'G', 'U')) or (f.alpha == input_sample.alpha): |
|
430 |
|
return input_sample._data |
395 |
431 |
|
|
|
432 |
|
|
|
433 |
|
# does sample is another f_model object? |
|
434 |
|
try: |
|
435 |
|
sample_ = getattr(input_sample, space) |
|
436 |
|
except AttributeError: |
|
437 |
|
# no |
|
438 |
|
sample_ = input_sample |
|
439 |
|
|
396 |
440 |
# new piece of code "alpha"-related |
# new piece of code "alpha"-related |
397 |
|
elif space in ('aR', 'aRn', 'aGK', 'aG', 'aP', 'aU'): |
|
398 |
|
# does sample is another f_model object? |
|
399 |
|
try: |
|
400 |
|
sample = getattr(sample, space) / f.alpha |
|
401 |
|
except AttributeError: |
|
402 |
|
sample = sample / f.alpha |
|
|
441 |
|
if space in ('aR', 'aRn', 'aGK', 'aG', 'aP', 'aU'): |
|
442 |
|
sample_ = sample_ / f.alpha |
403 |
443 |
space = space[1:] |
space = space[1:] |
404 |
444 |
|
|
405 |
445 |
elif space not in ('R', 'Rn', 'P', 'GK', 'G', 'U'): |
elif space not in ('R', 'Rn', 'P', 'GK', 'G', 'U'): |
406 |
446 |
# co jako, mám gettext sem tahnout?! |
# co jako, mám gettext sem tahnout?! |
407 |
|
raise ValueError('Zadaný prostor %s mi není znám' % space) |
|
408 |
|
raise ValueError('Unknown space %s' % space) |
|
|
447 |
|
raise ValueError('SNorm: zadaný prostor %s mi není znám' % space) |
|
448 |
|
raise ValueError('SNorm: unknown space %s' % space) |
409 |
449 |
|
|
410 |
|
# does sample is another f_model object? |
|
411 |
|
try: |
|
412 |
|
sample_ = getattr(sample, space) |
|
413 |
|
except AttributeError: |
|
414 |
|
# no |
|
415 |
|
sample_ = sample |
|
416 |
450 |
|
|
417 |
451 |
if space=='GK': |
if space=='GK': |
418 |
452 |
space='G' |
space='G' |
|
... |
... |
class UnCorD: # nic moc nazev, ale je přece lepší nez CommonJointDistribution |
427 |
461 |
pdf_R = np.prod(pdfs_R, axis=0).reshape(-1, 1) |
pdf_R = np.prod(pdfs_R, axis=0).reshape(-1, 1) |
428 |
462 |
# nvar_Rn + nvar_R + nvar_P + nvar_G + pdf_R + pdf_G |
# nvar_Rn + nvar_R + nvar_P + nvar_G + pdf_R + pdf_G |
429 |
463 |
newdata = np.hstack((sample_dict['Rn'], sample_dict['R'], sample_dict['P'], sample_dict['G'], pdf_R, pdf_G)) |
newdata = np.hstack((sample_dict['Rn'], sample_dict['R'], sample_dict['P'], sample_dict['G'], pdf_R, pdf_G)) |
430 |
|
|
|
431 |
|
f._data = np.vstack((f._data, newdata)) |
|
432 |
|
|
|
433 |
|
|
|
434 |
|
# drobná pomucka |
|
435 |
|
def new_sample(f, sample=None, space='R'): |
|
436 |
|
f_copy = f() |
|
437 |
|
if sample is not None: |
|
438 |
|
f_copy.add_sample(sample, space) |
|
439 |
|
return f_copy |
|
|
464 |
|
|
|
465 |
|
return newdata |
|
466 |
|
|
440 |
467 |
|
|
441 |
468 |
|
|
442 |
469 |
def _chain(f, sample_dict): |
def _chain(f, sample_dict): |