Instruction:
Can you generate the documentation for the following Python 3 function
def _make_authorization_header(region,
                               service,
                               canonical_request,
                               credentials,
                               instant):
    """
    Construct an AWS version 4 authorization value for use in an
    C{Authorization} header.

    @param region: The AWS region name (e.g., C{'us-east-1'}).
    @type region: L{str}

    @param service: The AWS service's name (e.g., C{'s3'}).
    @type service: L{str}

    @param canonical_request: The canonical form of the request.
    @type canonical_request: L{_CanonicalRequest} (use
        L{_CanonicalRequest.from_payload_and_headers})

    @param credentials: The AWS credentials.
    @type credentials: L{txaws.credentials.AWSCredentials}

    @param instant: The current UTC date and time
    @type instant: A naive local L{datetime.datetime} (as returned by
        L{datetime.datetime.utcnow})

    @return: A value suitable for use in an C{Authorization} header
    @rtype: L{bytes}
    """
    date_stamp = makeDateStamp(instant)
    amz_date = makeAMZDate(instant)

    scope = _CredentialScope(
        date_stamp=date_stamp,
        region=region,
        service=service
    )

    signable = _SignableAWS4HMAC256Token(
        amz_date,
        scope,
        canonical_request,
    )

    signature = signable.signature(
        getSignatureKey(credentials.secret_key,
                        date_stamp,
                        region,
                        service)
    )

    v4credential = _Credential(
        access_key=credentials.access_key,
        credential_scope=scope,
    )

    return (
        b"%s " % (_SignableAWS4HMAC256Token.ALGORITHM,) +
        b", ".join([
            b"Credential=%s" % (v4credential.serialize(),),
            b"SignedHeaders=%s" % (canonical_request.signed_headers,),
            b"Signature=%s" % (signature,),
        ]))

Response: Construct an AWS version 4 authorization header.


Instruction:
Can you implement a function in Python 3 that
interpolates all missing values using linear interpolation.

Response:
def linear_interpolation(first, last, steps):
    """Interpolates all missing values using linear interpolation.

    :param numeric first:    Start value for the interpolation.
    :param numeric last:    End Value for the interpolation
    :param integer steps:    Number of missing values that have to be calculated.

    :return:    Returns a list of floats containing only the missing values.
    :rtype: list

    :todo:     Define a more general interface!
    """
    result = []

    for step in xrange(0, steps):
        fpart = (steps - step) * first
        lpart = (step + 1)            * last
        value = (fpart + lpart) / float(steps + 1)
        result.append(value)

    return result


Instruction:
Can you write a function in Python 3 where it
functions run by CreateProjectCommand to create the project.

Response:
def upload_project_run(upload_context):
    """
    Function run by CreateProjectCommand to create the project.
    Runs in a background process.
    :param upload_context: UploadContext: contains data service setup and project name to create.
    """
    data_service = upload_context.make_data_service()
    project_name = upload_context.project_name_or_id.get_name_or_raise()
    result = data_service.create_project(project_name, project_name)
    return result.json()['id']


Instruction:
Can you generate a brief explanation for the following Python 3 code
def upload_folder_run(upload_context):
    data_service = upload_context.make_data_service()
    folder_name, parent_kind, parent_remote_id = upload_context.params
    result = data_service.create_folder(folder_name, parent_kind, parent_remote_id)
    return result.json()['id']

Response: Function run by CreateFolderCommand to create the folder.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_small_file(upload_context):
    data_service = upload_context.make_data_service()
    parent_data, path_data, remote_file_id = upload_context.params

    # The small file will fit into one chunk so read into memory and hash it.
    chunk = path_data.read_whole_file()
    hash_data = path_data.get_hash()

    # Talk to data service uploading chunk and creating the file.
    upload_operations = FileUploadOperations(data_service, upload_context)
    upload_id, url_info = upload_operations.create_upload_and_chunk_url(
        upload_context.project_id, path_data, hash_data, storage_provider_id=upload_context.config.storage_provider_id)
    upload_operations.send_file_external(url_info, chunk)
    return upload_operations.finish_upload(upload_id, hash_data, parent_data, remote_file_id)

Response: Function run by CreateSmallFileCommand to create a small file.


Instruction:
Can you generate the documentation for the following Python 3 function
def rebuild_data_service(config, data_service_auth_data):
        auth = DataServiceAuth(config)
        auth.set_auth_data(data_service_auth_data)
        return DataServiceApi(auth, config.url)

Response: Deserialize value into DataServiceApi object.


Instruction:
Make a summary of the following Python 3 code
def run(self, local_project):
        # Walk project adding small items to runner saving large items to large_items
        ProjectWalker.walk_project(local_project, self)
        # Run small items in parallel
        self.runner.run()
        # Run parts of each large item in parallel
        self.upload_large_items()

Response: Uploads a project by uploading folders and small files then uploading the large files.


Instruction:
How would you code a function in Python 3 to
add the file to the large or small task list.

Response:
def visit_file(self, item, parent):
        """
        If file is large add it to the large items to be processed after small task list.
        else file is small add it to the small task list.
        """
        if self.is_large_file(item):
            self.large_items.append((item, parent))
        else:
            self.small_item_task_builder.visit_file(item, parent)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def upload_large_items(self):
        for local_file, parent in self.large_items:
            if local_file.need_to_send:
                self.process_large_file(local_file, parent)

Response: Upload files that were too large.


Instruction:
Write a Python 3 function for
uploading a single file using multiple processes to upload multiple chunks at the same time.

Response:
def process_large_file(self, local_file, parent):
        """
        Upload a single file using multiple processes to upload multiple chunks at the same time.
        Updates local_file with it's remote_id when done.
        :param local_file: LocalFile: file we are uploading
        :param parent: LocalFolder/LocalProject: parent of the file
        """
        file_content_sender = FileUploader(self.settings.config, self.settings.data_service, local_file,
                                           self.settings.watcher, self.settings.file_upload_post_processor)
        remote_id = file_content_sender.upload(self.settings.project_id, parent.kind, parent.remote_id)
        local_file.set_remote_id_after_send(remote_id)


Instruction:
Can you implement a function in Python 3 that
adds create project command to task runner if project doesn t already exist.

Response:
def visit_project(self, item):
        """
        Adds create project command to task runner if project doesn't already exist.
        """
        if not item.remote_id:
            command = CreateProjectCommand(self.settings, item)
            self.task_runner_add(None, item, command)
        else:
            self.settings.project_id = item.remote_id


Instruction:
Can you generate a brief explanation for the following Python 3 code
def visit_folder(self, item, parent):
        if not item.remote_id:
            command = CreateFolderCommand(self.settings, item, parent)
            self.task_runner_add(parent, item, command)

Response: Adds create folder command to task runner if folder doesn t already exist.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def visit_file(self, item, parent):
        if item.need_to_send:
            if item.size > self.settings.config.upload_bytes_per_chunk:
                msg = "Programmer Error: Trying to upload large file as small item size:{} name:{}"
                raise ValueError(msg.format(item.size, item.name))
            else:
                command = CreateSmallFileCommand(self.settings, item, parent,
                                                 self.settings.file_upload_post_processor)
                self.task_runner_add(parent, item, command)

Response: Visit a file and create a command to upload it.


Instruction:
Make a summary of the following Python 3 code
def task_runner_add(self, parent, item, command):
        parent_task_id = self.item_to_id.get(parent)
        task_id = self.task_runner.add(parent_task_id, command)
        self.item_to_id[item] = task_id

Response: Add command to task runner with parent s task id creating a task id for item.


Instruction:
Here you have a function in Python 3, explain what it does
def after_run(self, result_id):
        self.local_project.set_remote_id_after_send(result_id)
        self.settings.project_id = result_id

Response: Save uuid associated with project we just created.


Instruction:
Here you have a function in Python 3, explain what it does
def create_context(self, message_queue, task_id):
        params = (self.remote_folder.name, self.parent.kind, self.parent.remote_id)
        return UploadContext(self.settings, params, message_queue, task_id)

Response: Create values to be used by upload_folder_run function.


Instruction:
How would you explain what the following Python 3 function does
def create_context(self, message_queue, task_id):
        parent_data = ParentData(self.parent.kind, self.parent.remote_id)
        path_data = self.local_file.get_path_data()
        params = parent_data, path_data, self.local_file.remote_id
        return UploadContext(self.settings, params, message_queue, task_id)

Response: Create values to be used by create_small_file function.


Instruction:
How would you explain what the following Python 3 function does
def after_run(self, remote_file_data):
        if self.file_upload_post_processor:
            self.file_upload_post_processor.run(self.settings.data_service, remote_file_data)
        remote_file_id = remote_file_data['id']
        self.settings.watcher.transferring_item(self.local_file)
        self.local_file.set_remote_id_after_send(remote_file_id)

Response: Called after the file is uploaded to the local file store.


Instruction:
Write a Python 3 script to
call when a message is received from create_small_file method.

Response:
def on_message(self, started_waiting):
        """
        Receives started_waiting boolean from create_small_file method and notifies project_status_monitor in settings.
        :param started_waiting: boolean: True when we start waiting, False when done
        """
        watcher = self.settings.watcher
        if started_waiting:
            watcher.start_waiting()
        else:
            watcher.done_waiting()


Instruction:
Make a summary of the following Python 3 code
def _visit_recur(self, item):
        if item.kind == KindType.file_str:
            if item.need_to_send:
                self.add_upload_item(item.path)
        else:
            if item.kind == KindType.project_str:
                pass
            else:
                if not item.remote_id:
                    self.add_upload_item(item.path)
            for child in item.children:
                self._visit_recur(child)

Response: Recursively visits the item and adds it to upload_items if needed.


Instruction:
How would you implement a function in Python 3 that
adds project_name argument to an ArgumentParser parser.

Response:
def add_project_name_arg(arg_parser, required, help_text):
    """
    Adds project_name parameter to a parser.
    :param arg_parser: ArgumentParser parser to add this argument to.
    :param help_text: str label displayed in usage
    """
    arg_parser.add_argument("-p", '--project-name',
                            metavar='ProjectName',
                            type=to_unicode,
                            dest='project_name',
                            help=help_text,
                            required=required)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_project_name_or_id_arg(arg_parser, required=True, help_text_suffix="manage"):
    project_name_or_id = arg_parser.add_mutually_exclusive_group(required=required)
    name_help_text = "Name of the project to {}.".format(help_text_suffix)
    add_project_name_arg(project_name_or_id, required=False, help_text=name_help_text)
    id_help_text = "ID of the project to {}.".format(help_text_suffix)
    add_project_id_arg(project_name_or_id, required=False, help_text=id_help_text)

