Ejemplo n.º 1
0
    def test_now(self):
        # GH#9000
        ts_from_string = Timestamp('now')
        ts_from_method = Timestamp.now()
        ts_datetime = datetime.now()

        ts_from_string_tz = Timestamp('now', tz='US/Eastern')
        ts_from_method_tz = Timestamp.now(tz='US/Eastern')

        # Check that the delta between the times is less than 1s (arbitrarily
        # small)
        delta = Timedelta(seconds=1)
        assert abs(ts_from_method - ts_from_string) < delta
        assert abs(ts_datetime - ts_from_method) < delta
        assert abs(ts_from_method_tz - ts_from_string_tz) < delta
        assert (abs(ts_from_string_tz.tz_localize(None) -
                    ts_from_method_tz.tz_localize(None)) < delta)
def year2pandas_latest_date(year_in, month_in):
    if year_in is None:
        return Timestamp.now()

    if month_in is None:
        return Timestamp(str(year_in) + '-12-31')

    year_string = str(year_in) + '-' + str(month_in) + '-' + choose_last_day(year_in, month_in)
    return Timestamp(year_string)
Ejemplo n.º 3
0
def is_currently_in_session():
    is_in_session = False
    now = Timestamp.now()
    today_session = now.normalize()
    if krx_calendar.is_session(today_session):
        opening, closing = krx_calendar.open_and_close_for_session(
            today_session)
        is_in_session = opening <= now <= closing
    return is_in_session
Ejemplo n.º 4
0
def timestamp_now() -> Timestamp:
    """Returns a pandas timezone (UTC) aware Timestamp for the current time.

    Returns:
        pandas.Timestamp: Timestamp at current time
    """
    from pandas import Timestamp

    return timestamp_tzaware(Timestamp.now())
Ejemplo n.º 5
0
    def test_now(self):
        # GH#9000
        ts_from_string = Timestamp('now')
        ts_from_method = Timestamp.now()
        ts_datetime = datetime.now()

        ts_from_string_tz = Timestamp('now', tz='US/Eastern')
        ts_from_method_tz = Timestamp.now(tz='US/Eastern')

        # Check that the delta between the times is less than 1s (arbitrarily
        # small)
        delta = Timedelta(seconds=1)
        assert abs(ts_from_method - ts_from_string) < delta
        assert abs(ts_datetime - ts_from_method) < delta
        assert abs(ts_from_method_tz - ts_from_string_tz) < delta
        assert (abs(
            ts_from_string_tz.tz_localize(None) -
            ts_from_method_tz.tz_localize(None)) < delta)
Ejemplo n.º 6
0
 def create_trade(cls, stock_symbol_, quantity_, buy_or_sell_, trade_price_):
     '''
     Trade is created by using the current time
     
     @stock_symbol_ - Symbol of the given stock
     @quantity_ - Quantity of the stock
     @buy_or_sell_ - The stocks are bought or sold
     @trade_price_ - The price of the stocks
     '''
     return cls(stock_symbol_, Timestamp.now(), quantity_, buy_or_sell_, trade_price_)
Ejemplo n.º 7
0
    def test_class_ops_dateutil(self):
        def compare(x, y):
            assert (int(np.round(Timestamp(x).value / 1e9)) == int(
                np.round(Timestamp(y).value / 1e9)))

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now('UTC'), datetime.now(tzutc()))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(Timestamp.utcfromtimestamp(current_time),
                datetime.utcfromtimestamp(current_time))
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(Timestamp.combine(date_component, time_component),
                datetime.combine(date_component, time_component))
Ejemplo n.º 8
0
    def test_class_ops_dateutil(self):
        def compare(x, y):
            assert (int(np.round(Timestamp(x).value / 1e9)) ==
                    int(np.round(Timestamp(y).value / 1e9)))

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now('UTC'), datetime.now(tzutc()))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(Timestamp.utcfromtimestamp(current_time),
                datetime.utcfromtimestamp(current_time))
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(Timestamp.combine(date_component, time_component),
                datetime.combine(date_component, time_component))
