Example #1
0
    def process(self, observation, telemetry):
        if not observation.visible:
            return DualMotorInstruction(0x00, 0x00)

        power_delta, pid_debug = self.pid.update(observation.cross_track_error,
                                                 observation.time_delta)
        telemetry["pid_debug"] = pid_debug

        left_power = min(self.BASE_POWER, self.BASE_POWER + power_delta)
        right_power = min(self.BASE_POWER, self.BASE_POWER - power_delta)

        # sanity check
        left_power = util.minmax(left_power, -0xFF, 0xFF)
        right_power = util.minmax(right_power, -0xFF, 0xFF)

        return DualMotorInstruction(left_power, right_power)
Example #2
0
def af(cost_file, pick_list, alpha=0.5):
    F1_score = np.array([])
    final_softlabel = np.zeros((lens, 8))
    for i in pick_list:
        val_df = pd.read_csv('{}'.format(sf_val_list[i]), header=None)
        F1 = multi_class_F1score(val_df, cost_file, mode='af')
        F1_score = np.append(F1_score, F1)
    F1_score = np.reshape(F1_score, (1, len(pick_list)))
    F1_score = minmax(F1_score, 0.1, 0.5)
    for i in pick_list:
        x_df = pd.read_csv('{}'.format(sf_test_list[i]), header=None)
        x_vec = np.array(x_df)
        confidence_score = (1 - alpha) * x_vec.max(1) + alpha * F1_score[pick_list.index(i)]
        confidence_score = np.reshape(confidence_score, (lens, 1))
        final_softlabel = final_softlabel + (x_vec * confidence_score)
        
    y_pred = np.argmax(final_softlabel, 1)
    # Compute the over all accuracy
    accuracy = accuracy_score(y_true, y_pred)
    
    # Get the Confusion Matrix
    cm = confusion_matrix(y_true, y_pred)

    # Generate CM (normalized) and compute total cost
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    cost = total_cost(cm, cost_file)

    return accuracy, cost
 def update_path_sides(self, f, t):
     """
     As of right now, this method is unfinished and deprecated.
     If completed, it should properly update all variables in the graph
     to represent a new edge added properly, including new constructions of
     walls, and all changes in the adjacency and reverse listed, even
     changes affecting other neighboring paths which get changed as a
     result of the new edge. As of right now, we consider this by simply
     re-traversing the entire graph every time an edge is added.
     """
     self.update_vertex_neighbors(f)
     self.update_vertex_neighbors(t)
     if self.path_orientation(f, t) == "V":
         min, max = util.minmax(f[1], t[1])
         for i in range(min, max + 1):
             print(f[0], i),
         print ""
     elif self.path_orientation(f, t) == "H":
         min, max = util.minmax(f[0], t[0])
         for i in range(min, max + 1):
             print(i, f[1]),
         print ""
Example #4
0
def _twoMedians(domainDists):
    """
    Runs k-means with k=2 to find two clusters with similar distance values.
    The input parameter <domainDists> contains tuples (<domainname>,
    <distance>).

    returns the two clusters as a tuple of two lists, where each list contains
    the <DomainStr> objects in the cluster
    """
    dists,domains = zip(*domainDists)
    #_,labels = kmeans2(np.array(dists), 2, minit='points')
    mn,mx = minmax(domainDists)
    _,labels = kmeans2(np.array(dists), np.array([mn[0], mx[0]]), minit='matrix')
    labeledDomains = zip(domains, labels)
    d1,d2 = splitOnCondition(labeledDomains, lambda x:x[1]==0)
    return (d1,d2)
