Пример #1
0
 def __get_byte_png(self, pl, dpi=50):
     BIOstream = BIO()
     pl.tight_layout()
     pl.savefig(BIOstream, format='png', dpi=dpi, bbox_inches='tight')
     BIOstream.seek(0)
     im = Image.open(BIOstream)
     im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)
     im2 = im2.resize(im2.size, Image.ANTIALIAS)
     BIOstream = BIO()
     im2.save(BIOstream, format='PNG', quality=95)
     BIOstream.seek(0)
     byte_png = BIOstream.getvalue()
     pl.close()
     return byte_png
Пример #2
0
def Q(delay, w, h, x, y, tidx):

    assert 0 <= tidx <= 255
    assert 0 <= delay < 2**16

    indices = [tidx] * (w * h)
    buf = BIO('')

    buf.write('\x21\xF9\x04\x05')
    buf.write(pk('H', delay))
    buf.write(pk('B', tidx))
    buf.write('\x00')

    buf.write('\x2c')
    buf.write(pk('H', x))
    buf.write(pk('H', y))
    buf.write(pk('H', w))
    buf.write(pk('H', h))
    buf.write('\x00')

    LZWMinimumCodeSize = 8

    cmprs, _ = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

    obuf = pk('B', LZWMinimumCodeSize) + WB(cmprs)

    buf.write(obuf)
    buf.seek(0)
    return buf.read()
Пример #3
0
    def get_plot_as_bytestring(self, list_of_datamatrix_idicies, *args, **kwargs):
        list_of_datamatrix_idicies = self.__single_int_to_list(list_of_datamatrix_idicies)
        dpi = 50
        tight = True
        black_models = False

        if 'dpi' in kwargs:
            try:
                dpi = int(kwargs['dpi'])
            except:
                pass
        if 'tight' in kwargs:
            try:
                tight = bool(kwargs['tight'])
            except:
                pass
        if 'black_models' in kwargs:
            try:
                black_models = bool(kwargs['black_models'])
            except:
                pass

        try:
            return BIO(self.plot(list_of_datamatrix_idicies, get_bytes=True, dpi=dpi, tight=tight, black_models=black_models))
        except Exception as e:
            raise Exception(f'Could not generate plot:\n\t{str(e)}')
Пример #4
0
def write_character_blocks(delay, w, h, x, y, transparent_color_index):

    assert 0 <= transparent_color_index <= 255
    assert 0 <= delay < 2**16

    indices = [transparent_color_index] * (w * h)
    buf = BIO('')

    # write the graphics control block
    buf.write('\x21\xF9\x04\x05')
    buf.write(pk('H', delay))
    buf.write(pk('B', transparent_color_index))
    buf.write('\x00')  # Terminator

    # after it, write a local image descriptor block
    buf.write('\x2c')
    buf.write(pk('H', x))
    buf.write(pk('H', y))
    buf.write(pk('H', w))
    buf.write(pk('H', h))
    buf.write('\x00')

    LZWMinimumCodeSize = 8

    # Compress the
    cmprs, _ = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

    obuf = pk('B', LZWMinimumCodeSize) + create_subblocks_with_buffer(cmprs)

    buf.write(obuf)
    buf.seek(0)
    return buf.read()
Пример #5
0
    def _fetch_apps(self, target, apps_shortlist=None, force=False):
        fetched_apps = []
        for app_name, app_uri in target.apps():
            if apps_shortlist and app_name not in apps_shortlist:
                logger.info('{} is not in the shortlist, skipping it'.format(app_name))
                continue

            uri = DockerRegistryClient.parse_image_uri(app_uri)
            app_dir = os.path.join(self.apps_dir(target.name), app_name, uri.hash)
            if os.path.exists(app_dir) and not force:
                logger.info('App has been already fetched; Target: {}, App: {}'.format(target.name, app_name))
                continue

            os.makedirs(app_dir, exist_ok=True)
            manifest_data = self._registry_client.pull_manifest(uri)
            with open(os.path.join(app_dir, self.ManifestFile), 'wb') as f:
                f.write(manifest_data)

            manifest = json.loads(manifest_data)
            app_blob_digest = manifest["layers"][0]["digest"]
            app_blob_hash = app_blob_digest[len('sha256:'):]
            app_blob = self._registry_client.pull_layer(uri, app_blob_digest)
            app_blob_file = os.path.join(app_dir, app_blob_hash + self.ArchiveFileExt)
            with open(app_blob_file, 'wb') as f:
                f.write(app_blob)

            with tarfile.open(fileobj=BIO(app_blob)) as t:
                t.extract('docker-compose.yml', app_dir)

            fetched_apps.append(ComposeApps.App(app_name, app_dir))
        return fetched_apps
