Index: src/sas/sasgui/perspectives/calculator/model_editor.py
===================================================================
--- src/sas/sasgui/perspectives/calculator/model_editor.py	(revision 9d806239f4f5eb2fa3b8a1925c417224ff8d28f8)
+++ src/sas/sasgui/perspectives/calculator/model_editor.py	(revision c351bf16bb7a9a739ee0423daba24e02125c5595)
@@ -521,5 +521,5 @@
         return [self.model1_name, self.model2_name]
 
-    def write_string(self, fname, name1, name2):
+    def write_string(self, fname, model1_name, model2_name):
         """
         Write and Save file
@@ -527,20 +527,14 @@
         self.fname = fname
         description = self.desc_tcl.GetValue().lstrip().rstrip()
-        if description == '':
-            description = name1 + self._operator + name2
-        operator_text = self._operator_choice.GetValue()
-        f_oper = '*' if '+' in operator_text else '+'
-        path = self.fname
-        output = SUM_TEMPLATE.format(model1=name1, model2=name2, 
-            scale_factor_default=1.0, background_default=0.001,
-            factor_operator=f_oper, operator=self._operator,
-            description=description)
-        if self._operator == '*':
-            # Multiplication models only have 1 overall scale factor. Don't use
-            # sub-models' individual scales as fitting params
-            output = output.replace("if name == 'background'", 
-                "if name == 'background' or name == 'scale'")
+        desc_line = ''
+        if description.strip() != '':
+            # Sasmodels generates a description for us. If the user provides
+            # their own description, add a line to overwrite the sasmodels one
+            desc_line = "\nmodel_info.description = '{}'".format(description)
+        name = os.path.splitext(os.path.basename(self.fname))[0]
+        output = SUM_TEMPLATE.format(name=name, model1=model1_name, 
+            model2=model2_name, operator=self._operator, desc_line=desc_line)
         with open(self.fname, 'w') as out_f:
-            out_f.write(output + "\n")
+            out_f.write(output)
 
     def compile_file(self, path):
@@ -1217,336 +1211,11 @@
 """
 SUM_TEMPLATE = """
-# A sample of an experimental model function for Sum/Multiply(Pmodel1,Pmodel2)
-import os
-import sys
-import copy
-import collections
-
-import numpy
-
-from sas.sascalc.fit.pluginmodel import Model1DPlugin
-from sasmodels.sasview_model import find_model
-
-class Model(Model1DPlugin):
-    name = os.path.splitext(os.path.basename(__file__))[0]
-    is_multiplicity_model = False
-    def __init__(self, multiplicity=1):
-        Model1DPlugin.__init__(self, name='')
-        P1 = find_model('{model1}')
-        P2 = find_model('{model2}')
-        p_model1 = P1()
-        p_model2 = P2()
-        ## Setting  model name model description
-        self.description = '{description}'
-        if self.name.rstrip().lstrip() == '':
-            self.name = self._get_name(p_model1.name, p_model2.name)
-        if self.description.rstrip().lstrip() == '':
-            self.description = p_model1.name
-            self.description += p_model2.name
-            self.fill_description(p_model1, p_model2)
-
-        ## Define parameters
-        self.params = collections.OrderedDict()
-
-        ## Parameter details [units, min, max]
-        self.details = {{}}
-        ## Magnetic Panrameters
-        self.magnetic_params = []
-        # non-fittable parameters
-        self.non_fittable = p_model1.non_fittable
-        self.non_fittable += p_model2.non_fittable
-
-        ##models
-        self.p_model1= p_model1
-        self.p_model2= p_model2
-
-
-        ## dispersion
-        self._set_dispersion()
-        ## Define parameters
-        self._set_params()
-        ## New parameter:scaling_factor
-        self.params['scale_factor'] = {scale_factor_default}
-        # Set each model's background to 0, and define our own background param
-        if 'background' in self.p_model1.params:
-            self.p_model1.setParam('background', 0.0)
-        if 'background' in self.p_model2.params:
-            self.p_model2.setParam('background', 0.0)
-        self.params['background'] = {background_default}
-
-        ## Parameter details [units, min, max]
-        self._set_details()
-        self.details['scale_factor'] = ['', 0.0, numpy.inf]
-        self.details['background'] = ['1/cm', 0.0, numpy.inf]
-
-        #list of parameter that can be fitted
-        self._set_fixed_params()
-
-        ## parameters with orientation
-        self.orientation_params = []
-        for item in self.p_model1.orientation_params:
-            new_item = "p1_" + item
-            if not new_item in self.orientation_params:
-                self.orientation_params.append(new_item)
-
-        for item in self.p_model2.orientation_params:
-            new_item = "p2_" + item
-            if not new_item in self.orientation_params:
-                self.orientation_params.append(new_item)
-        ## magnetic params
-        self.magnetic_params = []
-        for item in self.p_model1.magnetic_params:
-            new_item = "p1_" + item
-            if not new_item in self.magnetic_params:
-                self.magnetic_params.append(new_item)
-
-        for item in self.p_model2.magnetic_params:
-            new_item = "p2_" + item
-            if not new_item in self.magnetic_params:
-                self.magnetic_params.append(new_item)
-        # get multiplicity if model provide it, else 1.
-        try:
-            multiplicity1 = p_model1.multiplicity
-            try:
-                multiplicity2 = p_model2.multiplicity
-            except:
-                multiplicity2 = 1
-        except:
-            multiplicity1 = 1
-            multiplicity2 = 1
-        ## functional multiplicity of the model
-        self.multiplicity1 = multiplicity1
-        self.multiplicity2 = multiplicity2
-        self.multiplicity_info = []
-
-    def _clone(self, obj):
-        import copy
-        obj.params     = copy.deepcopy(self.params)
-        obj.description     = copy.deepcopy(self.description)
-        obj.details    = copy.deepcopy(self.details)
-        obj.dispersion = copy.deepcopy(self.dispersion)
-        obj.p_model1  = self.p_model1.clone()
-        obj.p_model2  = self.p_model2.clone()
-        #obj = copy.deepcopy(self)
-        return obj
-
-    def _get_name(self, name1, name2):
-        p1_name = self._get_upper_name(name1)
-        if not p1_name:
-            p1_name = name1
-        name = p1_name
-        name += "_and_"
-        p2_name = self._get_upper_name(name2)
-        if not p2_name:
-            p2_name = name2
-        name += p2_name
-        return name
-
-    def _get_upper_name(self, name=None):
-        if name is None:
-            return ""
-        upper_name = ""
-        str_name = str(name)
-        for index in range(len(str_name)):
-            if str_name[index].isupper():
-                upper_name += str_name[index]
-        return upper_name
-
-    def _set_dispersion(self):
-        self.dispersion = collections.OrderedDict()
-        ##set dispersion only from p_model
-        for name , value in self.p_model1.dispersion.iteritems():
-            #if name.lower() not in self.p_model1.orientation_params:
-            new_name = "p1_" + name
-            self.dispersion[new_name]= value
-        for name , value in self.p_model2.dispersion.iteritems():
-            #if name.lower() not in self.p_model2.orientation_params:
-            new_name = "p2_" + name
-            self.dispersion[new_name]= value
-
-    def function(self, x=0.0):
-        return 0
-
-    def getProfile(self):
-        try:
-            x,y = self.p_model1.getProfile()
-        except:
-            x = None
-            y = None
-
-        return x, y
-
-    def _set_params(self):
-        for name , value in self.p_model1.params.iteritems():
-            # Don't use the model's background param - we've defined our own
-            if name == 'background':
-                continue
-            new_name = "p1_" + name
-            self.params[new_name] = value
-
-        for name , value in self.p_model2.params.iteritems():
-            # Don't use the model's background param - we've defined our own
-            if name == 'background':
-                continue
-            new_name = "p2_" + name
-            self.params[new_name] = value
-
-        # Set "scale" as initializing
-        self._set_scale_factor()
-
-
-    def _set_details(self):
-        for name ,detail in self.p_model1.details.iteritems():
-            if name == 'background':
-                continue
-            new_name = "p1_" + name
-            #if new_name not in self.orientation_params:
-            self.details[new_name]= detail
-
-        for name ,detail in self.p_model2.details.iteritems():
-            if name == 'background':
-                continue
-            new_name = "p2_" + name
-            #if new_name not in self.orientation_params:
-            self.details[new_name]= detail
-
-    def _set_scale_factor(self):
-        pass
-
-
-    def setParam(self, name, value):
-        # set param to this (p1, p2) model
-        self._setParamHelper(name, value)
-
-        ## setParam to p model
-        model_pre = ''
-        new_name = ''
-        name_split = name.split('_', 1)
-        if len(name_split) == 2:
-            model_pre = name.split('_', 1)[0]
-            new_name = name.split('_', 1)[1]
-        if model_pre == "p1":
-            if new_name in self.p_model1.getParamList():
-                self.p_model1.setParam(new_name, value)
-        elif model_pre == "p2":
-             if new_name in self.p_model2.getParamList():
-                self.p_model2.setParam(new_name, value)
-        elif name == 'scale_factor' or name == 'background':
-            self.params[name] = value
-        else:
-            raise ValueError, "Model does not contain parameter %s" % name
-
-    def getParam(self, name):
-        # Look for dispersion parameters
-        toks = name.split('.')
-        if len(toks)==2:
-            for item in self.dispersion.keys():
-                # 2D not supported
-                if item.lower()==toks[0].lower():
-                    for par in self.dispersion[item]:
-                        if par.lower() == toks[1].lower():
-                            return self.dispersion[item][par]
-        else:
-            # Look for standard parameter
-            for item in self.params.keys():
-                if item.lower()==name.lower():
-                    return self.params[item]
-        return
-        #raise ValueError, "Model does not contain parameter %s" % name
-
-    def _setParamHelper(self, name, value):
-        # Look for dispersion parameters
-        toks = name.split('.')
-        if len(toks)== 2:
-            for item in self.dispersion.keys():
-                if item.lower()== toks[0].lower():
-                    for par in self.dispersion[item]:
-                        if par.lower() == toks[1].lower():
-                            self.dispersion[item][par] = value
-                            return
-        else:
-            # Look for standard parameter
-            for item in self.params.keys():
-                if item.lower()== name.lower():
-                    self.params[item] = value
-                    return
-
-        raise ValueError, "Model does not contain parameter %s" % name
-
-
-    def _set_fixed_params(self):
-        self.fixed = []
-        for item in self.p_model1.fixed:
-            new_item = "p1" + item
-            self.fixed.append(new_item)
-        for item in self.p_model2.fixed:
-            new_item = "p2" + item
-            self.fixed.append(new_item)
-
-        self.fixed.sort()
-
-
-    def run(self, x = 0.0):
-        self._set_scale_factor()
-        return self.params['scale_factor'] {factor_operator} \
-(self.p_model1.run(x) {operator} self.p_model2.run(x)) + self.params['background']
-
-    def runXY(self, x = 0.0):
-        self._set_scale_factor()
-        return self.params['scale_factor'] {factor_operator} \
-(self.p_model1.runXY(x) {operator} self.p_model2.runXY(x)) + self.params['background']
-
-    ## Now (May27,10) directly uses the model eval function
-    ## instead of the for-loop in Base Component.
-    def evalDistribution(self, x = []):
-        self._set_scale_factor()
-        return self.params['scale_factor'] {factor_operator} \
-(self.p_model1.evalDistribution(x) {operator} \
-self.p_model2.evalDistribution(x)) + self.params['background']
-
-    def set_dispersion(self, parameter, dispersion):
-        value= None
-        new_pre = parameter.split("_", 1)[0]
-        new_parameter = parameter.split("_", 1)[1]
-        try:
-            if new_pre == 'p1' and \
-new_parameter in self.p_model1.dispersion.keys():
-                value= self.p_model1.set_dispersion(new_parameter, dispersion)
-            if new_pre == 'p2' and \
-new_parameter in self.p_model2.dispersion.keys():
-                value= self.p_model2.set_dispersion(new_parameter, dispersion)
-            self._set_dispersion()
-            return value
-        except:
-            raise
-
-    def fill_description(self, p_model1, p_model2):
-        description = ""
-        description += "This model gives the summation or multiplication of"
-        description += "%s and %s. "% ( p_model1.name, p_model2.name )
-        self.description += description
-
-if __name__ == "__main__":
-    m1= Model()
-    #m1.setParam("p1_scale", 25)
-    #m1.setParam("p1_length", 1000)
-    #m1.setParam("p2_scale", 100)
-    #m1.setParam("p2_rg", 100)
-    out1 = m1.runXY(0.01)
-
-    m2= Model()
-    #m2.p_model1.setParam("scale", 25)
-    #m2.p_model1.setParam("length", 1000)
-    #m2.p_model2.setParam("scale", 100)
-    #m2.p_model2.setParam("rg", 100)
-    out2 = m2.p_model1.runXY(0.01) {operator} m2.p_model2.runXY(0.01)\n
-    print "My name is %s."% m1.name
-    print out1, " = ", out2
-    if out1 == out2:
-        print "===> Simple Test: Passed!"
-    else:
-        print "===> Simple Test: Failed!"
+from sasmodels.core import load_model_info
+from sasmodels.sasview_model import make_model_from_info
+
+model_info = load_model_info('{model1}{operator}{model2}', force_mixture=True)
+model_info.name = '{name}'{desc_line}
+Model = make_model_from_info(model_info)
 """
-
 if __name__ == "__main__":
 #    app = wx.PySimpleApp()
