예제 #1
0
파일: t_small.py 프로젝트: sichu366/gpkit
 def test_unitstr(self):
     x = gpkit.Variable("x", "ft")
     # pint issue 356
     footstrings = ("ft", "foot")  # backwards compatibility with pint 0.6
     if gpkit.units:
         self.assertEqual(unitstr(gpkit.Variable("n", "count")), "count")
         self.assertIn(unitstr(x), footstrings)
         self.assertIn(unitstr(x.key), footstrings)
     self.assertEqual(unitstr(gpkit.Variable("y"), dimless="---"), "---")
     self.assertEqual(unitstr(None, dimless="--"), "--")
예제 #2
0
파일: t_small.py 프로젝트: hoburg/gpkit
 def test_unitstr(self):
     x = gpkit.Variable("x", "ft")
     # pint issue 356
     footstrings = ("ft", "foot")  # backwards compatibility with pint 0.6
     if gpkit.units:
         self.assertEqual(unitstr(gpkit.Variable("n", "count")), "count")
         self.assertIn(unitstr(x), footstrings)
         self.assertIn(unitstr(x.key), footstrings)
     self.assertEqual(unitstr(gpkit.Variable("y"), dimless="---"), "---")
     self.assertEqual(unitstr(None, dimless="--"), "--")
예제 #3
0
def get_highestsens(model, res, varnames=None, N=10):
    " plot bar chart of sensitivities "
    pss = []
    ngs = []
    sens = {}
    if varnames:
        for vname in varnames:
            sen = res["sensitivities"]["constants"][vname]
            if hasattr(sen, "__len__"):
                val = max(np.abs(sen.values()))
                vk = [svk for svk in sen if abs(sen[svk]) == val][0]
                sen = sum(sen.values())
            else:
                vk = model[vname].key
            sens[vk] = sen
    else:
        for s in res["sensitivities"]["constants"]:
            val = res["sensitivities"]["constants"][s]
            if hasattr(val, "shape"):
                if len(val.shape) == 1:
                    val = sum(val)
                if len(val.shape) == 2:
                    val = sum(np.hstack(val))
            sens[model[s].key] = val

    labels = []
    i = 0
    sorted_sens = dict_sort(sens)

    for s in sorted_sens:
        if i > N:
            break
        i += 1
        vk = s[0]
        val = sum(np.hstack([res(vk)]))
        if "units" in vk.descr:
            uts = unitstr(vk.descr["units"])
        else:
            uts = ""

        lbl = vk.descr["label"]
        labels.append(lbl + "$ =%.2f$ %s" % (val, uts.replace("*", "")))
        if s[1] > 0:
            pss.append(s[1])
            ngs.append(0)
        else:
            ngs.append(abs(s[1]))
            pss.append(0)

    ind = np.arange(0.5, i + 0.5, 1)
    sensdict = {
        "positives": pss,
        "negatives": ngs,
        "indicies": ind,
        "labels": labels
    }
    return sensdict
예제 #4
0
def plot_sens(model, result, varnames=None):
    fig, ax = plt.subplots()
    pss = []
    ngs = []
    sens = {}
    if varnames:
        for vname in varnames:
            sen = result["sensitivities"]["constants"][vname]
            if hasattr(sen, "__len__"):
                val = max(np.abs(sen.values()))
                vk = [svk for svk in sen if abs(sen[svk]) == val][0]
                # sen = result["sensitivities"]["constants"][vk]
                sen = sum(sen.values())
            else:
                vk = model[vname].key
            sens[vk] = sen
    else:
        for s in result["sensitivities"]["constants"]:
            sens[s] = sum(np.hstack([result["sensitivities"]["constants"][s]]))

    labels = []
    i = 0
    sorted_sens = sorted(sens.items(),
                         key=lambda x: np.absolute(x[1]),
                         reverse=True)

    for s in sorted_sens:
        i += 1
        if i > 15:
            break
        if hasattr(model[s[0]], "__len__"):
            vk = model[s[0]][0]
        else:
            vk = s[0]
        val = sum(np.hstack([model.substitutions[vk]]))
        if "units" in vk.descr:
            uts = unitstr(vk.descr["units"])
        else:
            uts = "-"

        labels.append(vk.descr["label"] + " =%.2f [%s]" % (val, uts))
        if s[1] > 0:
            pss.append(s[1])
            ngs.append(0)
        else:
            ngs.append(abs(s[1]))
            pss.append(0)

    ind = np.arange(0.5, i + 0.5, 1)
    ax.bar(ind, pss, 0.5, color="#4D606E")
    ax.bar(ind, ngs, 0.5, color="#3FBAC2")
    ax.set_xlim([0.0, ind[-1] + 1.0])
    ax.set_xticks(ind)
    ax.set_xticklabels(labels, rotation=-45, ha="left")
    ax.legend(["Positive", "Negative"])
    ax.set_ylabel("sensitivities")
    return fig, ax
예제 #5
0
파일: t_small.py 프로젝트: sichu366/gpkit
 def test_pint_366(self):
     # test for https://github.com/hgrecco/pint/issues/366
     if gpkit.units:
         self.assertIn(unitstr(gpkit.units("nautical_mile")),
                       ("nmi", "nautical_mile"))
         self.assertEqual(gpkit.units("nautical_mile"), gpkit.units("nmi"))
예제 #6
0
파일: t_small.py 프로젝트: hoburg/gpkit
 def test_pint_366(self):
     # test for https://github.com/hgrecco/pint/issues/366
     if gpkit.units:
         self.assertIn(unitstr(gpkit.units("nautical_mile")),
                       ("nmi", "nautical_mile"))
         self.assertEqual(gpkit.units("nautical_mile"), gpkit.units("nmi"))