Пример #6
0
 def __get_byte_png(self, pl, dpi=50, tight=True):
     global is_savefig
     BIOstream = BIO()
     is_savefig = True
     pl.savefig(BIOstream, format='png', dpi=dpi, bbox_inches='tight')
     pl.close('all')
     is_savefig = False
     BIOstream.seek(0)
     im = Image.open(BIOstream)
     im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)
     im2 = im2.resize(im2.size, Image.ANTIALIAS)
     BIOstream = BIO()
     im2.save(BIOstream, format='PNG', quality=95)
     BIOstream.seek(0)
     byte_png = BIOstream.getvalue()
     pl.close('all')
     return byte_png
Пример #7
0
 def download_compose_app(self, app_uri, dest_dir, extract=True):
     app_layers = self.download_layers(app_uri, self.download_manifest(app_uri))
     compose_app_archive = app_layers[0]
     if extract:
         tar = tarfile.open(fileobj=BIO(compose_app_archive))
         tar.extractall(dest_dir)
     else:
         return compose_app_archive
Пример #8
0
def hide_flag(input_file, flag, output_file):
    global_colors, bgcoloridx, size_count, image_height, image_width = parse_gif_header(
        input_file)
    flag_unique_chars, flag_indexed_repr = get_hiding_values(flag)
    hdr_end = input_file.tell()
    input_file.seek(0)
    output_file.write(input_file.read(hdr_end))
    fc = 0

    # Add a comment block
    output_file.write('\x21\xFE')
    output_file.write(create_subblocks_with_buffer('RDBNB' +
                                                   flag_unique_chars))
    output_file.flush()

    for block_type, block_buffer in yield_blocks(input_file):
        print('.', end='')
        if block_type == BlockType.CommentBlock:
            continue
        if block_type == BlockType.GraphicsControlExtension:
            if flag_indexed_repr:
                delay = up('<H', block_buffer[4:6])
                assert delay >= 6
                # Slow the delay
                block_buffer = block_buffer[:4] + pk(
                    '<H', delay - 3) + block_buffer[6:]
            output_buffer = block_buffer

        elif block_type == BlockType.ImageDescriptor:
            fc += 1
            total_raw_blocks_data = ''
            block_bio = BIO(block_buffer)
            image_descriptor_header = block_bio.read(10)

            LZWMinimumCodeSize = ord(block_bio.read(1))
            total_raw_blocks_data = read_subblocks_without_length(block_bio)

            indices, _ = lzwlib.Lzwg.decompress(total_raw_blocks_data,
                                                LZWMinimumCodeSize)

            xxx = unpack('<B H H H H B', image_descriptor_header)

            cmprs, codes = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

            if flag_indexed_repr:
                char_index, char_is_upper = flag_indexed_repr.pop(0)
                output_buffer += encode_char(char_index, char_is_upper,
                                             image_width, image_height,
                                             len(global_colors) - 1)
        else:
            output_buffer = block_buffer

        output_file.write(output_buffer)
    output_file.flush()
    assert not flag_indexed_repr, ''

    return 0
Пример #9
0
def Q(delay, w, h, x, y, tidx):

    assert 0 <= tidx <= 255
    assert 0 <= delay < 2**16

    # this is used for the compression
    # multiplies tidx by (w*h) times
    # so eventually the size of list is w*h and each cell is tidx
    indices = [tidx] * (w * h)
    buf = BIO('')

    # 0x21 0xf9  = intro & label
    # 0x04 = block size
    # 0x05 = 0000 0101
    # bit 0 is reserved (???)
    # bit 2 is is also reserved (?????)
    buf.write('\x21\xF9\x04\x05')

    # write delay as unsigned short 2 bytes
    buf.write(pk('H', delay))
    # write index as unsigned char 1 byte
    buf.write(pk('B', tidx))
    # terminate block
    buf.write('\x00')

    # new image block
    buf.write('\x2c')
    # left pos
    buf.write(pk('H', x))
    # top pos
    buf.write(pk('H', y))
    # width & height
    buf.write(pk('H', w))
    buf.write(pk('H', h))
    # flag  section & local color table = 0
    buf.write('\x00')

    # min code size equals 8
    LZWMinimumCodeSize = 8

    # compression
    cmprs, _ = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

    # write min code size and the compressed blocks
    obuf = pk('B', LZWMinimumCodeSize) + WB(cmprs)

    # write obuf to buf
    buf.write(obuf)
    buf.seek(0)

    # read entire buffer
    return buf.read()
