Beispiel #1
0
    def test_to_dict_from_dict(self):
        time = datetime(2018, 1, 1)
        s = CompetitionSpec(
            title="",
            type=CompetitionType.CLASSIFY,
            expiration=time,
            prize=1.0,
            num_classes=2,
            dataset="http://test.com",
            metric=CompetitionMetric.LOGLOSS,
            answer="http://test.com",
        )

        print(str(s))
        d = s.to_dict()

        d["expiration"] = time.strftime("%Y-%m-%d %H:%M:%S")

        j = s.to_json()

        s2 = CompetitionSpec.from_dict(d)
        s3 = CompetitionSpec.from_json(j)

        for item in ["type", "expiration", "prize", "num_classes", "metric"]:
            assert getattr(s, item) == getattr(s2, item) == getattr(s3, item)
Beispiel #2
0
    def test_checkAnswer3(self):
        exp = datetime.now() + timedelta(minutes=2)
        competition = CompetitionSpec(
            title="",
            type=CompetitionType.PREDICT,
            when=exp,
            prize=1.0,
            dataset="https://feeds.citibikenyc.com/stations/stations.json",
            dataset_type=DatasetFormat.JSON,
            dataset_key="id",
            dataset_kwargs={"record_column": "stationBeanList"},
            metric=CompetitionMetric.ABSDIFF,
            targets=["availableBikes", "availableDocks"],
            expiration=exp,
        )

        c2 = Competition.from_spec(1, competition)
        ans = foo5(competition)
        s2 = SubmissionSpec.from_dict({
            "competition_id": 2,
            "answer": ans.to_json(orient="records"),
            "answer_type": DatasetFormat.JSON,
        })
        s = Submission.from_spec(1, 2, c2, s2)

        checkAnswer(s)
Beispiel #3
0
    def test_answerPrototype6(self):
        # AnswerType.SIX
        dataset = cfdg.ohlcv().reset_index()
        exp = datetime.utcfromtimestamp(
            dataset[-1:]['index'].values[0].astype(datetime) / 1000000000)
        competition = CompetitionSpec(title='',
                                      type=CompetitionType.PREDICT,
                                      expiration=datetime.now() +
                                      timedelta(minutes=1),
                                      prize=1.0,
                                      dataset=dataset.iloc[:-1],
                                      metric=CompetitionMetric.ABSDIFF,
                                      targets=dataset.columns[-1],
                                      answer=dataset.iloc[-1:],
                                      dataset_key='index',
                                      when=exp)

        df = answerPrototype(competition)[['when', dataset.columns[-1]]]
        ans = pd.DataFrame([{
            'when': exp,
            dataset.columns[-1]: np.nan
        } for x in dataset['index'].iloc[:-1]],
                           index=[x for x in dataset['index'].iloc[:-1]
                                  ])[['when', dataset.columns[-1]]]
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #4
0
    def test_answerPrototype62(self):
        # AnswerType.SIX
        exp = datetime.now() + timedelta(minutes=2)
        competition = CompetitionSpec(
            title='',
            type=CompetitionType.PREDICT,
            when=exp,
            prize=1.0,
            dataset='https://feeds.citibikenyc.com/stations/stations.json',
            dataset_type=DatasetFormat.JSON,
            dataset_key='id',
            dataset_kwargs={'record_column': 'stationBeanList'},
            metric=CompetitionMetric.ABSDIFF,
            targets=['availableBikes', 'availableDocks'],
            expiration=exp)

        dataset = fetchDataset(competition)
        df = answerPrototype(competition)[[
            'when', 'availableBikes', 'availableDocks'
        ]]
        ans = pd.DataFrame([{
            'when': exp,
            'availableBikes': np.nan,
            'availableDocks': np.nan
        } for x in dataset.id],
                           index=[id for id in dataset.id])[[
                               'when', 'availableBikes', 'availableDocks'
                           ]]
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #5
0
    def test_answerPrototype62(self):
        # AnswerType.SIX
        exp = datetime.now() + timedelta(minutes=2)
        competition = CompetitionSpec(
            title="",
            type=CompetitionType.PREDICT,
            when=exp,
            prize=1.0,
            dataset="https://feeds.citibikenyc.com/stations/stations.json",
            dataset_type=DatasetFormat.JSON,
            dataset_key="id",
            dataset_kwargs={"record_column": "stationBeanList"},
            metric=CompetitionMetric.ABSDIFF,
            targets=["availableBikes", "availableDocks"],
            expiration=exp,
        )

        dataset = fetchDataset(competition)
        df = answerPrototype(competition)[["when", "availableBikes", "availableDocks"]]
        ans = pd.DataFrame(
            [
                {"when": exp, "availableBikes": np.nan, "availableDocks": np.nan}
                for x in dataset.id
            ],
            index=[id for id in dataset.id],
        )[["when", "availableBikes", "availableDocks"]]
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #6
0
    def test_checkAnswer2(self):
        dataset = cfdg.ohlcv()
        competition = CompetitionSpec(
            title='',
            type=CompetitionType.PREDICT,
            expiration=datetime.now() + timedelta(minutes=1),
            prize=1.0,
            dataset=dataset.iloc[:-1],
            metric=CompetitionMetric.ABSDIFF,
            targets=dataset.columns[-1],
            answer=dataset.iloc[-1:],
            when=datetime.utcfromtimestamp(
                dataset[-1:].index.values[0].astype(datetime) / 1000000000))

        c2 = Competition.from_spec(1, competition)
        ans = foo3(competition)
        print(ans)
        d2 = SubmissionSpec.from_dict({
            'competition_id': 2,
            'answer': ans.to_json(),
            'answer_type': DatasetFormat.JSON
        })
        s = Submission.from_spec(1, 2, c2, d2)

        checkAnswer(s)
