示例#1
0
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        qApp.installEventFilter(self)
        self.setupUi(self)

        self.data = ImageData()

        self.errorMsg = QErrorMessage(self)
        self.errorMsg.setWindowModality(Qt.WindowModal)

        self.pushButtonLoadWL.clicked.connect(self.loadWhite)
        self.pushButtonLoadSpectra.clicked.connect(self.loadSpectra)
        self.pushButtonLoad.clicked.connect(self.loadAnnotations)
        self.pushButtonSave.clicked.connect(self.saveAnnotations)

        self.pushButtonClear.clicked.connect(self.plot_visual.clearSelected)

        #        self.comboBoxMethod.currentIndexChanged.connect()
        self.horizontalSliderWn.valueChanged.connect(self.wavenumberSlide)
        self.lineEditWn.editingFinished.connect(self.wavenumberEdit)

        self.comboBoxVisual.currentIndexChanged.connect(self.imageProjection)
        self.comboBoxCmaps.currentTextChanged.connect(self.plot_visual.setCmap)

        self.lineEditWn.setFormat("%.2f")
        #        self.lineEditWavenumber.setRange(min=wmin, max=wmax, default=.5*(wmin+wmax))

        self.comboBoxAnnotation.currentIndexChanged.connect(
            self.plot_visual.setAnnotation)

        self.plot_visual.addedPoint.connect(self.plot_whitelight.addPoint)
        self.plot_visual.removedPoint.connect(self.plot_whitelight.removePoint)
        self.plot_visual.alteredCounts.connect(self.updateCounts)
        self.plot_whitelight.clickedPoint.connect(
            self.plot_visual.clickOnePoint)
示例#2
0
    def run_instance(self, category_path, category):

        print 'Estimating volume for path', category_path

        try:
            image_data = ImageData(category_path)

            instance = category_path.split('/')[-2] if category_path.endswith(
                '/') else category_path.split('/')[-1]
            self.stats.add_true_for_instance(category, instance,
                                             image_data.volume['value'])

            volume = self.volume_estimator.with_kmeans().estimate(image_data)
            self.stats.add_predicted_for_instance(
                category, instance, self.volume_estimator.method.desc, volume)

            if debug():
                print u'Predicted volume for target %s = %3.9f %s\u00B3 with %s.' % (
                    instance, volume, image_data.volume['units'],
                    self.volume_estimator.method.desc)

            volume = self.volume_estimator.with_meanshift().estimate(
                image_data)
            self.stats.add_predicted_for_instance(
                category, instance, self.volume_estimator.method.desc, volume)

            if debug():
                print u'Predicted volume for target %s = %3.9f %s\u00B3 with %s.' % (
                    instance, volume, image_data.volume['units'],
                    self.volume_estimator.method.desc)

        except Exception as e:
            print "Exception occurred dring volume estimation, skipping image at", category_path, e
示例#3
0
    def scrape(self):
        # Get the HTML from a random page.
        page_offset = str(random.randint(0, self.UPPER_BOUND))
        html_doc = requests.get(self.URL + "?offset=" + page_offset).text

        # Create the soup object for extracting tags and stuff.
        soup = BeautifulSoup(html_doc)

        # Find all the thumbnail images.
        thumbnails = soup.find_all("a", {"class": self.THUMBNAIL_CLASS})

        image_data_items = []
        for thumbnail in thumbnails:
            if not thumbnail.img.has_attr("alt"):
                continue
            descr = thumbnail.img["alt"]
            # if thumbnail.has_attr(self.FULL_IMG_ATTR):
            #     image_data_items.append(ImageData(thumbnail[self.FULL_IMG_ATTR], descr))
            if thumbnail.has_attr(self.IMG_ATTR):
                image_data_items.append(
                    ImageData(thumbnail[self.IMG_ATTR], descr))
            else:
                image_data_items.append(ImageData(thumbnail.img["src"], descr))
        return image_data_items
    def setupThreads(self):
        # Shared data to connect all image services
        self.imData = ImageData(FalconEyeMap.VID_1)

        # Retireve image thread
        self.recvImg = RetrieveImage()
        self.recvImg.setSharedData(self.imData)

        # Processing image thread
        self.imgProcess = ProcessImage()
        self.imgProcess.setSharedData(self.imData)

        # Show the video window (Debbugging only)
        self.debugFeed = DebugVideoFeed()
        self.debugFeed.setSharedData(self.imData)
