Esempio n. 1
0
File: math.py Progetto: mgroth0/mlib
def nanmean(lll):
    if ndims(lll) > 2:
        err('no ready')
    elif ndims(lll) == 2:
        rrr = arr()
        for i in range(0, lll.shape[1]):
            colu = list(filter(lambda x: not isnan(x), lll[:, i]))
            rrr += safemean(colu)
    else:  # 1-d
        lll = list(filter(lambda x: not isnan(x), lll))
        rrr = safemean(lll)
    # noinspection PyUnboundLocalVariable
    return rrr
Esempio n. 2
0
def separate_comp_mat_by_classes_compared(
        normalized1: ComparisonMatrix,
        net: str,
        arch: str,
        method_name: str,
        sim_string: str,
        pattern: str
) -> StatisticalArrays:
    simsets = deepcopy(SYM_CLASS_SET_PATTERN_GROUPINGS[pattern])
    simkeys = listkeys(SYM_CLASS_SET_PATTERN_GROUPINGS[pattern])[1:]
    for k in simsets:
        simsets[k] = arr()  # rather than list
    average = np.nanmean(normalized1)

    for i, c in enum(RSA_CLASSES):
        for ii, cc in enum(RSA_CLASSES):
            if ii > i: continue
            comp_mat = normalized1[c, cc]
            normalized2 = arr([num for num in flatten(comp_mat).tolist() if not isnan(num)]) / average
            groupname1 = simkeys[SYM_CLASS_SET_PATTERNS[pattern][i]]
            groupname2 = simkeys[SYM_CLASS_SET_PATTERNS[pattern][ii]]
            if groupname1 == groupname2:
                simsets[groupname1] += flatten(normalized2)
            else:
                simsets['AC'] += flatten(normalized2)

    return StatisticalArrays(
        ylabel=f'{sim_string} Score ({method_strings[method_name]}) ({pattern_strings[pattern]})',
        xlabel='Class Comparison Group',
        data={k: v for k, v in simsets.items()},
        title_suffix=f'{net}:{RSA_LAYERS[arch]} ({method_name}) ({pattern})'
    )
Esempio n. 3
0
File: math.py Progetto: mgroth0/mlib
def nandiv(a, v):
    r = arr()
    for i in itr(a):
        if isnan(a[i]):
            r = append(r, None)
        else:
            r = append(r, a[i] / v)
    return r
Esempio n. 4
0
File: math.py Progetto: mgroth0/mlib
def nanstd(lll):
    # l = list(filter(lambda x: not isnan(x),l))
    lll = arr(lll)
    lll = lll[np.invert(isnan(lll))]
    if len(lll) > 0:
        # tolist needed because dtype is obj sometimes for some reason ????
        lll = lll[np.invert(isinf(lll.tolist()))]
    rr = safestd(lll)
    return rr
Esempio n. 5
0
File: math.py Progetto: mgroth0/mlib
def slope(m, x, y):
    from scipy.stats import linregress
    # log('slope1')
    # try:
    not_nans = invert(isnan(y))
    # except Exception as e:
    if np.count_nonzero(not_nans) < 2:
        return None
    not_infs = invert(isinf(y))
    x = x[bitwise_and(not_nans, not_infs)]
    y = y[bitwise_and(not_nans, not_infs)]
    # from PyMat import matfun
    # coefs = matfun(m,'polyfit', x, y, 1)
    # coefs = arr(coefs)
    # log('slopeR')
    # return coefs[0,0]
    # noinspection PyUnresolvedReferences
    return linregress(x, y).slope
Esempio n. 6
0
 def int_if_not_none_or_str(e):
     if isnan(e) or isstr(e):
         return e
     else:
         return int(e)
