apply head position to empty room

Hello,
We’ve been trying to improve our covariance matrix.
I read this excellent paper, and I’m working on trying to apply.

I started with this documentation, but I have several questions.

Can anyone provide an example command of how you might use tsss on empty room data with an applied real head movement position?

Thanks,
Megan

Apologies, here is the correct DOI for the paper: https://doi.org/10.3389/fneur.2022.827529

You’ll find some reference in this post: head position parameter for maxfiltering empty-room recording

Thank you! Looks very helpful!
Megan

Hello,
I’m stuck using mne.preprocessing.maxwell_filter_prepare_emptyroom. Could someone please provide a usage example?

My command looks like:

mne.preprocessing.maxwell_filter_prepare_emptyroom(empty_raw, raw)

I get the following error


TypeError Traceback (most recent call last)
----> 1 mne.preprocessing.maxwell_filter_prepare_emptyroom(empty_raw, raw)

TypeError: maxwell_filter_prepare_emptyroom() takes 1 positional argument but 2 were given

I also tried

mne.preprocessing.maxwell_filter_prepare_emptyroom(empty_raw, *, raw, bads=‘from_raw’, meas_date=‘keep’, verbose=None)

which gave this error:

SyntaxError: invalid syntax


TypeError Traceback (most recent call last)
----> 1 mne.preprocessing.maxwell_filter_prepare_emptyroom(empty_raw, raw, bads=‘from_raw’, meas_date=‘keep’, verbose=None)

TypeError: maxwell_filter_prepare_emptyroom() takes 1 positional argument but 2 positional arguments (and 3 keyword-only arguments) were given

Thank you,
Megan

Hello,
Perhaps there is an error in the documentation?

I tried

mne.preprocessing.maxwell_filter_prepare_emptyroom(empty_raw, raw =raw, verbose=True)

using raw as a keyword argument instead of positional, and the command was successful.

Thank you,
Megan

There is no error in the documentation:

mne.preprocessing.maxwell_filter_prepare_emptyroom(raw_er, *, raw, bads='from_raw', annotations='from_raw', meas_date='keep', emit_warning=False, verbose=None)

In Python, every argument after the * is a keyword-only argument. This is to force users to use keywords and don’t do e.g.

maxwell_filter_prepare_emptyroom(raw, raw_er)

You also have positional only arguments:

def foo(arg1, arg2, /, arg3, arg4, *, arg5, arg6, *args, **kwargs):
    ...

Before the / → positional argument only, thus foo(arg1=...) will raise.
Between the / and * → positional or keyword arguments
After the * → keyword only argument, thus foo(x1, x2, x3, x4, x5) will raise.

Mathieu

1 Like

Hi, Thanks for your explanation. This is new to me!
Should it be then, raw=raw instead of just
…, *, raw, …
? Is raw the keyword and the object being passed to the keyword parameter?

Thanks! I sincerely appreciate your help,
Megan

Correct, it should be either maxwell_filter_prepare_emptyroom(raw_er=..., raw=...) or maxwell_filter_prepare_emptyroom(..., raw=...) because all of the arguments after the * must be provided with their keyword name (raw=, bads=, …).
Note that the variable name passed can differ, e.g. maxwell_filter_prepare_emptyroom(empty_room_raw, raw=process_raw).

1 Like

@mschendel I’ve asked ChatGPT to generate an explanation.

Please explain Python’s keyword-only arguments in simple terms to somebody who’s not familiar with this concept. Please explain how to spot them and how to use them (from a user side). Please include examples of keyword-only arguments with and without default values.

Understanding Python’s Keyword-Only Arguments

In Python, keyword-only arguments are parameters that can only be passed by their name (keyword) and not by their position in the function call. These arguments provide more clarity and prevent confusion, especially when a function takes multiple parameters.

How to Spot Keyword-Only Arguments

You can identify keyword-only arguments in a function definition by looking for an asterisk (*) in the parameter list. Any parameter after the * must be passed as a keyword argument.

Using Keyword-Only Arguments

When calling a function with keyword-only arguments, you must explicitly name these arguments. This can improve readability and reduce errors.

