Пример #1
0
 def __getitem__(self, index):
     item = self.items[index].copy()
     bunch = item.pop('images')
     if self.channels_mode == 'six':
         channels = []
         for i, filename in sorted(bunch, key=itemgetter(0)):
             img = self.open_fn(filename)
             img = img if self.tr is None else self.tr(img)
             channels.append(img)
         sample = torch.cat(channels, dim=0)
     else:
         filenames = [filename for _, filename in bunch]
         t = rio.load_images_as_tensor(filenames)
         arr = rio.convert_tensor_to_rgb(t)
         img = PIL.Image.fromarray(arr.astype(np.uint8))
         sample = img if self.tr is None else self.tr(img)
     # y = self.targets[index]
     y = item[self.targets_key]
     if self.drop_meta:
         item = dict(features=sample, targets=y)
     else:
         item.update(dict(features=sample, targets=y))
     if self.onehot:
         y_enc = get_one_hot(y,
                             smoothing=self.label_smoothing,
                             num_classes=self.num_classes)
         sample['targets_one_hot'] = y_enc
     return item
Пример #2
0
def plot_rgb(data, figsize=10):
    """
    convert 6-channels to RGB (3-channel)
    and visualize the figure.

    """
    dirs = data[0].split("/")
    base_plates = [dirs.index(i) for i in dirs if i.startswith("Plate")][0]
    pre = "_".join(dirs[base_plates - 1:base_plates + 1])
    suf = "_".join(os.path.basename(data[0]).split("_")[:2])

    imgname = "_".join((pre, suf))

    plt.figure(figsize=(figsize, figsize))

    img_6channel = rio.load_images_as_tensor(data)
    img_as_rgb = rio.convert_tensor_to_rgb(img_6channel)
    plt.title(imgname)
    plt.imshow(img_as_rgb)
Пример #3
0
def plot_cell_sirna_rgb(filtered_data, figsize=20):
    """
    plot all data in a row (Actually mutliple rows)

    :param filtered_data: dataframe, after processed with 'filter_data_with_cell_sirna'
    :param figsize: column size
    :return:
    """
    data_num = len(filtered_data)
    row_num = math.ceil((data_num // 4))

    plt.figure(figsize=(figsize, figsize * row_num * 0.25))

    for i in range(data_num):
        plt.subplot(row_num, 4, i + 1)
        img_paths = filtered_data.iloc[i, -6:].tolist()

        img_6channel = rio.load_images_as_tensor(img_paths)
        img_as_rgb = rio.convert_tensor_to_rgb(img_6channel)
        plt.title(filtered_data.iat[i, 0])
        plt.imshow(img_as_rgb)
Пример #4
0
def upload():
    print('--> /upload')
    if 'POST' == request.method:
        HEPG2 = request.form.get('radio-choice-h-2a')
        HVUEC = request.form.get('radio-choice-h-2b')
        RPE = request.form.get('radio-choice-h-2c')
        U2OS = request.form.get('radio-choice-h-2d')

        cell_type = ''
        if HEPG2:
            cell_type = 'HEPG2'
        elif HVUEC:
            cell_type = 'HVUEC'
        elif RPE:
            cell_type = 'RPE'
        else:
            cell_type = 'U2OS'

        experiment_on = request.form.get('experiment-no')
        plate_no = request.form.get('plate-no')

        positive_control = request.form.get('well-radio-choice-h-2')
        negative_control = request.form.get('well-radio-choice-h-2b')
        treatment = request.form.get('well-radio-choice-h-2c')
        well_type = ''
        if positive_control:
            well_type = 'positive_control'
        elif negative_control:
            well_type = 'negative_control'
        else:
            well_type = 'treatment'

        print(HEPG2, HVUEC, RPE, U2OS)
        print(experiment_on)

        files = []
        rgb_file_name = ''
        for file in request.files.getlist('files'):
            if file and allowed_file(file.filename):
                filename = cell_type + '-' + experiment_on \
                           + '-Plate' + plate_no \
                           + '-' + secure_filename(file.filename)
                print('-->', file.filename, '==>', filename)

                t = filename.split('_')
                rgb_file_name = t[0] + '_rgb.png'

                files.append(filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        # load
        channel_paths = [
            os.path.join(app.config['UPLOAD_FOLDER'], f) for f in files
        ]
        tensor = rio.load_images_as_tensor(channel_paths)
        #
        rgb = rio.convert_tensor_to_rgb(tensor)
        rgb = np.array(rgb, dtype=np.uint8)
        matplotlib.image.imsave(
            os.path.join(app.config['UPLOAD_FOLDER'], rgb_file_name), rgb)
        # predict:
        tensor = np.expand_dims(tensor, axis=0)
        predictions = pred({'feature': tensor})

        print(predictions)

        id = str(uuid.uuid1())
        submit_time = isoformat(datetime.datetime.now())

        # save file to database
        record_file = File(id, session['email'], cell_type, experiment_on,
                           plate_no, well_type, ','.join(files), rgb_file_name,
                           submit_time, '%d' % predictions['classes'][0])
        db.session.add(record_file)
        db.session.commit()

    return redirect('file_list.html')