Example #5
0
    def process(self, observation, telemetry):
        if not observation.visible:
            return ThrottleSteeringInstruction(self.THROTTLE_NEUTRAL_TICKS,
                                               self.STEERING_NEUTRAL_TICKS)

        steering, pid_debug = self.pid.update(observation.cross_track_error,
                                              observation.time_delta)
        telemetry["pid_debug"] = pid_debug

        steering = util.minmax(steering, -1.0, 1.0)
        telemetry["steering"] = steering

        steering_ticks = int(self.STEERING_NEUTRAL_TICKS +
                             steering * self.STEERING_RANGE_TICKS)

        return ThrottleSteeringInstruction(self.THROTTLE_DEFAULT_FWD_TICKS,
                                           steering_ticks)
Example #6
0
def plot_4c(plotObjects, kwargs):
    """
  kwargs is a dict with all kwargsion needed to config the figure and the
  curves. The kwargsions will be:
   reference: Pd, SP or Pf
   operation: True or False
   set: tst or val
  plot 4 curves
  """
    from ROOT import kCyan, kRed, kGreen, kBlue, kBlack, kMagenta, kGray
    Colors = [kBlue, kRed, kMagenta, kBlack, kCyan, kGreen]
    from RingerCore import StdPair as std_pair
    from util import line

    ref = kwargs['reference']
    refVal = kwargs['refVal']
    dset = kwargs['set']
    isOperation = kwargs['operation']
    detailed = True if plotObjects.size() == 1 else False
    percent = 0.03  #(default for now)
    savename = kwargs['cname'] + '.pdf'
    lines = []

    #Some protection
    if not ('val' in dset or 'tst' in dset):
        raise ValueError('Option set must be: tst (test) or val (validation)')
    if not ('SP' in ref or 'Pd' in ref or 'Pf' in ref):
        raise ValueError('Option reference must be: SP, Pd or Pf')

    ylabel = {
        'mse': 'MSE (' + dset + ')',
        'sp': 'SP (' + dset + ')',
        'det': 'Det (' + dset + ')',
        'fa': 'FA (' + dset + ')'
    }

    #Create dict to hold all list plots
    curves = dict()
    #list of dicts to dict of lists
    for name in plotObjects.keys():
        curves[name] = plotObjects.tolist(name)

    #Adapt reference into the validation set
    #mse_trn, mse_val, mse_tst
    if ref == 'Pd':
        curves['sp_val'] = curves['det_point_sp_val']
        curves['det_val'] = curves['det_point_det_val']  # det_fitted
        curves['fa_val'] = curves['det_point_fa_val']
        curves['sp_tst'] = curves['det_point_sp_tst']
        curves['det_tst'] = curves['det_point_det_tst']
        curves['fa_tst'] = curves['det_point_fa_tst']
    elif ref == 'Pf':
        curves['sp_val'] = curves['fa_point_sp_val']
        curves['det_val'] = curves['fa_point_det_val']
        curves['fa_val'] = curves['fa_point_fa_val']  # fa_fitted
        curves['sp_tst'] = curves['fa_point_sp_tst']
        curves['det_tst'] = curves['fa_point_det_tst']
        curves['fa_tst'] = curves['fa_point_fa_tst']
    else:  # ref == 'SP'
        curves['sp_val'] = curves['bestsp_point_sp_val']  # best SP curve
        curves['det_val'] = curves['bestsp_point_det_val']
        curves['fa_val'] = curves['bestsp_point_fa_val']
        curves['sp_tst'] = curves['bestsp_point_sp_tst']
        curves['det_tst'] = curves['bestsp_point_det_tst']
        curves['fa_tst'] = curves['bestsp_point_fa_tst']

    from util import minmax

    #check if the test set is zeros
    hasTst = True if curves['mse_tst'][0].GetMean(2) > 1e-10 else False

    if dset == 'tst' and not hasTst:
        print 'We dont have test set into plotObjects, abort plot!'
        return False

    #If only one plot per key, enabled analysis using all sets
    if detailed:
        #train, validation and test
        paint_curves = [std_pair(i, Colors[i]) for i in range(3)]
        curves['mse'] = [curves['mse_trn'][0], curves['mse_val'][0]]
        curves['sp'] = [curves['sp_val'][0]]
        curves['det'] = [curves['det_val'][0]]
        curves['fa'] = [curves['fa_val'][0]]
        if hasTst:
            for key in ['mse', 'sp', 'det', 'fa']:
                curves[key].append(curves[key + '_tst'][0])
        ylabel = {'mse': 'MSE', 'sp': 'SP', 'det': 'Det', 'fa': 'FA'}

    else:  #Do analysis for each set type
        paintIdx = kwargs['paintListIdx']  # [best, worst]
        paint_curves = [
            std_pair(plotObjects.index_correction(paintIdx[0]), kBlack),
            std_pair(plotObjects.index_correction(paintIdx[1]), kRed)
        ]
        curves['mse'] = curves['mse_' + dset]
        curves['sp'] = curves['sp_' + dset]
        curves['det'] = curves['det_' + dset]
        curves['fa'] = curves['fa_' + dset]

    #Adapt the legend and percent vec
    pmask = [1, 1, 1, 1]
    if ref == 'Pd':
        ylabel['det'] = ylabel['det'] + ' [Reference]'
        ylabel['fa'] = ylabel['fa'] + ' [benchmark]'
        pmask = [1, 1, 0, 1]
    elif ref == 'Pf':
        ylabel['det'] = ylabel['det'] + ' [benchmark]'
        ylabel['fa'] = ylabel['fa'] + ' [Reference]'
        pmask = [1, 1, 1, 0]
    else:
        ylabel['sp'] = ylabel['sp'] + ' [benchmark]'

    #Build lines
    lines = {'mse': [], 'sp': [], 'det': [], 'fa': [], 'ref': None}
    if detailed:  # Hard code setting lines
        y = dict()
        x = dict()
        for idx, key in enumerate(['mse', 'sp', 'det', 'fa']):
            y[key] = minmax(curves[key], 8, pmask[idx] * percent)
            x[key] = curves[key + '_stop'][0]
        #Colors = [kBlue, kRed, kMagenta, kBlack, kCyan, kGreen]
        lines['mse'].append(
            line(x['mse'], y['mse'][0], x['mse'], y['mse'][1], Colors[3], 1,
                 2))
        lines['sp'].append(
            line(x['sp'], y['sp'][0], x['sp'], y['sp'][1], Colors[3], 1, 2))
        if ref == 'Pd':
            lines['det'].append(
                line(x['det'], y['det'][0], x['det'], y['det'][1], Colors[2],
                     1, 2))
            lines['fa'].append(
                line(x['det'], y['fa'][0], x['det'], y['fa'][1], Colors[2], 2,
                     2))
        if ref == 'Pf':
            lines['det'].append(
                line(x['fa'], y['det'][0], x['fa'], y['det'][1], Colors[2], 2,
                     2))
            lines['fa'].append(
                line(x['fa'], y['fa'][0], x['fa'], y['fa'][1], Colors[2], 1,
                     2))

    #Start to build all ROOT objects
    from ROOT import TCanvas, gROOT, kTRUE
    gROOT.SetBatch(kTRUE)
    canvas = TCanvas('canvas', 'canvas', 1600, 1300)
    canvas.Divide(1, 4)

    def __plot_curves(tpad, curves, y_limits, **kw):
        from ROOT import kCyan, kRed, kGreen, kBlue, kBlack, kMagenta, kGray
        title = kw.pop('title', '')
        xlabel = kw.pop('xlabel', '')
        ylabel = kw.pop('ylabel', '')
        paintCurves = kw.pop('paintCurves', None)
        colorCurves = kw.pop('colorCurves', kGray)
        lines = kw.pop('lines', [])
        #create dummy graph
        x_max = 0
        dummy = None
        for i in range(len(curves)):
            curves[i].SetLineColor(colorCurves)
            x = curves[i].GetXaxis().GetXmax()
            if x > x_max:
                x_max = x
                dummy = curves[i]
        dummy.SetTitle(title)
        dummy.GetXaxis().SetTitle(xlabel)
        dummy.GetYaxis().SetTitle(ylabel)
        dummy.GetHistogram().SetAxisRange(y_limits[0], y_limits[1], 'Y')
        dummy.Draw('AL')
        #Plot curves
        for c in curves:
            c.SetLineWidth(1)
            c.SetLineStyle(3)
            c.Draw('same')
        #Paint a specifical curve
        if paintCurves:
            if len(paintCurves) > len(curves):
                for idx, c in enumerate(curves):
                    c.SetLineWidth(1)
                    c.SetLineColor(paintCurves[idx].second)
                    c.SetLineStyle(1)
                    c.Draw('same')
            else:
                for pair in paintCurves:
                    curves[pair.first].SetLineWidth(1)
                    curves[pair.first].SetLineStyle(1)
                    curves[pair.first].SetLineColor(pair.second)
                    curves[pair.first].Draw('same')
        #Plot lines
        for l in lines:
            l.Draw()
        #Update TPad
        tpad.Modified()
        tpad.Update()
        return x_max

    #__plot_curves end

    xlimits = list()
    for idx, key in enumerate(['mse', 'sp', 'det', 'fa']):
        #There are more plots
        x_max = __plot_curves(canvas.cd(idx + 1),
                              curves[key],
                              minmax(curves[key], 8, pmask[idx] * percent),
                              xlabel='Epoch',
                              ylabel=ylabel[key],
                              paintCurves=paint_curves,
                              colorCurves=kGray + 1,
                              lines=lines[key])
        xlimits.append(x_max)
    #Loop over plots

    #Check if there is any label
    if 'label' in kwargs.keys():
        tpad = canvas.cd(1)
        from TuningStyle import Label
        Label(0.6, 0.7, kwargs['label'], 1, 0.15)
        tpad.Modified()
        tpad.Update()

    # Reference base line
    if ref == 'Pd':
        tpad = canvas.cd(3)
        lines['ref'] = line(0.0, refVal, xlimits[2], refVal, kGreen, 2, 1)
        lines['ref'].Draw()
        tpad.Modified()
        tpad.Update()
    if ref == 'Pf':
        tpad = canvas.cd(4)
        lines['ref'] = line(0.0, refVal, xlimits[3], refVal, kGreen, 2, 1)
        lines['ref'].Draw()
        tpad.Modified()
        tpad.Update()

    canvas.Modified()
    canvas.Update()
    canvas.SaveAs(savename)
    del canvas
    return savename