Пример #10
0
def E(f, s, o):
    global_colors, bgcoloridx, size_count, hh, ww = F(f)
    mp, ks = M(s)
    hdr_end = f.tell()
    f.seek(0)
    o.write(f.read(hdr_end))
    fc = 0

    o.write('\x21\xFE')
    o.write(WB('RDBNB' + mp))
    o.flush()

    for t, buf in C(f):
        print('.', end='')
        if t == T.EC:
            continue
        if t == T.EG:
            if ks:
                delay = up('<H', buf[4:6])
                assert delay >= 6
                buf = buf[:4] + pk('<H', delay - 3) + buf[6:]
            obuf = buf

        elif t == T.I:
            fc += 1
            total_raw_blocks_data = ''
            bf = BIO(buf)
            pref = bf.read(10)

            LZWMinimumCodeSize = ord(bf.read(1))
            total_raw_blocks_data = k(bf)

            indices, dcmprsdcodes = lzwlib.Lzwg.decompress(
                total_raw_blocks_data, LZWMinimumCodeSize)
            xxx = unpack('<B H H H H B', pref)

            cmprs, codes = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

            obuf = pref + pk('B', LZWMinimumCodeSize) + WB(cmprs)

            if ks:
                mpindx, isup = ks.pop(0)
                obuf += h(mpindx, isup, ww, hh, len(global_colors) - 1)
        else:
            obuf = buf

        o.write(obuf)
    o.flush()
    assert not ks, ''

    return 0
Пример #11
0
def E(f, s, o):
    # Headers of file
    global_colors, bgcoloridx, size_count, hh, ww = F(f)

    # (mutated) Flag (??)
    mp, ks = M(s)

    # By here, current file cursor should be at the end of the header
    hdr_end = f.tell()

    # Go back to file beginning
    f.seek(0)
    # Write header string to output
    o.write(f.read(hdr_end))

    fc = 0

    # write these 2 bytes.
    # 0x21 is extension into
    # 0xfe is COMMENT LABEL
    o.write('\x21\xFE')

    # writes the mapped flags with RDBNB prepended after mutating iwth WB
    o.write(WB('RDBNB' + mp))
    o.flush()

    # iterate on the generator C returns
    for t, buf in C(f):
        print('.', end='')
        # if comment
        # IGNORES COMMENT SECTION TO OUTPUT.
        if t == T.EC:
            continue

        # if graphic control label
        if t == T.EG:
            # if havent finished encoding the flag into file, mutate in the following way:
            if ks:
                # reads 2 bytes from buffer ([4,5]) and parses as H - unsigned short
                delay = up('<H', buf[4:6])
                # all delays of gif were over 6
                assert delay >= 6
                # changes the delay. decrease by 3
                buf = buf[:4] + pk('<H', delay - 3) + buf[6:]
            obuf = buf

        # else if image sep
        elif t == T.I:
            fc += 1
            total_raw_blocks_data = ''
            bf = BIO(buf)
            pref = bf.read(10)

            # reads the code size but parses it as char
            LZWMinimumCodeSize = ord(bf.read(1))
            # read block data
            total_raw_blocks_data = k(bf)

            indices, dcmprsdcodes = lzwlib.Lzwg.decompress(
                total_raw_blocks_data, LZWMinimumCodeSize)

            # < = little endian
            # B = unsigned char
            # H = unsigned short
            xxx = unpack('<B H H H H B', pref)

            cmprs, codes = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

            obuf = pref + pk('B', LZWMinimumCodeSize) + WB(cmprs)

            # pop ks here - only on image section
            if ks:
                mpindx, isup = ks.pop(0)
                obuf += h(mpindx, isup, ww, hh, len(global_colors) - 1)
        else:
            obuf = buf

        # write to output
        o.write(obuf)
    o.flush()

    # eventually, ks should be false
    assert not ks, ''

    return 0
Пример #12
0
 def __init__(self, buf=None, raw=None, bits=None):
     self.bytes = buf or BIO(raw)
     self.bits = bits or []