Ejemplo n.º 9
0
def test_maybe_get_tz_invalid_types():
    with pytest.raises(TypeError, match="<class 'float'>"):
        timezones.maybe_get_tz(44.0)

    with pytest.raises(TypeError, match="<class 'module'>"):
        timezones.maybe_get_tz(pytz)

    msg = "<class 'pandas._libs.tslibs.timestamps.Timestamp'>"
    with pytest.raises(TypeError, match=msg):
        timezones.maybe_get_tz(Timestamp.now("UTC"))
Ejemplo n.º 10
0
    async def zmq_sub_data(self, string_delimiter=';'):
        while self._ACTIVE:
            await sleep(0)
            try:
                msg = await self._SUB_SOCKET.recv_string()
                if msg != "":
                    _timestamp = str(Timestamp.now('UTC'))[:-6]
                    _symbol, _data = msg.split(" ")
                    if len(_data.split(string_delimiter)) == 2:
                        _bid, _ask = _data.split(string_delimiter)

                        if self._verbose:
                            self._logger.info("\n[" + _symbol + "] " +
                                              _timestamp + " (" + _bid + "/" +
                                              _ask + ") BID/ASK")

                        # Update Market Data DB
                        if _symbol not in self._Market_Data_DB.keys():
                            self._Market_Data_DB[_symbol] = {}

                        self._Market_Data_DB[_symbol][_timestamp] = (
                            float(_bid), float(_ask))

                    elif len(_data.split(string_delimiter)) == 8:
                        _time, _open, _high, _low, _close, _tick_vol, _spread, _real_vol = _data.split(
                            string_delimiter)
                        if self._verbose:
                            self._logger.debug(
                                "[%s] %s (%s/%s/%s/%s/%s/%s/%s/%s) TIME/OPEN/HIGH/LOW/CLOSE/TICKVOL/SPREAD/VOLUME"
                                %
                                (_symbol, _timestamp, _time, _open, _high,
                                 _low, _close, _tick_vol, _spread, _real_vol))
                        # Update Market Rate DB
                        if _symbol not in self._Market_Data_DB.keys():
                            self._Market_Data_DB[_symbol] = {}
                        self._Market_Data_DB[_symbol][_timestamp] = (
                            int(_time), float(_open), float(_high),
                            float(_low), float(_close), int(_tick_vol),
                            int(_spread), int(_real_vol))

                    # invokes data handlers on sub port
                    for hnd in self._subdata_handlers:
                        await hnd.onSubData(msg)

            except zmq.error.Again:
                pass  # resource temporarily unavailable, nothing to self._logger.info
            except ValueError:
                pass  # No data returned, passing iteration.
            except UnboundLocalError:
                # _symbol may sometimes get referenced before being
                # assigned.
                pass
            except Exception as e:
                self._logger.exception(e)
Ejemplo n.º 11
0
    def send_markethist_request(
        self,
        symbol='EURUSD',
        timeframe=1,
        start='2019.01.04 17:00:00',
        end=Timestamp.now().strftime('%Y.%m.%d %H:%M:00')):
        # _end='2019.01.04 17:05:00'):

        _msg = "{};{};{};{};{}".format('HIST', symbol, timeframe, start, end)
        # Send via PUSH Socket
        self.remote_send(self._push_socket, _msg)
Ejemplo n.º 12
0
    def test_compare_zerodim_array(self):
        # GH#26916
        ts = Timestamp.now()
        dt64 = np.datetime64("2016-01-01", "ns")
        arr = np.array(dt64)
        assert arr.ndim == 0

        result = arr < ts
        assert result is np.bool_(True)
        result = arr > ts
        assert result is np.bool_(False)
Ejemplo n.º 13
0
def is_currently_in_session():
    from pandas import Timestamp
    from koapy.utils.krx.calendar.XKRXExchangeCalendar import XKRXExchangeCalendar
    krx_calendar = XKRXExchangeCalendar.get_calendar()
    now = Timestamp.now(tz=krx_calendar.tz)
    is_in_session = False
    today_session = now.normalize()
    if krx_calendar.is_session(today_session):
        opening, closing = krx_calendar.open_and_close_for_session(today_session)
        is_in_session = opening <= now <= closing
    return is_in_session