Examples

Let’s go through examples of keyword-only arguments with and without default values.

Example 1: Keyword-Only Arguments without Default Values

def greet(name, *, greeting):
    return f"{greeting}, {name}!"

# Correct usage
print(greet("Alice", greeting="Hello"))  # Output: Hello, Alice!

# Incorrect usage (will raise a TypeError)
# print(greet("Alice", "Hello"))

In this example, greeting is a keyword-only argument. You must specify greeting when calling the function.

Example 2: Keyword-Only Arguments with Default Values

def introduce(name, *, age=None, city="Unknown"):
    return f"Meet {name}, who is {age} years old and lives in {city}."

# Correct usage with default values
print(introduce("Bob"))  # Output: Meet Bob, who is None years old and lives in Unknown.

# Correct usage with provided values
print(introduce("Bob", age=30, city="New York"))  # Output: Meet Bob, who is 30 years old and lives in New York.

# Incorrect usage (will raise a TypeError)
# print(introduce("Bob", 30, "New York"))

In this example, age and city are keyword-only arguments with default values. You can either use the defaults or provide specific values by naming the arguments.

Summary

  • Keyword-only arguments are specified after an asterisk (*) in the function definition.
  • They must be passed by name when calling the function.
  • They can have default values.
  • They improve code clarity and reduce potential errors.

By using keyword-only arguments, you can make your function calls more explicit and easier to understand.

What are advantages and disadvantages of keyword-only arguments?

Advantages and Disadvantages of Keyword-Only Arguments

Advantages

  1. Improved Readability:

    • By forcing arguments to be named, the code becomes more self-documenting and easier to understand.
    • Example:
      def configure_server(*, host, port):
          pass
      
      configure_server(host="localhost", port=8080)
      
      It’s clear what host and port represent, improving code readability.
  2. Reduced Risk of Errors:

    • Prevents accidental misplacement of arguments, which can lead to bugs.
    • Example:
      def connect_database(*, username, password):
          pass
      
      # Misplacing these would raise an error
      connect_database(username="admin", password="secret")
      
  3. Flexibility with Default Values:

    • Allows setting default values for certain arguments without affecting the required arguments.
    • Example:
      def send_email(to, *, subject="No Subject", body=""):
          pass
      
      send_email(to="example@example.com", subject="Hello")
      
  4. Better Support for APIs and Libraries:

    • Ensures that the function’s API is more explicit, which is particularly useful in public libraries and APIs where clarity is crucial.
  5. Easier Maintenance:

    • Future changes to the function signature (adding or reordering arguments) are less likely to break existing code, as long as the keyword names remain consistent.

Disadvantages

  1. Verbose Syntax:

    • Can make function calls longer and more verbose.
    • Example:
      def process_data(*, filename, format):
          pass
      
      process_data(filename="data.csv", format="csv")
      
      This requires more typing compared to positional arguments.
  2. Learning Curve:

    • For beginners, understanding and using keyword-only arguments might take some time and effort.
  3. Incompatibility with Positional-Only APIs:

    • Functions that expect positional-only arguments can be less flexible in some use cases where order is important.
  4. Potential for Overhead:

    • When overused, the requirement to specify every argument by name can lead to unnecessary overhead in code writing, especially for simple functions.
    • Example:
      def add_numbers(*, a, b):
          return a + b
      
      add_numbers(a=1, b=2)  # More typing for a simple addition
      

Summary

Advantages:

  • Improves code readability and clarity.
  • Reduces the risk of argument misplacement errors.
  • Provides flexibility with default values.
  • Enhances support for public APIs and libraries.
  • Easier maintenance with future changes to function signatures.

Disadvantages:

  • Can lead to verbose syntax.
  • May present a learning curve for new programmers.
  • Less compatible with positional-only argument expectations.
  • Potential for overhead in writing and maintaining simple functions.

Keyword-only arguments are a powerful feature in Python that, when used judiciously, can significantly enhance the clarity and robustness of your code.

1 Like

Hi all,
I really appreciate your help! That all helps me understand.

Sincerely, Megan

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.