示例#5
0
# 'plan' for TensorRT or 'onnx' for ONNX
backend = 'plan'

retina_backends = {'onnx': RetinaInferORT, 'plan': RetinaInferTRT}

model_dir, model_path = config.build_model_paths(model_name, backend)

retina_backend = retina_backends[backend](rec_name=model_path)

detector = FaceDetector(inference_backend=retina_backend)

detector.prepare(fix_image_size=(input_shape[1], input_shape[0]))

tt0 = time.time()
image = cv2.imread('test_images/Stallone.jpg.jpg', cv2.IMREAD_COLOR)
image = ImageData(image, input_shape)
image.resize_image(mode='pad')
tt1 = time.time()
print(f"Preparing image took: {tt1 - tt0}")

t0 = time.time()
for i in range(iters):

    tf0 = time.time()
    detections = detector.detect(image.transformed_image, threshold=0.3)
    tf1 = time.time()
    print(f"Full detection  took: {tf1 - tf0}")

t1 = time.time()
print(f'Took {t1 - t0} s. ({iters / (t1 - t0)} im/sec)')
示例#6
0
class MyMainWindow(QMainWindow, Ui_MainWindow):

    closing = pyqtSignal()

    fileOptions = QFileDialog.Options() | QFileDialog.DontUseNativeDialog

    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        qApp.installEventFilter(self)
        self.setupUi(self)

        self.data = ImageData()

        self.errorMsg = QErrorMessage(self)
        self.errorMsg.setWindowModality(Qt.WindowModal)

        self.pushButtonLoadWL.clicked.connect(self.loadWhite)
        self.pushButtonLoadSpectra.clicked.connect(self.loadSpectra)
        self.pushButtonLoad.clicked.connect(self.loadAnnotations)
        self.pushButtonSave.clicked.connect(self.saveAnnotations)

        self.pushButtonClear.clicked.connect(self.plot_visual.clearSelected)

        #        self.comboBoxMethod.currentIndexChanged.connect()
        self.horizontalSliderWn.valueChanged.connect(self.wavenumberSlide)
        self.lineEditWn.editingFinished.connect(self.wavenumberEdit)

        self.comboBoxVisual.currentIndexChanged.connect(self.imageProjection)
        self.comboBoxCmaps.currentTextChanged.connect(self.plot_visual.setCmap)

        self.lineEditWn.setFormat("%.2f")
        #        self.lineEditWavenumber.setRange(min=wmin, max=wmax, default=.5*(wmin+wmax))

        self.comboBoxAnnotation.currentIndexChanged.connect(
            self.plot_visual.setAnnotation)

        self.plot_visual.addedPoint.connect(self.plot_whitelight.addPoint)
        self.plot_visual.removedPoint.connect(self.plot_whitelight.removePoint)
        self.plot_visual.alteredCounts.connect(self.updateCounts)
        self.plot_whitelight.clickedPoint.connect(
            self.plot_visual.clickOnePoint)

    def closeEvent(self, event):
        self.closing.emit()
        plt.close('all')
        self.deleteLater()
        qApp.quit()

    def loadSpectra(self):
        "Load a file or return (error message, traceback) from trying"
        fileName, _ = QFileDialog.getOpenFileName(
            self,
            "Load FTIR image",
            "",
            "Matlab files (*.mat);;All files (*)",
            options=MyMainWindow.fileOptions)
        self.data.readmat(fileName)
        self.plot_visual.setData(self.data.wavenumber, self.data.raw,
                                 self.data.wh)
        self.horizontalSliderWn.setMaximum(len(self.data.wavenumber) - 1)
        self.lineEditWn.setRange(self.data.wmin, self.data.wmax)
        self.imageProjection()
        self.plot_whitelight.load(os.path.splitext(fileName)[0] + '.jpg')

    def loadAnnotations(self):
        fileName, _ = QFileDialog.getOpenFileName(
            self,
            "Load annotation map",
            "",
            "Matlab files (*.mat);;All files (*)",
            options=MyMainWindow.fileOptions)
        if fileName:
            s = scipy.io.loadmat(fileName)['annotations']
            self.plot_visual.setAnnotations(s)

    def saveAnnotations(self):
        fileName, _ = QFileDialog.getSaveFileName(
            self,
            "Save annotation map",
            "",
            "Matlab files (*.mat);;All files (*)",
            options=MyMainWindow.fileOptions)
        if fileName:
            scipy.io.savemat(
                fileName, {'annotations': self.plot_visual.getAnnotations()})

    # Image visualization
    def loadWhite(self):
        fileName, _ = QFileDialog.getOpenFileName(
            self,
            "Load white light image",
            "",
            "Image files (*.jpg *.png);;All files (*)",
            options=MyMainWindow.fileOptions)
        if fileName:
            self.plot_whitelight.load(fileName)

    def imageProjection(self):
        meth = self.comboBoxVisual.currentIndex()
        iswn = meth == 2
        self.lineEditWn.setEnabled(iswn)
        self.horizontalSliderWn.setEnabled(iswn)
        self.plot_visual.setProjection(meth,
                                       -1 - self.horizontalSliderWn.value())

    def updateCounts(self, counts):
        for i in range(self.comboBoxAnnotation.count()):
            t = self.comboBoxAnnotation.itemText(i)
            p = t.rfind('(')
            t = t[:p] if p >= 0 else t + ' '
            self.comboBoxAnnotation.setItemText(i, t + '(%d)' % counts[i])

    def sliderToBox(self, slider, box, ixfinder):
        wn = self.plot_visual.getWavenumbers()
        if wn is not None:
            if (not box.hasAcceptableInput()
                    or slider.value() != ixfinder(wn, box.value())):
                box.setValue(wn[-1 - slider.value()])
        elif (not box.hasAcceptableInput() or slider.value() != int(
                round(slider.maximum() * (box.value() - self.wmin) /
                      (self.wmax - self.wmin)))):
            box.setValue(self.wmin + (self.wmax - self.wmin) * slider.value() /
                         slider.maximum())

    def boxToSlider(self, slider, box, ixfinder):
        wn = self.plot_visual.getWavenumbers()
        if wn is not None:
            if box.hasAcceptableInput():
                slider.setValue(ixfinder(wn, box.value()))
            else:
                box.setValue(wn[-1 - slider.value()])
        else:
            slider.setValue(
                int(
                    round(slider.maximum() * (box.value() - self.wmin) /
                          (self.wmax - self.wmin))))

    def wavenumberSlide(self):
        self.sliderToBox(
            self.horizontalSliderWn, self.lineEditWn,
            lambda wn, val: len(wn) - 1 - (np.abs(wn - val)).argmin())
        self.plot_visual.setProjection(2, -1 - self.horizontalSliderWn.value())

    def wavenumberEdit(self):
        self.boxToSlider(
            self.horizontalSliderWn, self.lineEditWn,
            lambda wn, val: len(wn) - 1 - (np.abs(wn - val)).argmin())