Beispiel #7
0
    def test_checkAnswer3(self):
        exp = datetime.now() + timedelta(minutes=2)
        competition = CompetitionSpec(
            title='',
            type=CompetitionType.PREDICT,
            when=exp,
            prize=1.0,
            dataset='https://feeds.citibikenyc.com/stations/stations.json',
            dataset_type=DatasetFormat.JSON,
            dataset_key='id',
            dataset_kwargs={'record_column': 'stationBeanList'},
            metric=CompetitionMetric.ABSDIFF,
            targets=['availableBikes', 'availableDocks'],
            expiration=exp)

        c2 = Competition.from_spec(1, competition)
        ans = foo5(competition)
        s2 = SubmissionSpec.from_dict({
            'competition_id': 2,
            'answer': ans.to_json(orient='records'),
            'answer_type': DatasetFormat.JSON
        })
        s = Submission.from_spec(1, 2, c2, s2)

        checkAnswer(s)
Beispiel #8
0
 def test_init(self):
     dataset = make_classification()
     CompetitionSpec(title='',
                     type=CompetitionType.CLASSIFY,
                     expiration=datetime.now() + timedelta(minutes=1),
                     prize=1.0,
                     num_classes=2,
                     dataset=pandas.DataFrame(dataset[0]),
                     metric=CompetitionMetric.LOGLOSS,
                     answer=pandas.DataFrame(dataset[1]))
Beispiel #9
0
 def test_answerPrototype5(self):
     # AnswerType.FIVE
     exp = datetime.now() + timedelta(minutes=2)
     competition = CompetitionSpec(title='',
                                   type=CompetitionType.PREDICT,
                                   expiration=exp,
                                   prize=1.0,
                                   dataset='http://bonds.paine.nyc',
                                   dataset_type=DatasetFormat.JSON,
                                   metric=CompetitionMetric.ABSDIFF,
                                   targets={0: ['Price']})
     df = answerPrototype(competition)
     ans = pd.DataFrame([{'Price': np.nan}])
     print(df)
     print(ans)
     assert df.equals(ans)
Beispiel #10
0
    def test_answerPrototype1(self):
        # AnswerType.ONE
        dataset = make_classification()
        competition = CompetitionSpec(title='',
                                      type=CompetitionType.CLASSIFY,
                                      expiration=datetime.now() +
                                      timedelta(minutes=1),
                                      prize=1.0,
                                      num_classes=2,
                                      dataset=pd.DataFrame(dataset[0]),
                                      metric=CompetitionMetric.LOGLOSS,
                                      answer=pd.DataFrame(dataset[1]))

        dataset = fetchDataset(competition)
        df = answerPrototype(competition)
        ans = pd.DataFrame([{'class': np.nan} for x in dataset.index])
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #11
0
    def test_answerPrototype9(self):
        # AnswerType.NINE
        dataset = cfdg.ohlcv()
        competition = CompetitionSpec(
            title="",
            type=CompetitionType.PREDICT,
            expiration=datetime.now() + timedelta(minutes=1),
            prize=1.0,
            dataset=dataset.iloc[:-1],
            metric=CompetitionMetric.ABSDIFF,
            targets=dataset.columns[-1],
            answer=dataset.iloc[-1:],
        )

        df = answerPrototype(competition)
        ans = pd.DataFrame([{dataset.columns[-1]: np.nan} for x in dataset.index])
        ans = pd.DataFrame([{dataset.columns[-1]: np.nan}])
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #12
0
    def test_answerPrototype8(self):
        # AnswerType.EIGHT
        dataset = cfdg.ohlcv()
        exp = datetime.utcfromtimestamp(
            dataset[-1:].index.values[0].astype(datetime) / 1000000000)
        competition = CompetitionSpec(title='',
                                      type=CompetitionType.PREDICT,
                                      expiration=datetime.now() +
                                      timedelta(minutes=1),
                                      prize=1.0,
                                      dataset=dataset.iloc[:-1],
                                      metric=CompetitionMetric.ABSDIFF,
                                      targets=dataset.columns[-1],
                                      answer=dataset.iloc[-1:],
                                      when=exp)

        df = answerPrototype(competition)
        ans = pd.DataFrame([{dataset.columns[-1]: np.nan}], index=[exp])
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #13
0
    def test_answerPrototype7(self):
        # AnswerType.SEVEN
        dataset = cfdg.ohlcv().reset_index()
        competition = CompetitionSpec(title='',
                                      type=CompetitionType.PREDICT,
                                      expiration=datetime.now() +
                                      timedelta(minutes=1),
                                      prize=1.0,
                                      dataset=dataset.iloc[:-1],
                                      metric=CompetitionMetric.ABSDIFF,
                                      targets=dataset.columns[-1],
                                      dataset_key='index',
                                      answer=dataset.iloc[-1:])

        df = answerPrototype(competition)
        ans = pd.DataFrame([{
            dataset.columns[-1]: np.nan
        } for x in dataset['index'].iloc[:-1]],
                           index=dataset['index'].iloc[:-1])
        print(df)
        print(ans)
        assert df.equals(ans)