Ejemplo n.º 14
0
    def test_compare_zerodim_array(self):
        # GH#26916
        ts = Timestamp.now()
        dt64 = np.datetime64('2016-01-01', 'ns')
        arr = np.array(dt64)
        assert arr.ndim == 0

        result = arr < ts
        assert result is True
        result = arr > ts
        assert result is False
Ejemplo n.º 15
0
def get_last_krx_close_datetime():
    from pandas import Timestamp
    from koapy.utils.krx.calendar.XKRXExchangeCalendar import XKRXExchangeCalendar
    from tzlocal import get_localzone
    local_timezone = get_localzone()
    krx_calendar = XKRXExchangeCalendar.register()
    now = Timestamp.now(tz=local_timezone)
    last_close = krx_calendar.previous_close(now)
    last_close = last_close.astimezone(local_timezone)
    last_close = last_close.to_pydatetime()
    return last_close
Ejemplo n.º 16
0
 def test_td_sub_mixed_most_timedeltalike_object_dtype_array(self):
     # GH#21980
     now = Timestamp.now()
     arr = np.array([now,
                     Timedelta('1D'),
                     np.timedelta64(2, 'h')])
     exp = np.array([now - Timedelta('1D'),
                     Timedelta('0D'),
                     np.timedelta64(2, 'h') - Timedelta('1D')])
     res = arr - Timedelta('1D')
     tm.assert_numpy_array_equal(res, exp)
Ejemplo n.º 17
0
def main_prg():
    """Run the main programme."""
    args = parse_args()

    logger.setLevel(args.logLevel)

    timeUnit = TC[args.binSize]
    nUnit, tUnit = int(timeUnit[:-1]), timeUnit[-1]

    startTime = (
        Timestamp(STARTDATE_DFT[args.symbol]).round(timeUnit)
        if args.startTime is None
        else Timestamp(args.startTime)
    )
    # localising the timezone
    startTime = startTime.tz_localize(OS_TZ)

    # To avoid empty request we stop one unit befor the present date.
    endTime = (
        (Timestamp.now() - Timedelta(nUnit, tUnit)).round(timeUnit)
        if args.endTime is None
        else Timestamp(args.endTime)
    )

    # making_sure the format is also valid for windows
    endTime = endTime.tz_localize(OS_TZ)  # .strftime(STRF)

    query = {
        "binSize": args.binSize,
        "count": args.count,
        "partial": "false",
        "reverse": "false",
        "symbol": args.symbol,
    }

    _fout = Path(args.folder).joinpath(
        f"{args.symbol}-{args.binSize}-{endTime.strftime(STRF)}"
    )
    # kwargs stand for key words arguments
    kwargs = {
        "fout": f"{_fout}",
        "endTime": endTime,
        "startTime": startTime,
        "pause": args.pause,
    }

    # use live or test ids
    URL = URLS[args.live]

    logger.warning(f"Writting data to {kwargs['folder']}")

    _ = get_bucketed_trades(url=f"{URL}{args.entryPoint}", Q=query, **kwargs)
    return None
Ejemplo n.º 18
0
    def _DWX_MTX_SEND_MARKETDATA_REQUEST_(
        self,
        _symbol='EURUSD',
        _timeframe=1,
        _start='2019.01.04 17:00:00',
        _end=Timestamp.now().strftime('%Y.%m.%d %H:%M:00')):
        #_end='2019.01.04 17:05:00'):

        _msg = "{};{};{};{};{}".format('DATA', _symbol, _timeframe, _start,
                                       _end)
        # Send via PUSH Socket
        self.remote_send(self._PUSH_SOCKET, _msg)