Example #7
0
def plot_4c(plotObjects, kwargs):
    """
  kwargs is a dict with all kwargsion needed to config the figure and the
  curves. The kwargsions will be:
   reference: Pd, SP or Pf
   operation: True or False
   set: tst or val
  plot 4 curves
  """
    from ROOT import kCyan, kRed, kGreen, kBlue, kBlack, kMagenta, kGray

    Colors = [kBlue, kRed, kMagenta, kBlack, kCyan, kGreen]
    from RingerCore import StdPair as std_pair
    from util import line

    ref = kwargs["reference"]
    refVal = kwargs["refVal"]
    dset = kwargs["set"]
    isOperation = kwargs["operation"]
    detailed = True if plotObjects.size() == 1 else False
    percent = 0.03  # (default for now)
    savename = kwargs["cname"] + ".pdf"
    lines = []

    # Some protection
    if not ("val" in dset or "tst" in dset):
        raise ValueError("Option set must be: tst (test) or val (validation)")
    if not ("SP" in ref or "Pd" in ref or "Pf" in ref):
        raise ValueError("Option reference must be: SP, Pd or Pf")

    ylabel = {
        "mse": "MSE (" + dset + ")",
        "sp": "SP (" + dset + ")",
        "det": "Det (" + dset + ")",
        "fa": "FA (" + dset + ")",
    }

    # Create dict to hold all list plots
    curves = dict()
    # list of dicts to dict of lists
    for name in plotObjects.keys():
        curves[name] = plotObjects.tolist(name)

    # Adapt reference into the validation set
    # mse_trn, mse_val, mse_tst
    if ref == "Pd":
        curves["sp_val"] = curves["det_point_sp_val"]
        curves["det_val"] = curves["det_point_det_val"]  # det_fitted
        curves["fa_val"] = curves["det_point_fa_val"]
        curves["sp_tst"] = curves["det_point_sp_tst"]
        curves["det_tst"] = curves["det_point_det_tst"]
        curves["fa_tst"] = curves["det_point_fa_tst"]
    elif ref == "Pf":
        curves["sp_val"] = curves["fa_point_sp_val"]
        curves["det_val"] = curves["fa_point_det_val"]
        curves["fa_val"] = curves["fa_point_fa_val"]  # fa_fitted
        curves["sp_tst"] = curves["fa_point_sp_tst"]
        curves["det_tst"] = curves["fa_point_det_tst"]
        curves["fa_tst"] = curves["fa_point_fa_tst"]
    else:  # ref == 'SP'
        curves["sp_val"] = curves["bestsp_point_sp_val"]  # best SP curve
        curves["det_val"] = curves["bestsp_point_det_val"]
        curves["fa_val"] = curves["bestsp_point_fa_val"]
        curves["sp_tst"] = curves["bestsp_point_sp_tst"]
        curves["det_tst"] = curves["bestsp_point_det_tst"]
        curves["fa_tst"] = curves["bestsp_point_fa_tst"]

    from util import minmax

    # check if the test set is zeros
    hasTst = True if curves["mse_tst"][0].GetMean(2) > 1e-10 else False

    if dset == "tst" and not hasTst:
        print "We dont have test set into plotObjects, abort plot!"
        return False

    # If only one plot per key, enabled analysis using all sets
    if detailed:
        # train, validation and test
        paint_curves = [std_pair(i, Colors[i]) for i in range(3)]
        curves["mse"] = [curves["mse_trn"][0], curves["mse_val"][0]]
        curves["sp"] = [curves["sp_val"][0]]
        curves["det"] = [curves["det_val"][0]]
        curves["fa"] = [curves["fa_val"][0]]
        if hasTst:
            for key in ["mse", "sp", "det", "fa"]:
                curves[key].append(curves[key + "_tst"][0])
        ylabel = {"mse": "MSE", "sp": "SP", "det": "Det", "fa": "FA"}

    else:  # Do analysis for each set type
        paintIdx = kwargs["paintListIdx"]  # [best, worst]
        paint_curves = [
            std_pair(plotObjects.index_correction(paintIdx[0]), kBlack),
            std_pair(plotObjects.index_correction(paintIdx[1]), kRed),
        ]
        curves["mse"] = curves["mse_" + dset]
        curves["sp"] = curves["sp_" + dset]
        curves["det"] = curves["det_" + dset]
        curves["fa"] = curves["fa_" + dset]

    # Adapt the legend and percent vec
    pmask = [1, 1, 1, 1]
    if ref == "Pd":
        ylabel["det"] = ylabel["det"] + " [Reference]"
        ylabel["fa"] = ylabel["fa"] + " [benchmark]"
        pmask = [1, 1, 0, 1]
    elif ref == "Pf":
        ylabel["det"] = ylabel["det"] + " [benchmark]"
        ylabel["fa"] = ylabel["fa"] + " [Reference]"
        pmask = [1, 1, 1, 0]
    else:
        ylabel["sp"] = ylabel["sp"] + " [benchmark]"

    # Build lines
    lines = {"mse": [], "sp": [], "det": [], "fa": [], "ref": None}
    if detailed:  # Hard code setting lines
        y = dict()
        x = dict()
        for idx, key in enumerate(["mse", "sp", "det", "fa"]):
            y[key] = minmax(curves[key], 8, pmask[idx] * percent)
            x[key] = curves[key + "_stop"][0]
        # Colors = [kBlue, kRed, kMagenta, kBlack, kCyan, kGreen]
        lines["mse"].append(line(x["mse"], y["mse"][0], x["mse"], y["mse"][1], Colors[3], 1, 2))
        lines["sp"].append(line(x["sp"], y["sp"][0], x["sp"], y["sp"][1], Colors[3], 1, 2))
        if ref == "Pd":
            lines["det"].append(line(x["det"], y["det"][0], x["det"], y["det"][1], Colors[2], 1, 2))
            lines["fa"].append(line(x["det"], y["fa"][0], x["det"], y["fa"][1], Colors[2], 2, 2))
        if ref == "Pf":
            lines["det"].append(line(x["fa"], y["det"][0], x["fa"], y["det"][1], Colors[2], 2, 2))
            lines["fa"].append(line(x["fa"], y["fa"][0], x["fa"], y["fa"][1], Colors[2], 1, 2))

    # Start to build all ROOT objects
    from ROOT import TCanvas, gROOT, kTRUE

    gROOT.SetBatch(kTRUE)
    canvas = TCanvas("canvas", "canvas", 1600, 1300)
    canvas.Divide(1, 4)

    def __plot_curves(tpad, curves, y_limits, **kw):
        from ROOT import kCyan, kRed, kGreen, kBlue, kBlack, kMagenta, kGray

        title = kw.pop("title", "")
        xlabel = kw.pop("xlabel", "")
        ylabel = kw.pop("ylabel", "")
        paintCurves = kw.pop("paintCurves", None)
        colorCurves = kw.pop("colorCurves", kGray)
        lines = kw.pop("lines", [])
        # create dummy graph
        x_max = 0
        dummy = None
        for i in range(len(curves)):
            curves[i].SetLineColor(colorCurves)
            x = curves[i].GetXaxis().GetXmax()
            if x > x_max:
                x_max = x
                dummy = curves[i]
        dummy.SetTitle(title)
        dummy.GetXaxis().SetTitle(xlabel)
        dummy.GetYaxis().SetTitle(ylabel)
        dummy.GetHistogram().SetAxisRange(y_limits[0], y_limits[1], "Y")
        dummy.Draw("AL")
        # Plot curves
        for c in curves:
            c.SetLineWidth(1)
            c.SetLineStyle(3)
            c.Draw("same")
        # Paint a specifical curve
        if paintCurves:
            if len(paintCurves) > len(curves):
                for idx, c in enumerate(curves):
                    c.SetLineWidth(1)
                    c.SetLineColor(paintCurves[idx].second)
                    c.SetLineStyle(1)
                    c.Draw("same")
            else:
                for pair in paintCurves:
                    curves[pair.first].SetLineWidth(1)
                    curves[pair.first].SetLineStyle(1)
                    curves[pair.first].SetLineColor(pair.second)
                    curves[pair.first].Draw("same")
        # Plot lines
        for l in lines:
            l.Draw()
        # Update TPad
        tpad.Modified()
        tpad.Update()
        return x_max

    # __plot_curves end

    xlimits = list()
    for idx, key in enumerate(["mse", "sp", "det", "fa"]):
        # There are more plots
        x_max = __plot_curves(
            canvas.cd(idx + 1),
            curves[key],
            minmax(curves[key], 8, pmask[idx] * percent),
            xlabel="Epoch",
            ylabel=ylabel[key],
            paintCurves=paint_curves,
            colorCurves=kGray + 1,
            lines=lines[key],
        )
        xlimits.append(x_max)
    # Loop over plots

    # Check if there is any label
    if "label" in kwargs.keys():
        tpad = canvas.cd(1)
        from TuningStyle import Label

        Label(0.6, 0.7, kwargs["label"], 1, 0.15)
        tpad.Modified()
        tpad.Update()

    # Reference base line
    if ref == "Pd":
        tpad = canvas.cd(3)
        lines["ref"] = line(0.0, refVal, xlimits[2], refVal, kGreen, 2, 1)
        lines["ref"].Draw()
        tpad.Modified()
        tpad.Update()
    if ref == "Pf":
        tpad = canvas.cd(4)
        lines["ref"] = line(0.0, refVal, xlimits[3], refVal, kGreen, 2, 1)
        lines["ref"].Draw()
        tpad.Modified()
        tpad.Update()

    canvas.Modified()
    canvas.Update()
    canvas.SaveAs(savename)
    del canvas
    return savename
