Exemple #1
0
class Submitter:
    def __init__(self, compete, work_dir, default_submission_id=0):
        self.kaggle_api = KaggleApi()
        self.kaggle_api.authenticate()

        self.work_dir = work_dir
        self.compete = compete
        self.default_submission_id = default_submission_id

    def submit(self,
               predicted: pd.DataFrame,
               file_name='submission.csv',
               message=None,
               save_model=True,
               model=None,
               model_name=None,
               submit=True,
               submission_id=None,
               submission_name=None,
               open_in_browser=False):

        # Folder and files
        if submission_id is None:
            submission_id = self.default_submission_id
            self.default_submission_id += 1

        submission_folder_name = ' '.join(
            [str(submission_id), ' -- ', submission_name])

        new_folder_path = self.work_dir + f'/{submission_folder_name}'

        if not os.path.exists(new_folder_path):
            os.mkdir(new_folder_path)

        # Save model
        if model_name is None:
            model_name = str(model)

        if save_model:
            with open(f'{new_folder_path}/{model_name}.pickle',
                      'wb') as pickle_file:
                pickle.dump(model, pickle_file)

        # Submission
        predicted.to_csv(f'{new_folder_path}/{file_name}', index=False)
        if message is None:
            if model is None:
                message = file_name
            else:
                message = str(model)

        with open(new_folder_path + '/message.txt', 'w') as message_file:
            message_file.write(' '.join(
                [str(submission_id), ' -- ', submission_name, '\n', message]))
            message_file.close()

        # Upload
        if submit:
            print('Uploading submission...')
            command = f'kaggle competitions submit -c {self.compete} -f "{new_folder_path}/{file_name}" -m "{message}"'
            print(command)
            output = os.system(command)
            print('Output: ', output)

        # Open in browser
        if open_in_browser:
            webbrowser.open(
                f'https://www.kaggle.com/c/{self.compete}/submissions', new=2)

    def check_submission(self):
        last_submission = self.kaggle_api.competitions_submissions_list(
            self.compete)[0]
        print('Description: ', last_submission['description'])
        print('Date: ', last_submission['date'])
        print('Status: ', last_submission['status'])
        print('Score: ', last_submission['publicScore'])
Exemple #2
0
class Submission:
    def __init__(self,
                 compete,
                 name,
                 work_dir,
                 description=None,
                 create_readme=False):
        self.id = str(time.time())
        self.compete = compete
        self.name = f'{name} - {self.id}'
        self.description = description

        if work_dir[-1] == '/':
            self.new_folder_path = work_dir + self.name
        else:
            self.new_folder_path = work_dir + '/' + self.name

        self.kaggle_api = KaggleApi()
        self.kaggle_api.authenticate()

        if not os.path.exists(self.new_folder_path):
            os.mkdir(self.new_folder_path)

        self.readme = Readme(self.name,
                             self.description) if create_readme else None

    def save_model(self, model, file_name=None):
        if file_name is None:
            file_name = str(model).replace('\\', '')

        with open(f'{self.new_folder_path}/{file_name}.pickle',
                  'wb') as pickle_file:
            pickle.dump(model, pickle_file)

    def save_keras_model(self,
                         model,
                         file_name=None,
                         save_format='pickle',
                         save_summary_to_readme=True,
                         *args,
                         **kwargs):
        from tensorflow.keras import Model

        if not isinstance(model, Model):
            raise Exception(f'Model should be instance of keras.Model')

        if file_name is None:
            file_name = str(model).replace('\\', '')

        if save_summary_to_readme:
            summary = io.StringIO()
            model.summary(print_fn=lambda s: print(s, file=summary))

            if self.readme is None:
                raise Exception("'create_readme' should be True")

            self.readme.model_summary = summary.getvalue()

        if save_format == 'pickle':
            with open(f'{self.new_folder_path}/{file_name}.pickle',
                      'wb') as pickle_file:
                pickle.dump(model, pickle_file)

        elif save_format == 'config':
            with open(f'{self.new_folder_path}/{file_name}.json',
                      'w') as config_file:
                json.dump(model.get_config(), config_file)

        elif save_format == 'h5':
            model.save(f'{self.new_folder_path}/{file_name}', *args, **kwargs)

        else:
            raise Exception('Undefined save_format')

        return self

    def save_predictions(self,
                         predictions,
                         columns,
                         index,
                         file_name='predictions.csv'):
        pd.DataFrame(dict(zip(columns, [index, predictions]))) \
            .to_csv(f'{self.new_folder_path}/{file_name}', index=False)

        return self

    def open_in_browser(self):
        webbrowser.open(f'https://www.kaggle.com/c/{self.compete}/submissions',
                        new=2)

        return self

    def submit(self, predictions_file_name='predictions.csv'):
        print('Uploading submission...')
        command = f'kaggle competitions submit -c {self.compete} -f "{self.new_folder_path}/{predictions_file_name}" -m "{self.description}"'
        print(command)
        output = os.system(command)
        print()
        print('Output: ', output)

        return self

    def check_results(self, timeout=5):
        time.sleep(timeout)
        last_submission = self.kaggle_api.competitions_submissions_list(
            self.compete)[0]

        if self.readme is not None:
            self.readme.score = last_submission['publicScore']
            self.readme.date = last_submission['date']
            self.readme.status = last_submission['status']

        print('Description: ', last_submission['description'])
        print('Date: ', last_submission['date'])
        print('Status: ', last_submission['status'])
        print('Score: ', last_submission['publicScore'])

        return self

    def save_readme(self):
        readme_file = open(f'{self.new_folder_path}/README.md', 'w')
        readme_file.write(self.readme.markdown())

        return self