Beispiel #1
0
    def get(self):
        from gzip import GzipFile
        try:
            from cString import StringIO
        except ImportError:
            from StringIO import StringIO

        data = self.get_data()
        data['gzipped'] = True
        json_response = self.json_response(data, finish=False)

        tmp_buffer = StringIO()

        gziped_buffer = GzipFile(
            fileobj=tmp_buffer,
            mode="wb",
            compresslevel=7)
        gziped_buffer.write(json_response)
        gziped_buffer.close()

        gzipped_data = tmp_buffer.getvalue()

        self.set_header("Content-Encoding", 'gzip')
        self.set_header("Content-Length", str(len(gzipped_data)))

        tmp_buffer.close()
        self.finish(gzipped_data)
Beispiel #2
0
 def _end_profile_(prof):
     logger.debug('End of profiling, generate profiling report')
     prof.disable()
     _sio = StringIO()
     sortby = 'cumulative'
     import pstats
     _pstat = pstats.Stats(prof, stream=_sio).sort_stats('cumulative')
     _pstat.print_stats()
     print(_sio.getvalue())
Beispiel #3
0
def feeder_stations(feeder, impfile):
    result = _(summary=None, errors=[], passed=0, failed=0)
    result.summary = _make_imxsummary(result)

    try:
        impfile.file.seek(0)
        f = StringIO(impfile.file.read().decode())
        f.seek(0)

        dialect = csv.Sniffer().sniff(f.read(1024))
        f.seek(0)

        reader = csv.DictReader(f, dialect=dialect)
        for row in reader:
            try:
                r = {}
                r['code'] = row['code'].upper()
                r['name'] = row['name'].title()
                r['capacity'] = row['capacity']
                r['public'] = row['type'].upper().strip() == 'P'
                r['type'] = row.get('category', '') or 'D'
                r['source_feeder'] = feeder.code
                r['vratio'] = str(db.Volt.MVOLTL_LVOLT if feeder.voltage ==
                                  '11' else db.Volt.MVOLTH_LVOLT)
                db.Station.insert_one(r)
                result.passed += 1
            except Exception as ex:
                result.failed += 1
    except Exception as ex:
        result.errors.append(str(ex))
    return result
Beispiel #4
0
    def test_decode_gzip(self):
        from gzip import GzipFile
        try:
            from cString import StringIO
        except ImportError:
            from io import StringIO

        data_for_gzip = Request.__doc__
        tmp_buffer = StringIO()

        gziped_buffer = GzipFile(fileobj=tmp_buffer,
                                 mode="wb",
                                 compresslevel=7)

        gziped_buffer.write(data_for_gzip)
        gziped_buffer.close()

        gzipped_data = tmp_buffer.getvalue()
        tmp_buffer.close()
        self.assertEqual(data_for_gzip, decode_gzip(gzipped_data))
Beispiel #5
0
    def test_decode_gzip(self):
        from gzip import GzipFile
        try:
            from cString import StringIO
        except ImportError:
            from StringIO import StringIO

        data_for_gzip = Request.__doc__
        tmp_buffer = StringIO()

        gziped_buffer = GzipFile(
            fileobj=tmp_buffer,
            mode="wb",
            compresslevel=7)

        gziped_buffer.write(data_for_gzip)
        gziped_buffer.close()

        gzipped_data = tmp_buffer.getvalue()
        tmp_buffer.close()
        self.assertEquals(data_for_gzip, decode_gzip(gzipped_data))
Beispiel #6
0
def feeder_stations(feeder, impfile):
    result = _(summary=None, errors=[], passed=0, failed=0)
    result.summary = _make_imxsummary(result)

    try:
        impfile.file.seek(0)
        f = StringIO(impfile.file.read().decode())
        f.seek(0)

        dialect = csv.Sniffer().sniff(f.read(1024))
        f.seek(0)

        reader = csv.DictReader(f, dialect=dialect)
        for row in reader:
            try:
                r = {}
                r["code"] = row["code"].upper()
                r["name"] = row["name"].title()
                r["capacity"] = row["capacity"]
                r["public"] = row["type"].upper().strip() == "P"
                r["type"] = row.get("category", "") or "D"
                r["source_feeder"] = feeder.code
                r["vratio"] = str(db.Volt.MVOLTL_LVOLT if feeder.voltage == "11" else db.Volt.MVOLTH_LVOLT)
                db.Station.insert_one(r)
                result.passed += 1
            except Exception as ex:
                result.failed += 1
    except Exception as ex:
        result.errors.append(str(ex))
    return result
