Exemple #1
0
def matrix(m):
    is_correlation = True
    for i in range(len(m)):
        if m[i][i] != 1.0:
            is_correlation = False
            break

    if not is_correlation:
        n = len(m)
        args = []
        for mi in m:
            for mj in mi:
                args.append(mj)
        nums = matrix_format(*args)

    grad = Gradient(
        (-1.0, 120.0, 120.0, 250.0),
        (0.0, 250.0, 250.0, 250.0),
        (1.0, 250.0, 100.0, 100.0),
    )

    s = Html()
    with table(s):
        with tr(s):
            s += "<td/>\n"
            for v in m.names:
                with th(s):
                    s += v
        for i, v in enumerate(m.names):
            with tr(s):
                with th(s):
                    s += v
                for j in range(len(m.names)):
                    val = m[i][j]
                    if is_correlation:
                        if i == j:
                            with td(s):
                                s += " 1.00"
                        else:
                            color = grad.rgb(val)
                            with td(s, style="background-color:" + color):
                                s += "%5.2f" % val
                    else:
                        if i == j:
                            with td(s):
                                s += nums[n * i + j]
                        else:
                            color = grad.rgb(val / (m[i][i] * m[j][j])**0.5)
                            with td(s, style="background-color:" + color):
                                s += nums[n * i + j]

    return str(s)
Exemple #2
0
def matrix(m):
    is_correlation = True
    for i in range(len(m)):
        if m[i][i] != 1.0:
            is_correlation = False
            break

    if not is_correlation:
        n = len(m)
        args = []
        for mi in m:
            for mj in mi:
                args.append(mj)
        nums = matrix_format(*args)

    grad = Gradient(
        (-1.0, 120.0, 120.0, 250.0),
        (0.0, 250.0, 250.0, 250.0),
        (1.0, 250.0, 100.0, 100.0),
    )

    rows = []
    for i, v in enumerate(m.names):
        cols = [th(v)]
        for j in range(len(m.names)):
            val = m[i][j]
            if is_correlation:
                if i == j:
                    t = td("1.00")
                else:
                    color = grad.rgb(val)
                    t = td(
                        "%5.2f" % val,
                        style="background-color:" + color + ";color:black",
                    )
            else:
                if i == j:
                    t = td(nums[n * i + j])
                else:
                    num = m[i][i] * m[j][j]
                    color = grad.rgb(val / num**0.5 if num > 0 else 0)
                    t = td(
                        nums[n * i + j],
                        style="background-color:" + color + ";color:black",
                    )
            cols.append(t)
        rows.append(tr(*cols))

    return to_str(table(tr(td(), *[th(v) for v in m.names]), *rows))
Exemple #3
0
    def build_matrix(cls, vnames, matrix, latex_map=None):
        """build latex correlation matrix"""
        # ret_link  = '<a onclick="$(\'#%s\').toggle()" href="#">Show Latex</a>'%uid
        headers = [""] + list(vnames)
        data = []
        color = {}
        grad = Gradient(
            (-1.0, 120.0, 120.0, 250.0),
            (0.0, 250.0, 250.0, 250.0),
            (1.0, 250.0, 100.0, 100.0),
        )
        for i, v1 in enumerate(vnames):
            tmp = [v1]
            for j in range(len(vnames)):
                m = matrix[i][j]
                tmp.append(m)
                color[(i + 1, j + 1)] = grad(m)
                # +1 for header on the side and top
            data.append(tmp)

        table = LatexTable(headers=headers,
                           data=data,
                           rotate_header=True,
                           latex_map=latex_map)
        table.float_format = "%.2g"
        for (i, j), c in color.items():
            table.set_cell_color(i, j, c)
        return table
Exemple #4
0
def matrix(m):
    is_correlation = True
    for i in range(len(m)):
        if m[i][i] != 1.0:
            is_correlation = False
            break

    if not is_correlation:
        n = len(m)
        args = []
        for mi in m:
            for mj in mi:
                args.append(mj)
        nums = format_numbers(*args)

    s = Html()
    with table(s):
        with tr(s):
            s += "<td/>\n"
            for v in m.names:
                with th(s):
                    s += v
        for i, v in enumerate(m.names):
            with tr(s):
                with th(s):
                    s += v
                for j in range(len(m.names)):
                    val = m[i][j]
                    if is_correlation:
                        if i == j:
                            with td(s):
                                s += "1.00"                        
                        else:
                            color = Gradient.rgb_color_for(val)
                            with td(s, style="background-color:"+color):
                                s += "%.2f" % val
                    else:
                        if i == j:
                            with td(s):
                                s += nums[n*i + j]
                        else:
                            color = Gradient.rgb_color_for(val / (m[i][i] * m[j][j]) ** 0.5)
                            with td(s, style="background-color:"+color):
                                s += nums[n*i + j]

    return str(s)
Exemple #5
0
def test_color_2():
    g = Gradient((-1, 10, 10, 20), (2, 20, 20, 10))
    assert g.rgb(-1) == "rgb(10,10,20)"
    assert g.rgb(2) == "rgb(20,20,10)"
    assert g.rgb(-1.00001) == "rgb(10,10,20)"
    assert g.rgb(1.99999) == "rgb(20,20,10)"
    assert g.rgb(0.5) == "rgb(15,15,15)"
Exemple #6
0
def test_color_3():
    g = Gradient((-1, 50, 50, 250), (0, 100, 100, 100), (1, 250, 50, 50))
    assert g.rgb(-1) == "rgb(50,50,250)"
    assert g.rgb(-0.5) == "rgb(75,75,175)"
    assert g.rgb(0) == "rgb(100,100,100)"
    assert g.rgb(0.5) == "rgb(175,75,75)"
    assert g.rgb(1) == "rgb(250,50,50)"
