def test_exlusion_filter(self):
        arab_medium = [
            p.id
            for p in Products().search("Arab Medium").to_list()
            if p.layer == ["grade"]
        ]
        meg = [
            g.id
            for g in Geographies().search("MEG/AG").to_list()
            if "trading_region" in g.layer
        ]
        iraq = [
            g.id
            for g in Geographies().search("Iraq").to_list()
            if "country" in g.layer
        ]

        cols = [
            "cargo_movement_id",
            "vessels.0.name",
            "events.cargo_port_load_event.0.location.port.id",
            "events.cargo_port_load_event.0.location.port.label",
            "events.cargo_port_load_event.0.location.country.id",
            "events.cargo_port_load_event.0.location.country.label",
            "events.cargo_port_load_event.0.start_timestamp",
            "events.cargo_port_load_event.0.end_timestamp",
            "product.grade.id",
            "product.grade.label",
            "product.group_product.label",
            "parent_ids.0.id",
            "parent_ids.0.splinter_timestamp"
        ]

        df = (
            CargoMovements()
            .search(
                filter_activity="loading_end",
                filter_products=[
                    "54af755a090118dcf9b0724c9a4e9f14745c26165385ffa7f1445bc768f06f11"
                ],
                filter_origins=meg,
                exclude_origins=iraq[0],
                exclude_products=arab_medium,
                filter_time_min=datetime(2019, 10, 1),
                filter_time_max=datetime(2019, 11, 1),
                cm_unit="b",
            )
            .to_df(columns=cols)
        )

        df_excl = df.loc[
            (
                df["events.cargo_port_load_event.0.location.country.id"]
                == iraq[0]
            )
            | (df["product.grade.id"] == arab_medium[0])
        ]

        assert df_excl.empty
Exemplo n.º 2
0
    def test_convert_to_geography_ids(self):
        rotterdam_id = (
            "68faf65af1345067f11dc6723b8da32f00e304a6f33c000118fccd81947deb4e")
        rotterdam_name = Geographies().reference(rotterdam_id)["name"]

        result = convert_to_geography_ids([rotterdam_name])

        assert [rotterdam_id] == result
    def test_search(self):
        rotterdam = [
            g.id for g in Geographies().search("rotterdam").to_list()
            if "port" in g.layer
        ]
        v = VesselMovements().search(
            filter_time_min=datetime(2017, 10, 1, 0, 0),
            filter_time_max=datetime(2017, 10, 1, 0, 10),
            filter_origins=rotterdam,
        )

        assert len(v) > 10
    def test_to_df_all_columns(self):
        rotterdam = [
            g.id for g in Geographies().search("rotterdam").to_list()
            if "port" in g.layer
        ]
        df = (VesselMovements().search(
            filter_time_min=datetime(2017, 10, 1, 0, 0),
            filter_time_max=datetime(2017, 10, 1, 0, 10),
            filter_origins=rotterdam,
        ).to_df(columns="all").head(2))

        assert len(df) == 2
    def test_search_to_dataframe(self):
        rotterdam = [
            g.id for g in Geographies().search("rotterdam").to_list()
            if "port" in g.layer
        ]
        df = (VesselMovements().search(
            filter_time_min=datetime(2017, 10, 1, 0, 0),
            filter_time_max=datetime(2017, 10, 1, 0, 10),
            filter_origins=rotterdam,
        ).to_df().head(2))

        assert list(df.columns) == vessel_movements_result.DEFAULT_COLUMNS
        assert len(df) == 2
    def test_exclusion_filter(self):
        meg = [
            g.id for g in Geographies().search("MEG/AG").to_list()
            if "trading_region" in g.layer
        ]
        iraq = [
            g.id for g in Geographies().search("Iraq").to_list()
            if "country" in g.layer
        ]
        bahri = [c.id for c in Corporations().search("BAHRI").to_list()]

        cols = [
            "vessel_movement_id",
            "vessel.name",
            "start_timestamp",
            "end_timestamp",
            "origin.location.country.id",
            "origin.location.country.label",
            "destination.location.country.id",
            "destination.location.country.label",
            "cargoes.0.product.group.label",
            "vessel.corporate_entities.charterer.id",
            "vessel.corporate_entities.charterer.label",
        ]

        df = (VesselMovements().search(
            filter_origins=meg,
            exclude_origins=iraq,
            exclude_charterers=bahri[0],
            filter_time_min=datetime(2019, 10, 15),
            filter_time_max=datetime(2019, 11, 1),
        ).to_df(columns=cols))

        mask = (df["origin.location.country.id"] == iraq[0]) | (
            df["vessel.corporate_entities.charterer.id"] == bahri[0])
        df_excl = df.loc[mask]

        assert df_excl.empty
    def test_search_to_dataframe_subset_of_columns(self):
        cols = ["vessel.imo", "vessel.name"]

        rotterdam = [
            g.id for g in Geographies().search("rotterdam").to_list()
            if "port" in g.layer
        ]
        df = (VesselMovements().search(
            filter_time_min=datetime(2017, 10, 1, 0, 0),
            filter_time_max=datetime(2017, 10, 1, 0, 10),
            filter_origins=rotterdam,
        ).to_df(columns=cols).head(2))

        assert list(df.columns) == cols
        assert len(df) == 2