示例#7
0
 def set_image_data(self, image, um_per_pixel):
     self.image_data = ImageData(image.index, um_per_pixel, image)
     log.info("Image Index: %s" % image.index)
示例#8
0
class FastqImageAligner(object):
    """A class to find the alignment of fastq data and image data."""
    def __init__(self, microns_per_pixel):
        self.fastq_tiles = {}
        self.fastq_tiles_keys = []
        self.microns_per_pixel = microns_per_pixel
        self.image_data = None
        self.fq_w = 935  #um
        self.control_corr = 0
        self.non_mutual_hits = set()
        self.mutual_hits = set()
        self.bad_mutual_hits = set()
        self.good_mutual_hits = set()
        self.exclusive_hits = set()
        self.hitting_tiles = []

    def load_reads(self, tile_data, valid_keys=None):
        start = time.time()
        for tile_key, read_names in tile_data.items():
            if valid_keys is None or tile_key in valid_keys:
                self.fastq_tiles[tile_key] = FastqTileRCs(
                    tile_key, read_names, self.microns_per_pixel)

    @property
    def fastq_tiles_list(self):
        for _, tile in sorted(self.fastq_tiles.items()):
            yield tile

    def all_reads_fic_from_aligned_fic(self, other_fic, all_reads):
        self.load_reads(
            all_reads,
            valid_keys=[tile.key for tile in other_fic.hitting_tiles])
        self.image_data = deepcopy(other_fic.image_data)
        self.fq_w = other_fic.fq_w
        self.set_fastq_tile_mappings()
        self.set_all_fastq_image_data()
        self.hitting_tiles = [
            self.fastq_tiles[tile.key] for tile in other_fic.hitting_tiles
        ]
        self.clusters = other_fic.clusters
        for other_tile in other_fic.hitting_tiles:
            tile = self.fastq_tiles[other_tile.key]
            try:
                tile.set_aligned_rcs_given_transform(other_tile.scale,
                                                     other_tile.rotation,
                                                     other_tile.offset)
            except AttributeError:
                # I'm not sure why this is happening - tiles aren't getting aligned?
                # We will make sure tiles are aligned before writing results to disk
                log.debug("Skipping tile that lacks rotation!")
                continue

    def set_tile_alignment(self, tile_key, scale, fq_w, rotation, rc_offset):
        if self.fastq_tiles[tile_key] not in self.hitting_tiles:
            self.hitting_tiles.append(self.fastq_tiles[tile_key])
        self.fq_w = fq_w
        self.set_fastq_tile_mappings()
        self.set_all_fastq_image_data()
        tile = self.fastq_tiles[tile_key]
        tile.set_aligned_rcs_given_transform(scale, rotation, rc_offset)

    def alignment_from_alignment_file(self, path):
        self.hitting_tiles = []
        with open(path) as f:
            astats = stats.AlignmentStats().from_file(f)
        for tile_key, scaling, tile_width, rotation, rc_offset, _ in astats:
            self.set_tile_alignment(tile_key, scaling, tile_width, rotation,
                                    rc_offset)

    def set_sexcat_from_file(self, fpath, cluster_strategy):
        with open(fpath) as f:
            #FLETCHER TIMING            start = time.time()
            self.clusters = clusters.Clusters(f, cluster_strategy)