Esempio n. 7
0
def jsonReprCP(o):
    import numpy as np
    # log('jsonReprCP of a ' + str(type(o)))
    if isinstsafe(o, np.ndarray):
        o = o.tolist()
    if not isitr(o) and isnan(o):
        return None
    if o == np.inf or o == -np.inf:
        return str(o)
    if isinstance(o, str) or isinstance(o, float) or isinstance(
            o, int) or o is None:
        return o
    if isinstance(o, np.int64):
        return int(o)
    if isinstance(o, np.float32):
        return float(o)

    if isitr(o) and not isinstance(o, str):
        cp = []
        for vv in o:
            cp.append(jsonReprCP(vv))
        return cp

    import copy
    cp = copy.deepcopy(o)
    # debug_on = False
    if hasattr(o, 'item_type') and o.item_type == 'violin':
        debug_on = True

        # import mdb; mdb.remote_breakpoint()
    for v in cp.__dict__:
        debug(f'V:{v}')
        # if debug_on:
        #     breakpoint()
        val = getattr(cp, v)
        if isitr(val) and not isinstance(val, str):
            if isinstance(val, np.ndarray):
                setattr(cp, v, jsonReprCP(val.tolist()))
            else:
                newval = []
                for vv in val:
                    newval.append(jsonReprCP(vv))
                setattr(cp, v, newval)
        if not isitr(val) and (val == np.inf or val == -np.inf):
            setattr(cp, v, str(val))
        if isinstance(val, np.int64):
            setattr(cp, v, int(val))
        import pandas
        if not isitr(val) and pandas.isnull(val):
            setattr(cp, v, None)
        if isinstance(val, JsonSerializable):

            setattr(cp, v, jsonReprCP(val).__dict__)
        if isitr(val) and not isempty(val) and isinstance(
                val[0], JsonSerializable):
            newval = []
            for i in itr(val):
                newval.append(jsonReprCP(val[i]).__dict__)
            setattr(cp, v, newval)
    # if debug_on:
    #     breakpoint()
    # if hasattr(o, 'item_type') and o.item_type == 'violin':
    #     breakpoint()
    # import mdb; mdb.remote_breakpoint()
    return cp