Response: Adds project name or project id argument. These two are mutually exclusive.


Instruction:
Write a Python 3 function for
raising error if path doesn t exist.

Response:
def _paths_must_exists(path):
    """
    Raises error if path doesn't exist.
    :param path: str path to check
    :return: str same path passed in
    """
    path = to_unicode(path)
    if not os.path.exists(path):
        raise argparse.ArgumentTypeError("{} is not a valid file/folder.".format(path))
    return path


Instruction:
Create a Python 3 function for
validating path for invalid characters.

Response:
def _path_has_ok_chars(path):
    """
    Validate path for invalid characters.
    :param path: str possible filesystem path
    :return: path if it was ok otherwise raises error
    """
    basename = os.path.basename(path)
    if any([bad_char in basename for bad_char in INVALID_PATH_CHARS]):
            raise argparse.ArgumentTypeError("{} contains invalid characters for a directory.".format(path))
    return path


Instruction:
Create a Python 3 function for
adding optional auth_role argument to an ArgumentParser parser.

Response:
def _add_auth_role_arg(arg_parser, default_permissions):
    """
    Adds optional auth_role parameter to a parser.
    :param arg_parser: ArgumentParser parser to add this argument to.
    :param default_permissions: default value to use for this argument
    """
    help_text = "Specifies which project permissions to give to the user. Example: 'project_admin'. "
    help_text += "See command list_auth_roles for AuthRole values."
    arg_parser.add_argument("--auth-role",
                            metavar='AuthRole',
                            type=to_unicode,
                            dest='auth_role',
                            help=help_text,
                            default=default_permissions)


Instruction:
Create a Python 3 function to
add optional auth_role argument to an ArgumentParser parser.

Response:
def _add_project_filter_auth_role_arg(arg_parser):
    """
    Adds optional auth_role filtering parameter to a parser.
    :param arg_parser: ArgumentParser parser to add this argument to.
    """
    help_text = "Filters project listing to just those projects with the specified role. "
    help_text += "See command list_auth_roles for AuthRole values."
    arg_parser.add_argument("--auth-role",
                            metavar='AuthRole',
                            type=to_unicode,
                            dest='auth_role',
                            help=help_text,
                            default=None)


Instruction:
Write a Python 3 function for
adding resend parameter to an ArgumentParser parser.

Response:
def _add_resend_arg(arg_parser, resend_help):
    """
    Adds resend parameter to a parser.
    :param arg_parser: ArgumentParser parser to add this argument to.
    :param type_str
    """
    arg_parser.add_argument("--resend",
                            action='store_true',
                            default=False,
                            dest='resend',
                            help=resend_help)


Instruction:
Can you implement a function in Python 3 that
adds optional repeatable include parameter to an ArgumentParser parser.

Response:
def _add_include_arg(arg_parser):
    """
    Adds optional repeatable include parameter to a parser.
    :param arg_parser: ArgumentParser parser to add this argument to.
    """
    arg_parser.add_argument("--include",
                            metavar='Path',
                            action='append',
                            type=to_unicode,
                            dest='include_paths',
                            help="Specifies a single path to include. This argument can be repeated.",
                            default=[])


Instruction:
Write a Python 3 script for
adding a message file argument to the argument parser.

Response:
def _add_message_file(arg_parser, help_text):
    """
    Add mesage file argument with help_text to arg_parser.
    :param arg_parser: ArgumentParser parser to add this argument to.
    :param help_text: str: help text for this argument
    """
    arg_parser.add_argument('--msg-file',
                            type=argparse.FileType('r'),
                            help=help_text)


Instruction:
Implement a function in Python 3 to
register the command line for the upload command

Response:
def register_upload_command(self, upload_func):
        """
        Add the upload command to the parser and call upload_func(project_name, folders, follow_symlinks) when chosen.
        :param upload_func: func Called when this option is chosen: upload_func(project_name, folders, follow_symlinks).
        """
        description = "Uploads local files and folders to a remote host."
        upload_parser = self.subparsers.add_parser('upload', description=description)
        _add_dry_run(upload_parser, help_text="Instead of uploading displays a list of folders/files that "
                                              "need to be uploaded.")
        add_project_name_or_id_arg(upload_parser, help_text_suffix="upload files/folders to.")
        _add_folders_positional_arg(upload_parser)
        _add_follow_symlinks_arg(upload_parser)
        upload_parser.set_defaults(func=upload_func)


Instruction:
Can you create a Python 3 function that
registers the add - user command to the parser and call add_user_func when chosen.

Response:
def register_add_user_command(self, add_user_func):
        """
        Add the add-user command to the parser and call add_user_func(project_name, user_full_name, auth_role)
        when chosen.
        :param add_user_func: func Called when this option is chosen: upload_func(project_name, user_full_name, auth_role).
        """
        description = "Gives user permission to access a remote project."
        add_user_parser = self.subparsers.add_parser('add-user', description=description)
        add_project_name_or_id_arg(add_user_parser, help_text_suffix="add a user to")
        user_or_email = add_user_parser.add_mutually_exclusive_group(required=True)
        add_user_arg(user_or_email)
        add_email_arg(user_or_email)
        _add_auth_role_arg(add_user_parser, default_permissions='project_admin')
        add_user_parser.set_defaults(func=add_user_func)


Instruction:
Implement a function in Python 3 to
register a remove - user command to the parser and call remove_user_func when chosen.

Response:
def register_remove_user_command(self, remove_user_func):
        """
        Add the remove-user command to the parser and call remove_user_func(project_name, user_full_name) when chosen.
        :param remove_user_func: func Called when this option is chosen: remove_user_func(project_name, user_full_name).
        """
        description = "Removes user permission to access a remote project."
        remove_user_parser = self.subparsers.add_parser('remove-user', description=description)
        add_project_name_or_id_arg(remove_user_parser, help_text_suffix="remove a user from")
        user_or_email = remove_user_parser.add_mutually_exclusive_group(required=True)
        add_user_arg(user_or_email)
        add_email_arg(user_or_email)
        remove_user_parser.set_defaults(func=remove_user_func)


Instruction:
Can you tell what is the following Python 3 function doing
def register_download_command(self, download_func):
        description = "Download the contents of a remote remote project to a local folder."
        download_parser = self.subparsers.add_parser('download', description=description)
        add_project_name_or_id_arg(download_parser, help_text_suffix="download")
        _add_folder_positional_arg(download_parser)
        include_or_exclude = download_parser.add_mutually_exclusive_group(required=False)
        _add_include_arg(include_or_exclude)
        _add_exclude_arg(include_or_exclude)
        download_parser.set_defaults(func=download_func)

Response: Register a command to download a remote project to a local folder.


Instruction:
How would you explain what the following Python 3 function does
def register_share_command(self, share_func):
        description = "Share a project with another user with specified  permissions. " \
                      "Sends the other user an email message via D4S2 service. " \
                      "If not specified this command gives user download permissions."
        share_parser = self.subparsers.add_parser('share', description=description)
        add_project_name_or_id_arg(share_parser)
        user_or_email = share_parser.add_mutually_exclusive_group(required=True)
        add_user_arg(user_or_email)
        add_email_arg(user_or_email)
        _add_auth_role_arg(share_parser, default_permissions='file_downloader')
        _add_resend_arg(share_parser, "Resend share")
        _add_message_file(share_parser, "Filename containing a message to be sent with the share. "
                                        "Pass - to read from stdin.")
        share_parser.set_defaults(func=share_func)

Response: Register a command to add view only project permissions and sending email via another service.


Instruction:
Create a Python 3 function to
register the deliver command to the parser

Response:
def register_deliver_command(self, deliver_func):
        """
        Add 'deliver' command for transferring a project to another user.,
        :param deliver_func: function to run when user choses this option
        """
        description = "Initiate delivery of a project to another user. Removes other user's current permissions. " \
                      "Send message to D4S2 service to send email and allow access to the project once user " \
                      "acknowledges receiving the data."
        deliver_parser = self.subparsers.add_parser('deliver', description=description)
        add_project_name_or_id_arg(deliver_parser)
        user_or_email = deliver_parser.add_mutually_exclusive_group(required=True)
        add_user_arg(user_or_email)
        add_email_arg(user_or_email)
        add_share_usernames_arg(deliver_parser)
        add_share_emails_arg(deliver_parser)
        _add_copy_project_arg(deliver_parser)
        _add_resend_arg(deliver_parser, "Resend delivery")
        include_or_exclude = deliver_parser.add_mutually_exclusive_group(required=False)
        _add_include_arg(include_or_exclude)
        _add_exclude_arg(include_or_exclude)
        _add_message_file(deliver_parser, "Filename containing a message to be sent with the delivery. "
                                          "Pass - to read from stdin.")
        deliver_parser.set_defaults(func=deliver_func)


Instruction:
Explain what the following Python 3 code does
def register_list_command(self, list_func):
        description = "Show a list of project names or folders/files of a single project."
        list_parser = self.subparsers.add_parser('list', description=description)
        project_name_or_auth_role = list_parser.add_mutually_exclusive_group(required=False)
        _add_project_filter_auth_role_arg(project_name_or_auth_role)
        add_project_name_or_id_arg(project_name_or_auth_role, required=False,
                                   help_text_suffix="show details for")
        _add_long_format_option(list_parser, 'Display long format.')
        list_parser.set_defaults(func=list_func)

Response: Register a command to get a list of projects or folders or files of a single project.


Instruction:
Can you create a Python 3 function that
registers a delete command to the remote store.

Response:
def register_delete_command(self, delete_func):
        """
        Add 'delete' command delete a project from the remote store.
        :param delete_func: function: run when user choses this option.
        """
        description = "Permanently delete a project."
        delete_parser = self.subparsers.add_parser('delete', description=description)
        add_project_name_or_id_arg(delete_parser, help_text_suffix="delete")
        _add_force_arg(delete_parser, "Do not prompt before deleting.")
        delete_parser.set_defaults(func=delete_func)


Instruction:
Create a Python 3 function to
register a command to list project authorization roles that can be used with add_user command.

Response:
def register_list_auth_roles_command(self, list_auth_roles_func):
        """
        Add 'list_auth_roles' command to list project authorization roles that can be used with add_user.
        :param list_auth_roles_func: function: run when user choses this option.
        """
        description = "List authorization roles for use with add_user command."
        list_auth_roles_parser = self.subparsers.add_parser('list-auth-roles', description=description)
        list_auth_roles_parser.set_defaults(func=list_auth_roles_func)


