Esempio n. 1
0
def makefigs(
        root,
        fig_backend: str,
        overwrite=False,
        force=False,
        silent=True,
        filter=''
):
    def inTags(fd):
        tagFile = File(fd['dataFile']).resrepext(FigData.TAG_EXT)
        if not tagFile: return False
        return filter in [s.lower() for s in json.loads(
            tagFile.read()
        )]
    figDats = li([
        {
            'dataFile': mfig,
            'imgFile' : mfig.resrepext('png') if mfig.resrepext('png').exists else mfig.resrepext('svg')
        } for mfig in root.rglob('*.mfig')  # root.glob('**/*.mfig')
    ]).filter(lambda fd: filter in fd['dataFile'].name.lower() or inTags(fd))
    log(f'{len(figDats)=}')
    if fig_backend == 'wolfram':
        from mlib.wolf.wolf_figs import WolfMakeFigsBackend
        backend = WolfMakeFigsBackend
    else:
        backend = MPLFigsBackend
    figDats = [obj(fd) for fd in figDats if overwrite or not fd['imgFile'].exists]
    backend.makeAllPlots(figDats, overwrite, force, silent=silent)
Esempio n. 2
0
File: file.py Progetto: mgroth0/mlib
 def paths(self):
     if not self.isdir:
         log(f'{self}:{self.isdir=}')
         err('not dir!')
     # assert self.isdir
     a = li([self.join(name) for name in sort(os.listdir(self.abspath))])
     if self.DELETE_DS_STORE:
         Folder(self)['.DS_Store'].deleteIfExists(silent=True)
     if self.IGNORE_DS_STORE:
         a = a.filtered(lambda n: File(n).name != '.DS_Store')
     return a
Esempio n. 3
0
File: file.py Progetto: mgroth0/mlib
 def files_recursive(self,
                     include_self=False,
                     include_stupid_icon=False) -> Li:
     r = []
     for f in self.files_safe:
         if not include_stupid_icon:
             if f.name == 'Icon\r':
                 continue
         r += [f]
         if f.isdir:
             r += f.files_recursive(include_self=False)
     if include_self:
         r += [self]
     return li(r)
Esempio n. 4
0
File: lang.py Progetto: mgroth0/mlib
def listvalues(d):
    from mlib.boot.stream import li
    return li(d.values())
Esempio n. 5
0
File: lang.py Progetto: mgroth0/mlib
def listkeys(d):
    from mlib.boot.stream import li
    return li(d.keys())
Esempio n. 6
0
 def vals(self) -> li:
     return li(listvalues(self))
Esempio n. 7
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
Esempio n. 8
0
File: file.py Progetto: mgroth0/mlib
 def files_recursive_or_just_self_if_not_dir(self) -> Li:
     if self.isdir:
         return self.files_recursive(include_self=True)
     else:
         return li([self])
Esempio n. 9
0
File: file.py Progetto: mgroth0/mlib
 def glob(self, *g):
     from glob import glob
     return li([[File(m) for m in glob(self.join(gg))] for gg in g]).flat()