Esempio n. 8
0
    def table(cls, fd, force_wolf=False, debug=False):
        from mlib.wolf.wolf_lang import wlexpr
        from matplotlib import pyplot as plt  # 1 FULL SECOND IMPORT
        wolf = 'Wolf' in cls.__name__ or force_wolf
        if wolf:
            from mlib.wolf.wolf_figs import addHeaderLabels, LinePlotGrid, OneWayOfShowingARaster
        if cls == MPLFigsBackend:
            if BLACK_FIGS:
                cls.fig = plt.figure(figsize=(16, 12), facecolor='black')
            else:
                cls.fig = plt.figure(figsize=(16, 12))
            if BLACK_FIGS:
                cls.ax = cls.fig.add_subplot(111, facecolor='black')
            else:
                cls.ax = cls.fig.add_subplot(111)
            cls.ax.axis("off")
            cls.tabl = None
        data = fd.data
        backgrounds = None
        if fd.confuse:
            low = fd.confuse_min
            high = fd.confuse_max

            # scaleBits = []
            # for i in range(1, 21):
            #     scaleBits += [[[0, 0, i / 21]]]
            scaleBits = JET().tolist()

            show_nums = fd.show_nums
            from copy import deepcopy
            backgrounds = deepcopy(data)
            for rrr in itr(data):
                for c in itr(data[rrr]):
                    if BLACK_FIGS:
                        backgrounds[rrr][c] = cls.color(0, 0, 0)
                    else:
                        backgrounds[rrr][c] = cls.color(1, 1, 1)  # 256?
                    if isnan(data[rrr][c]):
                        if wolf:
                            data[rrr][c] = ''
                            backgrounds[rrr][c] = cls.none()
                        else:
                            data[rrr][c] = [0, 0, 0]
                            # if BLACK_FIGS:
                            backgrounds[rrr][c] = cls.color(0, 0, 0)
                            # else:
                            #     backgrounds[rrr][c] = cls.color(255, 255, 255)  # 256?
                    elif not isstr(data[rrr][c]):
                        dat = data[rrr][c]
                        if isnan(dat):  # wait! this dealt with above
                            b = None
                        elif high != 0:
                            if fd.y_log_scale:
                                b = ((dat**2) / (high**2))
                            else:
                                # try:
                                b = (dat / high)

                                if cls.debug:  # and data == 0:
                                    breakpoint()

                            # except:
                        else:
                            if fd.y_log_scale:
                                # b = np.log10(b)
                                b = dat**2
                            else:
                                b = dat

                        if show_nums:
                            if b is None:
                                data[rrr][c] = 'NaN'
                            else:
                                data[rrr][c] = sigfig(dat, 2)
                        else:
                            # data[rrr][c] = [0, 0, b]
                            # try:

                            if b is None:  # NaN
                                breakpoint()
                                data[rrr][c]
                            else:
                                # data[rrr][c] = JET[round(b * 256) - 1].tolist() # causes zeros to show wrong!
                                data[rrr][c] = JET()[round(b * 255)].tolist()
                                # except:

                        if (fd.headers_included and rrr > 0
                                and c > 0) or not fd.headers_included:
                            backgrounds[rrr][c] = cls.color(0, 0, b)
            block_len = fd.block_len
            if block_len is not None and fd.headers_included is False:
                divs = [[], []]
                for i in range(len(data)):
                    if i == 1 or i % block_len == 0:
                        divs[0] += [True]
                        divs[1] += [True]
                    else:
                        divs[0] += [False]
                        divs[1] += [False]
        if not fd.confuse or fd.headers_included:
            if cls == MPLFigsBackend and not fd.headers_included:
                # breakpoint()
                tbl = cls.ax.table(
                    cellText=data,
                    # rowLabels=rows,
                    # rowColours=colors,
                    # colLabels=columns,
                    fontsize=fd.fontsize,
                    loc='center')
                tbl.auto_set_font_size(False)
                tbl.set_fontsize(fd.fontsize)
                tbl.scale(1, (fd.fontsize + 10) / 10)
                # tbl.auto_set_column_width([0])
                tbl.auto_set_column_width(list(range(len(data[0]))))
            else:
                breakpoint()
                for ri, row in enumerate(data):
                    for ci, el in enumerate(row):
                        if cls == MPLFigsBackend:
                            if cls.tabl is None:
                                cls.tabl = cls.ax.table([[1]], loc='center')
                            width = 1 / len(data[0])
                            height = 1 / len(data)

                            if ri == 0:
                                height = 0.05
                            if ci == 0:
                                width = 0.1
                            cell = cls.tabl.add_cell(
                                ri,
                                ci,
                                width,
                                height,
                                loc="center",
                                text=str(el) if el != 'Non' else "",
                                # text="TEST_TEXT",
                                # facecolor='green'
                                **({
                                    'facecolor': backgrounds[ri][ci]
                                } if backgrounds is not None else {}))
                            cell.get_text().set_fontsize(20)
                            if len(data) > 5:
                                cell.get_text().set_fontsize(10)
                            if BLACK_FIGS:
                                cell.get_text().set_color('white')
                        else:
                            data[ri][ci] = cls.tableItem(
                                el, backgrounds[ri][ci])
            if fd.top_header_label is not None or fd.side_header_label is not None:
                if wolf:
                    data = addHeaderLabels(data, fd.top_header_label,
                                           fd.side_header_label).tolist()
                else:
                    cls.tabl.auto_set_font_size(False)
                    h = cls.tabl.get_celld()[(0, 0)].get_height()
                    w = cls.tabl.get_celld()[(0, 0)].get_width()
                    # Create an additional Header

                    # weird = "Header Header Header Header"
                    weird = fd.top_header_label * 4

                    weird = fd.top_header_label

                    header = [
                        cls.tabl.add_cell(
                            -1,
                            pos,
                            w,
                            h,
                            loc="center",  # fontsize=40.0 #facecolor="red",
                        ) for pos in range(1,
                                           len(data[0]) + 1)
                    ]
                    if len(header) > 2:
                        for idx, head in enum(header):
                            if idx == 0:
                                # head.visible_edges = "TBL"
                                head.visible_edges = ""
                            elif idx == len(header) - 1:
                                # head.visible_edges = "TBR"
                                head.visible_edges = ""
                            else:
                                # head.visible_edges = 'TB'
                                head.visible_edges = ""
                        header[1].get_text().set_text(weird)
                        header[1].set_fontsize(40.0)
                    elif len(header) == 2:
                        header[0].visible_edges = 'TBL'
                        header[1].visible_edges = 'TBR'
                        header[1].get_text().set_text(weird)
                    else:
                        header[0].visible_edges = 'TBLR'
                        header[0].get_text().set_text(weird)

                    # Create an additional Header
                    weird = fd.side_header_label * 4

                    weird = fd.side_header_label

                    header = [
                        cls.tabl.add_cell(pos,
                                          -1,
                                          w,
                                          h,
                                          loc="center",
                                          facecolor="none")
                        for pos in range(0,
                                         len(data) + 1)
                    ]
                    if len(header) > 2:
                        for idx, head in enum(header):
                            if idx == 0:
                                head.visible_edges = "LTR"
                                head.visible_edges = ""
                            elif idx == len(header) - 1:
                                head.visible_edges = "LRB"
                                head.visible_edges = ""
                            else:
                                head.visible_edges = 'LR'
                                head.visible_edges = ""
                        header[1].get_text().set_text(weird)
                        header[1].set_fontsize(40.0)
                        # header[1].set_rotation(90.0)
                    elif len(header) == 2:
                        header[0].visible_edges = 'TLR'
                        header[1].visible_edges = 'BLR'
                        header[1].get_text().set_text(weird)
                        header[1].set_fontsize(40.0)
                        # header[1].set_rotation(90.0)
                    else:
                        header[0].visible_edges = 'TBLR'
                        header[0].get_text().set_text(weird)
                        header[0].set_fontsize(40.0)
                        # header[0].set_rotation(90.0)

        if cls != MPLFigsBackend:
            insets = [
                Inset(
                    Rasterize(Grid(
                        data,
                        Dividers(False),
                    ), RasterSize(), ImageSize(), Background()))
            ]
        if fd.confuse and fd.block_len is not None and fd.block_len > 1:
            if fd.confuse_is_identical:
                for rrr in itr(data):
                    for c in itr(data[0]):
                        if c > rrr:
                            if BLACK_FIGS:
                                data[rrr][c] = [0, 0, 0]
                            else:
                                data[rrr][c] = [1, 1, 1]  # 256?

            if cls != MPLFigsBackend:
                scale = Graphics(
                    [
                        Raster(scaleBits),
                        Inset(Text(round(low), fontSize=30), [0.5, -1]),
                        Inset(Text(round(high), fontSize=30), [0.5, 21]),
                    ],
                    ImagePadding([[75, 75], [20, 20]]),
                )
            else:
                # create an axes on the right side of ax. The width of cax will be 5%
                # of ax and the padding between cax and ax will be fixed at 0.05 inch.
                from mpl_toolkits.axes_grid1 import make_axes_locatable  # 1 FULL SECOND IMPORT
                divider = make_axes_locatable(cls.ax)

                cax = divider.append_axes("right", size="5%", pad=0.05)

                # cmap = matplotlib.colors.Colormap('name', N=256)
                # cmap = LinearSegmentedColormap.from_list(
                #     'bluemap', li(scaleBits).reshape(20, 3), N=20)

                from matplotlib.colors import LinearSegmentedColormap  # 1 FULL SECOND IMPORT
                cmap = LinearSegmentedColormap.from_list(
                    'jet',
                    li(scaleBits)  # .reshape(20, 3)
                    ,
                    N=len(scaleBits)  # 20
                )
                import matplotlib
                sm = matplotlib.cm.ScalarMappable(norm=None, cmap=cmap)
                cbar = cls.fig.colorbar(
                    sm,
                    cax=cax,
                    orientation='vertical',
                    ticks=np.linspace(
                        # low,
                        0,
                        # high,
                        1,
                        num=4))

                base_scale_ticks = np.linspace(low, high, num=4)
                if fd.y_log_scale:
                    base_scale_ticks = [bst**2 for bst in base_scale_ticks]
                cbar.ax.set_yticklabels(
                    [sigfig(n, 3) for n in base_scale_ticks])

            gl = len(data)
            nt = len(fd.row_headers)
            line_values = np.linspace(fd.block_len, fd.block_len * nt, nt)
            half = fd.block_len / 2
            labellocs_labels = ziplist(line_values - half, fd.row_headers)

            # ticks start from the bottom but I want these to start from the top
            # labellocs_labels.reverse()
            if wolf:
                gridlines = LinePlotGrid(line_values,
                                         triangle=fd.confuse_is_identical)
            else:
                # gl = line_values[-1]
                listpoints = []
                for i in line_values:
                    i = i - 0.5

                    if fd.confuse_is_identical:
                        listpoints += [[[i, gl - 0.5], [i, i]]]

                        listpoints += [[[i, i], [-0.5, i]]]

                    else:
                        listpoints += [[[i, -0.5], [i, gl - 0.5]]]
                        listpoints += [[[-0.5, i], [gl - 0.5, i]]]
                listpoints = arr(listpoints)
                for sub in listpoints:
                    # cls.ax.line(sub[:, 0], sub[:, 1], 'y--')
                    # cls.ax.plot(sub[:, 0], sub[:, 1], 'y--')
                    cls.ax.plot(sub[:, 0], sub[:, 1], 'k--')
            # rasters start from the bottom but I want this to start from the top
            if wolf:
                rast = OneWayOfShowingARaster(Raster(reversed(data)), gl)
            else:
                cls.ax.imshow(list(data))

            x_ticks = []
            xt_mpl_t = []
            xt_mpl_l = []
            y_ticks = []
            yt_mpl_t = []
            yt_mpl_l = []
            for t in labellocs_labels:
                if wolf:
                    x_ticks += [
                        Text(
                            t[1],
                            coords=[t[0] / gl, -.02],
                            direction=[1, 0],
                            fontSize=DEFAULT_TICK_SIZE,
                            offset=[0, 0],
                        )
                    ]
                xt_mpl_t += [t[0] / gl]
                xt_mpl_l += [t[1]]

            for t in labellocs_labels:
                if wolf:
                    y_ticks += [
                        Text(
                            t[1],
                            # y ticks need to be reversed
                            coords=[-.01, 1 - (t[0] / gl)],
                            direction=[1, 0],
                            fontSize=DEFAULT_TICK_SIZE,
                            offset=[1, 0],
                        )
                    ]
                # y ticks not reversed for mpl?
                yt_mpl_t += [(t[0] / gl)]
                yt_mpl_l += [t[1]]
            if wolf:
                insets = [
                    Inset(obj=Rasterize(scale),
                          pos=[1.2, 0],
                          opos=[Center, Bottom]),
                    Inset(obj=Rasterize(
                        rast,
                        ImageResolution(),
                    ),
                          opos=[Left, Bottom]),
                    Inset(
                        obj=Rasterize(gridlines, ImageResolution(),
                                      Background(wlexpr('None'))),
                        opos=[Left, Bottom],
                        background=Background(
                            # wl.Red
                            wlexpr('None')))
                ]
                [insets.extend(ticks) for ticks in zip(x_ticks, y_ticks)]
                insets += [
                    Inset(Rasterize(
                        Text(fd.title,
                             fontSize=(40 if fd.headers_included else 20)
                             if fd.title_size is None else fd.title_size),
                        Background(wlexpr('None'))),
                          scale=(1, 1),
                          pos=Scaled([0.5, 2]),
                          background=Background(wlexpr('None')))
                ]
                rrr = Graphics(insets)
            else:
                title_obj = cls.ax.set_title(fd.title,
                                             fontsize=fd.title_size / 3)

                from matplotlib import pyplot as plt  # 1 FULL SECOND IMPORT
                plt.setp(title_obj, color=text_color)
                cls.ax.axis(True)
                cls.ax.spines['left'].set_color(text_color)
                cls.ax.spines['bottom'].set_color(text_color)
                cls.ax.xaxis.label.set_color(text_color)
                cls.ax.yaxis.label.set_color(text_color)
                cls.ax.tick_params(axis='x', colors=text_color)
                cls.ax.tick_params(axis='y', colors=text_color)
                cls.ax.set_xticks(arr(xt_mpl_t) * gl)
                cls.ax.set_xticklabels(xt_mpl_l, rotation=90)
                # cls.ax.xticks(rotation=90)
                cls.ax.set_yticks(arr(yt_mpl_t) * gl)
                cls.ax.set_yticklabels(yt_mpl_l)

                cax.axis(True)
                cax.spines['left'].set_color(text_color)
                cax.spines['bottom'].set_color(text_color)
                cax.spines['top'].set_color(text_color)
                cax.spines['right'].set_color(text_color)
                cax.xaxis.label.set_color(text_color)
                cax.yaxis.label.set_color(text_color)
                cax.tick_params(axis='x', colors=text_color)
                cax.tick_params(axis='y', colors=text_color)
                # cax.set_xticks(li(xt_mpl_t) * gl)
                # cax.set_xticklabels(xt_mpl_l, rotation=90)
                # cls.ax.xticks(rotation=90)
                # cax.set_yticks(li(yt_mpl_t) * gl)
                # cax.set_yticklabels(yt_mpl_l)
        if not wolf and (fd.confuse and fd.block_len is not None
                         and fd.block_len > 1) == False:
            title_obj = cls.ax.set_title(fd.title, fontsize=fd.title_size / 3)

            from matplotlib import pyplot as plt  # 1 FULL SECOND IMPORT
            plt.setp(title_obj, color=text_color)

        if wolf:
            return rrr