Instruction:
Make a summary of the following Python 3 code
def run_command(self, args):
        parsed_args = self.parser.parse_args(args)
        if hasattr(parsed_args, 'func'):
            parsed_args.func(parsed_args)
        else:
            self.parser.print_help()

Response: Parse command line arguments and run function registered for the appropriate command.


Instruction:
Make a summary of the following Python 3 code
def filter_signal(self, data_frame, ts='mag_sum_acc'):
        b, a = signal.butter(self.filter_order, 2*self.cutoff_frequency/self.sampling_frequency,'high', analog=False)
        filtered_signal = signal.lfilter(b, a, data_frame[ts].values)
        data_frame['filtered_signal'] = filtered_signal

        logging.debug("filter signal")
        return data_frame

Response: This method filters a data frame by a given time series name.


Instruction:
Explain what the following Python 3 code does
def amplitude_by_welch(self, data_frame):
        frq, Pxx_den = signal.welch(data_frame.filtered_signal.values, self.sampling_frequency, nperseg=self.window)
        freq = frq[Pxx_den.argmax(axis=0)]
        ampl = sum(Pxx_den[(frq > self.lower_frequency) & (frq < self.upper_frequency)])

        logging.debug("tremor amplitude by welch calculated")

        return ampl, freq

Response: This method calculates the power spectral density of the current object in the specified data frame using the Welch method.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def approximate_entropy(self, x, m=None, r=None):
        if m is None or r is None:
            m = 2
            r = 0.3
        entropy = feature_calculators.approximate_entropy(x, m, r)
        logging.debug("approximate entropy by tsfresh calculated")
        return entropy

Response: Calculates the approximate entropy of a time series.


Instruction:
Explain what the following Python 3 code does
def autocorrelation(self, x, lag):
        # This is important: If a series is passed, the product below is calculated
        # based on the index, which corresponds to squaring the series.
        if lag is None:
            lag = 0
        _autoc = feature_calculators.autocorrelation(x, lag)
        logging.debug("autocorrelation by tsfresh calculated")
        return _autoc

Response: Calculates the autocorrelation of the specified lag of the time series x.


Instruction:
Here you have a function in Python 3, explain what it does
def partial_autocorrelation(self, x, param=None):
        if param is None:
            param = [{'lag': 3}, {'lag': 5}, {'lag': 6}]
        _partialc = feature_calculators.partial_autocorrelation(x, param)
        logging.debug("partial autocorrelation by tsfresh calculated")
        return _partialc

Response: This method calculates the partial autocorrelation function at the given lag.


Instruction:
Can you tell what is the following Python 3 function doing
def ratio_value_number_to_time_series_length(self, x):
        ratio = feature_calculators.ratio_value_number_to_time_series_length(x)
        logging.debug("ratio value number to time series length by tsfresh calculated")
        return ratio

Response: ratio_value_number_to_time_series_length - Calculates the ratio of the value of the feature of the given time series


Instruction:
How would you code a function in Python 3 to
calculate the change quantiles of the time series x.

Response:
def change_quantiles(self, x, ql=None, qh=None, isabs=None, f_agg=None):
        """
            As in tsfresh `change_quantiles <https://github.com/blue-yonder/tsfresh/blob/master/tsfresh/\
            feature_extraction/feature_calculators.py#L1248>`_

            First fixes a corridor given by the quantiles ql and qh of the distribution of x. Then calculates the \
            average, absolute value of consecutive changes of the series x inside this corridor. Think about selecting \
            a corridor on the y-Axis and only calculating the mean of the absolute change of the time series inside \
            this corridor.

            :param x: the time series to calculate the feature of
            :type x: pandas.Series
            :param ql: the lower quantile of the corridor
            :type ql: float
            :param qh: the higher quantile of the corridor
            :type qh: float
            :param isabs: should the absolute differences be taken?
            :type isabs: bool
            :param f_agg: the aggregator function that is applied to the differences in the bin
            :type f_agg: str, name of a numpy function (e.g. mean, var, std, median)
            :return: the value of this feature
            :rtype: float
        """
        if ql is None or qh is None or isabs is None or f_agg is None:
            f_agg = 'mean'
            isabs = True
            qh = 0.2
            ql = 0.0
        quantile = feature_calculators.change_quantiles(x, ql, qh, isabs, f_agg)
        logging.debug("change_quantiles by tsfresh calculated")
        return quantile


Instruction:
Implement a function in Python 3 to
calculate the number of peaks of at least support n in the time series x.

Response:
def number_peaks(self, x, n=None):
        """
            As in tsfresh `number_peaks <https://github.com/blue-yonder/tsfresh/blob/master/tsfresh/feature_extraction/\
            feature_calculators.py#L1003>`_

            Calculates the number of peaks of at least support n in the time series x. A peak of support n is defined \
            as a subsequence of x where a value occurs, which is bigger than its n neighbours to the left and to the right.

            Hence in the sequence

            >>> x = [3, 0, 0, 4, 0, 0, 13]

            4 is a peak of support 1 and 2 because in the subsequences

            >>> [0, 4, 0]
            >>> [0, 0, 4, 0, 0]

            4 is still the highest value. Here, 4 is not a peak of support 3 because 13 is the 3th neighbour to the \
            right of 4 and its bigger than 4.

            :param x: the time series to calculate the feature of
            :type x: pandas.Series
            :param n: the support of the peak
            :type n: int
            :return: the value of this feature
            :rtype: float
            """
        if n is None:
            n = 5
        peaks = feature_calculators.number_peaks(x, n)
        logging.debug("agg linear trend by tsfresh calculated")
        return peaks


Instruction:
Can you tell what is the following Python 3 function doing
def percentage_of_reoccurring_datapoints_to_all_datapoints(self, x):
        _perc = feature_calculators.percentage_of_reoccurring_datapoints_to_all_datapoints(x)
        logging.debug("percentage of reoccurring datapoints to all datapoints by tsfresh calculated")
        return _perc

Response: Calculates the percentage of reoccurring datapoints to all datapoints.


Instruction:
Implement a Python 3 function for
calculating the absolute energy of the time series x

Response:
def abs_energy(self, x):
        """
        As in tsfresh `abs_energy <https://github.com/blue-yonder/tsfresh/blob/master/tsfresh/feature_extraction/\
        feature_calculators.py#L390>`_ \
        Returns the absolute energy of the time series which is the sum over the squared values\

        .. math::

            E=\\sum_{i=1,\ldots, n}x_i^2

        :param x: the time series to calculate the feature of
        :type x: pandas.Series
        :return: the value of this feature
        :rtype: float
        """
        _energy = feature_calculators.abs_energy(x)
        logging.debug("abs energy by tsfresh calculated")
        return _energy


Instruction:
Given the following Python 3 function, write the documentation
def fft_coefficient(self, x, param=None):
        if param is None:
            param = [{'attr': 'abs', 'coeff': 44}, {'attr': 'abs', 'coeff': 63}, {'attr': 'abs', 'coeff': 0},
                     {'attr': 'real', 'coeff': 0}, {'attr': 'real', 'coeff': 23}]
        _fft_coef = feature_calculators.fft_coefficient(x, param)
        logging.debug("fft coefficient by tsfresh calculated")
        return list(_fft_coef)

Response: Calculates the fourier coefficients of the real input by fast Fourier Transform.


Instruction:
Make a summary of the following Python 3 code
def dc_remove_signal(self, data_frame):

        mean_signal = np.mean(data_frame.mag_sum_acc)
        data_frame['dc_mag_sum_acc'] = data_frame.mag_sum_acc - mean_signal
        logging.debug("dc remove signal")
        return data_frame

Response: Removes the dc component of the signal as per Kassavetis2015


Instruction:
Can you generate the documentation for the following Python 3 function
def main(argv, reactor=None):
    if reactor is None:
        from twisted.internet import gtk2reactor
        gtk2reactor.install()
        from twisted.internet import reactor
    try:
        AWSStatusIndicator(reactor)
        gobject.set_application_name("aws-status")
        reactor.run()
    except ValueError:
        # In this case, the user cancelled, and the exception bubbled to here.
        pass

Response: Run the client GUI.


Instruction:
How would you implement a function in Python 3 that
parses command line arguments and returns a dictionary of key - value pairs.

Response:
def parse_options(arguments):
    """Parse command line arguments.

    The parsing logic is fairly simple.  It can only parse long-style
    parameters of the form::

      --key value

    Several parameters can be defined in the environment and will be used
    unless explicitly overridden with command-line arguments.  The access key,
    secret and endpoint values will be loaded from C{AWS_ACCESS_KEY_ID},
    C{AWS_SECRET_ACCESS_KEY} and C{AWS_ENDPOINT} environment variables.

    @param arguments: A list of command-line arguments.  The first item is
        expected to be the name of the program being run.
    @raises OptionError: Raised if incorrectly formed command-line arguments
        are specified, or if required command-line arguments are not present.
    @raises UsageError: Raised if C{--help} is present in command-line
        arguments.
    @return: A C{dict} with key/value pairs extracted from the argument list.
    """
    arguments = arguments[1:]
    options = {}
    while arguments:
        key = arguments.pop(0)
        if key in ("-h", "--help"):
            raise UsageError("Help requested.")
        if key.startswith("--"):
            key = key[2:]
            try:
                value = arguments.pop(0)
            except IndexError:
                raise OptionError("'--%s' is missing a value." % key)
            options[key] = value
        else:
            raise OptionError("Encountered unexpected value '%s'." % key)

    default_key = os.environ.get("AWS_ACCESS_KEY_ID")
    if "key" not in options and default_key:
        options["key"] = default_key
    default_secret = os.environ.get("AWS_SECRET_ACCESS_KEY")
    if "secret" not in options and default_secret:
        options["secret"] = default_secret
    default_endpoint = os.environ.get("AWS_ENDPOINT")
    if "endpoint" not in options and default_endpoint:
        options["endpoint"] = default_endpoint
    for name in ("key", "secret", "endpoint", "action"):
        if name not in options:
            raise OptionError(
                "The '--%s' command-line argument is required." % name)

    return options


Instruction:
Write a Python 3 script for
parsing the command - line arguments and configure a Command instance.