Пример #13
0
    def writexlsx(self, filename, **kwargs):
        xlsx = xlsx_out(filename)
        xlsx.sheet_name = kwargs.get("sheet_name")
        xlsx.add_header(kwargs.get("header_list"))
        xlsx.set_col_widths(kwargs.get("col_width_list"))
        xlsx.set_row_heights(kwargs.get("row_heights"))

        if 'Yscale' in kwargs and kwargs['Yscale'].lower().strip() == 'common':
            ymax = 0
            ymin = 0
            for i in range(1, self.inst.data.matrix.length() + 1):
                y_vec = self.inst.data.matrix.buffer(i).data.y.get()
                ymax = np.max(y_vec) if ymax < max(y_vec) else ymax
                ymin = np.min(y_vec) if ymin > min(y_vec) else ymin
            for i in range(1, self.inst.data.matrix.length() + 1):
                self.inst.data.matrix.buffer(i).plot.axis.y.range.set(
                    [ymin * 1.05, ymax * 1.05])

        if 'color' in kwargs:
            c = kwargs['color']
            for i in range(1, self.inst.data.matrix.length() + 1):
                self.inst.data.matrix.buffer(i).plot.series.color.set(c)
                self.inst.data.matrix.buffer(i).model.color.set('k')

        plot_out = plot.plotter(self.data)

        fitter = fitfxns.datafit(self.inst)
        fitter.funcindex = self.data.matrix.buffer(1).fit.function_index.get()
        fitter.applyfxns()

        if not xlsx.header:
            auto_header = ['Sample']
            for val in fitter.paramid:
                auto_header.extend([val, str(val + '_error')])
            auto_header.extend(['xsq', 'rsq', 'plot'])
            xlsx.add_header(auto_header)

        if not xlsx.row_heights:
            xlsx.set_row_heights(152)

        if not xlsx.col_widths:
            xlsx.set_col_widths([20] * len(xlsx.header))

        if not xlsx.sheet_name:
            xlsx.sheet_name = "Output"

        for i in range(1, self.data.matrix.length() + 1):
            xl_line = []
            buffer = self.data.matrix.buffer(i)
            xl_line.extend([buffer.plot.series.name.get()])
            for j in range(len(buffer.fit.parameter.get())):
                xl_line.extend([buffer.fit.parameter.get()[j]])
                xl_line.extend([buffer.fit.parameter_error.get()[j]])
            xl_line.append(buffer.fit.chisq.get())
            xl_line.append(buffer.fit.rsq.get())
            xl_line.append(BIO(plot_out([i], get_bytes=True)))
            xlsx.add_line(xl_line)
        xlsx.write_xlsx()

        for i in range(1, self.data.matrix.length() + 1):
            xlsx = xlsx_out(filename[:-5] + f"_sensor{i}_raw.xlsx")
            xlsx.sheet_name = "Output"
            xlsx.add_line(
                [buffer.plot.series.name.get(), 'X', 'Y', 'X-breaks'])  #header
            buffer = self.data.matrix.buffer(i)
            tbufx = buffer.data.x.get()
            tbufy = buffer.data.y.get()
            tbreakx = buffer.plot.axis.x.lines.get()
            for j in range(len(tbufx)):
                if j < len(tbreakx):
                    xlsx.add_line(['', tbufx[j], tbufy[j], tbreakx[j]])
                else:
                    xlsx.add_line(['', tbufx[j], tbufy[j]])
            xlsx.write_xlsx()
        return
Пример #14
0
def link_to_csv(link, fname):
    zf = ZF(BIO((requests.get(link)).content))
    return pd.read_excel(BIO(zf.read(fname)))