#FLETCHER            log.info("Clusters File: %s " % self.clusters.point_rcs)

    def set_image_data(self, image, um_per_pixel):
        self.image_data = ImageData(image.index, um_per_pixel, image)
        log.info("Image Index: %s" % image.index)

    def set_all_fastq_image_data(self):
        for key, tile in self.fastq_tiles.items():
            tile.set_fastq_image_data(self.fq_im_offset, self.fq_im_scale,
                                      self.fq_im_scaled_dims, self.fq_w)

    def rotate_all_fastq_data(self, degrees):
        im_shapes = [
            tile.rotate_data(degrees) for tile in self.fastq_tiles_list
        ]
        self.fq_im_scaled_dims = np.array(im_shapes).max(axis=0)
        for tile in self.fastq_tiles_list:
            tile.image_shape = self.fq_im_scaled_dims

    def set_fastq_tile_mappings(self):
        """Calculate parameters for mapping fastq tiles for ffts."""
        log.info("Setting Fastq Tile Mappings")
        assert self.image_data is not None, 'No image data loaded.'
        assert self.fastq_tiles != {}, 'No fastq data loaded.'
        all_data = np.concatenate(
            [tile.rcs for key, tile in self.fastq_tiles.items()])
        x_min, y_min = all_data.min(axis=0)
        x_max, y_max = all_data.max(axis=0)
        self.fq_im_offset = np.array([-x_min, -y_min])
        log.info("Fastq Image Offset: %s" % self.fq_im_offset)
        log.info("x_min, y_min: %s, %s" % (x_min, y_min))
        self.fq_im_scale = (float(self.fq_w) /
                            (x_max - x_min)) / self.image_data.um_per_pixel
        log.info('Fastq Image Scale: %s' % self.fq_im_scale)
        self.fq_im_scaled_maxes = self.fq_im_scale * np.array(
            [x_max - x_min, y_max - y_min])
        self.fq_im_scaled_dims = (self.fq_im_scaled_maxes + [1, 1]).astype(
            np.int)

    def find_hitting_tiles(self,
                           possible_tile_keys,
                           cluster_size,
                           snr_thresh=1.2):
        log.info('Locating Hitting Tiles')
        possible_tiles = [
            self.fastq_tiles[key] for key in possible_tile_keys
            if key in self.fastq_tiles
        ]
        #FLETCHER        log.debug("Possible Tiles: %s" % (possible_tile_keys))
        impossible_tiles = [
            tile for tile in self.fastq_tiles.values()
            if tile not in possible_tiles
        ]
        impossible_tiles.sort(key=lambda tile: -len(tile.read_names))
        log.info("Sorting Impossible Tiles Method: %s" %
                 (-len(tile.read_names)))
        control_tiles = impossible_tiles[:2]
        #FLETCHER TIMING
        #        start = time.time()
        self.image_data.set_fft(self.fq_im_scaled_dims)
        self.control_corr = 0
        start_control = time.time()
        for control_tile in control_tiles:
            log.info("Starting Control Correlation")
            corr, _ = control_tile.fft_align_with_im(self.image_data,
                                                     cluster_size)
            if corr > self.control_corr:
                self.control_corr = corr