Response:
def get_command(arguments, output=None):
    """Parse C{arguments} and configure a L{Command} instance.

    An access key, secret key, endpoint and action are required.  Additional
    parameters included with the request are passed as parameters to the
    method call.  For example, the following command will create a L{Command}
    object that can invoke the C{DescribeRegions} method with the optional
    C{RegionName.0} parameter included in the request::

      txaws-discover --key KEY --secret SECRET --endpoint URL \
                     --action DescribeRegions --RegionName.0 us-west-1

    @param arguments: The command-line arguments to parse.
    @raises OptionError: Raised if C{arguments} can't be used to create a
        L{Command} object.
    @return: A L{Command} instance configured to make an EC2 API method call.
    """
    options = parse_options(arguments)
    key = options.pop("key")
    secret = options.pop("secret")
    endpoint = options.pop("endpoint")
    action = options.pop("action")
    return Command(key, secret, endpoint, action, options, output)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def execute(self, timeSeries):
        # determine the number of values to forecast, if necessary
        self._calculate_values_to_forecast(timeSeries)

        # extract the required parameters, performance improvement
        alpha            = self._parameters["smoothingFactor"]
        valuesToForecast = self._parameters["valuesToForecast"]

        # initialize some variables
        resultList  = []
        estimator   = None
        lastT       = None

        # "It's always about performance!"
        append = resultList.append

        # smooth the existing TimeSeries data
        for idx in xrange(len(timeSeries)):
            # get the current to increase performance
            t = timeSeries[idx]

            # get the initial estimate
            if estimator is None:
                estimator = t[1]
                continue

            # add the first value to the resultList without any correction
            if 0 == len(resultList):
                append([t[0], estimator])
                lastT = t
                continue

            # calculate the error made during the last estimation
            error = lastT[1] - estimator

            # calculate the new estimator, based on the last occured value, the error and the smoothingFactor
            estimator = estimator + alpha * error

            # save the current value for the next iteration
            lastT = t

            # add an entry to the result
            append([t[0], estimator])

        # forecast additional values if requested
        if valuesToForecast > 0:
            currentTime        = resultList[-1][0]
            normalizedTimeDiff = currentTime - resultList[-2][0]

            for idx in xrange(valuesToForecast):
                currentTime += normalizedTimeDiff

                # reuse everything
                error     = lastT[1] - estimator
                estimator = estimator + alpha * error

                # add a forecasted value
                append([currentTime, estimator])

                # set variables for next iteration
                lastT         = resultList[-1]

        # return a TimeSeries, containing the result
        return TimeSeries.from_twodim_list(resultList)

Response: Executes the forecasting process.


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_parameter_intervals(self):
        parameterIntervals = {}

        parameterIntervals["smoothingFactor"]      = [0.0, 1.0, False, False]
        parameterIntervals["trendSmoothingFactor"] = [0.0, 1.0, False, False]

        return parameterIntervals

Response: Returns the intervals for the methods parameter.


Instruction:
Make a summary of the following Python 3 code
def execute(self, timeSeries):
        # determine the number of values to forecast, if necessary
        self._calculate_values_to_forecast(timeSeries)

        # extract the required parameters, performance improvement
        alpha            = self._parameters["smoothingFactor"]
        beta             = self._parameters["trendSmoothingFactor"]

        # initialize some variables
        resultList  = []
        estimator   = None
        trend       = None
        lastT       = None

        # "It's always about performance!"
        append = resultList.append

        # smooth the existing TimeSeries data
        for idx in xrange(len(timeSeries)):
            # get the current to increase performance
            t = timeSeries[idx]

            # get the initial estimate
            if estimator is None:
                estimator = t[1]
                lastT     = t
                continue

            # add the first value to the resultList without any correction
            if 0 == len(resultList):
                append([t[0], estimator])
                trend         = t[1] - lastT[1]

                # store current values for next iteration
                lastT         = t
                lastEstimator = estimator
                continue

            # calculate the new estimator and trend, based on the last occured value, the error and the smoothingFactor
            estimator = alpha * t[1] + (1 - alpha) * (estimator + trend)
            trend     = beta * (estimator - lastEstimator) + (1 - beta) * trend

            # add an entry to the result
            append([t[0], estimator])

            # store current values for next iteration
            lastT         = t
            lastEstimator = estimator

        # forecast additional values if requested
        if self._parameters["valuesToForecast"] > 0:
            currentTime        = resultList[-1][0]
            normalizedTimeDiff = currentTime - resultList[-2][0]

            for idx in xrange(1, self._parameters["valuesToForecast"] + 1):
                currentTime += normalizedTimeDiff

                # reuse everything
                forecast = estimator + idx * trend

                # add a forecasted value
                append([currentTime, forecast])

        # return a TimeSeries, containing the result
        return TimeSeries.from_twodim_list(resultList)

Response: Executes the forecasting process.


Instruction:
Given the following Python 3 function, write the documentation
def execute(self, timeSeries):
        # determine the number of values to forecast, if necessary
        self._calculate_values_to_forecast(timeSeries)

        seasonLength = self.get_parameter("seasonLength")
        if len(timeSeries) < seasonLength:
            raise ValueError("The time series must contain at least one full season.")

        alpha = self.get_parameter("smoothingFactor")
        beta = self.get_parameter("trendSmoothingFactor")
        gamma = self.get_parameter("seasonSmoothingFactor")

        seasonValues = self.initSeasonFactors(timeSeries)
        resultList = []
        lastEstimator = 0

        for idx in xrange(len(timeSeries)):
            t = timeSeries[idx][0]
            x_t = timeSeries[idx][1]
            if idx == 0:
                lastTrend = self.initialTrendSmoothingFactors(timeSeries)
                lastEstimator = x_t
                resultList.append([t, x_t])
                continue

            lastSeasonValue = seasonValues[idx % seasonLength]

            estimator = alpha * x_t/lastSeasonValue + (1 - alpha) * (lastEstimator + lastTrend)
            lastTrend = beta * (estimator - lastEstimator) + (1 - beta) * lastTrend
            seasonValues[idx % seasonLength] = gamma * x_t/estimator + (1 - gamma) * lastSeasonValue

            lastEstimator = estimator
            resultList.append([t, estimator])

        resultList += self._calculate_forecast(timeSeries, resultList, seasonValues, [lastEstimator, lastSeasonValue, lastTrend])
        return TimeSeries.from_twodim_list(resultList)

Response: This method calculates the exponentially smoothed values of the given time series.


Instruction:
Explain what the following Python 3 code does
def _calculate_forecast(self, originalTimeSeries, smoothedData, seasonValues, lastSmoothingParams):
        forecastResults = []

        lastEstimator, lastSeasonValue, lastTrend = lastSmoothingParams

        seasonLength = self.get_parameter("seasonLength")

        #Forecasting. Determine the time difference between two points for extrapolation
        currentTime        = smoothedData[-1][0]
        normalizedTimeDiff = currentTime - smoothedData[-2][0]

        for m in xrange(1, self._parameters["valuesToForecast"] + 1):
            currentTime += normalizedTimeDiff
            lastSeasonValue = seasonValues[(len(originalTimeSeries) + m - 2) % seasonLength]
            forecast = (lastEstimator + m * lastTrend) * lastSeasonValue
            forecastResults.append([currentTime, forecast])

        return forecastResults

Response: Calculates the actual forecast based on the input data.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def initSeasonFactors(self, timeSeries):

        seasonLength = self.get_parameter("seasonLength")
        try:
            seasonValues = self.get_parameter("seasonValues")
            assert seasonLength == len(seasonValues), "Preset Season Values have to have to be of season's length"
            return seasonValues
        except KeyError:
            pass

        seasonValues = []
        completeCycles = len(timeSeries) / seasonLength
        A = {} #cache values for A_j

        for i in xrange(seasonLength):
            c_i = 0
            for j in xrange(completeCycles):
                if j not in A:
                    A[j] = self.computeA(j, timeSeries)
                c_i += timeSeries[(seasonLength * j) + i][1] / A[j] #wikipedia suggests j-1, but we worked with indices in the first place
            c_i /= completeCycles
            seasonValues.append(c_i)
        return seasonValues

Response: Computes the initial season smoothing factors.


Instruction:
Write a Python 3 script for
calculating the initial trend smoothing factor b0.

Response:
def initialTrendSmoothingFactors(self, timeSeries):
        """ Calculate the initial Trend smoothing Factor b0.

        Explanation:
            http://en.wikipedia.org/wiki/Exponential_smoothing#Triple_exponential_smoothing

        :return:   Returns the initial trend smoothing factor b0
        """

        result = 0.0
        seasonLength = self.get_parameter("seasonLength")
        k = min(len(timeSeries) - seasonLength, seasonLength) #In case of only one full season, use average trend of the months that we have twice
        for i in xrange(0, k):
            result += (timeSeries[seasonLength + i][1] - timeSeries[i][1]) / seasonLength
        return result / k


Instruction:
Can you implement a function in Python 3 that
calculates A_j. Aj is the average value of x in the jth cycle of your data :return: A_j :rtype: numeric

Response:
def computeA(self, j, timeSeries):
        """ Calculates A_j. Aj is the average value of x in the jth cycle of your data

        :return:    A_j
        :rtype:     numeric
        """
        seasonLength = self.get_parameter("seasonLength")
        A_j = 0
        for i in range(seasonLength):
            A_j += timeSeries[(seasonLength * (j)) + i][1]
        return A_j / seasonLength


Instruction:
Make a summary of the following Python 3 code
def _split_quoted(text):
    quoted = False
    escaped = False
    result = []
    for i, ch in enumerate(text):
        if escaped:
            escaped = False
            result.append(ch)
        elif ch == u'\\':
            escaped = True
        elif ch == u'"':
            quoted = not quoted
        elif not quoted and ch == u' ':
            return u"".join(result), text[i:].lstrip()
        else:
            result.append(ch)
    return u"".join(result), u""

Response: Splits a unicode string on SPACE characters and escapes them with QUOTATION MARKs.


