def hist_k(*args, **kwargs) -> str: """Returns the contents of the file created by ISIS hist as a string. If there is a TO= parameter in the arguments, ``hist_k()`` will create the file, and return its contents as a string """ to_pathlike = None for (k, v) in kwargs.items(): if 'to' == k or 'to_' == k: to_pathlike = v f = None if not to_pathlike: f = tempfile.NamedTemporaryFile(mode='w+') kwargs['to'] = f.name isis.hist(*args, **kwargs) if not f: f = open(to_pathlike, 'r') contents = f.read() f.close() return contents
def get_dncnt(cub: os.PathLike, hmin=0.01, hmax=99.99, keep=False) -> int: """Extract DN count from the histogram of a cub file""" # I'm not sure about this method. # The code below is the exact logic that the original Perl program has, # but this is just counting the number of histogram bins # that are within the boundaries, not the number of DN. # And the # of bins is automatically computed by isis.hist, # so could be different for each cube. # logger.info(get_dncnt.__doc__) histfile = Path(cub).with_suffix(".hist") if not histfile.is_file(): isis.hist(cub, to=histfile) h = isis.Histogram(histfile) count: int = 0 for row in h: if hmin <= float(row.CumulativePercent) <= hmax: count += 1 if not keep: histfile.unlink() return count
def setUp(self): self.cube = Path('test_Histogram.cub') self.histfile = Path('test_Histogram.hist') isis.hi2isis(img, to=self.cube) isis.hist(self.cube, to=self.histfile)