Example #8
0
def domainDist(domObj1, domObj2):
    """
    Compute the distance between two domains.
    """

    if domObj1==domObj2:
        return 0.0

# FIXME, debugging
    if isinstance(domObj1, tuple) or isinstance(domObj2, tuple):
        msg=unicode(domObj1)
        msg=msg.encode('utf-8')
        logging.error(msg)
        msg=unicode(domObj2)
        msg=msg.encode('utf-8')
        logging.error(msg)

    sx=domObj1.rSplitView()
    sy=domObj2.rSplitView()

    mn,mx=minmax((domObj1.numDomainLevels(), domObj2.numDomainLevels()))

    dist=0.0

    """
    """
    totWeight=0

    """
    we consider the ratio of identical domain levels for the distance.
    the  eight of each identical domain level is computed as
    1/(offset+domainLevel), where domainLevel=0 is the top level domain. I.e.,
    the less significant a domain level is, the less weight it gets. <offset>
    is used to control the decline rate of the weight from one level to the
    next.
    """

    # FIXME, hardcoded parameter; needs to be FLOAT, else the divisions below
    # give integers!
    offset=3.0
    tldPenalty=0.0

    """
    First, compare all domain levels which exist in both domains, starting from
    the top-level-domain
    """
    for dLevel, (curSx, curSy) in enumerate(zip(sx[:mn], sy[:mn])):

        if dLevel==0:
            """
            this is the TLD: weight both the top level domain and the second
            level domain with maximum weight
            """
            #pWeight=1/offset
            if curSx != curSy:
                # TLDs are different
                #dist+=pWeight
                tldPenalty=0.05
            #totWeight+=pWeight
        else:
            """
            weight both the top level domain and the second level domain with
            maximum weight
            """
            pWeight=(1/(offset+dLevel-1))

            """
            the weight of this partial distance corresponds to the length of the
            longer partial string
            """
            lWeight=max(len(curSx), len(curSy))

            weight=lWeight*pWeight
            #print 'pweight',pWeight
            #print 'lweight',lWeight
            #print 'weight',weight

            if curSx != curSy:
                dd=(1-lev.ratio(curSx,curSy))*weight
                #print 'level',dLevel
                #print 'dd',dd
                dist+=dd
                #print 'dist',dist

            totWeight+=weight

    """
    Second, consider also the domain levels that exist only in one of the two
    domains
    """
    if mn!=mx:
        lx=domObj1.numDomainLevels()
        ly=domObj2.numDomainLevels()
        if lx<ly: longer = sy
        else: longer = sx

        """
        if one domain has more levels than the other, we need to consider
        these additional letter insertions. the number of insertions is
        simply the length of the longer substring (without dots)
        """
        for dLevel, j in enumerate(longer[mn:], start=mn):
            lWeight=len(j)
            pWeight=1/(offset+dLevel)
            weight=lWeight*pWeight
            dist+=weight
            totWeight+=weight

    #print 'dist',dist
    #print 'weight',weight

    if totWeight:
        dist=dist/totWeight+tldPenalty
    else:
        try:
            logging.warn('strange fqdns: '+str(domObj1)+', '+str(domObj2))
        except UnicodeEncodeError:
            pass
        dist=tldPenalty

    dist=min(dist,1.0)

    return dist