Exemplo n.º 8
0
    def test_filter_geographies_and_products(self):
        start = datetime(2019, 1, 1)
        end = datetime(2019, 11, 1)

        rotterdam = [
            g.id
            for g in Geographies().search(term="rotterdam").to_list()
            if "port" in g.layer
        ]
        crude = [
            p.id
            for p in Products().search("crude").to_list()
            if "Crude" == p.name
        ]

        rotterdam_crude_timeseries = (
            CargoTimeSeries()
            .search(
                filter_activity="loading_state",
                timeseries_unit="bpd",
                timeseries_frequency="month",
                filter_time_min=start,
                filter_time_max=end,
                filter_origins=rotterdam,
                filter_products=crude,
            )
            .to_df()
        )

        rotterdam_all_products_timeseries = (
            CargoTimeSeries()
            .search(
                filter_activity="loading_state",
                timeseries_unit="bpd",
                timeseries_frequency="month",
                filter_time_min=start,
                filter_time_max=end,
                filter_origins=rotterdam,
            )
            .to_df()
        )

        assert (
            rotterdam_all_products_timeseries["value"].sum()
            > rotterdam_crude_timeseries["value"].sum()
        )

        print(rotterdam_crude_timeseries.head())
Exemplo n.º 9
0
def check_can_retrieve_geographies():
    global all_tests_pass

    # noinspection PyBroadException
    try:
        from vortexasdk import Geographies

        europe = (
            "f39d455f5d38907394d6da3a91da4e391f9a34bd6a17e826d6042761067e88f4")
        geography = Geographies().reference(europe)
        assert geography["id"] == europe
        print("Python successfully retrieved a sample piece of reference data")
    except Exception as e:
        all_tests_pass = False
        print(e)
        print("Python unable to retrieve a sample piece of reference data")
    def test_search_single_filter_waypoint_name(self):
        df = (
            CargoMovements()
            .search(
                filter_activity="any_activity",
                filter_waypoints=[
                    g.id for g in Geographies().search(term="suez").to_list()
                ],
                filter_time_min=datetime(2019, 8, 29),
                filter_time_max=datetime(2019, 8, 29, 0, 10),
            )
            .to_df()
            .head(2)
        )

        assert len(df) == 2
    def test_age_flag_scrubbers_filters(self):
        panama = [
            g.id for g in Geographies().search("panama").to_list()
            if "country" in g.layer
        ]

        df = (VesselMovements().search(
            filter_vessel_scrubbers="inc",
            filter_vessel_age_min=2,
            filter_vessel_age_max=15,
            filter_vessel_flags=panama,
            filter_time_min=datetime(2017, 10, 1),
            filter_time_max=datetime(2017, 10, 1),
        ).to_df().head(2))

        print(df.head())
        assert len(df) == 2
    def test_search_single_filter_origin_name(self):
        df = (
            CargoMovements()
            .search(
                filter_activity="loading_state",
                filter_origins=[
                    g.id
                    for g in Geographies().search(term="rotterdam").to_list()
                    if "port" in g.layer
                ],
                filter_time_min=datetime(2019, 8, 29),
                filter_time_max=datetime(2019, 8, 29, 0, 10),
            )
            .to_df()
            .head(2)
        )

        assert len(df) == 2
