Changeset 4e96703 in sasmodels for sasmodels/sasview_model.py


Ignore:
Timestamp:
Nov 9, 2018 12:21:36 PM (5 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master
Children:
e2da671
Parents:
12f77e9 (diff), cf3d0ce (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'beta_approx' into ticket-608-user-defined-weights

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    r12f77e9 r4e96703  
    288288    attrs['category'] = model_info.category 
    289289    attrs['is_structure_factor'] = model_info.structure_factor 
    290     attrs['is_form_factor'] = model_info.ER is not None 
     290    attrs['is_form_factor'] = model_info.effective_radius_type is not None 
    291291    attrs['is_multiplicity_model'] = variants[0] > 1 
    292292    attrs['multiplicity_info'] = variants 
     
    562562            # pylint: disable=unpacking-non-sequence 
    563563            q, phi = x 
    564             return self.calculate_Iq([q*math.cos(phi)], [q*math.sin(phi)])[0] 
     564            result, _ = self.calculate_Iq([q*math.cos(phi)], [q*math.sin(phi)]) 
     565            return result[0] 
    565566        else: 
    566             return self.calculate_Iq([x])[0] 
     567            result, _ = self.calculate_Iq([x]) 
     568            return result[0] 
    567569 
    568570 
     
    579581        """ 
    580582        if isinstance(x, (list, tuple)): 
    581             return self.calculate_Iq([x[0]], [x[1]])[0] 
     583            result, _ = self.calculate_Iq([x[0]], [x[1]]) 
     584            return result[0] 
    582585        else: 
    583             return self.calculate_Iq([x])[0] 
     586            result, _ = self.calculate_Iq([x]) 
     587            return result[0] 
    584588 
    585589    def evalDistribution(self, qdist): 
     
    615619            # Check whether we have a list of ndarrays [qx,qy] 
    616620            qx, qy = qdist 
    617             return self.calculate_Iq(qx, qy) 
     621            result, _ = self.calculate_Iq(qx, qy) 
     622            return result 
    618623 
    619624        elif isinstance(qdist, np.ndarray): 
    620625            # We have a simple 1D distribution of q-values 
    621             return self.calculate_Iq(qdist) 
     626            result, _ = self.calculate_Iq(qdist) 
     627            return result 
    622628 
    623629        else: 
     
    638644        # so that it returns a results object containing all the bits: 
    639645        #     the A, B, C, ... of the composition model (and any subcomponents?) 
    640         #     the P and S of the product model, 
     646        #     the P and S of the product model 
    641647        #     the combined model before resolution smearing, 
    642648        #     the sasmodel before sesans conversion, 
     
    659665        if composition and composition[0] == 'product': # only P*S for now 
    660666            with calculation_lock: 
    661                 self._calculate_Iq(qx) 
    662                 return self._intermediate_results 
     667                _, lazy_results = self._calculate_Iq(qx) 
     668                # for compatibility with sasview 4.x 
     669                results = lazy_results() 
     670                pq_data = results.get("P(Q)") 
     671                sq_data = results.get("S(Q)") 
     672                return pq_data, sq_data 
    663673        else: 
    664674            return None 
    665675 
    666     def calculate_Iq(self, qx, qy=None): 
    667         # type: (Sequence[float], Optional[Sequence[float]]) -> np.ndarray 
     676    def calculate_Iq(self, 
     677                     qx,     # type: Sequence[float] 
     678                     qy=None # type: Optional[Sequence[float]] 
     679                     ): 
     680        # type: (...) -> Tuple[np.ndarray, Callable[[], collections.OrderedDict[str, np.ndarray]]] 
    668681        """ 
    669682        Calculate Iq for one set of q with the current parameters. 
     
    673686        This should NOT be used for fitting since it copies the *q* vectors 
    674687        to the card for each evaluation. 
     688 
     689        The returned tuple contains the scattering intensity followed by a 
     690        callable which returns a dictionary of intermediate data from 
     691        ProductKernel. 
    675692        """ 
    676693        ## uncomment the following when trying to debug the uncoordinated calls 
     
    683700            return self._calculate_Iq(qx, qy) 
    684701 
    685     def _calculate_Iq(self, qx, qy=None): 
     702    def _calculate_Iq(self, qx, qy=None, Fq=False, effective_radius_type=1): 
    686703        if self._model is None: 
    687704            self._model = core.build_model(self._model_info) 
     
    703720        #print("values", values) 
    704721        #print("is_mag", is_magnetic) 
     722        if Fq: 
     723            result = calculator.Fq(call_details, values, cutoff=self.cutoff, 
     724                                   magnetic=is_magnetic, 
     725                                   effective_radius_type=effective_radius_type) 
    705726        result = calculator(call_details, values, cutoff=self.cutoff, 
    706727                            magnetic=is_magnetic) 
     728        lazy_results = getattr(calculator, 'results', 
     729                               lambda: collections.OrderedDict()) 
    707730        #print("result", result) 
    708         self._intermediate_results = getattr(calculator, 'results', None) 
     731 
    709732        calculator.release() 
    710733        #self._model.release() 
    711         return result 
    712  
    713     def calculate_ER(self): 
     734 
     735        return result, lazy_results 
     736 
     737 
     738    def calculate_ER(self, mode=1): 
    714739        # type: () -> float 
    715740        """ 
     
    718743        :return: the value of the effective radius 
    719744        """ 
    720         if self._model_info.ER is None: 
    721             return 1.0 
    722         else: 
    723             value, weight = self._dispersion_mesh() 
    724             fv = self._model_info.ER(*value) 
    725             #print(values[0].shape, weights.shape, fv.shape) 
    726             return np.sum(weight * fv) / np.sum(weight) 
     745        Fq = self._calculate_Iq([0.1], True, mode) 
     746        return Fq[2] 
    727747 
    728748    def calculate_VR(self): 
     
    731751        Calculate the volf ratio for P(q)*S(q) 
    732752 
    733         :return: the value of the volf ratio 
    734         """ 
    735         if self._model_info.VR is None: 
    736             return 1.0 
    737         else: 
    738             value, weight = self._dispersion_mesh() 
    739             whole, part = self._model_info.VR(*value) 
    740             return np.sum(weight * part) / np.sum(weight * whole) 
     753        :return: the value of the form:shell volume ratio 
     754        """ 
     755        Fq = self._calculate_Iq([0.1], True, mode) 
     756        return Fq[4] 
    741757 
    742758    def set_dispersion(self, parameter, dispersion): 
     
    848864    P = _make_standard_model('cylinder')() 
    849865    model = MultiplicationModel(P, S) 
     866    model.setParam(product.RADIUS_MODE_ID, 1.0) 
    850867    value = model.evalDistribution([0.1, 0.1]) 
    851868    if np.isnan(value): 
     
    908925    q = np.linspace(-0.35, 0.35, 500) 
    909926    qx, qy = np.meshgrid(q, q) 
    910     result = model.calculate_Iq(qx.flatten(), qy.flatten()) 
     927    result, _ = model.calculate_Iq(qx.flatten(), qy.flatten()) 
    911928    result = result.reshape(qx.shape) 
    912929 
Note: See TracChangeset for help on using the changeset viewer.