コード例 #1
0
ファイル: bampolish.py プロジェクト: shravanjeevan/bampolish
def greedyCoverage(inFile, outFile, maxCoverage, verboseFlag):
    '''
    This method is greedy but reads in low coverage areas may be missed if the
    current set is full.
    '''

    if verboseFlag:
        print("Reducing coverage using the Greedy method")

    curr = SortedSet()
    mapped = 0
    filtered = 0
    for r in inFile.fetch(until_eof=True):
        if (r.is_unmapped): continue
        mapped += 1

        # Attempt to find read that ends before this one
        itr = curr.irange(maximum=r.reference_start)
        try:
            ending = itr.__next__()
            # Some read is ending, replace it in the current set
            curr.discard(ending)
            curr.add(r.reference_end)
            outFile.write(r)
            filtered += 1
        except StopIteration:
            if (len(curr) < maxCoverage):
                # There is still room to include this read
                curr.add(r.reference_end)
                outFile.write(r)
                filtered += 1

    if verboseFlag:
        print("Reduced BAM from " + str(mapped) + " to " + str(filtered) +
              " reads")
コード例 #2
0
class _CoinDataSet(object):
    def __init__(self, init_from_file=True):
        self._market = coinmarketcap.Market()
        self._data = SortedSet()
        if init_from_file:
            for filename in os.listdir(STORAGE_DIR):
                with open(os.path.join(STORAGE_DIR, filename), 'r') as fp:
                    datapoint_list = json.load(
                        fp, object_hook=_CDPEncoder.decode_hook)
                    self._data.update(datapoint_list)

    def _DownloadNewDataPoint(self):
        cmc_dict = self._market.ticker(limit=0)
        data_to_store = {
            coin["symbol"]: coin["price_usd"]
            for coin in cmc_dict
        }
        self._data.add(_CoinDataPoint(timestamp(), data_to_store))
        self._DumpCurrentDayToFile()

    def _DumpAllToFile(self, filestr):
        data_to_dump = list(self._data)

        with open(filestr, 'w') as fp:
            json.dump(data_to_dump, fp, cls=_CDPEncoder)

    def _DumpCurrentDayToFile(self):
        # Midnight in unix time (system time zone)
        midnight = datetime.combine(date.today(), time.min)
        midnight_unix = int(midnight.timestamp())

        # All data since midnight.
        data_to_dump = list(self._data.irange(_CoinDataPoint(midnight_unix)))

        filestr = os.path.join(STORAGE_DIR,
                               midnight.strftime('%Y-%m-%d.coinjson'))
        with open(filestr, 'w') as fp:
            json.dump(data_to_dump, fp, cls=_CDPEncoder)

    def GetValue(self, symbol, time=None):
        try:
            if not time:
                return float(self._data[-1].coin_data[symbol.upper()])
            else:
                bisect_point = self._data.bisect(_CoinDataPoint(time))
                if (bisect_point) is 0:
                    return None
                return float(self._data[bisect_point -
                                        1].coin_data[symbol.upper()])
        except (IndexError, KeyError):
            return None

    def GetDayChange(self, symbol):
        currentVal = self.GetValue(symbol)
        yesterday_time = datetime.today() - timedelta(days=1)
        oldVal = self.GetValue(symbol, yesterday_time.timestamp())
        if oldVal is None:
            return None
        return 100 * ((currentVal - oldVal) / oldVal)
コード例 #3
0
ファイル: 074_LIS.py プロジェクト: sylabtechnologies/demo_DP
    def lengthOfLIS(self, nums: List[int]) -> int:
        s = SortedSet()
        for x in nums:
            if x in s:
                continue

            s.add(x)
            it = s.irange(x + 1)
            nxt = next(it, None)
            if nxt:
                s.remove(nxt)

        return len(s)
コード例 #4
0
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        if k < 1 or t < 0 or nums == None or len(nums) < 2:
            return False

        treeset = SortedSet()

        for i in xrange(len(nums)):
            # Solution 1
            subset = [x for x in treeset.irange(nums[i] - t, nums[i] + t)]
            if len(subset) > 0:
                return True
            treeset.add(nums[i])

            if i >= k:
                treeset.discard(nums[i - k])

        return False
コード例 #5
0
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        """
        :type nums: List[int]
        :type k: int
        :type t: int
        :rtype: bool
        """
        if k < 1 or t < 0 or nums == None or len(nums) < 2:
            return False

        treeset = SortedSet()

        for i in xrange(len(nums)):
            # Solution 1
            subset = [x for x in treeset.irange(nums[i] - t, nums[i] + t)]
            if len(subset) > 0:
                return True
            treeset.add(nums[i])

            if i >= k:
                treeset.discard(nums[i - k])

        return False
コード例 #6
0
def test_irange():
    ss = SortedSet(load=7)

    assert [] == list(ss.irange())

    values = list(range(53))
    ss.update(values)

    for start in range(53):
        for end in range(start, 53):
            assert list(ss.irange(start, end)) == values[start:(end + 1)]
            assert list(ss.irange(start, end, reverse=True)) == values[start:(end + 1)][::-1]

    for start in range(53):
        for end in range(start, 53):
            assert list(range(start, end)) == list(ss.irange(start, end, (True, False)))

    for start in range(53):
        for end in range(start, 53):
            assert list(range(start + 1, end + 1)) == list(ss.irange(start, end, (False, True)))

    for start in range(53):
        for end in range(start, 53):
            assert list(range(start + 1, end)) == list(ss.irange(start, end, (False, False)))

    for start in range(53):
        assert list(range(start, 53)) == list(ss.irange(start))

    for end in range(53):
        assert list(range(0, end)) == list(ss.irange(None, end, (True, False)))

    assert values == list(ss.irange(inclusive=(False, False)))

    assert [] == list(ss.irange(53))
    assert values == list(ss.irange(None, 53, (True, False)))
コード例 #7
0
def test_irange():
    ss = SortedSet()
    ss._reset(7)

    assert [] == list(ss.irange())

    values = list(range(53))
    ss.update(values)

    for start in range(53):
        for end in range(start, 53):
            assert list(ss.irange(start, end)) == values[start:(end + 1)]
            assert list(ss.irange(start, end, reverse=True)) == values[start:(end + 1)][::-1]

    for start in range(53):
        for end in range(start, 53):
            assert list(range(start, end)) == list(ss.irange(start, end, (True, False)))

    for start in range(53):
        for end in range(start, 53):
            assert list(range(start + 1, end + 1)) == list(ss.irange(start, end, (False, True)))

    for start in range(53):
        for end in range(start, 53):
            assert list(range(start + 1, end)) == list(ss.irange(start, end, (False, False)))

    for start in range(53):
        assert list(range(start, 53)) == list(ss.irange(start))

    for end in range(53):
        assert list(range(0, end)) == list(ss.irange(None, end, (True, False)))

    assert values == list(ss.irange(inclusive=(False, False)))

    assert [] == list(ss.irange(53))
    assert values == list(ss.irange(None, 53, (True, False)))