Changeset c1799d3 in sasmodels for sasmodels/sasview_model.py


Ignore:
Timestamp:
Nov 9, 2018 12:16:05 PM (5 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
c11d09f
Parents:
17695aa (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-1157

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/sasview_model.py

    r17695aa rc1799d3  
    283283    attrs['category'] = model_info.category 
    284284    attrs['is_structure_factor'] = model_info.structure_factor 
    285     attrs['is_form_factor'] = model_info.ER is not None 
     285    attrs['is_form_factor'] = model_info.effective_radius_type is not None 
    286286    attrs['is_multiplicity_model'] = variants[0] > 1 
    287287    attrs['multiplicity_info'] = variants 
     
    556556            # pylint: disable=unpacking-non-sequence 
    557557            q, phi = x 
    558             return self.calculate_Iq([q*math.cos(phi)], [q*math.sin(phi)])[0] 
     558            result, _ = self.calculate_Iq([q*math.cos(phi)], [q*math.sin(phi)]) 
     559            return result[0] 
    559560        else: 
    560             return self.calculate_Iq([x])[0] 
     561            result, _ = self.calculate_Iq([x]) 
     562            return result[0] 
    561563 
    562564 
     
    573575        """ 
    574576        if isinstance(x, (list, tuple)): 
    575             return self.calculate_Iq([x[0]], [x[1]])[0] 
     577            result, _ = self.calculate_Iq([x[0]], [x[1]]) 
     578            return result[0] 
    576579        else: 
    577             return self.calculate_Iq([x])[0] 
     580            result, _ = self.calculate_Iq([x]) 
     581            return result[0] 
    578582 
    579583    def evalDistribution(self, qdist): 
     
    609613            # Check whether we have a list of ndarrays [qx,qy] 
    610614            qx, qy = qdist 
    611             return self.calculate_Iq(qx, qy) 
     615            result, _ = self.calculate_Iq(qx, qy) 
     616            return result 
    612617 
    613618        elif isinstance(qdist, np.ndarray): 
    614619            # We have a simple 1D distribution of q-values 
    615             return self.calculate_Iq(qdist) 
     620            result, _ = self.calculate_Iq(qdist) 
     621            return result 
    616622 
    617623        else: 
     
    632638        # so that it returns a results object containing all the bits: 
    633639        #     the A, B, C, ... of the composition model (and any subcomponents?) 
    634         #     the P and S of the product model, 
     640        #     the P and S of the product model 
    635641        #     the combined model before resolution smearing, 
    636642        #     the sasmodel before sesans conversion, 
     
    653659        if composition and composition[0] == 'product': # only P*S for now 
    654660            with calculation_lock: 
    655                 self._calculate_Iq(qx) 
    656                 return self._intermediate_results 
     661                _, lazy_results = self._calculate_Iq(qx) 
     662                # for compatibility with sasview 4.x 
     663                results = lazy_results() 
     664                pq_data = results.get("P(Q)") 
     665                sq_data = results.get("S(Q)") 
     666                return pq_data, sq_data 
    657667        else: 
    658668            return None 
    659669 
    660     def calculate_Iq(self, qx, qy=None): 
    661         # type: (Sequence[float], Optional[Sequence[float]]) -> np.ndarray 
     670    def calculate_Iq(self, 
     671                     qx,     # type: Sequence[float] 
     672                     qy=None # type: Optional[Sequence[float]] 
     673                     ): 
     674        # type: (...) -> Tuple[np.ndarray, Callable[[], collections.OrderedDict[str, np.ndarray]]] 
    662675        """ 
    663676        Calculate Iq for one set of q with the current parameters. 
     
    667680        This should NOT be used for fitting since it copies the *q* vectors 
    668681        to the card for each evaluation. 
     682 
     683        The returned tuple contains the scattering intensity followed by a 
     684        callable which returns a dictionary of intermediate data from 
     685        ProductKernel. 
    669686        """ 
    670687        ## uncomment the following when trying to debug the uncoordinated calls 
     
    677694            return self._calculate_Iq(qx, qy) 
    678695 
    679     def _calculate_Iq(self, qx, qy=None): 
     696    def _calculate_Iq(self, qx, qy=None, Fq=False, effective_radius_type=1): 
    680697        if self._model is None: 
    681698            self._model = core.build_model(self._model_info) 
     
    697714        #print("values", values) 
    698715        #print("is_mag", is_magnetic) 
     716        if Fq: 
     717            result = calculator.Fq(call_details, values, cutoff=self.cutoff, 
     718                                   magnetic=is_magnetic, 
     719                                   effective_radius_type=effective_radius_type) 
    699720        result = calculator(call_details, values, cutoff=self.cutoff, 
    700721                            magnetic=is_magnetic) 
     722        lazy_results = getattr(calculator, 'results', 
     723                               lambda: collections.OrderedDict()) 
    701724        #print("result", result) 
    702         self._intermediate_results = getattr(calculator, 'results', None) 
     725 
    703726        calculator.release() 
    704727        #self._model.release() 
    705         return result 
    706  
    707     def calculate_ER(self): 
     728 
     729        return result, lazy_results 
     730 
     731 
     732    def calculate_ER(self, mode=1): 
    708733        # type: () -> float 
    709734        """ 
     
    712737        :return: the value of the effective radius 
    713738        """ 
    714         if self._model_info.ER is None: 
    715             return 1.0 
    716         else: 
    717             value, weight = self._dispersion_mesh() 
    718             fv = self._model_info.ER(*value) 
    719             #print(values[0].shape, weights.shape, fv.shape) 
    720             return np.sum(weight * fv) / np.sum(weight) 
     739        Fq = self._calculate_Iq([0.1], True, mode) 
     740        return Fq[2] 
    721741 
    722742    def calculate_VR(self): 
     
    725745        Calculate the volf ratio for P(q)*S(q) 
    726746 
    727         :return: the value of the volf ratio 
    728         """ 
    729         if self._model_info.VR is None: 
    730             return 1.0 
    731         else: 
    732             value, weight = self._dispersion_mesh() 
    733             whole, part = self._model_info.VR(*value) 
    734             return np.sum(weight * part) / np.sum(weight * whole) 
     747        :return: the value of the form:shell volume ratio 
     748        """ 
     749        Fq = self._calculate_Iq([0.1], True, mode) 
     750        return Fq[4] 
    735751 
    736752    def set_dispersion(self, parameter, dispersion): 
     
    842858    P = _make_standard_model('cylinder')() 
    843859    model = MultiplicationModel(P, S) 
     860    model.setParam(product.RADIUS_MODE_ID, 1.0) 
    844861    value = model.evalDistribution([0.1, 0.1]) 
    845862    if np.isnan(value): 
     
    930947    q = np.linspace(-0.35, 0.35, 500) 
    931948    qx, qy = np.meshgrid(q, q) 
    932     result = model.calculate_Iq(qx.flatten(), qy.flatten()) 
     949    result, _ = model.calculate_Iq(qx.flatten(), qy.flatten()) 
    933950    result = result.reshape(qx.shape) 
    934951 
Note: See TracChangeset for help on using the changeset viewer.