Ejemplo n.º 19
0
    def test_rsub_dtscalars(self, tz_naive_fixture):
        # In particular, check that datetime64 - Timestamp works GH#28286
        td = Timedelta(1235345642000)
        ts = Timestamp.now(tz_naive_fixture)
        other = ts + td

        assert other - ts == td
        assert other.to_pydatetime() - ts == td
        if tz_naive_fixture is None:
            assert other.to_datetime64() - ts == td
        else:
            with pytest.raises(TypeError, match="subtraction must have"):
                other.to_datetime64() - ts
Ejemplo n.º 20
0
    def test_class_ops_pytz(self):
        def compare(x, y):
            assert int((Timestamp(x).value - Timestamp(y).value) / 1e9) == 0

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now("UTC"), datetime.now(timezone("UTC")))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(
            Timestamp.utcfromtimestamp(current_time),
            datetime.utcfromtimestamp(current_time),
        )
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(
            Timestamp.combine(date_component, time_component),
            datetime.combine(date_component, time_component),
        )
Ejemplo n.º 21
0
 def add(self, trade_):
     '''
     adding a new trade
     
     @trade_ - The trade
     
     Note: appending time is saved for dataframe validation
     '''
     if isinstance(trade_, Trade):
         self.__storage.append(trade_)
         self.__append_time = Timestamp.now()
     else:
         raise TradeManagerException('Invalid trade')
Ejemplo n.º 22
0
    def test_comparison_dt64_ndarray_tzaware(self, reverse, all_compare_operators):
        op = getattr(operator, all_compare_operators.strip("__"))

        ts = Timestamp.now("UTC")
        arr = np.array([ts.asm8, ts.asm8], dtype="M8[ns]")

        left, right = ts, arr
        if reverse:
            left, right = arr, ts

        msg = "Cannot compare tz-naive and tz-aware timestamps"
        with pytest.raises(TypeError, match=msg):
            op(left, right)
 def _DWX_MTX_SEND_MARKETDATA_REQUEST_(
     self,
     _action=GET_DATA,
     _symbol='EURUSD',
     _timeframe=16385,
     _start='2019.06.04 17:00:00',
     _end=Timestamp.now().strftime('%Y.%m.%d %H:%M:00')):
     _msg = "{};{};{};{};{}".format(_action, _symbol, _timeframe, _start,
                                    _end)
     """
     Function to construct messages for sending DATA commands to MetaTrader
     """
     # Send via PUSH Socket
     self.remote_send(self._PUSH_SOCKET, _msg)
Ejemplo n.º 24
0
 def run(self) -> None:
     try:
         while True:
             t: Timestamp = Timestamp.now(tz='Asia/Shanghai')
             t = t.round(freq=Timedelta(seconds=1))
             logging.info("当前时间:{}".format(t))
             for cond in self.time_event_conditions:
                 if cond.is_match(t):
                     event = Event(EventType.TIME, cond.name, t, {})
                     self.subscriber.on_event(event)
             time.sleep(0.8)
     except RuntimeError as e:
         import traceback
         logging.error("{}".format(traceback.format_exc()))
Ejemplo n.º 25
0
    def test_class_ops_pytz(self):
        def compare(x, y):
            assert int((Timestamp(x).value - Timestamp(y).value) / 1e9) == 0

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now("UTC"), datetime.now(timezone("UTC")))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        msg = "timezone-aware Timestamp with UTC"
        with tm.assert_produces_warning(FutureWarning, match=msg):
            # GH#22451
            ts_utc = Timestamp.utcfromtimestamp(current_time)
        compare(
            ts_utc,
            datetime.utcfromtimestamp(current_time),
        )
        compare(
            Timestamp.fromtimestamp(current_time), datetime.fromtimestamp(current_time)
        )
        compare(
            # Support tz kwarg in Timestamp.fromtimestamp
            Timestamp.fromtimestamp(current_time, "UTC"),
            datetime.fromtimestamp(current_time, utc),
        )
        compare(
            # Support tz kwarg in Timestamp.fromtimestamp
            Timestamp.fromtimestamp(current_time, tz="UTC"),
            datetime.fromtimestamp(current_time, utc),
        )

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(
            Timestamp.combine(date_component, time_component),
            datetime.combine(date_component, time_component),
        )
