#else:
            #    single_band_output = list()
            #    for band_sp in spectrum:
            #        raw = "\t".join([str(f) for f in band_sp['raw']])
            #        mean = str(band_sp['mean'])
            #        std_dev = str(band_sp['std_dev'])
            #        # putting it all together in output text for a single band
            #        single_band_output.append("\t".join((raw, mean, std_dev)))
            #    output.append("\t".join([str(x) for x in [plot_id, cp['x'], cp['y']]] + ["\t".join(single_band_output)]))
    else:
        # checking whether we operate in some nxn-neighborhood
        if neighborhood > 1:
            single_band_output = list()
            for band in good_bands:
                raw_output = list()
                for suff in general_utils.letter_generator(len(spectrum[0]['raw'])):
                    raw_output.append("%d_raw_%s" % (band, suff))
                single_band_output.append("%s\t%d_mean\t%d_std_dev" % ("\t".join(raw_output), band, band))
                #single_band_output.append("%d_raw_a\t%d_raw_b\t%d_raw_c\t%d_raw_d\t%d_mean\t%d_std_dev" % tuple([band] * 6))
            output.insert(0, "\t".join((id_field, "x", "y", "\t".join(single_band_output))))
        # otherwise the header is much easier to build
        #else:
        #    output.insert(0, "\t".join((id_field, "x", "y", "\t".join([str(k) for k in good_bands]))))
        

    if neighborhood > 1:
        tgt_file = tgt_file.replace(".txt", "_%dx%d.txt" % tuple([neighborhood] * 2))
    tgt_path = os.path.join(tgt_dir, tgt_file)
    open(tgt_path, 'wb').write("\n".join(output))

Exemple #2
0
    def dump_spectra(self, include_bad_bands=True, include_raw_data=True):
        u"""
        Prepare output of extracted spectra data to an external file.
        """
        if not self.spectra:
            return

        # initializing output array
        output = list()

        # determining output bands
        if include_bad_bands:
            output_bands = sorted(self.bad_bands + self.good_bands)
        else:
            output_bands = self.good_bands

        # if there is more than one source image, we'll include the origin of the
        # current spectra
        if len(self.image_data) > 1:
            include_spectra_source = True
        else:
            include_spectra_source = False

        # iterating over all spectra and adding them to output
        for sp in sorted(self.spectra, key=attrgetter("id")):
            output.append(sp.__str__(include_bad_bands, include_spectra_source, include_raw_data))

        # finally adding a header line
        header_items = [self.roi_id, "img_id", "x", "y"]
        if len(self.image_data) == 1:
            header_items.remove("img_id")
        tmp_sp = self.spectra[0]
        if len(tmp_sp.attributes) > 0:
            for attr in sorted(tmp_sp.attributes.keys()):
                header_items.append(attr)
        if self.context_type == "circle":
            header_items.append("count")

        if self.context_range > 1:
            single_band_output = list()
            for band in output_bands:
                if band in self.good_bands:
                    raw_output = list()
                    if self.context_type == "square":
                        for suff in general_utils.letter_generator(self.context_range * self.context_range):
                            raw_output.append("%d_raw_%s" % (band, suff))
                    elif self.context_type == "circle":
                        raw_output.append("\t".join(("%d_min" % band, "%d_max" % band)))
                    single_band_output.append("%s\t%d_mean\t%d_std_dev" % ("\t".join(raw_output), band, band))
                else:
                    single_band_output.append(str(band))
            header_items.append("\t".join(single_band_output))
            # output.insert(0, "\t".join((self.loc_id_field, "img_id", "x", "y", "\t".join(single_band_output))))
        else:
            header_items.append("\t".join([str(k) for k in output_bands]))
            # output.insert(0, "\t".join((self.loc_id_field, "img_id", "x", "y", "\t".join([str(k) for k in output_bands]))))

        output.insert(0, "\t".join(header_items))

        # defining target file path
        self.tgt_path = os.path.join(self.tgt_dir, self.tgt_file)
        print "+ Dumping spectra to external file '%s'..." % self.tgt_path
        # writing result to specified output file path
        open(self.tgt_path, "wb").write("\n".join(output))
Exemple #3
0
    def dump_spectra(self, include_bad_bands=True, include_raw_data=True):
        u"""
        Prepare output of extracted spectra data to an external file.
        """
        # initializing output array
        output = list()

        # determining output bands
        if include_bad_bands:
            output_bands = sorted(self.bad_bands + self.good_bands)
        else:
            output_bands = self.good_bands

        # if there is more than on source image, we'll include the origin of the
        # current spectra
        if len(self.image_data) > 1:
            include_spectra_source = True
        else:
            include_spectra_source = False

        # iterating over all spectra and adding them to output
        for sp in sorted(self.spectra, key=attrgetter('id')):
            output.append(
                sp.__str__(include_bad_bands, include_spectra_source,
                           include_raw_data))

        # finally adding a header line
        header_items = [self.loc_id_field, "img_id", "x", "y"]
        if len(self.image_data) == 1:
            header_items.remove("img_id")
        if self.spectra:
            tmp_sp = self.spectra[0]
        if len(tmp_sp.attributes) > 0:
            for attr in sorted(tmp_sp.attributes.keys()):
                header_items.append(attr)
        if self.neighborhood_type == 'circle':
            header_items.append("count")

        if self.neighborhood > 1:
            single_band_output = list()
            for band in output_bands:
                if band in self.good_bands:
                    raw_output = list()
                    if self.neighborhood_type == 'square':
                        for suff in general_utils.letter_generator(
                                self.neighborhood * self.neighborhood):
                            raw_output.append("%d_raw_%s" % (band, suff))
                    elif self.neighborhood_type == 'circle':
                        raw_output.append("\t".join(
                            ("%d_min" % band, "%d_max" % band)))
                    single_band_output.append(
                        "%s\t%d_mean\t%d_std_dev" %
                        ("\t".join(raw_output), band, band))
                else:
                    single_band_output.append(str(band))
            header_items.append("\t".join(single_band_output))
            #output.insert(0, "\t".join((self.loc_id_field, "img_id", "x", "y", "\t".join(single_band_output))))
        else:
            header_items.append("\t".join([str(k) for k in output_bands]))
            #output.insert(0, "\t".join((self.loc_id_field, "img_id", "x", "y", "\t".join([str(k) for k in output_bands]))))

        output.insert(0, "\t".join(header_items))

        # defining target file path
        self.tgt_path = os.path.join(self.tgt_dir, self.tgt_file)
        print "+ Dumping spectra to external file '%s'..." % self.tgt_path
        # writing result to specified output file path
        open(self.tgt_path, 'wb').write("\n".join(output))