Пример #15
0
def saveplot(exp,
             outfile,
             stackdepth=0,
             returnBytesIO=False,
             associationstep=1,
             dissociationstep=2,
             *args,
             **kwargs):
    offrange = None
    onrange = None
    ylabel = "Response (nm)"
    xlabel = "Time (sec)"
    if 'off_xrange' in kwargs.keys() and kwargs['off_xrange'] is not None:
        offrange = kwargs['off_xrange']
    if 'on_xrange' in kwargs.keys() and kwargs['on_xrange'] is not None:
        onrange = kwargs['on_xrange']
    if 'xlabel' in kwargs.keys():
        xlabel = kwargs['xlabel']
    if 'ylabel' in kwargs.keys():
        ylabel = kwargs['ylabel']
    filename = ''
    if exp == []:
        return False
    if not returnBytesIO:
        fileext = os.path.splitext(outfile)[1]
        outpath, outfile = os.path.split(os.path.abspath(outfile))
        if fileext == '':
            fileext = '.png'
            outpath = os.path.join(outpath, outfile)
            outfile = 'plot' + fileext
        if not os.path.exists(outpath):
            try:
                os.mkdir(outpath)
            except OSError:
                return False
        outfile = outfile[:len(outfile) - len(fileext)]
        filename = os.path.join(outpath, outfile + fileext)
    plot = pl.figure()
    ax = plot.add_subplot(1, 1, 1)
    pl.ylabel(ylabel, size=20)
    pl.xlabel(xlabel, size=20)
    pl.grid()
    pl.gcf().subplots_adjust(bottom=0.15)
    pl.gcf().subplots_adjust(left=0.15)
    pl.xticks(
        np.arange(min(exp['x_data'][associationstep]),
                  max(exp['x_data'][dissociationstep]), 100))
    pl.tick_params(axis='both', which='major', labelsize=20)
    pl.xlim(exp['x_data'][associationstep][0],
            exp['x_data'][dissociationstep][-1])
    pl.plot(np.array(exp['x_data'][associationstep]),
            np.array(exp['y_data'][associationstep]),
            linewidth=2,
            color="blue")
    pl.plot(np.array(exp['x_data'][dissociationstep]),
            np.array(exp['y_data'][dissociationstep]),
            linewidth=2,
            color="blue")
    if stackdepth >= 1:
        data_x1 = np.ma.array(exp['x_data'][associationstep])
        data_x2 = np.ma.array(exp['x_data'][dissociationstep])
        data_y1 = analytical1to1modelon(
            np.ma.array(exp['x_data'][associationstep]), exp['fit_param'])
        data_y2 = analytical1to1modeloff(
            np.ma.array(exp['x_data'][dissociationstep]), exp['fit_param'])
        if onrange is not None and len(offrange) == 2:
            m = np.ma.masked_outside(data_x1, onrange[0], onrange[1])
            data_x1 = data_x1[~m.mask]
            data_y1 = data_y1[~m.mask]
        pl.plot(data_x1, data_y1, linewidth=2, color="red")
        if offrange is not None and len(offrange) == 2:
            m = np.ma.masked_outside(data_x2, offrange[0], offrange[1])
            data_x2 = data_x2[~m.mask]
            data_y2 = data_y2[~m.mask]
        pl.plot(data_x2, data_y2, linewidth=2, color="red")
    if not returnBytesIO:
        pl.savefig(filename)
        pl.close()
        return
    else:
        BIOstream = BIO()
        pl.savefig(BIOstream, format='png')
        BIOstream.seek(0)
        byte_png = BIOstream.getvalue()
        pl.close()
        return byte_png

    return False
Пример #16
0
def decrypt(encryptedFile, decodedFile):
    # Headers of file
    global_colors, bgcoloridx, size_count, hh, ww = readHeader(encryptedFile)

    # By here, current file cursor should be at the end of the header
    hdr_end = encryptedFile.tell()

    # Go back to file beginning
    encryptedFile.seek(0)
    # Write header string to output
    decodedFile.write(encryptedFile.read(hdr_end))

    # Reaching here means the header section of decoded file is done
    # Moving on ...

    # The first block should be a comment section which includes the secret flag (mutated)
    # should look like:
    # 0x21 extension introducer
    # 0xfe comment label
    # [{
    #   blockSize, RDBNB+flag
    # }]
    # 0x00

    readFirstComment(encryptedFile)

    encryptedFlag = 'OE7AUKL}_GY#0FR{!HMTWS'
    originalFlag = ''

    print('Finished reading first block (comment)')
    print 'The flag is: {0}'.format(encryptedFlag)
    print 'Flag size: {0}'.format(len(encryptedFlag))
    print 'Now pointing to the next section...'

    firstPotential = 0
    secondPotential = 0
    encodedFlag = []

    # After this, there should be no comment sections at all, becuase the encoder doesnt write any comments.

    gen = readBlocks(f)
    # Iterate on sections using the generator
    for t, buf in gen:
        if t == BlockType.GRAPHIC_CONTROL:
            # Get delay
            delay = up('<H', buf[4:6])
            # Flags
            flags = up('<B', buf[3:4])
            # Trans Color Idx
            transColIdx = up('<B', buf[6:7])

            if flags == 5 and delay == 3 and (transColIdx >= 0
                                              and transColIdx <= 63):
                secondPotential += 1
                encodedFlag.append(
                    (BlockType.GRAPHIC_CONTROL, secondPotential, {
                        'delay': delay,
                        'flags': flags,
                        'transColIdx': transColIdx
                    }))

            obuf = buf

        elif t == BlockType.IMAGE:
            total_raw_blocks_data = ''
            # re-read buffer
            bf = BIO(buf)

            # skip first 10 bytes to get to lzw size
            pref = bf.read(10)

            # reads the code size but parses it as char
            LZWMinimumCodeSize = ord(bf.read(1))

            # Read all blocks of image data
            total_raw_blocks_data = readImageBlock(bf)

            # Decompress the data (compressed by lzw)
            indices, dcmprsdcodes = lzwlib.Lzwg.decompress(
                total_raw_blocks_data, LZWMinimumCodeSize)

            # < = little endian
            # B = unsigned char = 1 byte
            # H = unsigned short = 2 bytes
            sep, leftp, topp, width, height, flags = unpack(
                '<B H H H H B', pref)

            if (LZWMinimumCodeSize == 8 and flags == 0):
                firstPotential += 1
                encodedFlag.append((BlockType.IMAGE, firstPotential, {
                    'leftp': leftp,
                    'topp': topp,
                    'width': width,
                    'height': height,
                    'flags': flags,
                    'minCodeSize': LZWMinimumCodeSize,
                    'indices': indices,
                    'dcmprsdcodes': dcmprsdcodes
                }))

            cmprs, codes = lzwlib.Lzwg.compress(indices, LZWMinimumCodeSize)

            obuf = pref + pk('B', LZWMinimumCodeSize) + writeBlock(cmprs)

            # until flag has been encoded, 2 more blocks are added:
            # 1. Graphic Control Extension Block withe the following:
            # 1.1. block size = 4 (constant ?)
            # 1.2. flags = 0000 0101 = 5 = bit 0 & bit 2
            # 1.3. delay = 3
            # 1.4. transp color idx = if even then lowercase, else uppercase
            # 2. Image Block
            # 2.1. left pos, top pos, width, height = nothing useful now
            # 2.2. flags = 0
            # 2.3. Local color table = 0
            # 2.4. lzw min code size = 8
            # 2.5. blocks = array of length (width*height) containing only [tidx (from 1.4)]

        # if not graphic control or image, just write without mutating
        else:
            obuf = buf

        o.write(obuf)
    o.flush()

    i = 0

    while i < len(encodedFlag):
        grphic = encodedFlag[i]
        img = encodedFlag[i + 1]
        i += 2

        leftp = img[2]['leftp']
        topp = img[2]['topp']
        width = img[2]['width']
        height = img[2]['height']

        letterIdxInEnc = firstDecFunc(leftp, topp, width, height)

        isLower = grphic[2]['transColIdx'] % 2 == 0
        originalFlag += encryptedFlag[
            letterIdxInEnc] if not isLower else encryptedFlag[
                letterIdxInEnc].lower()

    print originalFlag
    return 0