Ejemplo n.º 26
0
 def orderStatus(self, orderId: OrderId, status: str, filled: float, remaining: float, avgFillPrice: float,
                 permId: int, parentId: int, lastFillPrice: float, clientId: int, whyHeld: str, mktCapPrice: float):
     super().orderStatus(orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId,
                         whyHeld, mktCapPrice)
     if orderId not in self.ib_order_id_to_order:
         raise RuntimeError("orderId非法")
     # 由于目前策略只支持filled和cancelled两种状态,所以先只考虑这两种状态
     order: Order = self.ib_order_id_to_order[orderId]
     now = Timestamp.now(tz='Asia/Shanghai')
     if status == 'Filled':
         # 订单成交
         if order.status == OrderStatus.FILLED:
             #  表示已经发送过成交事件
             logging.info("重复的订单成交消息,不做处理")
             return
         data: OrderFilledData = OrderFilledData(order, avgFillPrice, filled, start_filled_time=now,
                                                 end_filled_time=now)
         self.order_filled(data)
         event = Event(event_type=EventType.ACCOUNT, sub_type="order_filled",
                       visible_time=Timestamp.now(tz="Asia/Shanghai"), data=data)
         self.subscriber.on_event(event)
     if status == "Cancelled":
         event = Event(event_type=EventType.ACCOUNT, sub_type="order_cancelled", visible_time=now, data=None)
         self.subscriber.on_event(event)
Ejemplo n.º 27
0
    def add_timestamp(data_input: pd.DataFrame, timestamp_input: pd.Timestamp = None) \
            -> pd.DataFrame:
        """
        #TODO add docstring to param timestamp
        #TODO If timestamp input has timestamp return EXCEPTION ALREADY TIMESTAMP EXCEPTION
        This functions adds Tİmestamp as a column to DataFrame input
        :param data_input: DataFrame. Input data
        :return: DataFrame. Timestamped dataframe
        """
        if timestamp_input == None:
            data_input['Timestamp'] = Timestamp.now()
        else:
            data_input['Timestamp'] = timestamp_input

        return data_input
Ejemplo n.º 28
0
 def tickString(self, reqId: TickerId, tickType: TickType, value: str):
     code = Request.find(reqId).code
     if tickType == 48:
         # 45表示RTVolume
         values = value.split(';')
         if len(values[0]) > 0:
             price_time = Timestamp(int(values[2]), unit='ms', tz='Asia/Shanghai')
             p = Price(code, float(values[0]), price_time)
             self.latest_price[code] = p
     values = {
         'tick_type': tickType,
         'value': value,
     }
     md = MarketData(self.name(), Timestamp.now(tz='Asia/Shanghai'), code=code, values=values)
     for sub in self.sub_map[code]:
         sub.on_data(md)
Ejemplo n.º 29
0
    def _DWX_MTX_SEND_RATESDATA_REQUEST_(self,
                                 _symbol='EURUSD',
                                 _timeframe=1,
                                 _start='2019.01.04 17:00:00',
                                 _end=Timestamp.now().strftime('%Y.%m.%d %H:%M:00')):
                                 #_end='2019.01.04 17:05:00'):
        messageID = self.generateMessageID()
        _msg = "{};{};{};{};{};{}".format(messageID, 'DATA_RATES',
                                     _symbol,
                                     _timeframe,
                                     _start,
                                     _end)
        # Send via PUSH Socket
        self.remote_send(self._PUSH_SOCKET, _msg)

        return messageID
Ejemplo n.º 30
0
 def __init__(self,
              start: Timestamp,
              end: Timestamp,
              codes: List[str],
              window: int = 100):
     if not end:
         end = Timestamp.now(tz='Asia/Shanghai')
     if not (start or window > 0):
         raise RuntimeError("必须指定start和end 或者 end和window")
     if not codes or len(codes) <= 0:
         raise RuntimeError("codes不能为空")
     self.start = start
     self.end = end
     self.codes = codes
     self.window = window
     self.calendar: TradingCalendar = None