Instruction:
Given the following Python 3 function, write the documentation
def freeze_of_gait(self, x):

        data = self.resample_signal(x).values

        f_res = self.sampling_frequency / self.window

        f_nr_LBs = int(self.loco_band[0] / f_res)
        f_nr_LBe = int(self.loco_band[1] / f_res)
        f_nr_FBs = int(self.freeze_band[0] / f_res)
        f_nr_FBe = int(self.freeze_band[1] / f_res)

        jPos = self.window + 1
        i = 0

        time = []
        sumLocoFreeze = []
        freezeIndex = []

        while jPos < len(data):

            jStart = jPos - self.window
            time.append(jPos)

            y = data[int(jStart):int(jPos)]
            y = y - np.mean(y)

            Y = np.fft.fft(y, int(self.window))
            Pyy = abs(Y*Y) / self.window

            areaLocoBand = numerical_integration( Pyy[f_nr_LBs-1 : f_nr_LBe], self.sampling_frequency )
            areaFreezeBand = numerical_integration( Pyy[f_nr_FBs-1 : f_nr_FBe], self.sampling_frequency )

            sumLocoFreeze.append(areaFreezeBand + areaLocoBand)

            freezeIndex.append(areaFreezeBand / areaLocoBand)

            jPos = jPos + self.step_size
            i = i + 1

        freeze_time = np.asarray(time, dtype=np.int32)
        freeze_index = np.asarray(freezeIndex, dtype=np.float32)
        locomotor_freeze_index = np.asarray(sumLocoFreeze, dtype=np.float32)

        return freeze_time, freeze_index, locomotor_freeze_index

Response: This method assesses freeze of gait on.


Instruction:
How would you explain what the following Python 3 function does
def speed_of_gait(self, x, wavelet_type='db3', wavelet_level=6):

        coeffs = wavedec(x.values, wavelet=wavelet_type, level=wavelet_level)

        energy = [sum(coeffs[wavelet_level - i]**2) / len(coeffs[wavelet_level - i]) for i in range(wavelet_level)]

        WEd1 = energy[0] / (5 * np.sqrt(2))
        WEd2 = energy[1] / (4 * np.sqrt(2))
        WEd3 = energy[2] / (3 * np.sqrt(2))
        WEd4 = energy[3] / (2 * np.sqrt(2))
        WEd5 = energy[4] / np.sqrt(2)
        WEd6 = energy[5] / np.sqrt(2)

        gait_speed = 0.5 * np.sqrt(WEd1+(WEd2/2)+(WEd3/3)+(WEd4/4)+(WEd5/5)+(WEd6/6))

        return gait_speed

Response: This method assess the speed of gait on the time series x.


Instruction:
Explain what the following Python 3 code does
def walk_direction_preheel(self, data_frame):

        # Sum of absolute values across accelerometer axes:
        data = data_frame.x.abs() + data_frame.y.abs() + data_frame.z.abs()

        # Find maximum peaks of smoothed data:
        dummy, ipeaks_smooth = self.heel_strikes(data)
        data = data.values

        # Compute number of samples between peaks using the real part of the FFT:
        interpeak = compute_interpeak(data, self.sampling_frequency)
        decel = np.int(np.round(self.stride_fraction * interpeak))

        # Find maximum peaks close to maximum peaks of smoothed data:
        ipeaks = []
        for ipeak_smooth in ipeaks_smooth:
            ipeak = np.argmax(data[ipeak_smooth - decel:ipeak_smooth + decel])
            ipeak += ipeak_smooth - decel
            ipeaks.append(ipeak)

        # Compute the average vector for each deceleration phase:
        vectors = []
        for ipeak in ipeaks:
            decel_vectors = np.asarray([[data_frame.x[i], data_frame.y[i], data_frame.z[i]]
                                        for i in range(ipeak - decel, ipeak)])
            vectors.append(np.mean(decel_vectors, axis=0))

        # Compute the average deceleration vector and take the opposite direction:
        direction = -1 * np.mean(vectors, axis=0)

        # Return the unit vector in this direction:
        direction /= np.sqrt(direction.dot(direction))

        return direction

Response: Finding Walking Direction Leveraging Smartphone s Inertial Sensors


Instruction:
Write a Python 3 function for
estimating the heel strike times between sign changes in accelerometer data.

Response:
def heel_strikes(self, x):
        """ Estimate heel strike times between sign changes in accelerometer data.

            :param x: The time series to assess freeze of gait on. This could be x, y, z or mag_sum_acc.
            :type x: pandas.Series
            :return strikes: Heel strike timings measured in seconds.
            :rtype striles: numpy.ndarray
            :return strikes_idx: Heel strike timing indices of the time series.
            :rtype strikes_idx: numpy.ndarray
        """

        # Demean data:
        data = x.values
        data -= data.mean()

        # TODO: fix this
        # Low-pass filter the AP accelerometer data by the 4th order zero lag
        # Butterworth filter whose cut frequency is set to 5 Hz:
        filtered = butter_lowpass_filter(data, self.sampling_frequency, self.cutoff_frequency, self.filter_order)

        # Find transitional positions where AP accelerometer changes from
        # positive to negative.
        transitions = crossings_nonzero_pos2neg(filtered)

        # Find the peaks of AP acceleration preceding the transitional positions,
        # and greater than the product of a threshold and the maximum value of
        # the AP acceleration:
        strike_indices_smooth = []
        filter_threshold = np.abs(self.delta * np.max(filtered))
        for i in range(1, np.size(transitions)):
            segment = range(transitions[i-1], transitions[i])
            imax = np.argmax(filtered[segment])
            if filtered[segment[imax]] > filter_threshold:
                strike_indices_smooth.append(segment[imax])

        # Compute number of samples between peaks using the real part of the FFT:
        interpeak = compute_interpeak(data, self.sampling_frequency)
        decel = np.int(interpeak / 2)

        # Find maximum peaks close to maximum peaks of smoothed data:
        strikes_idx = []
        for ismooth in strike_indices_smooth:
            istrike = np.argmax(data[ismooth - decel:ismooth + decel])
            istrike = istrike + ismooth - decel
            strikes_idx.append(istrike)

        strikes = np.asarray(strikes_idx)
        strikes -= strikes[0]
        strikes = strikes / self.sampling_frequency

        return strikes, np.array(strikes_idx)


Instruction:
Make a summary of the following Python 3 code
def gait_regularity_symmetry(self, x, average_step_duration='autodetect', average_stride_duration='autodetect', unbias=1, normalize=2):
        if (average_step_duration=='autodetect') or (average_stride_duration=='autodetect'):

            strikes, _ = self.heel_strikes(x)

            step_durations = []
            for i in range(1, np.size(strikes)):
                step_durations.append(strikes[i] - strikes[i-1])

            average_step_duration = np.mean(step_durations)

            number_of_steps = np.size(strikes)

            strides1 = strikes[0::2]
            strides2 = strikes[1::2]

            stride_durations1 = []
            for i in range(1, np.size(strides1)):
                stride_durations1.append(strides1[i] - strides1[i-1])

            stride_durations2 = []
            for i in range(1, np.size(strides2)):
                stride_durations2.append(strides2[i] - strides2[i-1])

            strides = [strides1, strides2]
            stride_durations = [stride_durations1, stride_durations2]

            average_stride_duration = np.mean((np.mean(stride_durations1),
                                        np.mean(stride_durations2)))

            return self.gait_regularity_symmetry(x, average_step_duration, average_stride_duration)

        else:
            coefficients, _ = autocorrelate(x, unbias=1, normalize=2)

            step_period = np.int(np.round(1 / average_step_duration))
            stride_period = np.int(np.round(1 / average_stride_duration))

            step_regularity = coefficients[step_period]
            stride_regularity = coefficients[stride_period]
            symmetry = np.abs(stride_regularity - step_regularity)

            return step_regularity, stride_regularity, symmetry

Response: Compute step and stride regularity and symmetry from accelerometer data.


Instruction:
Can you generate the documentation for the following Python 3 function
def gait(self, x):

        data = x

        strikes, _ = self.heel_strikes(data)

        step_durations = []
        for i in range(1, np.size(strikes)):
            step_durations.append(strikes[i] - strikes[i-1])

        avg_step_duration = np.mean(step_durations)
        sd_step_durations = np.std(step_durations)

        number_of_steps = np.size(strikes)

        strides1 = strikes[0::2]
        strides2 = strikes[1::2]

        stride_durations1 = []
        for i in range(1, np.size(strides1)):
            stride_durations1.append(strides1[i] - strides1[i-1])

        stride_durations2 = []
        for i in range(1, np.size(strides2)):
            stride_durations2.append(strides2[i] - strides2[i-1])

        strides = [strides1, strides2]
        stride_durations = [stride_durations1, stride_durations2]

        avg_number_of_strides = np.mean([np.size(strides1), np.size(strides2)])
        avg_stride_duration = np.mean((np.mean(stride_durations1),
                                    np.mean(stride_durations2)))
        sd_stride_durations = np.mean((np.std(stride_durations1),
                                    np.std(stride_durations2)))

        step_period = np.int(np.round(1 / avg_step_duration))
        stride_period = np.int(np.round(1 / avg_stride_duration))

        step_regularity, stride_regularity, symmetry = self.gait_regularity_symmetry(data,
                                                                                average_step_duration=avg_step_duration,
                                                                                average_stride_duration=avg_stride_duration)

        cadence = None
        if self.duration:
            cadence = number_of_steps / self.duration

        velocity = None
        avg_step_length = None
        avg_stride_length = None

        if self.distance:
            velocity = self.distance / self.duration
            avg_step_length = number_of_steps / self.distance
            avg_stride_length = avg_number_of_strides / self.distance

        return [number_of_steps, cadence,
               velocity,
               avg_step_length,
               avg_stride_length,
               step_durations,
               avg_step_duration,
               sd_step_durations,
               strides,
               stride_durations,
               avg_number_of_strides,
               avg_stride_duration,
               sd_stride_durations,
               step_regularity,
               stride_regularity,
               symmetry]

Response: Extracts gait features from estimated heel strikes and accelerometer data.


Instruction:
Can you create a Python 3 function that
divides a univariate time - series data_frame into states contiguous segments using Bellman k - segmentation algorithm on the peak prominences of the data.

Response:
def bellman_segmentation(self, x, states):
        """
            Divide a univariate time-series, data_frame, into states contiguous segments, using Bellman k-segmentation algorithm on the peak prominences of the data.

            :param x: The time series to assess freeze of gait on. This could be x, y, z or mag_sum_acc.
            :type x: pandas.Series
            :param states: Number of contigous segments.
            :type states: int
            :return peaks: The peaks in our data_frame.
            :rtype peaks: list
            :return prominences: Peaks prominences.
            :rtype prominences: list
            :return bellman_idx: The indices of the segments.
            :rtype bellman_idx: list
        """
        peaks, prominences = get_signal_peaks_and_prominences(x)
        bellman_idx = BellmanKSegment(prominences, states)

        return peaks, prominences, bellman_idx


Instruction:
Can you create a Python 3 function that
divides a univariate time - series data_frame into states contiguous segments using sk - learn clustering algorithms on the peak prominences of the data.