Пример #17
0
def xlsxmodelreport(experiment, outfile, *args, **kwargs):
    offrange = None
    onrange = None
    figures = False
    xlsout = False
    rsq = "full"
    if 'off_xrange' in kwargs.keys():
        offrange = kwargs['off_xrange']
    if 'on_xrange' in kwargs.keys():
        onrange = kwargs['on_xrange']
    if 'r_sq' in kwargs.keys():
        rsq = kwargs['r_sq']
    if 'figures' in kwargs.keys():
        figures = kwargs['figures']

    outpath, outfile, fileext = filewritecheck(experiment, outfile)
    if not outpath:
        return
    if fileext == '.xls':
        fileext = '.xlsx'
        xlsout = True
    elif fileext == '.xlsx':
        xlsout = False
    filename = os.path.join(outpath, outfile + fileext)
    wb = XL.Workbook(filename)
    ws = wb.add_worksheet("Result Table")
    ws.set_column(33, 33, 32)
    ws.set_row(0, 30)
    ws.write(0, 0, 'Comments')
    ws.write(0, 1, 'Flags')
    ws.write(0, 2, 'Index')
    ws.write(0, 3, 'Color')
    ws.write(0, 4, 'Sensor Location')
    ws.write(0, 5, 'Sensor Type')
    ws.write(0, 6, 'Sensor Info')
    ws.write(0, 7, 'Replicate Group')
    ws.write(0, 8, 'Baseline Loc.')
    ws.write(0, 9, 'Assoc. (Sample) Loc.')
    ws.write(0, 10, 'Sample ID')
    ws.write(0, 11, 'Dissoc. Loc.')
    ws.write(0, 12, 'Loading Well Location')
    ws.write(0, 13, 'Loading Sample ID')
    ws.write(0, 14, 'Cycle')
    ws.write(0, 15, 'Conc. (nM)')
    ws.write(0, 16, 'Response')
    ws.write(0, 17, 'KD (M)')
    ws.write(0, 18, 'KD Error')
    ws.write(0, 19, 'kon(1/Ms)')
    ws.write(0, 20, 'kon Error')
    ws.write(0, 21, 'kdis(1/s)')
    ws.write(0, 22, 'kdis Error')
    ws.write(0, 23, 'Rmax')
    ws.write(0, 24, 'Rmax Error')
    ws.write(0, 25, 'kobs(1/s)')
    ws.write(0, 26, 'Req')
    ws.write(0, 27, 'Req/Rmax(%)')
    if rsq == "on":
        ws.write(0, 28, 'Assoc X^2')
        ws.write(0, 29, 'Assoc R^2')
    elif rsq == "off":
        ws.write(0, 28, 'Dissoc X^2')
        ws.write(0, 29, 'Dissoc R^2')
    else:
        ws.write(0, 28, 'Full X^2')
        ws.write(0, 29, 'Full R^2')
    ws.write(0, 30, 'SSG KD')
    ws.write(0, 31, 'SSG Rmax')
    ws.write(0, 32, 'SSG R^2')

    ws.freeze_panes(1, 0)
    skipped = 0
    for i in range(len(experiment)):
        if experiment[i]['sensor_info'] is None:
            skipped = skipped + 1
            continue
        if str(experiment[i]['step_status'])[0].upper() == "ERROR":
            ws.write(i + 1, 1, "Sensor Error")
            continue
        ws.write(i - skipped + 1, 0, str(experiment[i]['comments']))
        ws.write(i - skipped + 1, 1, str(experiment[i]['flags']))
        ws.write(i - skipped + 1, 2, int(i - skipped))
        ws.write(i - skipped + 1, 3, str('0'))
        ws.write(i - skipped + 1, 4, str(experiment[i]['sensor']))
        ws.write(i - skipped + 1, 5, str(experiment[i]['sensor_type']))
        ws.write(i - skipped + 1, 6, str(experiment[i]['sensor_info']))
        ws.write(i - skipped + 1, 7, str(experiment[i]['sample_group'][1]))
        for w in range(len(experiment[i]['step_type'])):
            if experiment[i]['step_type'][w] == 'BASELINE':
                ws.write(i - skipped + 1, 8, str(experiment[i]['step_loc'][w]))
            elif experiment[i]['step_type'][w] == 'ASSOC':
                ws.write(i - skipped + 1, 9, str(experiment[i]['step_loc'][w]))
                rawy = np.array(np.array(experiment[i]['y_data'][w]))
            elif experiment[i]['step_type'][w] == 'DISASSOC':
                ws.write(i - skipped + 1, 11,
                         str(experiment[i]['step_loc'][w]))
                rawyoff = np.array(np.array(experiment[i]['y_data'][w]))
            elif experiment[i]['step_type'][w] == 'LOADING':
                ws.write(i - skipped + 1, 12,
                         str(experiment[i]['step_loc'][w]))
                ws.write(i - skipped + 1, 13,
                         str(experiment[i]['sampleid'][w]))

        if rsq == "full" or "on":
            rawyon = rawy
            modelon = datafitting.analytical1to1modelon(
                np.array(experiment[i]['x_data'][1]),
                experiment[i]['fit_param'])
            rsqtempon = np.sum(np.power(modelon - rawyon, 2))
            rawyon = np.sum(np.power(rawyon - np.average(rawyon), 2))
            if rsq == "on":
                rsqreport = rsqtempon
                rawyreport = rawyon
        if rsq == "full" or "off":
            modeloff = datafitting.analytical1to1modeloff(
                np.array(experiment[i]['x_data'][2]),
                experiment[i]['fit_param'])
            rsqtempoff = np.sum(np.power(modeloff - rawyoff, 2))
            rawyoff = np.sum(np.power(rawyoff - np.average(rawyoff), 2))
            if rsq == "off":
                rsqreport = rsqtempoff
                rawyreport = rawyoff
            else:
                rsqreport = (rsqtempoff + rsqtempon) / 2
                rawyreport = (rawyoff + rawyon) / 2

        ws.write(i - skipped + 1, 10, experiment[i]['sampleid'][1])
        ws.write(
            i - skipped + 1, 14,
            str(experiment[i]['fit_param'][4]) + " " +
            str(experiment[i]['fit_param'][5]) + " " +
            str(experiment[i]['fit_param'][7]))
        ws.write(i - skipped + 1, 15,
                 float(experiment[i]['molarconcentration'][1]))
        ws.write(i - skipped + 1, 16, float(np.max(rawy) - np.min(rawy)))
        #Rmax, kd, ka, Cpro, m, c, X0, kds
        eqkdtemp = float(experiment[i]['fit_param'][1]) / float(
            experiment[i]['fit_param'][2])
        ctemp = float(experiment[i]['fit_param'][3])
        rmtemp = float(experiment[i]['fit_param'][0])
        reqtemp = (ctemp / (ctemp + eqkdtemp) * rmtemp)
        ws.write(i - skipped + 1, 17, eqkdtemp)
        ws.write(i - skipped + 1, 18,
                 ((float(experiment[i]['param_error'][1]) /
                   float(experiment[i]['fit_param'][1]))**2 +
                  (float(experiment[i]['param_error'][2]) /
                   float(experiment[i]['fit_param'][2]))**2))**0.5
        ws.write(i - skipped + 1, 19, float(experiment[i]['fit_param'][2]))
        ws.write(i - skipped + 1, 20, float(experiment[i]['param_error'][2]))
        ws.write(i - skipped + 1, 21, float(experiment[i]['fit_param'][1]))
        ws.write(i - skipped + 1, 22, float(experiment[i]['param_error'][1]))
        ws.write(i - skipped + 1, 23, rmtemp)
        #ws.write(i - skipped + 1, 23, (float(experiment[i]['fit_param'][2]) *
        #           (float(experiment[i]['molarconcentration'][1]) / 1E9)) + float(experiment[i]['fit_param'][1]))
        ws.write(
            i - skipped + 1, 24,
            np.sqrt(
                float(experiment[i]['param_error'][2]) *
                (float(experiment[i]['molarconcentration'][1]) / 1E9)**2 +
                float(experiment[i]['param_error'][1])**2))
        ws.write(i - skipped + 1, 25, reqtemp)
        ws.write(i - skipped + 1, 26,
                 (np.sqrt((float(experiment[i]['param_error'][1]) /
                           float(experiment[i]['fit_param'][1]))**2 +
                          (float(experiment[i]['param_error'][2]) /
                           float(experiment[i]['fit_param'][2]))**2 +
                          (float(experiment[i]['param_error'][0]) /
                           float(experiment[i]['fit_param'][0]))**2)) / ctemp)
        ws.write(i - skipped + 1, 27, 100 * reqtemp / rmtemp)
        ws.write(i - skipped + 1, 28, str(''))  #association chi sq
        ws.write(i - skipped + 1, 29, (1 - (rsqreport / rawyreport)))
        ws.write(i - skipped + 1, 30,
                 str(float(experiment[i]['signal2noise'])))
        ws.write(i - skipped + 1, 31, str(''))
        ws.write(i - skipped + 1, 32, str(experiment[i]['fit_param'][7]))
        if figures:
            pngfigure = BIO(
                datafitting.saveplot(experiment[i],
                                     None,
                                     1,
                                     True,
                                     on_xrange=onrange,
                                     off_xrange=offrange))
            ws.set_row(i - skipped + 1, 132)
            ws.insert_image(i - skipped + 1, 33, 'figure.png', {
                'image_data': pngfigure,
                'x_scale': 0.3,
                'y_scale': 0.3
            })
    wb.close()
    if xlsout:
        convertxlsx2xls(filename)
        if not figures:
            os.remove(filename)
    return