Exemplo n.º 13
0
|    | events.cargo_port_unload_event.0.start_timestamp   | product.group.label   | product.grade.label   |   quantity | vessels.0.name   |
|---:|:---------------------------------------------------|:----------------------|:----------------------|-----------:|:-----------------|
|  0 | 2019-10-08T00:41:00+0000                           | Crude                 | Djeno                 |     123457 | AROME            |
|  1 | 2019-11-08T00:41:52+0000                           | Crude                 | Arab Medium           |      99898 | SCOOBYDOO        |
|  2 | 2019-09-30T23:49:41+0000                           | Crude                 | Arab Heavy            |    9879878 | DAVID            |
|  3 | 2019-12-01T01:40:00+0000                           | Crude                 | Usan                  |     999999 | DUCK             |


"""
from datetime import datetime

from vortexasdk import CargoMovements, Geographies, Vessels

if __name__ == "__main__":
    # Find china ID
    china = Geographies().search(term="China",
                                 exact_term_match=True).to_list()[0].id

    # Find the ID of all VLCCs
    vlccs = [
        v.id for v in Vessels().search(vessel_classes="vlcc_plus").to_list()
    ]

    # Query API
    search_result = CargoMovements().search(
        filter_activity="loading_start",
        filter_vessels=vlccs,
        filter_destinations=china,
        filter_time_min=datetime(2019, 9, 29),
        filter_time_max=datetime(2019, 10, 30),
    )
    def test_search_to_df(self):
        geographies = (Geographies().search(
            term=["Liverpool", "Southampton"]).to_df())

        print(to_markdown(geographies))
    def test_search(self):
        geographies = Geographies().search(term=["Liverpool", "Southampton"])
        names = [g["name"] for g in geographies]

        assert "Liverpool [GB]" in names
Exemplo n.º 16
0
from tests.testcases import TestCaseUsingRealAPI
from vortexasdk import Products, Geographies, Corporations, Vessels

endpoints_and_searchterms = [
    (Products(), "Gasoil"),
    (Geographies(), "China"),
    (Corporations(), "Oil"),
    (Vessels(), "Ocean"),
]


class TestSearchReal(TestCaseUsingRealAPI):
    def test_search_exact_match_yields_fewer_results_than_non_exact_match(
        self, ):

        for endpoint, term in endpoints_and_searchterms:
            result_loose_match = endpoint.search(term=term,
                                                 exact_term_match=False)
            result_exact_match = endpoint.search(term=term,
                                                 exact_term_match=True)

            assert len(result_exact_match) < len(result_loose_match)

    def test_search_exact_match_yields_exact_matches_only(self):

        for endpoint, term in endpoints_and_searchterms:
            result_exact_match = endpoint.search(term=term,
                                                 exact_term_match=True)

            actual_names = {e["name"] for e in result_exact_match}
Exemplo n.º 17
0
 def test_search_empty_args(self):
     Geographies().search()
Exemplo n.º 18
0
 def test_search_to_list(self):
     geographies = (Geographies().search(
         term=["Liverpool", "Southampton"]).to_list())
     names = [g.name for g in geographies]
     assert "Liverpool [GB]" in names
"""
from datetime import datetime

from dateutil.relativedelta import relativedelta

from vortexasdk import CargoMovements, Geographies, Products

if __name__ == "__main__":
    now = datetime.utcnow()
    one_month_ago = now - relativedelta(months=1)

    # For this analysis we need the geography ID for India, and the geography ID for Saudi Arabia. We're going to
    # show 2 ways to retrieve geography IDs. You'll want to chose method 1 or 2 depending on your use case.

    # Option 1. We look up a geography with an exact matching name
    saudi_arabia = Geographies().search("Saudi Arabia", exact_term_match=True).to_list()[0].id

    # Option 2. We search for geographies with similar names, then pick the one we're looking for

    # First we find the ID for the country India. Note that when searching geographies with the term 'india', we'll
    # retrieve all geographies with india in the name, ie Indiana, British Indian Ocean Territory...
    all_geogs_with_india_in_the_name = Geographies().search("india").to_list()

    # If running interactively, you may want to print all the names here to inspect them for yourself
    for g in all_geogs_with_india_in_the_name:
        print(g.name)

    # We're only interested in the country India here
    india = [
        g.id for g in all_geogs_with_india_in_the_name if g.name == "India"
    ]
Exemplo n.º 20
0
    def test_load_all(self):
        all_geogs = Geographies().load_all()

        assert len(all_geogs) > 1000