Beispiel #7
0
def feeder_stations(feeder, impfile):
    result = _(summary=None, errors=[], passed=0, failed=0)
    result.summary = _make_imxsummary(result)

    try:
        impfile.file.seek(0)
        f = StringIO(impfile.file.read().decode())
        f.seek(0)

        dialect = csv.Sniffer().sniff(f.read(1024))
        f.seek(0)

        reader = csv.DictReader(f, dialect=dialect)
        for row in reader:
            try:
                r = {}
                r['code'] = row['code'].upper()
                r['name'] = row['name'].title()
                r['capacity'] = row['capacity']
                r['public'] = row['type'].upper().strip() == 'P'
                r['type'] = row.get('category', '') or 'D'
                r['source_feeder']  = feeder.code
                r['vratio'] = str(db.Volt.MVOLTL_LVOLT 
                        if feeder.voltage == '11'
                        else db.Volt.MVOLTH_LVOLT)
                db.Station.insert_one(r)
                result.passed += 1
            except Exception as ex:
                result.failed += 1
    except Exception as ex:
        result.errors.append(str(ex))
    return result
Beispiel #8
0
def construct_bvdata(bpm):
    # just read the last image
    image = _control_ref().ReadImage()

    last_acq_time, last_x, last_y, last_intensity,\
    last_fwhm_x, last_fwhm_y, last_max_intensity,\
    last_proj_x, last_proj_y = bpm.get_bpm_result(image.frameNumber, image.timestamp)
    lima_roi = _control_ref().image().getRoi()
    roi_top_left = lima_roi.getTopLeft()
    roi_size = lima_roi.getSize()
    jpegFile = StringIO()
    image_type = _control_ref().image().getImageType()

    bpp = bpm.ImageType2Bpp[image_type]

    # manual scaling: use the user image min/max intensity to filter
    if not bpm.autoscale:
        min_val = bpm.min_max[0]
        max_val = bpm.min_max[1]
        scale_image = image.buffer.clip(min_val, max_val)

    # auto scaling: use natural image intensity
    else:
        min_val = image.buffer.min()
        max_val = image.buffer.max()
        if max_val == 0: max_val = 1
        scale_image = image.buffer

    # logarithmic scaling
    if bpm.lut_method == "LOG":
        if min_val > 0:
            scale_image = numpy.log10(scale_image)
        else:
            scale_image = numpy.log10(scale_image.clip(1, None))
            min_val += 1
        min_val = numpy.log10(min_val)
        max_val = numpy.log10(max_val)

    # scale the image to the whole range 16bit before palette transformation for 0 to 65535
    if max_val == min_val: max_val += 1
    scaling = (2**16 - 1.) / (max_val - min_val)
    scale_image = ((scale_image - min_val) * scaling).astype(numpy.uint16)

    # last transformation ot color or greys palette
    if bpm.color_map:
        img_buffer = bpm.palette["color"].take(scale_image, axis=0)
    else:
        img_buffer = bpm.palette["grey"].take(scale_image, axis=0)

    I = Image.fromarray(img_buffer, "RGB")
    if turbo_jpeg:
        jpegFile.write(
            turbo_jpeg.encode(numpy.asarray(I),
                              pixel_format=TJPF_RGB,
                              quality=bpm.jpeg_quality))
    else:
        I.save(jpegFile, "jpeg", quality=bpm.jpeg_quality)

    raw_jpeg_data = jpegFile.getvalue()
    image_jpeg = base64.b64encode(raw_jpeg_data)
    if bpm.return_bpm_profiles:
        profile_x = last_proj_x.tobytes()
        profile_y = last_proj_y.tobytes()
    else:
        profile_y = image.buffer[:, bpm.beammark[0]].astype(numpy.uint64)
        profile_y = profile_y.tobytes()
        profile_x = image.buffer[bpm.beammark[1], :].astype(numpy.uint64)
        profile_x = profile_x.tobytes()

    bvdata_format = 'dldddliiiidd%ds%ds%ds' % (len(profile_x), len(profile_y),
                                               len(image_jpeg))
    bvdata = struct.pack(bvdata_format, last_acq_time, image.frameNumber,
                         last_x, last_y, last_intensity, last_max_intensity,
                         roi_top_left.x, roi_top_left.y, roi_size.getWidth(),
                         roi_size.getHeight(), last_fwhm_x, last_fwhm_y,
                         profile_x, profile_y, image_jpeg)
    return bvdata, bvdata_format