Beispiel #14
0
 def test_answerPrototype2(self):
     # AnswerType.TWO
     exp = datetime.now() + timedelta(minutes=2)
     competition = CompetitionSpec(title='',
                                   type=CompetitionType.PREDICT,
                                   expiration=exp,
                                   when=exp,
                                   prize=1.0,
                                   dataset='http://bonds.paine.nyc',
                                   dataset_type=DatasetFormat.JSON,
                                   metric=CompetitionMetric.ABSDIFF,
                                   dataset_key='Name',
                                   targets={'ABC Corp': ['Price']})
     df = answerPrototype(competition)[['Price', 'when']]
     ans = pd.DataFrame([{
         'when': exp,
         'Price': np.nan
     }],
                        index=['ABC Corp'])[['Price', 'when']]
     print(df)
     print(ans)
     assert df.equals(ans)
Beispiel #15
0
 def test_answerPrototype3(self):
     # AnswerType.THREE
     competition = CompetitionSpec(title='',
                                   type=CompetitionType.PREDICT,
                                   expiration=datetime.now() +
                                   timedelta(minutes=1),
                                   prize=1.0,
                                   dataset='http://bonds.paine.nyc',
                                   dataset_type=DatasetFormat.JSON,
                                   metric=CompetitionMetric.ABSDIFF,
                                   dataset_key='Name',
                                   targets={'ABC Corp': ['Price']})
     dataset = fetchDataset(competition)
     df = answerPrototype(competition, dataset)
     index = dataset[dataset['Name'] == 'ABC Corp'].index
     ans = pd.DataFrame([{
         'Name': 'ABC Corp',
         'Price': np.nan
     } for _ in index],
                        index=index)
     print(df)
     print(ans)
     assert df.equals(ans)
Beispiel #16
0
    def test_to_dict_from_dict(self):
        time = datetime(2018, 1, 1)
        s = CompetitionSpec(title='',
                            type=CompetitionType.CLASSIFY,
                            expiration=time,
                            prize=1.0,
                            num_classes=2,
                            dataset='http://test.com',
                            metric=CompetitionMetric.LOGLOSS,
                            answer='http://test.com')

        print(str(s))
        d = s.to_dict()

        d['expiration'] = time.strftime("%Y-%m-%d %H:%M:%S")

        j = s.to_json()

        s2 = CompetitionSpec.from_dict(d)
        s3 = CompetitionSpec.from_json(j)

        for item in ['type', 'expiration', 'prize', 'num_classes', 'metric']:
            assert getattr(s, item) == getattr(s2, item) == getattr(s3, item)
Beispiel #17
0
    def test_checkAnswer(self):
        dataset = make_classification()
        competition = CompetitionSpec(title='',
                                      type=CompetitionType.CLASSIFY,
                                      expiration=datetime.now() +
                                      timedelta(minutes=1),
                                      prize=1.0,
                                      num_classes=2,
                                      dataset=pd.DataFrame(dataset[0]),
                                      metric=CompetitionMetric.LOGLOSS,
                                      answer=pd.DataFrame(dataset[1]))
        c2 = Competition.from_spec(1, competition)
        d2 = SubmissionSpec.from_dict({
            'competition_id':
            2,
            'answer':
            pd.DataFrame(dataset[1]).to_json(),
            'answer_type':
            DatasetFormat.JSON
        })
        s = Submission.from_spec(1, 2, c2, d2)

        checkAnswer(s)
Beispiel #18
0
import pandas
import ujson
from datetime import datetime, timedelta
from sklearn.datasets import make_classification
from crowdsource.client import Client
from mock import patch, MagicMock
from crowdsource.types.competition import CompetitionSpec
from crowdsource.enums import CompetitionType, CompetitionMetric

dataset = make_classification()
competition = CompetitionSpec(
    title="",
    type=CompetitionType.CLASSIFY,
    expiration=datetime.now() + timedelta(minutes=1),
    prize=1.0,
    num_classes=2,
    dataset=pandas.DataFrame(dataset[0]),
    metric=CompetitionMetric.LOGLOSS,
    answer=pandas.DataFrame(dataset[1]),
)


class TestClient:
    def test_init(self):
        with patch("requests.post") as m:
            m.return_value = MagicMock()
            m.return_value.text = '{"id":1}'
            c = Client("test")
            assert c

    def test_register(self):