def subtract_bg(samplename, bgname, factor=<NUM_LIT:1>, distance=None, disttolerance=<NUM_LIT:2>,<EOL>subname=None, qrange=(), graph_extension='<STR_LIT>', graph_dpi=<NUM_LIT>):
ip = get_ipython()<EOL>data1d = ip.user_ns['<STR_LIT>']<EOL>data2d = ip.user_ns['<STR_LIT>']<EOL>if '<STR_LIT>' not in ip.user_ns:<EOL><INDENT>ip.user_ns['<STR_LIT>'] = set()<EOL><DEDENT>subtractedsamplenames = ip.user_ns['<STR_LIT>']<EOL>if subname is None:<EOL><INDENT>if isinstance(bgname, str):<EOL><INDENT>subname = samplename + '<STR_LIT:->' + bgname<EOL><DEDENT>else:<EOL><INDENT>subname = samplename + '<STR_LIT>'<EOL><DEDENT><DEDENT>if distance is None:<EOL><INDENT>dists = data1d[samplename]<EOL><DEDENT>else:<EOL><INDENT>dists = [d for d in data1d[samplename] if abs(d - distance) < disttolerance]<EOL><DEDENT>for dist in dists:<EOL><INDENT>if isinstance(bgname, str):<EOL><INDENT>if not disttolerance:<EOL><INDENT>if dist not in data1d[bgname]:<EOL><INDENT>print(<EOL>'<STR_LIT>' % (<EOL>dist, samplename, bgname))<EOL>continue<EOL><DEDENT>else:<EOL><INDENT>bgdist = dist<EOL><DEDENT><DEDENT>else:<EOL><INDENT>bgdist = sorted([(d, r) for (d, r) in [(d, np.abs(d - dist)) for d in list(data1d[bgname].keys())] if<EOL>r <= disttolerance], key=lambda x: x[<NUM_LIT:1>])[<NUM_LIT:0>][<NUM_LIT:0>]<EOL><DEDENT><DEDENT>if subname not in data1d:<EOL><INDENT>data1d[subname] = {}<EOL><DEDENT>if subname not in data2d:<EOL><INDENT>data2d[subname] = {}<EOL><DEDENT>if subname not in ip.user_ns['<STR_LIT>']:<EOL><INDENT>ip.user_ns['<STR_LIT>'][subname] = {}<EOL><DEDENT>data1_s = data1d[samplename][dist]<EOL>data2_s = data2d[samplename][dist]<EOL>if isinstance(bgname, str):<EOL><INDENT>data1_bg = data1d[bgname][bgdist]<EOL>data2_bg = data2d[bgname][bgdist]<EOL>if factor is None:<EOL><INDENT>factor = data1_s.trim(*qrange).momentum(<NUM_LIT:0>) / data1_bg.trim(*qrange).momentum(<NUM_LIT:0>)<EOL><DEDENT><DEDENT>elif bgname is None:<EOL><INDENT>data1_bg = data1_s.trim(*qrange).momentum(<NUM_LIT:0>)<EOL>data2_bg = data1_bg<EOL><DEDENT>else:<EOL><INDENT>data1_bg = bgname<EOL>data2_bg = bgname<EOL><DEDENT>if factor is None:<EOL><INDENT>factor = <NUM_LIT:1><EOL><DEDENT>data1d[subname][dist] = data1_s - factor * data1_bg<EOL>data2d[subname][dist] = data2_s - factor * data2_bg<EOL>data1d[subname][dist].save(<EOL>os.path.join(ip.user_ns['<STR_LIT>'], subname + '<STR_LIT:_>' + ('<STR_LIT>' % dist).replace('<STR_LIT:.>', '<STR_LIT:_>') + '<STR_LIT>'))<EOL>ip.user_ns['<STR_LIT>'][subname][dist] = ip.user_ns['<STR_LIT>'][samplename][<EOL>dist]  <EOL>plt.figure()<EOL>plotsascurve(samplename, dist=dist)<EOL>if isinstance(bgname, str):<EOL><INDENT>plotsascurve(bgname, dist=dist, factor=factor)<EOL><DEDENT>plotsascurve(subname, dist=dist)<EOL>plt.savefig(os.path.join(ip.user_ns['<STR_LIT>'],<EOL>'<STR_LIT>' + samplename + '<STR_LIT:.>' + graph_extension),<EOL>dpi=graph_dpi)<EOL>subtractedsamplenames.add(subname)<EOL><DEDENT>
Subtract background from measurements.

    Inputs:
        samplename: the name of the sample
        bgname: the name of the background measurements. Alternatively, it can
            be a numeric value (float or ErrorValue), which will be subtracted.
            If None, this constant will be determined by integrating the
            scattering curve in the range given by qrange.
        factor: the background curve will be multiplied by this
        distance: if None, do the subtraction for all sample-to-detector distances.
            Otherwise give here the value of the sample-to-detector distance.
        qrange: a tuple (qmin, qmax)
        disttolerance: the tolerance in which two distances are considered
            equal.
        subname: the sample name of the background-corrected curve. The default
            is samplename + '-' + bgname