#FLETCHER        log.info('Control Correlation: %s seconds' % round(time.time()- start_control))
        del control_tiles
        self.hitting_tiles = []
        log.info("Starting Experimental Correlation")
        for tile in possible_tiles:
            start = time.time()
            log.info("Trying Tile: %s" % tile.key.title())
            max_corr, align_tr = tile.fft_align_with_im(
                self.image_data, cluster_size)
            log.info("Correlations: Max: %s  Control: %s" %
                     (max_corr, self.control_corr))
            log.info("FM Cross-correllation Time: %s second" %
                     round(time.time() - start, 0))
            #FLETCHER            log.debug("Image Data: %s" % self.image_data.um_dims)
            if max_corr > snr_thresh * self.control_corr:
                tile.set_aligned_rcs(align_tr)
                self.max_correlation = max_corr
                log.info("Tile Max Correlation: %s" % self.max_correlation)
                tile.snr = max_corr / self.control_corr
                self.hitting_tiles.append(tile)

    def find_points_in_frame(self, consider_tiles='all'):
        self.rcs_in_frame = []
        aligned_rcs_in_frame = []
        im_shape = self.image_data.image.shape

        if consider_tiles == 'all':
            considered_tiles = self.hitting_tiles
        else:
            considered_tiles = [consider_tiles]

        for tile in considered_tiles:
            rcs = tile.rcs.astype(np.int)
            for i, pt in enumerate(tile.aligned_rcs):
                if 0 <= pt[0] < im_shape[0] and 0 <= pt[1] < im_shape[1]:
                    aligned_rcs_in_frame.append(pt)
                    self.rcs_in_frame.append((tile.key, rcs[i]))
        self.aligned_rcs_in_frame = np.array(aligned_rcs_in_frame)

    def hit_dists(self, hits):
        return [self.single_hit_dist(hit) for hit in hits]

    def single_hit_dist(self, hit):
        return np.linalg.norm(self.clusters.point_rcs[hit[0]] -
                              self.aligned_rcs_in_frame[hit[1]])

    def remove_longest_hits(self, hits, pct_thresh):
        if not hits:
            return []
        dists = self.hit_dists(hits)
        thresh = np.percentile(dists, pct_thresh * 100)
        return [hit for hit in hits if self.single_hit_dist(hit) <= thresh]

    def find_hits(self, consider_tiles='all'):
        # --------------------------------------------------------------------------------
        # Find nearest neighbors
        # --------------------------------------------------------------------------------
        self.find_points_in_frame(consider_tiles)
        cluster_tree = KDTree(self.clusters.point_rcs)
        aligned_tree = KDTree(self.aligned_rcs_in_frame)

        # All indices are in the order (cluster_index, aligned_in_frame_idx)
        cluster_to_aligned_indexes = set()
        for i, pt in enumerate(self.clusters.point_rcs):
            dist, idx = aligned_tree.query(pt)
            cluster_to_aligned_indexes.add((i, idx))

        aligned_to_cluster_indexs_rev = set()
        for i, pt in enumerate(self.aligned_rcs_in_frame):
            dist, idx = cluster_tree.query(pt)
            aligned_to_cluster_indexs_rev.add((idx, i))

        # --------------------------------------------------------------------------------
        # Find categories of hits
        # --------------------------------------------------------------------------------
        mutual_hits = cluster_to_aligned_indexes & aligned_to_cluster_indexs_rev
        non_mutual_hits = cluster_to_aligned_indexes ^ aligned_to_cluster_indexs_rev

        cluster_in_non_mutual = set(i for i, j in non_mutual_hits)
        aligned_in_non_mutual = set(j for i, j in non_mutual_hits)
        exclusive_hits = set((i, j) for i, j in mutual_hits
                             if i not in cluster_in_non_mutual
                             and j not in aligned_in_non_mutual)

        # --------------------------------------------------------------------------------
        # Recover good non-exclusive mutual hits.
        # --------------------------------------------------------------------------------
        # If the distance to second neighbor is too close, that suggests a bad peak call combining
        # two peaks into one. Filter those out with a gaussian-mixture-model-determined threshold.
        if int(16.0 / self.image_data.um_per_pixel) == 60:
            # Value decided by observation of our data. May vary with equipment.

            good_hit_threshold = 5
        else:
            good_hit_threshold = np.percentile(self.hit_dists(exclusive_hits),
                                               95)
        second_neighbor_thresh = 2 * good_hit_threshold

        exclusive_hits = set(
            hit for hit in exclusive_hits
            if self.single_hit_dist(hit) <= good_hit_threshold)

        good_mutual_hits = set()
        for i, j in (mutual_hits - exclusive_hits):
            if self.hit_dists([(i, j)])[0] > good_hit_threshold:
                continue
            third_wheels = [
                tup for tup in non_mutual_hits if i == tup[0] or j == tup[1]
            ]
            if min(self.hit_dists(third_wheels)) > second_neighbor_thresh:
                good_mutual_hits.add((i, j))
        bad_mutual_hits = mutual_hits - exclusive_hits - good_mutual_hits

        # --------------------------------------------------------------------------------
        # Test that the four groups form a partition of all hits and finalize
        # --------------------------------------------------------------------------------
        assert (non_mutual_hits | bad_mutual_hits | good_mutual_hits
                | exclusive_hits
                == cluster_to_aligned_indexes | aligned_to_cluster_indexs_rev
                and len(non_mutual_hits) + len(bad_mutual_hits) +
                len(good_mutual_hits) + len(exclusive_hits)
                == len(cluster_to_aligned_indexes
                       | aligned_to_cluster_indexs_rev))

        self.non_mutual_hits = non_mutual_hits
        self.mutual_hits = mutual_hits
        self.bad_mutual_hits = bad_mutual_hits
        self.good_mutual_hits = good_mutual_hits
        self.exclusive_hits = exclusive_hits

        if consider_tiles != 'all':
            log.debug('Non-mutual hits: %s' % len(non_mutual_hits))
            log.debug('Mutual hits: %s' % len(mutual_hits))
            log.debug('Bad mutual hits: %s' % len(bad_mutual_hits))
            log.debug('Good mutual hits: %s' % len(good_mutual_hits))
            log.debug('Exclusive hits: %s' % len(exclusive_hits))

    def least_squares_mapping(self, pct_thresh=0.9, min_hits=50):
        """least_squares_mapping(self, hit_type='exclusive')

        "Input": set of tuples of (cluster_index, in_frame_idx) mappings.

        "Output": scaling lambda, rotation theta, x_offset, y_offset, and aligned_rcs

        We here solve the matrix least squares equation Ax = b, where

                [ x0r -y0r 1 0 ]
                [ y0r  x0r 0 1 ]
            A = [ x1r -y1r 1 0 ]
                [ y1r  x1r 0 1 ]
                      . . .
                [ xnr -ynr 1 0 ]
                [ ynr  xnr 0 1 ]

        and

            b = [ x0s y0s x1s y1s . . . xns yns ]^T

        The r and s subscripts indicate rcs and cluster coords.

        The interpretation of x is then given by

            x = [ alpha beta x_offset y_offset ]^T

        where
            alpha = lambda cos(theta), and
            beta = lambda sin(theta)

        This system of equations is then finally solved for lambda and theta.
        """
        def get_hits(hit_type):
            if isinstance(hit_type, str):
                hit_type = [hit_type]
            hits = []
            for ht in hit_type:
                hits.extend(getattr(self, ht + '_hits'))
            return hits

        found_good_mapping = False

        for tile in self.hitting_tiles:
            self.find_hits(consider_tiles=tile)
            # Reminder: All indices are in the order (cluster_index, in_frame_idx)
            raw_hits = get_hits(('exclusive', 'good_mutual'))
            hits = self.remove_longest_hits(raw_hits, pct_thresh)
            if len(hits) < min_hits:
                continue
            else:
                found_good_mapping = True
            A = np.zeros((2 * len(hits), 4))
            b = np.zeros((2 * len(hits), ))
            for i, (cluster_index, in_frame_idx) in enumerate(hits):
                tile_key, (xir, yir) = self.rcs_in_frame[in_frame_idx]
                A[2 * i, :] = [xir, -yir, 1, 0]
                A[2 * i + 1, :] = [yir, xir, 0, 1]

                xis, yis = self.clusters.point_rcs[cluster_index]
                b[2 * i] = xis
                b[2 * i + 1] = yis

            alpha, beta, x_offset, y_offset = np.linalg.lstsq(A, b)[0]
            offset = np.array([x_offset, y_offset])
            theta = np.arctan2(beta, alpha)
            lbda = alpha / np.cos(theta)
            tile.set_aligned_rcs_given_transform(lbda, theta, offset)
            tile.set_correlation(self.image_data.image)
            if hasattr(self, 'control_corr'):
                tile.set_snr_with_control_corr(self.control_corr)
        return found_good_mapping

    def rough_align(self,
                    possible_tile_keys,
                    rotation_est,
                    fq_w_est=927,
                    cluster_size=0.25,
                    snr_thresh=1.2):
        self.fq_w = fq_w_est
        self.set_fastq_tile_mappings()
        self.set_all_fastq_image_data()
        self.rotate_all_fastq_data(rotation_est)
        start_time = time.time()
        self.find_hitting_tiles(possible_tile_keys, cluster_size, snr_thresh)
        log.debug('Rough alignment time: %.3f seconds' %
                  (time.time() - start_time))

    def precision_align_only(self, min_hits):
        start_time = time.time()
        if not self.hitting_tiles:
            raise RuntimeError('Alignment not found')
        found_good_mapping = self.least_squares_mapping(min_hits=min_hits)
        if not found_good_mapping:
            raise ValueError("Could not precision align!")
        log.debug('Precision alignment time: %.3f seconds' %
                  (time.time() - start_time))
        start_time = time.time()
        self.find_hits()
        log.debug('Hit finding time: %.3f seconds' %
                  (time.time() - start_time))

    @property
    def alignment_stats(self):
        hits = {
            'exclusive': len(self.exclusive_hits),
            'good_mutual': len(self.good_mutual_hits),
            'bad_mutual': len(self.bad_mutual_hits),
            'non_mutual': len(self.non_mutual_hits)
        }
        offsets = [
            tuple(map(float, tile.offset)) for tile in self.hitting_tiles
        ]
        return stats.AlignmentStats().from_data(
            [str(tile.key) for tile in self.hitting_tiles],
            [float(tile.scale) for tile in self.hitting_tiles],
            [float(tile.width) for tile in self.hitting_tiles],
            [float(tile.rotation_degrees)
             for tile in self.hitting_tiles], offsets, hits)

    @property
    def read_names_rcs(self):
        im_shape = self.image_data.image.shape
        for tile in self.hitting_tiles:
            if not hasattr(tile, 'rotation'):
                # hack because I don't understand why tiles aren't getting rotations
                # not having rotations implies they aren't getting aligned at all, which is very bad
                continue
            for read_name, pt in izip(tile.read_names, tile.aligned_rcs):
                if 0 <= pt[0] < im_shape[0] and 0 <= pt[1] < im_shape[1]:
                    yield '%s\t%f\t%f\n' % (read_name, pt[0], pt[1])