Пример #18
0
def image_scrape():
    
    image_amount = input("How many images would you like to scrape? (No more than 12)")

    exceed = True
    while exceed == True:

        try:
            int(image_amount)
        except:
            image_amount = input("Value entered was not a number. Please enter a number >= 12:")
            continue

        if int(image_amount) > 12:
            image_amount = input("No more than 12 images can be saved. Please enter a new value: ")

            try:
                int(image_amount)
            except:
                image_amount = input("Value entered was not a number. Please enter a number >= 12:")
                continue
            if int(image_amount) > 12:
                continue
            else:
                exceed = False
        elif int(image_amount) <= 12:
            exceed = False


    user_search = input("Enter an image search:")
    bing_search_variable = {"q": user_search} 
    dir_name = user_search.replace(" ", "-") + "-images" 


    r = requests.get("http://www.bing.com/images/search", bing_search_variable)
    soup = bs(r.text, "html.parser")
    image_links = soup.findAll("a", {"class": "thumb"})

    if not os.path.isdir(dir_name):
        os.makedirs(dir_name)

    i = 0
    print(image_amount, "images being saved to", dir_name, "directory.")
    
    for item in image_links:
        try:
            
            obj_img = requests.get(item.attrs["href"])
            print("Link of photo being saved:", item.attrs["href"], "\n")
            file_name= item.attrs["href"].split("/")[-1] 
            
            try:
                img = Image.open(BIO(obj_img.content))  
                img.save("./" + dir_name + "/" + file_name, img.format) 
                i += 1
                if i == image_amount:
                    break
            
            except:
                print("Image was not saved.")
        
        except:
            print("Image request failed.")