Response:
def sklearn_segmentation(self, x, cluster_fn):
        """
            Divide a univariate time-series, data_frame, into states contiguous segments, using sk-learn clustering algorithms on the peak prominences of the data.

            :param x: The time series to assess freeze of gait on. This could be x, y, z or mag_sum_acc.
            :type x: pandas.Series
            :param cluster_fn: Any unsupervised learning algorithm from the sklearn library. It needs to have the `fit_predict` method.
            :param cluster_fn: sklearn.aglorithm
            :return peaks: The peaks in our data_frame.
            :rtype peaks: list
            :return prominences: Peaks prominences.
            :rtype prominences: list
            :return sklearn_idx: The indices of the segments.
            :rtype sklearn_idx: list
        """
        peaks, prominences = get_signal_peaks_and_prominences(x)

        # sklearn fix: reshape to (-1, 1)
        sklearn_idx = cluster_fn.fit_predict(prominences.reshape(-1, 1))

        return peaks, prominences, sklearn_idx


Instruction:
Can you generate a brief explanation for the following Python 3 code
def error_wrapper(error, errorClass):
    http_status = 0
    if error.check(TwistedWebError):
        xml_payload = error.value.response
        if error.value.status:
            http_status = int(error.value.status)
    else:
        error.raiseException()
    if http_status >= 400:
        if not xml_payload:
            error.raiseException()
        try:
            fallback_error = errorClass(
                xml_payload, error.value.status, str(error.value),
                error.value.response)
        except (ParseError, AWSResponseParseError):
            error_message = http.RESPONSES.get(http_status)
            fallback_error = TwistedWebError(
                http_status, error_message, error.value.response)
        raise fallback_error
    elif 200 <= http_status < 300:
        return str(error.value)
    else:
        error.raiseException()

Response: Wrapper for error messages from Amazon s EC2 API.


Instruction:
Write a Python 3 function that can
return the un - urlencoded path component but joined by slashes.

Response:
def _get_joined_path(ctx):
    """
    @type ctx: L{_URLContext}
    @param ctx: A URL context.

    @return: The path component, un-urlencoded, but joined by slashes.
    @rtype: L{bytes}
    """
    return b'/' + b'/'.join(seg.encode('utf-8') for seg in ctx.path)


Instruction:
Here you have a function in Python 3, explain what it does
def get_page(self, url, *args, **kwds):
        contextFactory = None
        scheme, host, port, path = parse(url)
        data = kwds.get('postdata', None)
        self._method = method = kwds.get('method', 'GET')
        self.request_headers = self._headers(kwds.get('headers', {}))
        if (self.body_producer is None) and (data is not None):
            self.body_producer = FileBodyProducer(StringIO(data))
        if self.endpoint.ssl_hostname_verification:
            contextFactory = None
        else:
            contextFactory = WebClientContextFactory()
        agent = _get_agent(scheme, host, self.reactor, contextFactory)
        if scheme == "https":
            self.client.url = url
        d = agent.request(method, url, self.request_headers, self.body_producer)
        d.addCallback(self._handle_response)
        return d

Response: Get a page from the given url.


Instruction:
Can you tell what is the following Python 3 function doing
def _headers(self, headers_dict):
        return Headers(dict((k,[v]) for (k,v) in headers_dict.items()))

Response: Convert dictionary of headers into twisted. web. client. Headers object.


Instruction:
Given the following Python 3 function, write the documentation
def _unpack_headers(self, headers):
        return dict((k,v[0]) for (k,v) in headers.getAllRawHeaders())

Response:
Unpack twisted. web. client. Headers object to dict. This is to provide
        backwards compatability.


Instruction:
Can you tell what is the following Python 3 function doing
def _handle_response(self, response):
        self.client.status = response.code
        self.response_headers = response.headers
        # XXX This workaround (which needs to be improved at that) for possible
        # bug in Twisted with new client:
        # http://twistedmatrix.com/trac/ticket/5476
        if self._method.upper() == 'HEAD' or response.code == NO_CONTENT:
            return succeed('')
        receiver = self.receiver_factory()
        receiver.finished = d = Deferred()
        receiver.content_length = response.length
        response.deliverBody(receiver)
        if response.code >= 400:
            d.addCallback(self._fail_response, response)
        return d

Response: Handle the HTTP response by memoizing the headers and then delivering the bytes.


Instruction:
Create a Python 3 function to
create a JWT token for a GOV. UK Notify base token.

Response:
def create_jwt_token(secret, client_id):
    """
    Create JWT token for GOV.UK Notify

    Tokens have standard header:
    {
        "typ": "JWT",
        "alg": "HS256"
    }

    Claims consist of:
    iss: identifier for the client
    iat: issued at in epoch seconds (UTC)

    :param secret: Application signing secret
    :param client_id: Identifier for the client
    :return: JWT token for this request
    """
    assert secret, "Missing secret key"
    assert client_id, "Missing client id"

    headers = {
        "typ": __type__,
        "alg": __algorithm__
    }

    claims = {
        'iss': client_id,
        'iat': epoch_seconds()
    }

    return jwt.encode(payload=claims, key=secret, headers=headers).decode()


Instruction:
How would you code a function in Python 3 to
return the issuer of a token

Response:
def get_token_issuer(token):
    """
    Issuer of a token is the identifier used to recover the secret
    Need to extract this from token to ensure we can proceed to the signature validation stage
    Does not check validity of the token
    :param token: signed JWT token
    :return issuer: iss field of the JWT token
    :raises TokenIssuerError: if iss field not present
    :raises TokenDecodeError: if token does not conform to JWT spec
    """
    try:
        unverified = decode_token(token)

        if 'iss' not in unverified:
            raise TokenIssuerError

        return unverified.get('iss')
    except jwt.DecodeError:
        raise TokenDecodeError


Instruction:
Write a Python 3 script for
validating and decodes the JWT token and returns a boolean if the token is valid False otherwise

Response:
def decode_jwt_token(token, secret):
    """
    Validates and decodes the JWT token
    Token checked for
        - signature of JWT token
        - token issued date is valid

    :param token: jwt token
    :param secret: client specific secret
    :return boolean: True if valid token, False otherwise
    :raises TokenIssuerError: if iss field not present
    :raises TokenIssuedAtError: if iat field not present
    :raises jwt.DecodeError: If signature validation fails
    """
    try:
        # check signature of the token
        decoded_token = jwt.decode(
            token,
            key=secret.encode(),
            verify=True,
            algorithms=[__algorithm__],
            leeway=__bound__
        )
        # token has all the required fields
        if 'iss' not in decoded_token:
            raise TokenIssuerError
        if 'iat' not in decoded_token:
            raise TokenIssuedAtError

        # check iat time is within bounds
        now = epoch_seconds()
        iat = int(decoded_token['iat'])
        if now > (iat + __bound__):
            raise TokenExpiredError("Token has expired", decoded_token)
        if iat > (now + __bound__):
            raise TokenExpiredError("Token can not be in the future", decoded_token)

        return True
    except jwt.InvalidIssuedAtError:
        raise TokenExpiredError("Token has invalid iat field", decode_token(token))
    except jwt.DecodeError:
        raise TokenDecodeError


Instruction:
Given the following Python 3 function, write the documentation
def parse(stream, with_text=False):  # type: (Iterator[str], bool) -> Iterator[Union[Tuple[str, LexicalUnit], LexicalUnit]]

    buffer = ''
    text_buffer = ''
    in_lexical_unit = False
    in_superblank = False

    for char in stream:
        if in_superblank:
            if char == ']':
                in_superblank = False
                text_buffer += char
            elif char == '\\':
                text_buffer += char
                text_buffer += next(stream)
            else:
                text_buffer += char
        elif in_lexical_unit:
            if char == '$':
                if with_text:
                    yield (text_buffer, LexicalUnit(buffer))
                else:
                    yield LexicalUnit(buffer)
                buffer = ''
                text_buffer = ''
                in_lexical_unit = False
            elif char == '\\':
                buffer += char
                buffer += next(stream)
            else:
                buffer += char
        else:
            if char == '[':
                in_superblank = True
                text_buffer += char
            elif char == '^':
                in_lexical_unit = True
            elif char == '\\':
                text_buffer += char
                text_buffer += next(stream)
            else:
                text_buffer += char

Response: Generates a list of lexical units from a character stream.


Instruction:
Given the following Python 3 function, write the documentation
def to_gnuplot_datafile(self, datafilepath):
        try:
            datafile = file(datafilepath, "wb")
        except Exception:
            return False

        if self._timestampFormat is None:
            self._timestampFormat = _STR_EPOCHS

        datafile.write("# time_as_<%s> value\n" % self._timestampFormat)

        convert = TimeSeries.convert_epoch_to_timestamp
        for datapoint in self._timeseriesData:
            timestamp, value = datapoint
            if self._timestampFormat is not None:
                timestamp = convert(timestamp, self._timestampFormat)

            datafile.write("%s %s\n" % (timestamp, value))

        datafile.close()
        return True

Response: Dumps the TimeSeries into a gnuplot compatible data file.


Instruction:
Implement a Python 3 function for
creating a new TimeSeries instance from the data stored inside a two dimensional list.

Response:
def from_twodim_list(cls, datalist, tsformat=None):
        """Creates a new TimeSeries instance from the data stored inside a two dimensional list.

        :param list datalist:    List containing multiple iterables with at least two values.
            The first item will always be used as timestamp in the predefined format,
            the second represents the value. All other items in those sublists will be ignored.
        :param string tsformat:    Format of the given timestamp. This is used to convert the
            timestamp into UNIX epochs, if necessary. For valid examples take a look into
            the :py:func:`time.strptime` documentation.

        :return:    Returns a TimeSeries instance containing the data from datalist.
        :rtype:     TimeSeries
        """
        # create and fill the given TimeSeries
        ts = TimeSeries()
        ts.set_timeformat(tsformat)

        for entry in datalist:
            ts.add_entry(*entry[:2])

        # set the normalization level
        ts._normalized = ts.is_normalized()
        ts.sort_timeseries()

        return ts


Instruction:
Write a Python 3 script for
initializing the TimeSeries s data from the given SQL cursor.

