Changeset b32dafd in sasmodels
- Timestamp:
- Aug 5, 2016 5:59:13 PM (8 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 99f446a
- Parents:
- 7722b4a
- Location:
- sasmodels
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/compare.py
rbd49c79 rb32dafd 184 184 pass 185 185 186 def __exit__(self, type,value, traceback):186 def __exit__(self, exc_type, exc_value, traceback): 187 187 # type: (Any, BaseException, Any) -> None 188 188 # TODO: better typing for __exit__ method … … 436 436 smearer.model = model[0] 437 437 return smearer.get_value() 438 theory = lambda: _call_smearer()438 theory = _call_smearer 439 439 else: 440 440 theory = lambda: model[0].evalDistribution([data.qx_data[index], … … 503 503 return calculator 504 504 505 def time_calculation(calculator, pars, Nevals=1):505 def time_calculation(calculator, pars, evals=1): 506 506 # type: (Calculator, ParameterSet, int) -> Tuple[np.ndarray, float] 507 507 """ … … 512 512 """ 513 513 # initialize the code so time is more accurate 514 if Nevals > 1:514 if evals > 1: 515 515 calculator(**suppress_pd(pars)) 516 516 toc = tic() 517 517 # make sure there is at least one eval 518 518 value = calculator(**pars) 519 for _ in range( Nevals-1):519 for _ in range(evals-1): 520 520 value = calculator(**pars) 521 average_time = toc()*1000. /Nevals521 average_time = toc()*1000. / evals 522 522 #print("I(q)",value) 523 523 return value, average_time … … 591 591 parameters. 592 592 """ 593 Nbase, Ncomp = opts['n1'], opts['n2']593 n_base, n_comp = opts['n1'], opts['n2'] 594 594 pars = opts['pars'] 595 595 data = opts['data'] 596 596 597 597 # silence the linter 598 base = opts['engines'][0] if Nbase else None599 comp = opts['engines'][1] if Ncomp else None598 base = opts['engines'][0] if n_base else None 599 comp = opts['engines'][1] if n_comp else None 600 600 base_time = comp_time = None 601 601 base_value = comp_value = resid = relerr = None 602 602 603 603 # Base calculation 604 if Nbase > 0:604 if n_base > 0: 605 605 try: 606 base_raw, base_time = time_calculation(base, pars, Nbase)606 base_raw, base_time = time_calculation(base, pars, n_base) 607 607 base_value = np.ma.masked_invalid(base_raw) 608 608 print("%s t=%.2f ms, intensity=%.0f" … … 611 611 except ImportError: 612 612 traceback.print_exc() 613 Nbase = 0613 n_base = 0 614 614 615 615 # Comparison calculation 616 if Ncomp > 0:616 if n_comp > 0: 617 617 try: 618 comp_raw, comp_time = time_calculation(comp, pars, Ncomp)618 comp_raw, comp_time = time_calculation(comp, pars, n_comp) 619 619 comp_value = np.ma.masked_invalid(comp_raw) 620 620 print("%s t=%.2f ms, intensity=%.0f" … … 623 623 except ImportError: 624 624 traceback.print_exc() 625 Ncomp = 0625 n_comp = 0 626 626 627 627 # Compare, but only if computing both forms 628 if Nbase > 0 and Ncomp > 0:628 if n_base > 0 and n_comp > 0: 629 629 resid = (base_value - comp_value) 630 relerr = resid/np.where(comp_value !=0., abs(comp_value), 1.0)630 relerr = resid/np.where(comp_value != 0., abs(comp_value), 1.0) 631 631 _print_stats("|%s-%s|" 632 632 % (base.engine, comp.engine) + (" "*(3+len(comp.engine))), … … 642 642 if limits is None: 643 643 vmin, vmax = np.Inf, -np.Inf 644 if Nbase > 0:644 if n_base > 0: 645 645 vmin = min(vmin, base_value.min()) 646 646 vmax = max(vmax, base_value.max()) 647 if Ncomp > 0:647 if n_comp > 0: 648 648 vmin = min(vmin, comp_value.min()) 649 649 vmax = max(vmax, comp_value.max()) 650 650 limits = vmin, vmax 651 651 652 if Nbase > 0:653 if Ncomp > 0: plt.subplot(131)652 if n_base > 0: 653 if n_comp > 0: plt.subplot(131) 654 654 plot_theory(data, base_value, view=view, use_data=False, limits=limits) 655 655 plt.title("%s t=%.2f ms"%(base.engine, base_time)) 656 656 #cbar_title = "log I" 657 if Ncomp > 0:658 if Nbase > 0: plt.subplot(132)657 if n_comp > 0: 658 if n_base > 0: plt.subplot(132) 659 659 plot_theory(data, comp_value, view=view, use_data=False, limits=limits) 660 660 plt.title("%s t=%.2f ms"%(comp.engine, comp_time)) 661 661 #cbar_title = "log I" 662 if Ncomp > 0 and Nbase > 0:662 if n_comp > 0 and n_base > 0: 663 663 plt.subplot(133) 664 664 if not opts['rel_err']: … … 678 678 fig.suptitle(opts['name']) 679 679 680 if Ncomp > 0 and Nbase > 0 and '-hist' in opts:680 if n_comp > 0 and n_base > 0 and '-hist' in opts: 681 681 plt.figure() 682 682 v = relerr … … 732 732 ] 733 733 734 def columnize( L, indent="", width=79):734 def columnize(items, indent="", width=79): 735 735 # type: (List[str], str, int) -> str 736 736 """ … … 739 739 Returns a string with carriage returns ready for printing. 740 740 """ 741 column_width = max(len(w) for w in L) + 1741 column_width = max(len(w) for w in items) + 1 742 742 num_columns = (width - len(indent)) // column_width 743 num_rows = len( L) // num_columns744 L = L + [""] * (num_rows*num_columns - len(L))745 columns = [ L[k*num_rows:(k+1)*num_rows] for k in range(num_columns)]743 num_rows = len(items) // num_columns 744 items = items + [""] * (num_rows * num_columns - len(items)) 745 columns = [items[k*num_rows:(k+1)*num_rows] for k in range(num_columns)] 746 746 lines = [" ".join("%-*s"%(column_width, entry) for entry in row) 747 747 for row in zip(*columns)] … … 766 766 for ext, val in parts: 767 767 if p.length > 1: 768 dict(("%s%d%s"%(p.id,k,ext), val) for k in range(1, p.length+1)) 768 dict(("%s%d%s" % (p.id, k, ext), val) 769 for k in range(1, p.length+1)) 769 770 else: 770 pars[p.id +ext] = val771 pars[p.id + ext] = val 771 772 772 773 # Plug in values given in demo … … 887 888 n1 = int(args[1]) if len(args) > 1 else 1 888 889 n2 = int(args[2]) if len(args) > 2 else 1 889 use_sasview = any(engine =='sasview' and count>0890 use_sasview = any(engine == 'sasview' and count > 0 890 891 for engine, count in zip(engines, [n1, n2])) 891 892 -
sasmodels/rst2html.py
r7722b4a rb32dafd 1 """1 r""" 2 2 Convert a restructured text document to html. 3 3 4 4 Inline math markup can uses the *math* directive, or it can use latex 5 style *\$expression\$*. Math can be rendered using simple html and 5 style *\$expression\$*. Math can be rendered using simple html and 6 6 unicode, or with mathjax. 7 7 """ … … 41 41 # others don't work properly with math_output! 42 42 if math_output == "mathjax": 43 settings = { "math_output": math_output}43 settings = {"math_output": math_output} 44 44 else: 45 settings = { "math-output": math_output}45 settings = {"math-output": math_output} 46 46 47 47 # math2html and mathml do not support \frac12 … … 89 89 Convert dollar signs to inline math markup in rst. 90 90 """ 91 content = _dollar.sub(r":math:`\1`", content)91 content = _dollar.sub(r":math:`\1`", content) 92 92 content = _notdollar.sub("$", content) 93 93 return content … … 95 95 96 96 def test_dollar(): 97 assert replace_dollar(u"no dollar")==u"no dollar" 98 assert replace_dollar(u"$only$")==u":math:`only`" 99 assert replace_dollar(u"$first$ is good")==u":math:`first` is good" 100 assert replace_dollar(u"so is $last$")==u"so is :math:`last`" 101 assert replace_dollar(u"and $mid$ too")==u"and :math:`mid` too" 102 assert replace_dollar(u"$first$, $mid$, $last$")==u":math:`first`, :math:`mid`, :math:`last`" 103 assert replace_dollar(u"dollar\$ escape")==u"dollar$ escape" 104 assert replace_dollar(u"dollar \$escape\$ too")==u"dollar $escape$ too" 105 assert replace_dollar(u"spaces $in the$ math")==u"spaces :math:`in the` math" 106 assert replace_dollar(u"emb\ $ed$\ ed")==u"emb\ :math:`ed`\ ed" 107 assert replace_dollar(u"$first$a")==u"$first$a" 108 assert replace_dollar(u"a$last$")==u"a$last$" 109 assert replace_dollar(u"$37")==u"$37" 110 assert replace_dollar(u"($37)")==u"($37)" 111 assert replace_dollar(u"$37 - $43")==u"$37 - $43" 112 assert replace_dollar(u"($37, $38)")==u"($37, $38)" 113 assert replace_dollar(u"a $mid$dle a")==u"a $mid$dle a" 114 assert replace_dollar(u"a ($in parens$) a")==u"a (:math:`in parens`) a" 115 assert replace_dollar(u"a (again $in parens$) a")==u"a (again :math:`in parens`) a" 97 """ 98 Test substitution of dollar signs with equivalent RST math markup 99 """ 100 assert replace_dollar(u"no dollar") == u"no dollar" 101 assert replace_dollar(u"$only$") == u":math:`only`" 102 assert replace_dollar(u"$first$ is good") == u":math:`first` is good" 103 assert replace_dollar(u"so is $last$") == u"so is :math:`last`" 104 assert replace_dollar(u"and $mid$ too") == u"and :math:`mid` too" 105 assert replace_dollar(u"$first$, $mid$, $last$") == u":math:`first`, :math:`mid`, :math:`last`" 106 assert replace_dollar(ur"dollar\$ escape") == u"dollar$ escape" 107 assert replace_dollar(ur"dollar \$escape\$ too") == u"dollar $escape$ too" 108 assert replace_dollar(u"spaces $in the$ math") == u"spaces :math:`in the` math" 109 assert replace_dollar(u"emb\ $ed$\ ed") == ur"emb\ :math:`ed`\ ed" 110 assert replace_dollar(u"$first$a") == u"$first$a" 111 assert replace_dollar(u"a$last$") == u"a$last$" 112 assert replace_dollar(u"$37") == u"$37" 113 assert replace_dollar(u"($37)") == u"($37)" 114 assert replace_dollar(u"$37 - $43") == u"$37 - $43" 115 assert replace_dollar(u"($37, $38)") == u"($37, $38)" 116 assert replace_dollar(u"a $mid$dle a") == u"a $mid$dle a" 117 assert replace_dollar(u"a ($in parens$) a") == u"a (:math:`in parens`) a" 118 assert replace_dollar(u"a (again $in parens$) a") == u"a (again :math:`in parens`) a" 116 119 117 120 if __name__ == "__main__": -
sasmodels/sasview_model.py
r50ec515 rb32dafd 45 45 MODELS = {} 46 46 def find_model(modelname): 47 # type: (str) -> SasviewModelType 48 """ 49 Find a model by name. If the model name ends in py, try loading it from 50 custom models, otherwise look for it in the list of builtin models. 51 """ 47 52 # TODO: used by sum/product model to load an existing model 48 53 # TODO: doesn't handle custom models properly … … 340 345 else: 341 346 value = np.array([self.params.get(p.id+str(k), np.NaN) 342 for k in range(1, p.length+1)])347 for k in range(1, p.length+1)]) 343 348 args[p.id] = value 344 349 … … 587 592 # new dispersers in the set_dispersion call and create a new 588 593 # one instead of trying to assign parameters. 589 from . import weights590 594 disperser = weights.dispersers[dispersion.__class__.__name__] 591 595 dispersion = weights.MODELS[disperser]() … … 634 638 Cylinder = _make_standard_model('cylinder') 635 639 cylinder = Cylinder() 636 return cylinder.evalDistribution([0.1, 0.1])640 return cylinder.evalDistribution([0.1, 0.1]) 637 641 638 642 def test_rpa(): … … 643 647 RPA = _make_standard_model('rpa') 644 648 rpa = RPA(3) 645 return rpa.evalDistribution([0.1, 0.1])649 return rpa.evalDistribution([0.1, 0.1]) 646 650 647 651
Note: See TracChangeset
for help on using the changeset viewer.