<commit_before>#!/usr/bin/env python
""" 
    Provide Line function (y= A + Bx) as a BaseComponent model
"""

from sans.models.BaseComponent import BaseComponent
import math
import numpy


class LineModel(BaseComponent):
    """ 
        Class that evaluates a linear model.
        
        f(x) = A + Bx
         
        List of default parameters:
          A = 1.0
          B = 1.0 
    """
        
    def __init__(self):
        """ Initialization """
        
        # Initialize BaseComponent first, then sphere
        BaseComponent.__init__(self)
        
        ## Name of the model
        self.name = "LineModel"

        ## Define parameters
        self.params = {}
        self.params['A'] = 1.0
        self.params['B'] = 1.0
        self.description='f(x) = A + Bx'
        ## Parameter details [units, min, max]
        self.details = {}
        self.details['A'] = ['', None, None]
        self.details['B'] = ['', None, None]
        # fixed paramaters
        self.fixed=[]
    def _line(self, x):
        """
            Evaluate the function
            @param x: x-value
            @return: function value
        """
        return  self.params['A'] + x *self.params['B']
    
    def run(self, x = 0.0):
        """ Evaluate the model
            @param x: simple value
            @return: (Line value)
        """
        if x.__class__.__name__ == 'list':
            return self._line(x[0]*math.cos(x[1]))*self._line(x[0]*math.sin(x[1]))
        elif x.__class__.__name__ == 'tuple':
            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
        else:
            return self._line(x)
   
    def runXY(self, x = 0.0):
        """ Evaluate the model
            @param x: simple value
            @return: Line value
        """
        if x.__class__.__name__ == 'list':
            return self._line(x[0])*self._line(x[1])
        elif x.__class__.__name__ == 'tuple':
            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
        else:
            return self._line(x)
        
    def evalDistribution(self, qdist):
        """
        Evaluate a distribution of q-values.
        
        * For 1D, a numpy array is expected as input:
        
            evalDistribution(q)
            
        where q is a numpy array.
        
        
        * For 2D, a list of numpy arrays are expected: [qx_prime,qy_prime],
          where 1D arrays,
                
        :param qdist: ndarray of scalar q-values or list [qx,qy] 
                    where qx,qy are 1D ndarrays 
        
        """
        if qdist.__class__.__name__ == 'list':
            # Check whether we have a list of ndarrays [qx,qy]
            if len(qdist)!=2 or \
                qdist[0].__class__.__name__ != 'ndarray' or \
                qdist[1].__class__.__name__ != 'ndarray':
                    raise RuntimeError, "evalDistribution expects a list of 2 ndarrays"
                
            # Extract qx and qy for code clarity<commit_msg>fixed 2D line runxy<commit_after>#!/usr/bin/env python
""" 
    Provide Line function (y= A + Bx) as a BaseComponent model
"""

from sans.models.BaseComponent import BaseComponent
import math
import numpy


class LineModel(BaseComponent):
    """ 
        Class that evaluates a linear model.
        
        f(x) = A + Bx
         
        List of default parameters:
          A = 1.0
          B = 1.0 
    """
        
    def __init__(self):
        """ Initialization """
        
        # Initialize BaseComponent first, then sphere
        BaseComponent.__init__(self)
        
        ## Name of the model
        self.name = "LineModel"

        ## Define parameters
        self.params = {}
        self.params['A'] = 1.0
        self.params['B'] = 1.0
        self.description='f(x) = A + Bx'
        ## Parameter details [units, min, max]
        self.details = {}
        self.details['A'] = ['', None, None]
        self.details['B'] = ['', None, None]
        # fixed paramaters
        self.fixed=[]
    def _line(self, x):
        """
            Evaluate the function
            @param x: x-value
            @return: function value
        """
        return  self.params['A'] + x *self.params['B']
    
    def run(self, x = 0.0):
        """ Evaluate the model
            @param x: simple value
            @return: (Line value)
        """
        if x.__class__.__name__ == 'list':
            return self._line(x[0]*math.cos(x[1]))*self._line(x[0]*math.sin(x[1]))
        elif x.__class__.__name__ == 'tuple':
            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
        else:
            return self._line(x)
   
    def runXY(self, x = 0.0):
        """ Evaluate the model
            @param x: simple value
            @return: Line value
        """
        if x.__class__.__name__ == 'list':
            return self._line(x[1])
        elif x.__class__.__name__ == 'tuple':
            raise ValueError, "Tuples are not allowed as input to BaseComponent models"
        else:
            return self._line(x)
        
    def evalDistribution(self, qdist):
        """
        Evaluate a distribution of q-values.
        
        * For 1D, a numpy array is expected as input:
        
            evalDistribution(q)
            
        where q is a numpy array.
        
        
        * For 2D, a list of numpy arrays are expected: [qx_prime,qy_prime],
          where 1D arrays,
                
        :param qdist: ndarray of scalar q-values or list [qx,qy] 
                    where qx,qy are 1D ndarrays 
        
        """
        if qdist.__class__.__name__ == 'list':
            # Check whether we have a list of ndarrays [qx,qy]
            if len(qdist)!=2 or \
                qdist[0].__class__.__name__ != 'ndarray' or \
                qdist[1].__class__.__name__ != 'ndarray':
                    raise RuntimeError, "evalDistribution expects a list of 2 ndarrays"
                
            # Extract qx and qy for code clarity
{'fraction_non_alphanumeric': 0.10922290051146676, 'fraction_numerical': 0.008579442336248144, 'mean_word_length': 1.558885605740819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '117383', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1716, 'n_words': 510}