Response:
def initialize_from_sql_cursor(self, sqlcursor):
        """Initializes the TimeSeries's data from the given SQL cursor.

        You need to set the time stamp format using :py:meth:`TimeSeries.set_timeformat`.

        :param SQLCursor sqlcursor:    Cursor that was holds the SQL result for any given
            "SELECT timestamp, value, ... FROM ..." SQL query.
            Only the first two attributes of the SQL result will be used.

        :return:    Returns the number of entries added to the TimeSeries.
        :rtype:     integer
        """
        # initialize the result
        tuples = 0

        # add the SQL result to the time series
        data = sqlcursor.fetchmany()
        while 0 < len(data):
            for entry in data:
                self.add_entry(str(entry[0]), entry[1])

            data = sqlcursor.fetchmany()

        # set the normalization level
        self._normalized = self._check_normalization

        # return the number of tuples added to the timeseries.
        return tuples


Instruction:
Can you create a Python 3 function that
converts the given timestamp into a float representing the UNIX - epochs for the given timestamp in the defined format.

Response:
def convert_timestamp_to_epoch(cls, timestamp, tsformat):
        """Converts the given timestamp into a float representing UNIX-epochs.

        :param string timestamp: Timestamp in the defined format.
        :param string tsformat:    Format of the given timestamp. This is used to convert the
            timestamp into UNIX epochs. For valid examples take a look into
            the :py:func:`time.strptime` documentation.

        :return:    Returns an float, representing the UNIX-epochs for the given timestamp.
        :rtype: float
        """
        return time.mktime(time.strptime(timestamp, tsformat))


Instruction:
Here you have a function in Python 3, explain what it does
def convert_epoch_to_timestamp(cls, timestamp, tsformat):
        return time.strftime(tsformat, time.gmtime(timestamp))

Response: Converts the given float representing UNIX - epochs into an actual timestamp.


Instruction:
How would you code a function in Python 3 to
add a new entry to the data list.

Response:
def add_entry(self, timestamp, data):
        """Adds a new data entry to the TimeSeries.

        :param timestamp:    Time stamp of the data.
            This has either to be a float representing the UNIX epochs
            or a string containing a timestamp in the given format.
        :param numeric data:    Actual data value.
        """
        self._normalized = self._predefinedNormalized
        self._sorted     = self._predefinedSorted

        tsformat = self._timestampFormat
        if tsformat is not None:
            timestamp = TimeSeries.convert_timestamp_to_epoch(timestamp, tsformat)

        self._timeseriesData.append([float(timestamp), float(data)])


Instruction:
How would you code a function in Python 3 to
sort the data points within the time series according to their occurrence inline.

Response:
def sort_timeseries(self, ascending=True):
        """Sorts the data points within the TimeSeries according to their occurrence inline.

        :param boolean ascending: Determines if the TimeSeries will be ordered ascending or
            descending. If this is set to descending once, the ordered parameter defined in
            :py:meth:`TimeSeries.__init__` will be set to False FOREVER.

        :return:    Returns :py:obj:`self` for convenience.
        :rtype:     TimeSeries
        """
        # the time series is sorted by default
        if ascending and self._sorted:
            return

        sortorder = 1
        if not ascending:
            sortorder = -1
            self._predefinedSorted = False

        self._timeseriesData.sort(key=lambda i: sortorder * i[0])

        self._sorted = ascending

        return self


Instruction:
Create a Python 3 function to
return a sorted copy of the TimeSeries preserving the original one.

Response:
def sorted_timeseries(self, ascending=True):
        """Returns a sorted copy of the TimeSeries, preserving the original one.

        As an assumption this new TimeSeries is not ordered anymore if a new value is added.

        :param boolean ascending:    Determines if the TimeSeries will be ordered ascending
            or descending.

        :return:    Returns a new TimeSeries instance sorted in the requested order.
        :rtype:     TimeSeries
        """
        sortorder = 1
        if not ascending:
            sortorder = -1

        data = sorted(self._timeseriesData, key=lambda i: sortorder * i[0])

        newTS = TimeSeries(self._normalized)
        for entry in data:
            newTS.add_entry(*entry)

        newTS._sorted = ascending

        return newTS


Instruction:
Create a Python 3 function for
normalizing the data points in a single time series.

Response:
def normalize(self, normalizationLevel="minute", fusionMethod="mean", interpolationMethod="linear"):
        """Normalizes the TimeSeries data points.

        If this function is called, the TimeSeries gets ordered ascending
        automatically. The new timestamps will represent the center of each time
        bucket. Within a normalized TimeSeries, the temporal distance between two consecutive data points is constant.

        :param string normalizationLevel:    Level of normalization that has to be applied.
            The available normalization levels are defined in :py:data:`timeseries.NormalizationLevels`.
        :param string fusionMethod:    Normalization method that has to be used if multiple data entries exist
            within the same normalization bucket. The available methods are defined in :py:data:`timeseries.FusionMethods`.
        :param string interpolationMethod: Interpolation method that is used if a data entry at a specific time
            is missing. The available interpolation methods are defined in :py:data:`timeseries.InterpolationMethods`.

        :raise: Raises a :py:exc:`ValueError` if a normalizationLevel, fusionMethod or interpolationMethod hanve an unknown value.
        """
        # do not normalize the TimeSeries if it is already normalized, either by
        # definition or a prior call of normalize(*)
        if self._normalizationLevel == normalizationLevel:
            if self._normalized:    # pragma: no cover
                return

        # check if all parameters are defined correctly
        if normalizationLevel not in NormalizationLevels:
            raise ValueError("Normalization level %s is unknown." % normalizationLevel)
        if fusionMethod not in FusionMethods:
            raise ValueError("Fusion method %s is unknown." % fusionMethod)
        if interpolationMethod not in InterpolationMethods:
            raise ValueError("Interpolation method %s is unknown." % interpolationMethod)

        # (nearly) empty TimeSeries instances do not require normalization
        if len(self) < 2:
            self._normalized = True
            return

        # get the defined methods and parameter
        self._normalizationLevel = normalizationLevel
        normalizationLevel       = NormalizationLevels[normalizationLevel]
        fusionMethod             = FusionMethods[fusionMethod]
        interpolationMethod      = InterpolationMethods[interpolationMethod]

        # sort the TimeSeries
        self.sort_timeseries()

        # prepare the required buckets
        start           = self._timeseriesData[0][0]
        end             = self._timeseriesData[-1][0]
        span            = end - start
        bucketcnt       = int(span / normalizationLevel) + 1

        buckethalfwidth = normalizationLevel / 2.0
        bucketstart     = start + buckethalfwidth
        buckets         = [[bucketstart + idx * normalizationLevel] for idx in xrange(bucketcnt)]

        # Step One: Populate buckets
        # Initialize the timeseries data iterators
        tsdStartIdx = 0
        tsdEndIdx   = 0
        tsdlength   = len(self)

        for idx in xrange(bucketcnt):
            # get the bucket to avoid multiple calls of buckets.__getitem__()
            bucket = buckets[idx]

            # get the range for the given bucket
            bucketend   = bucket[0] + buckethalfwidth

            while tsdEndIdx < tsdlength and self._timeseriesData[tsdEndIdx][0] < bucketend:
                tsdEndIdx += 1

            # continue, if no valid data entries exist
            if tsdStartIdx == tsdEndIdx:
                continue

            # use the given fusion method to calculate the fusioned value
            values = [i[1] for i in self._timeseriesData[tsdStartIdx:tsdEndIdx]]
            bucket.append(fusionMethod(values))

            # set the new timeseries data index
            tsdStartIdx = tsdEndIdx

        # Step Two: Fill missing buckets
        missingCount   = 0
        lastIdx        = 0
        for idx in xrange(bucketcnt):
            # bucket is empty
            if 1 == len(buckets[idx]):
                missingCount += 1
                continue

            # This is the first bucket. The first bucket is not empty by definition!
            if idx == 0:
                lastIdx = idx
                continue

            # update the lastIdx, if none was missing
            if 0 == missingCount:
                lastIdx = idx
                continue

            # calculate and fill in missing values
            missingValues = interpolationMethod(buckets[lastIdx][1], buckets[idx][1], missingCount)
            for idx2 in xrange(1, missingCount + 1):
                buckets[lastIdx + idx2].append(missingValues[idx2 - 1])

            lastIdx = idx
            missingCount = 0

        self._timeseriesData = buckets

        # at the end set self._normalized to True
        self._normalized = True


Instruction:
Can you tell what is the following Python 3 function doing
def _check_normalization(self):
        lastDistance = None
        distance     = None
        for idx in xrange(len(self) - 1):
            distance = self[idx+1][0] - self[idx][0]

            # first run
            if lastDistance is None:
                lastDistance = distance
                continue

            if lastDistance != distance:
                return False

            lastDistance = distance

        return True

Response: Checks if the TimeSeries is normalized.


Instruction:
Write a Python 3 function that can
apply the given ForecastingAlgorithm or SmoothingMethod from the pycast. methods module to the TimeSeries.

Response:
def apply(self, method):
        """Applies the given ForecastingAlgorithm or SmoothingMethod from the :py:mod:`pycast.methods`
        module to the TimeSeries.

        :param BaseMethod method: Method that should be used with the TimeSeries.
            For more information about the methods take a look into their corresponding documentation.

        :raise:    Raises a StandardError when the TimeSeries was not normalized and hte method requires a
            normalized TimeSeries
        """
        # check, if the methods requirements are fullfilled
        if method.has_to_be_normalized() and not self._normalized:
            raise StandardError("method requires a normalized TimeSeries instance.")

        if method.has_to_be_sorted():
            self.sort_timeseries()

        return method.execute(self)


Instruction:
Can you write a function in Python 3 where it
samples with replacement from the TimeSeries. Returns the sample and the remaining timeseries.

Response:
def sample(self, percentage):
        """Samples with replacement from the TimeSeries. Returns the sample and the remaining timeseries.
        The original timeseries is not changed.

        :param float percentage: How many percent of the original timeseries should be in the sample

        :return:    A tuple containing (sample, rest) as two TimeSeries.
        :rtype: tuple(TimeSeries,TimeSeries)

        :raise:    Raises a ValueError if percentage is not in (0.0, 1.0).
        """
        if not (0.0 < percentage < 1.0):
            raise ValueError("Parameter percentage has to be in (0.0, 1.0).")

        cls         = self.__class__
        value_count = int(len(self) * percentage)
        values      = random.sample(self, value_count)

        sample      = cls.from_twodim_list(values)
        rest_values = self._timeseriesData[:]

        for value in values:
            rest_values.remove(value)

        rest = cls.from_twodim_list(rest_values)

        return sample, rest


Instruction:
How would you code a function in Python 3 to
add a new entry to the data list.