Exemple #7
0
 def print_matrix(self, vnames, matrix, latex_map=None):
     from IPython.core.display import display_html
     latexuid = randid()
     latextable = LatexFactory.build_matrix(vnames,
                                            matrix,
                                            latex_map=latex_map)
     to_print = """
         <table>
             <tr>
                 <td>%s</td>
     """ % self.toggle_sign(latexuid)
     for v in vnames:
         to_print += """
         <td>
         <div style="width:20px;position:relative; width: -moz-fit-content;">
         <div style="display:inline-block;-webkit-writing-mode:vertical-rl;-moz-writing-mode: vertical-rl;writing-mode: vertical-rl;">
         {v}
         </div>
         </div>
         </td>
         """.format(**locals())
     to_print += """
             </tr>
             """
     for i, v1 in enumerate(vnames):
         to_print += """
         <tr>
             <td>{v1}</td>
         """.format(**locals())
         for j, v2 in enumerate(vnames):
             val = matrix[i][j]
             color = Gradient.rgb_color_for(val)
             to_print += """
             <td style="background-color:{color}">
             {val:3.2f}
             </td>
             """.format(**locals())
         to_print += """
         </tr>
         """
     to_print += '</table>\n'
     to_print += self.hidden_table(str(latextable), latexuid)
     display_html(to_print, raw=True)
Exemple #8
0
 def print_matrix(self, vnames, matrix, latex_map=None):
     from IPython.core.display import display_html
     latexuid = randid()
     latextable = LatexFactory.build_matrix(vnames, matrix,
                                            latex_map=latex_map)
     to_print = """
         <table>
             <tr>
                 <td>%s</td>
     """ % self.toggle_sign(latexuid)
     for v in vnames:
         to_print += """
         <td>
         <div style="width:20px;position:relative; width: -moz-fit-content;">
         <div style="display:inline-block;-webkit-writing-mode:vertical-rl;-moz-writing-mode: vertical-rl;writing-mode: vertical-rl;">
         {v}
         </div>
         </div>
         </td>
         """.format(**locals())
     to_print += """
             </tr>
             """
     for i, v1 in enumerate(vnames):
         to_print += """
         <tr>
             <td>{v1}</td>
         """.format(**locals())
         for j, v2 in enumerate(vnames):
             val = matrix[i][j]
             color = Gradient.rgb_color_for(val)
             to_print += """
             <td style="background-color:{color}">
             {val:3.2f}
             </td>
             """.format(**locals())
         to_print += """
         </tr>
         """
     to_print += '</table>\n'
     to_print += self.hidden_table(str(latextable), latexuid)
     display_html(to_print, raw=True)
Exemple #9
0
    def build_matrix(cls, vnames, matrix, latex_map=None):
        """build latex correlation matrix"""
        # ret_link  = '<a onclick="$(\'#%s\').toggle()" href="#">Show Latex</a>'%uid
        headers = [''] + list(vnames)
        data = []
        color = {}
        for i, v1 in enumerate(vnames):
            tmp = [v1]
            for j, v2 in enumerate(vnames):
                m = matrix[i][j]
                tmp.append(m)
                color[(i + 1, j + 1)] = Gradient.color_for(abs(m))
                # +1 for header on the side and top
            data.append(tmp)

        table = LatexTable(headers=headers, data=data, rotate_header=True,
                           latex_map=latex_map)
        table.float_format = '%3.2f'
        for (i, j), c in color.items():
            table.set_cell_color(i, j, c)
        return table
Exemple #10
0
    def build_matrix(cls, vnames, matrix, latex_map=None):
        """build latex correlation matrix"""
        # ret_link  = '<a onclick="$(\'#%s\').toggle()" href="#">Show Latex</a>'%uid
        headers = [''] + list(vnames)
        data = []
        color = {}
        for i, v1 in enumerate(vnames):
            tmp = [v1]
            for j, v2 in enumerate(vnames):
                m = matrix[i][j]
                tmp.append(m)
                color[(i + 1, j + 1)] = Gradient.color_for(abs(m))
                # +1 for header on the side and top
            data.append(tmp)

        table = LatexTable(headers=headers, data=data, rotate_header=True,
                           latex_map=latex_map)
        table.float_format = '%.2g'
        for (i, j), c in color.items():
            table.set_cell_color(i, j, c)
        return table
Exemple #11
0
 def print_matrix(self, vnames, matrix, latex_map=None):
     latexuid = randid(self.rng)
     latextable = LatexFactory.build_matrix(vnames,
                                            matrix,
                                            latex_map=latex_map)
     to_print = """<table>
 <tr>
     <td>%s</td>""" % self.toggle_sign(latexuid)
     for v in vnames:
         to_print += " <td>{v}</td>".format(**locals())
     to_print += "\n    </tr>\n"
     for i, v1 in enumerate(vnames):
         to_print += """    <tr>
     <td>{v1}</td>""".format(**locals())
         for j, v2 in enumerate(vnames):
             val = matrix[i][j]
             color = Gradient.rgb_color_for(val)
             to_print += r""" <td style="background-color:{color}">{val:3.2f}</td>""".format(
                 **locals())
         to_print += "\n    </tr>\n"
     to_print += '</table>\n'
     to_print += self.hidden_table(str(latextable), latexuid)
     self.display(to_print)