Ejemplo n.º 31
0
def main():
    parser = ArgumentParser()
    parser.add_argument('type', choices=list(recorders))
    parser.add_argument('area', )
    parser.add_argument('value')
    parser.add_argument('--at', default=Timestamp.now(), type=Timestamp)
    args = parser.parse_args()

    recorder = recorders[args.type]
    session = Session(connect(), future=True)

    value = float(config.tanks.get(args.value, args.value))
    recorder(session, args.area, value, args.at)

    session.commit()
    print(f'recorded {args.area} {args.type} of {value} at {args.at}')
Ejemplo n.º 32
0
    def test_rsub_object(self):
        # GH#19369
        index = pd.Index([Decimal(1), Decimal(2)])
        expected = pd.Index([Decimal(1), Decimal(0)])

        result = Decimal(2) - index
        tm.assert_index_equal(result, expected)

        result = np.array([Decimal(2), Decimal(2)]) - index
        tm.assert_index_equal(result, expected)

        msg = "unsupported operand type"
        with pytest.raises(TypeError, match=msg):
            "foo" - index

        with pytest.raises(TypeError, match=msg):
            np.array([True, Timestamp.now()]) - index
Ejemplo n.º 33
0
    def test_timestamp(self):
        # GH#17329
        # tz-naive --> treat it as if it were UTC for purposes of timestamp()
        ts = Timestamp.now()
        uts = ts.replace(tzinfo=utc)
        assert ts.timestamp() == uts.timestamp()

        tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central')
        utsc = tsc.tz_convert('UTC')

        # utsc is a different representation of the same time
        assert tsc.timestamp() == utsc.timestamp()

        # datetime.timestamp() converts in the local timezone
        with tm.set_timezone('UTC'):
            # should agree with datetime.timestamp method
            dt = ts.to_pydatetime()
            assert dt.timestamp() == ts.timestamp()
Ejemplo n.º 34
0
def to_pickle(entityset, path):
    """Save the entityset at the given path.

       Args:
           entityset (:class:`featuretools.BaseEntitySet`) : EntitySet to save
           path : pathname of a directory to save the entityset
            (includes a CSV file for each entity, as well as a metadata
            pickle file)

    """
    entityset_path = os.path.abspath(os.path.expanduser(path))
    try:
        os.makedirs(entityset_path)
    except OSError:
        pass

    entity_store_dframes = {}
    entity_store_index_bys = {}

    entity_sizes = {}

    temp_dir = mkdtemp()

    for e_id, entity_store in entityset.entity_stores.items():
        entity_path = os.path.join(temp_dir, e_id)
        filename = e_id + '.csv'
        os.mkdir(entity_path)
        entity_store.df.to_csv(os.path.join(entity_path, filename),
                               index=False,
                               encoding=entity_store.encoding,
                               compression='gzip')
        entity_sizes[e_id] = \
            os.stat(os.path.join(entity_path, filename)).st_size
        datatypes = {'dtype': {}, 'parse_dates': []}
        for column in entity_store.df.columns:
            if entity_store.get_column_type(column) in _datetime_types:
                datatypes['parse_dates'].append(column)
            else:
                datatypes['dtype'][column] = entity_store.df[column].dtype
        pd_to_pickle(datatypes, os.path.join(entity_path, 'datatypes.p'))

        entity_store_dframes[e_id] = entity_store.df
        entity_store.df = None

        pd_to_pickle(entity_store.indexed_by, os.path.join(entity_path, 'indexed_by.p'))

        entity_store_index_bys[e_id] = entity_store.indexed_by
        entity_store.indexed_by = None

    entityset.entity_sizes = entity_sizes
    timestamp = Timestamp.now().isoformat()
    with open(os.path.join(temp_dir, 'save_time.txt'), 'w') as f:
        f.write(timestamp)
    pd_to_pickle(entityset, os.path.join(temp_dir, 'entityset.p'))

    for e_id, entity_store in entityset.entity_stores.items():
        setattr(entity_store, 'df', entity_store_dframes[e_id])
        setattr(entity_store, 'indexed_by', entity_store_index_bys[e_id])

    # can use a lock here if need be
    if os.path.exists(entityset_path):
        shutil.rmtree(entityset_path)
    shutil.move(temp_dir, entityset_path)