Response:
def add_entry(self, timestamp, data):
        """Adds a new data entry to the TimeSeries.

        :param timestamp:    Time stamp of the data.
            This has either to be a float representing the UNIX epochs
            or a string containing a timestamp in the given format.
        :param list data:    A list containing the actual dimension values.

        :raise:    Raises a :py:exc:`ValueError` if data does not contain as many dimensions as
            defined in __init__.
        """
        if not isinstance(data, list):
            data = [data]

        if len(data) != self._dimensionCount:
            raise ValueError("data does contain %s instead of %s dimensions.\n   %s" % (len(data), self._dimensionCount, data))

        self._normalized = self._predefinedNormalized
        self._sorted     = self._predefinedSorted

        tsformat = self._timestampFormat
        if tsformat is not None:
            timestamp = TimeSeries.convert_timestamp_to_epoch(timestamp, tsformat)

        self._timeseriesData.append([float(timestamp)] + [float(dimensionValue) for dimensionValue in data])


Instruction:
Write a Python 3 function that can
serialize the MultiDimensionalTimeSeries data into a two dimensional list of [ timestamp values pairs.

Response:
def to_twodim_list(self):
        """Serializes the MultiDimensionalTimeSeries data into a two dimensional list of [timestamp, [values]] pairs.

        :return:    Returns a two dimensional list containing [timestamp, [values]] pairs.
        :rtype:     list
        """
        if self._timestampFormat is None:
            return self._timeseriesData

        datalist = []
        append   = datalist.append
        convert  = TimeSeries.convert_epoch_to_timestamp
        for entry in self._timeseriesData:
            append([convert(entry[0], self._timestampFormat), entry[1:]])

        return datalist


Instruction:
Can you create a Python 3 function that
creates a new MultiDimensionalTimeSeries instance from the data stored inside a two dimensional list.

Response:
def from_twodim_list(cls, datalist, tsformat=None, dimensions=1):
        """Creates a new MultiDimensionalTimeSeries instance from the data stored inside a two dimensional list.

        :param list datalist:    List containing multiple iterables with at least two values.
            The first item will always be used as timestamp in the predefined format,
            the second is a list, containing the dimension values.
        :param string format:    Format of the given timestamp. This is used to convert the
            timestamp into UNIX epochs, if necessary. For valid examples take a look into
            the :py:func:`time.strptime` documentation.
        :param integer dimensions:    Number of dimensions the MultiDimensionalTimeSeries contains.

        :return:    Returns a MultiDimensionalTimeSeries instance containing the data from datalist.
        :rtype:     MultiDimensionalTimeSeries
        """
        # create and fill the given TimeSeries
        ts = MultiDimensionalTimeSeries(dimensions=dimensions)
        ts.set_timeformat(tsformat)

        for entry in datalist:
            ts.add_entry(entry[0], entry[1])

        # set the normalization level
        ts._normalized = ts.is_normalized()
        ts.sort_timeseries()

        return ts


Instruction:
Make a summary of the following Python 3 code
def to_gnuplot_datafile(self, datafilepath):
        try:
            datafile = file(datafilepath, "wb")
        except Exception:
            return False

        if self._timestampFormat is None:
            self._timestampFormat = _STR_EPOCHS

        datafile.write("# time_as_<%s> value..." % self._timestampFormat)

        convert = TimeSeries.convert_epoch_to_timestamp
        for datapoint in self._timeseriesData:
            timestamp = datapoint[0]
            values = datapoint[1:]
            if self._timestampFormat is not None:
                timestamp = convert(timestamp, self._timestampFormat)

            datafile.write("%s %s" % (timestamp, " ".join([str(entry) for entry in values])))

        datafile.close()
        return True

Response: Dumps the TimeSeries into a gnuplot compatible data file.


Instruction:
Can you tell what is the following Python 3 function doing
def include(self, filename, is_file):
        if self.exclude_regex and is_file:
            if self.exclude_regex.match(filename):
                return False
            return True
        else:
            return True

Response: Determines if a file should be included in a project for uploading.


Instruction:
Write a Python 3 script for
adding a Unix shell - style wildcard pattern underneath the specified directory.

Response:
def add_filename_pattern(self, dir_name, pattern):
        """
        Adds a Unix shell-style wildcard pattern underneath the specified directory
        :param dir_name: str: directory that contains the pattern
        :param pattern: str: Unix shell-style wildcard pattern
        """
        full_pattern = '{}{}{}'.format(dir_name, os.sep, pattern)
        filename_regex = fnmatch.translate(full_pattern)
        self.regex_list.append(re.compile(filename_regex))


Instruction:
How would you explain what the following Python 3 function does
def include(self, path):
        for regex_item in self.regex_list:
            if regex_item.match(path):
                return False
        return True

Response:
Returns True if any pattern matches the path
        Returns False if any pattern matches the path
        Returns False if any pattern matches the path
        Returns True if any pattern matches the path
        Returns False if any pattern matches the path
        Returns True if any pattern matches the path


Instruction:
Make a summary of the following Python 3 code
def load_directory(self, top_path, followlinks):
        for dir_name, child_dirs, child_files in os.walk(top_path, followlinks=followlinks):
            for child_filename in child_files:
                if child_filename == DDS_IGNORE_FILENAME:
                    pattern_lines = self._read_non_empty_lines(dir_name, child_filename)
                    self.add_patterns(dir_name, pattern_lines)

Response: Load patterns from a directory and save them in the internal cache.


Instruction:
Explain what the following Python 3 code does
def add_patterns(self, dir_name, pattern_lines):
        for pattern_line in pattern_lines:
            self.pattern_list.add_filename_pattern(dir_name, pattern_line)

Response: Add patterns the should apply below dir_name


Instruction:
Can you write a function in Python 3 where it
returns True if any pattern matches the path

Response:
def include(self, path, is_file):
        """
        Returns False if any pattern matches the path
        :param path: str: filename path to test
        :return: boolean: True if we should include this path
        """
        return self.pattern_list.include(path) and self.file_filter.include(os.path.basename(path), is_file)


Instruction:
Can you implement a function in Python 3 that
creates a project with the specified name and description.

Response:
def create_project(self, name, description):
        """
        Create a project with the specified name and description
        :param name: str: unique name for this project
        :param description: str: long description of this project
        :return: str: name of the project
        """
        self._cache_project_list_once()
        if name in [project.name for project in self.projects]:
            raise DuplicateNameError("There is already a project named {}".format(name))
        self.client.create_project(name, description)
        self.clear_project_cache()
        return name


Instruction:
Can you write a function in Python 3 where it
deletes a project with the specified name. Raises ItemNotFound if no such project exists

Response:
def delete_project(self, project_name):
        """
        Delete a project with the specified name. Raises ItemNotFound if no such project exists
        :param project_name: str: name of the project to delete
        :return:
        """
        project = self._get_project_for_name(project_name)
        project.delete()
        self.clear_project_cache()


Instruction:
How would you code a function in Python 3 to
return a list of file paths that make up project_name

Response:
def list_files(self, project_name):
        """
        Return a list of file paths that make up project_name
        :param project_name: str: specifies the name of the project to list contents of
        :return: [str]: returns a list of remote paths for all files part of the specified project qq
        """
        project = self._get_project_for_name(project_name)
        file_path_dict = self._get_file_path_dict_for_project(project)
        return list(file_path_dict)


Instruction:
How would you explain what the following Python 3 function does
def download_file(self, project_name, remote_path, local_path=None):
        project = self._get_project_for_name(project_name)
        file = project.get_child_for_path(remote_path)
        file.download_to_path(local_path)

Response: Download a file from a project containing the given remote path.


Instruction:
Given the following Python 3 function, write the documentation
def upload_file(self, project_name, local_path, remote_path=None):
        project = self._get_or_create_project(project_name)
        file_upload = FileUpload(project, remote_path, local_path)
        file_upload.run()

Response: Upload a file into a project creating a new version if it does not exist.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete_file(self, project_name, remote_path):
        project = self._get_or_create_project(project_name)
        remote_file = project.get_child_for_path(remote_path)
        remote_file.delete()

Response: Delete a file or folder from a project


Instruction:
Can you tell what is the following Python 3 function doing
def _determineLength(self, fObj):
        try:
            seek = fObj.seek
            tell = fObj.tell
        except AttributeError:
            return UNKNOWN_LENGTH
        originalPosition = tell()
        seek(0, self._SEEK_END)
        end = tell()
        seek(originalPosition, self._SEEK_SET)
        return end - originalPosition

Response: Determine how many bytes can be read out of a file.


Instruction:
Write a Python 3 function that can
start a cooperative task which will read all available bytes from the input file and write them to the output file and return a Deferred which fires after all bytes have been written.

Response:
def startProducing(self, consumer):
        """
        Start a cooperative task which will read bytes from the input file and
        write them to C{consumer}.  Return a L{Deferred} which fires after all
        bytes have been written.

        @param consumer: Any L{IConsumer} provider
        """
        self._task = self._cooperate(self._writeloop(consumer))
        d = self._task.whenDone()
        def maybeStopped(reason):
            # IBodyProducer.startProducing's Deferred isn't support to fire if
            # stopProducing is called.
            reason.trap(task.TaskStopped)
            return defer.Deferred()
        d.addCallbacks(lambda ignored: None, maybeStopped)
        return d


Instruction:
Create a Python 3 function to
return an iterator which reads one chunk of bytes from the input file and writes them to the consumer for each time it is iterated.

Response:
def _writeloop(self, consumer):
        """
        Return an iterator which reads one chunk of bytes from the input file
        and writes them to the consumer for each time it is iterated.
        """
        while True:
            bytes = self._inputFile.read(self._readSize)
            if not bytes:
                self._inputFile.close()
                break
            consumer.write(bytes)
            yield None


Instruction:
Can you tell what is the following Python 3 function doing
def _count_differences(self):
        different_items = LocalOnlyCounter(self.config.upload_bytes_per_chunk)
        different_items.walk_project(self.local_project)
        return different_items

Response: Count how many things we will send from the local project we will send to


Instruction:
How would you explain what the following Python 3 function does
def run(self):
        progress_printer = ProgressPrinter(self.different_items.total_items(), msg_verb='sending')
        upload_settings = UploadSettings(self.config, self.remote_store.data_service, progress_printer,
                                         self.project_name_or_id, self.file_upload_post_processor)
        project_uploader = ProjectUploader(upload_settings)
        project_uploader.run(self.local_project)
        progress_printer.finished()

Response: Runs the remote store upload