示例#9
0
 def set_image_data(self, image, um_per_pixel):
     self.image_data = ImageData(image.index, um_per_pixel, image)
示例#10
0
if(verbose): print('\n\n##########Limpando Inúteis!\n#############################################')
for filename in os.listdir(path_final_biomemaps):
  filename = os.path.splitext(filename)[0]
  filename = filename.split('_')
  ImageData.limpainutil(path_final_biomemaps+'biomemap_'+filename[1]+'.tif',path_final_heightmaps+'heightmap_'+filename[1]+'.tif')
'''

if(verbose): print('\n\n##########Gerando Descriptor!\n#############################################')
imagedata_list = []
descriptor = open(path_final+"imagedescriptor.txt","wb")
descriptor.truncate(0)

for filename in os.listdir(path_final_biomemaps):
  filename = os.path.splitext(filename)[0]
  filename = filename.split('_')
  imagedata = ImageData(path_final_biomemaps+'biomemap_'+filename[1]+'.tif',path_final_heightmaps+'heightmap_'+filename[1]+'.tif')
  imagedata_list.append(imagedata)
  #if(verbose): print(imagedata.biomedata)
  
if(verbose): print('\n\n##########Salvando Descriptor!\n#############################################')
pickle.dump(imagedata_list,descriptor)
descriptor.close()
if(verbose): print('foram salvos '+str(len(imagedata_list))+' dados!')
if(verbose): print('\n\n##########Fim de Execução!\n#############################################')






 def pick_n_images(self, n):
     set_size = self.redis.scard(self.set_name)
     random_members = self.redis.srandmember(self.set_name, min(n, set_size))
     return [ImageData(None, None).deserialize(x) for x in random_members]