Ejemplo n.º 35
0
Archivo: men.py Proyecto: pastas/pasta
def dump(fname, data, version=3):
    # version can also be a specific version, like '2.x.g.t (beta)', or an integer (see below)
    if version == 3:
        version = '3.x.b.c (gamma)'
    elif version == 2:
        version = '2.x.g.t (beta)'
    # load an empty menyanthes file
    io_dir, this_script_name = path.split(__file__)
    base_fname = path.join(io_dir, 'men', version + '.men')

    if not path.exists(base_fname):
        msg = "No Menyanthes file found for version {}".format(version)
        raise Exception(msg)

    men = loadmat(base_fname, matlab_compatible=True)

    # add the oseries
    dtypeH = men['H'].dtype
    fields = dtypeH.fields.keys()
    Hdict = {}
    for field in fields:
        if field == 'ID':
            Hdict[field] = men['ID']['H'][0][0][0][0]
            men['ID']['H'] += 1
        elif field == 'Name':
            Hdict[field] = data['oseries']['name']
        elif field in ['NITGCode', 'OLGACode', 'NITGnr', 'OLGAnr']:
            Hdict[field] = '<no data> (1)'
        elif field == 'Type':
            Hdict[field] = 'Head'
        elif field in ['Project', 'layercode', 'LoggerSerial', 'area',
                       'datlog_serial']:
            Hdict[field] = ''
        elif field == 'values':
            date = array(
                [datetime2matlab(x) for x in data['oseries']['series'].index])
            vals = data['oseries']['series'].values
            Hdict[field] = [vstack((date, vals)).transpose()]
        elif field == 'filtnr':
            Hdict[field] = 1
        elif field in ['handmeas', 'aerialphoto', 'BWImage', 'photo']:
            Hdict[field] = [zeros(shape=(0, 0))]
        elif field in ['LastTUFExport', 'surflev', 'measpointlev', 'upfiltlev',
                       'lowfiltlev', 'sedsumplength', 'LoggerDepth', 'tranche',
                       'datlog_depth']:
            Hdict[field] = NaN
        elif field == 'xcoord':
            if 'x' in data['oseries']['metadata']:
                Hdict[field] = data['oseries']['metadata']['x']
            else:
                Hdict[field] = NaN
        elif field == 'ycoord':
            if 'y' in data['oseries']['metadata']:
                Hdict[field] = data['oseries']['metadata']['y']
            else:
                Hdict[field] = NaN
        elif field == 'date':
            Hdict[field] = datetime2matlab(Timestamp.now())
        elif field == 'comment':
            # Hdict[field] = [np.array(['',''])]
            obj_arr = zeros((2,), dtype=object)
            obj_arr[0] = ''
            obj_arr[1] = ''
            Hdict[field] = [obj_arr]
        elif field in ['LoggerBrand', 'LoggerType', 'VegTypo', 'VegType',
                       'Organization']:
            Hdict[field] = 'Unknown'
        elif field == 'Status':
            Hdict[field] = 'Active'
        elif field == 'Comments':
            # TODO: has to be a matlab-table
            Hdict[field] = ''
        elif field == 'Meta':
            # TODO: has to be a matlab-table
            Hdict[field] = ''
        elif field == 'diver_files':
            # has to be a matlab-struct
            dtype = [('name', 'O'), ('LoggerSerial', 'O'), ('values', 'O'),
                     ('orig_values', 'O'),
                     ('changes', 'O'), ('current_change', 'O'), ('drift', 'O'),
                     ('importedby', 'O'),
                     ('importdate', 'O'), ('validated', 'O'),
                     ('validatedby', 'O'), ('validatedate', 'O'),
                     ('battery_cap', 'O'), ('iscomp', 'O'), ('density', 'O'),
                     ('compID', 'O'), ('ref', 'O'),
                     ('IsEquidistant', 'O'), ('IsLoggerfile', 'O'),
                     ('timeshift', 'O')]
            Hdict[field] = [array([], dtype=dtype)]
        elif field == 'oldmetadata':
            # for version='2.x.g.t (beta)'
            dtype = [('xcoord', 'O'), ('ycoord', 'O'), ('upfiltlev', 'O'),
                     ('lowfiltlev', 'O'),
                     ('surflev', 'O'), ('measpointlev', 'O'),
                     ('sedsumplength', 'O'), ('datlog_serial', 'O'),
                     ('datlog_depth', 'O'), ('date', 'O'), ('comment', 'O'),
                     ('LoggerBrand', 'O'),
                     ('LoggerType', 'O'), ('VegTypo', 'O'), ('VegType', 'O')]
            Hdict[field] = [array([], dtype=dtype)]

        else:
            raise (ValueError('Unknown field ' + field))

    Hnew = zeros((1,), dtype=dtypeH)
    for key, val in Hdict.items():
        Hnew[key] = val
    men['H'] = vstack((men['H'], Hnew))

    # add the stressmodels
    dtypeIN = men['IN'].dtype
    fields = dtypeIN.fields.keys()
    for key in data['stressmodels'].keys():
        sm = data['stressmodels'][key]
        for istress, stress in enumerate(sm['stress']):
            INdict = {}
            for field in fields:
                if field == 'ID':
                    INdict[field] = men['ID']['IN'][0][0][0][0]
                    men['ID']['IN'] += 1
                elif field == 'Name':
                    INdict[field] = stress['name']
                elif field == 'Type':
                    if sm['stressmodel'] == 'StressModel2' and istress == 1:
                        INdict[field] = 'EVAP'
                    else:
                        INdict[field] = 'PREC'
                elif field in ['LoggerSerial', 'datlog_serial']:
                    INdict[field] = ''
                elif field == 'values':
                    date = array(
                        [datetime2matlab(x) for x in stress['series'].index])
                    vals = stress['series'].values
                    INdict[field] = [vstack((date, vals)).transpose()]
                elif field == 'filtnr':
                    INdict[field] = 1
                elif field in ['surflev', 'upfiltlev', 'lowfiltlev']:
                    INdict[field] = NaN
                elif field == 'xcoord':
                    INdict[field] = NaN
                elif field == 'ycoord':
                    INdict[field] = NaN
                elif field == 'date':
                    INdict[field] = datetime2matlab(Timestamp.now())
                elif field == 'Meta':
                    # TODO: has to be a matlab-table
                    INdict[field] = ''
                else:
                    raise (ValueError('Unknown field ' + field))
            INnew = zeros((1,), dtype=dtypeIN)
            for key, val in INdict.items():
                INnew[key] = val
            men['IN'] = vstack((men['IN'], INnew))

    if False:
        # correct an error from loadmat
        if 'AutoImport' in men['PS'].dtype.fields.keys():
            for i in range(len(men['PS']['AutoImport'][0][0][0])):
                men['PS']['AutoImport'][0][0][0][i]['IsEnabled'] = 0
        men['PS']['AutoLocalExportEnabled'] = False
        men['PS']['DrainVisEnabled'] = False
        if 'RemoteDb' in men['PS'].dtype.fields.keys():
            men['PS']['RemoteDb'][0][0]['IsConnected'] = 0

        for key in men['ID'].dtype.fields.keys():
            men['ID'][key] = 0

    # currently does not generate a model yet

    # save the file
    if not fname.endswith('.men'):
        fname = fname + '.men'
    savemat(fname, men, appendmat=False)
    return print("%s file succesfully exported" % fname)
Ejemplo n.º 36
0
    def test_expired(self):
        always_expired = CachedObject.expired()

        for dt in Timestamp.min, Timestamp.now(), Timestamp.max:
            with self.assertRaises(Expired):
                always_expired.unwrap(dt)