Tag: パイソン

  • What Exactly Is YORU, a New Behavior Analysis Tool Powered by Object Detection?

    What Exactly Is YORU, a New Behavior Analysis Tool Powered by Object Detection?

    In November 2024, a preprint was posted on bioRxiv describing YORU, a behavior analysis tool that takes a different approach from existing behavior analysis tools.
    In this post, I would like to explain what kind of tool it is.

    YORU preprint

    YORU documentation

    What is YORU?

    YORU is a deep learning-based tool for analyzing animal behavior.

    YORUのサイトより引用


    Many deep learning-based tools, such as DeepLabCut and SLEAP, have already been released.
    These tools use deep learning to track animal body parts and estimate posture (here I will call them tracking tools).

    By estimating posture, they reveal what posture an animal is in and use this for behavior analysis.

    YORU, by contrast, recognizes behavior using an object detection algorithm.
    In short, it does not track body parts; instead, it defines behavior directly from the appearance of the behaving animal.

    When we distinguish an apple from an orange, we rely on cues such as shape and color.
    Object detection excels at classifying objects, so it makes exactly this kind of appearance-based distinction.

    Applying this to animal behavior analysis means classifying animal behavior from its appearance.
    Within YORU, this is referred to as a “behavior object.”

    Whereas tracking tools represent behavior as points and lines, YORU analyzes behavior by enclosing it in a bounding box.

    Advantages of defining behavior with object detection

    The advantages of object detection-based behavior analysis include:

    ・Errors are less likely even as the number of individuals increases

    ・Less affected by the animal’s orientation

    ・Fast analysis speed

    ・Can capture behaviors that are hard to detect by tracking (such as a mouse crouching)

    and so on.

    It overcomes a weakness of tracking tools by making multi-animal behavior analysis simple and far less computationally costly.
    It also makes it easy to analyze behaviors that are difficult to define from body part coordinates.

    Consider, for example, the actions of opening and closing a hand.
    With a tracking tool, you would track the fingertips with a camera and define the hand as open or closed, but if the fingertips become hidden when the hand closes, tracking fails and the positional information can no longer be computed accurately.

    With object detection, however, the model looks at the overall shape of the open and closed hand, so it can still make an accurate call even when part of the hand is occluded.

    As for robustness to increasing numbers of individuals: with a tracking tool, as the number of animals grows you must assign each body part to the correct individual in order to capture each animal’s behavior accurately, whereas YORU analyzes only the shape of each individual, so no identity assignment or part-to-individual matching is required.

    Because analysis is also fast, real-time analysis is possible—you can trigger device control when a particular behavior occurs, enabling automation of behavioral experiments.

    Disadvantages of defining behavior with object detection

    There are, of course, drawbacks as well:

    ・Cannot capture a behavioral sequence

    ・Cannot identify individuals

    ・Cannot capture unknown behaviors

    ・Cannot capture behaviors that are not visually distinctive

    ・Provides no fine-grained detail about the defined behavior

    These are precisely the strengths of tracking tools. Rather than one approach being better than the other, the two complement each other’s advantages and disadvantages.

    Experimenters therefore need to choose the approach that best suits their situation.

    What makes YORU remarkable?

    Having covered the advantages and disadvantages of the object detection-based behavior analysis that underlies YORU, what exactly makes YORU itself stand out?

    Everything runs through a GUI

    YORU lets you perform behavior analysis without writing any code.

    The same is true of tools such as DeepLabCut and SLEAP, and it goes a long way toward making these analyses accessible to biologists.

    Design


    Unlike previous tools, YORU’s design doesn’t really look like that of a research tool.

    Built-in real-time analysis

    Unusually for tools of this kind, YORU comes with a full GUI for real-time analysis.

    Building a real-time analysis system is not that hard if you can program to some degree, but otherwise the barrier is quite high.

    With YORU, however, you can carry out real-time analysis—and even fire external triggers—entirely without programming.

    Adaptable to a wide range of experimental setups

    The biggest hurdle in real-time analysis is figuring out how to combine the code that drives your own experimental apparatus with the analysis itself.

    YORU adopts a plugin system that lets you choose the program used to control your external devices.

    In other words, you simply select the plugin that matches your own system.

    You can also write your own plugins: simply turning your device’s control program into a plugin makes it easy to link that hardware to YORU’s real-time analysis.

    This makes for a remarkably user-friendly system that no other tool currently offers.

    Closing thoughts

    YORU has only just been released, so there are still rough edges in usability and clear gaps in the documentation.

    That said, as these are addressed over time, I am very much looking forward to seeing how people put it to use.

  • How to Install SLEAP [Updated February 2024]

    How to Install SLEAP [Updated February 2024]

    Here I’d like to walk through how to install SLEAP, a machine learning-based tool for tracking animal body parts.

    DeepLabCut is the best-known tracking tool, and I’ve written about how to install it myself in the past, while many others have posted articles about it as well.

    SLEAP, however, has relatively few articles written about it in Japanese.
    SLEAP has many advantages over DeepLabCut, so I decided to write up the installation procedure to make it a viable option for more people.

    SLEAP’s documentation covers everything from installation to usage very clearly, complete with videos, so anyone comfortable with English should find it easy to get started.

    The SLEAP paper is available here

    The official SLEAP website is here

    The SLEAP GitHub repository is here

    Introduction

    First, a note about the approach described in the official SLEAP installation guide:

    mamba create -y -n sleap -c conda-forge -c nvidia -c sleap -c anaconda sleap=1.3.3

    the one-line command that is supposed to handle environment creation, package installation, and everything else at once. I tried it on several computers, but in every case it hung indefinitely during the environment creation step and never completed.

    So in this post I’ll introduce the alternative installation method that the developers also suggest.

    Installation

    We’ll basically follow the installation instructions on the official SLEAP site.

    Environment

    ・Windows 11 Pro
    ・Verified on an NVIDIA 3080
    ・SLEAP 1.3.3
    ・Python 3.7.12

    Downloading the files

    First, download the files from the SLEAP GitHub repository to your computer.

    If you’re using Git, run the following command.

    git clone https://github.com/talmolab/sleap

    If the git command isn’t available, either install git or download the files directly from GitHub.

    Installing the GPU driver

    Install the NVIDIA driver.
    (Skip this step if it’s already installed.)

    Installing Anaconda

    Next, download and install the Windows 10 64-bit version from the Anaconda website.
    Click Free Download on the Anaconda site and scroll down; you’ll see a screen like the one below.
    Install the Windows installer on the far left.

    Once Anaconda is installed, you should find “Anaconda Prompt” in your Windows app list.
    We’ll use it to run the commands below to create the virtual environment, install the packages, and finally launch SLEAP.

    Creating the virtual environment and installing

    Next, run the following commands to create the virtual environment and install SLEAP.
    The environment will be named “sleap”.

    cd sleap
    conda env create -f environment.yml -n sleap

    Incidentally, the commands above work on computers with a GPU;
    on machines without one, use the following instead.

    cd sleap
    conda env create -f environment_no_cuda.yml -n sleap

    That completes the installation.

    Checking that the installation worked

    Activate the virtual environment and launch SLEAP with sleap-label.

    conda activate sleap
    sleap-label

    You’ll need to activate the virtual environment (the conda activate sleap command) every time you reopen Anaconda Prompt.

    After activating the environment, the prompt should change from (base) to (sleap).

    If it launches, the installation was successful.

    Checking version information for reporting in a paper

    To check the version, activate the sleap environment and then run the following command.

    python -c "import sleap; sleap.versions()"

    This outputs:

    SLEAP: 1.3.3
    TensorFlow: 2.7.0
    Numpy: 1.21.5
    Python: 3.7.12
    OS: Windows-10-10.0.22621-SP0

    which shows the SLEAP version and related information.

    To check whether SLEAP can use the GPU, run:

    python -c "import sleap; sleap.system_summary()"

    If the GPU is available, you’ll see output like the following.

    GPUs: 1/1 available
      Device: /physical_device:GPU:0
             Available: True
           Initialized: False
         Memory growth: None

    In future posts, I hope to walk through how to actually use SLEAP.

    Bonus

    For those who aren’t sure which computer to buy, I’ve started offering PC purchase consultations on Coconala!

    あなたの要望に合わせてパソコンを選び、提案します パソコン選びに困っている方々へ!様々な目的に対応できます!

    I often pick out computers and give advice about them, and many friends have told me I could make money doing PC consultations.
    That inspired me to give it a try!

    I’ll recommend a machine that fits both what you want to use it for and your budget.
    In particular, I’ve chosen and used many computers intended for machine learning.

    And if you’d like, I can also advise you on what to look for the next time you buy a computer.

    Computers I’ve picked out so far include lab analysis machines, everyday-use machines, game streaming machines, machines for incoming university students, machines that can run CAD for architecture students, and simple entry-level machines.

    I use both Mac and Windows, so I can discuss and recommend either one!

    Please feel free to make use of it.

  • How to Install Python Video Annotator

    How to Install Python Video Annotator

    What is Python Video Annotator?

    Python Video Annotator is an application that lets you analyze recorded videos and annotate events within them along a timeline.

    Researchers in neuroscience and ethology can use it to record videos of animals and then analyze and quantify their behavior.
    For example, suppose you are recording mouse behavior and want to score events such as tongue extension, tail flicks, or ear movements.
    When does each event occur in the video, and how long does it last?
    Watching the video and logging everything in Excel each time quickly becomes overwhelming when you have defined many behaviors.
    With a tool like this, you can annotate the video directly and export the timing and event information.

    There used to be an open-source application for scoring animal behavior called VCode.
    The problem, however, is that it no longer runs on current computer operating systems.

    Python Video Annotator runs on modern PCs, retains the features you need, and can additionally be combined with external sensor data (such as pressure gauges) and behavior quantification tools like DeepLabCut, making it a very useful tool for researchers.

    How to install

    The official site describes the installation procedure in detail, but I could not get it to install properly on my machine. (Installing it directly may have caused conflicts with packages already present on my system.)
    So instead I followed the approach described on the GitHub page: building a virtual environment with Anaconda and installing there.

    It sounds complicated when written out, but the steps are actually very simple.
    As of now (October 20, 2021) it does not appear to support the latest macOS (Big Sur 11.6), though this will likely be fixed soon.
    For that reason, I will use Windows as the example here.

    That said, once you have installed Anaconda on macOS and can use the conda command, the steps are essentially the same, so please refer to this guide once support arrives. (For details, see my previous post.)

    Install Anaconda and open the Anaconda Prompt.
    Then create a virtual environment and activate it.

    conda create -n videoannotator python=3.6
    conda activate videoannotator

    Next, install the required packages.

    pip install opencv-python-headless pyqt5==5.14.1 pyqtwebengine==5.14.0

    Then install Python Video Annotator.

    pip install python-video-annotator

    Once the various processes finish, the installation is complete.

    To launch it, activate the virtual environment first, then run the command.

    conda activate videoannotator
    start-video-annotator

    If the software starts up, you are all set.

  • Using Conda Commands on macOS

    Using Conda Commands on macOS

    Here I’ll walk through installing Anaconda on macOS and setting up the terminal environment.

    Installing Anaconda

    Go to the Anaconda homepage and scroll down to find the downloads.

    Choose the installer that matches your environment.
    Since this guide covers macOS, select the 64-bit Graphical Installer.

    Launch the installer and follow the instructions to complete the installation.

    There are two installers for Mac. The 64-bit Graphical Installer installs Anaconda through a GUI, which is the more familiar approach, and unless you have a reason to do otherwise, you can install Anaconda this way.

    The 64-bit Command Line Installer downloads a *.sh file.
    This is a shell script installer, used when you want to install from the terminal.

    Since installing software on Linux is normally done from the terminal, people who are used to Linux may find this route easier. (Probably.)

    Setting up the command line

    When you install on macOS, all that appears in your applications list is Anaconda-Navigator, and as it stands you can’t use Anaconda or Python from the terminal.

    If you want to use the conda command, for example to install packages, you need to activate Anaconda.
    To activate Anaconda (that is, to make the conda command available):

    conda activate

    Run the above.
    The conda command should now work.

    If activating the conda environment every time is a hassle, you can have it activate automatically.
    To do so, run the following in the terminal:

    ~/opt/anaconda3/bin/conda init シェル名

    Use the name of the shell you’re actually using. If you have no idea and have never touched this setting, you’re most likely on the default shell (zsh on macOS), so substitute that for the shell name.

    ~/opt/anaconda3/bin/conda init zsh

    Now, when you restart the terminal, the conda environment will be activated automatically and you won’t need to type conda activate.

    To turn off automatic activation,

    conda config --set auto_activate_base false

    run the following in the terminal.

  • try and except: Exception Handling in Python

    try and except: Exception Handling in Python

    Errors sometimes occur when writing programs in Python.
    Errors are actually helpful, since they tell you that your program has not been put together correctly,
    but there are times when an error stops your program from running at all.

    Sometimes code is syntactically correct yet still raises an error at runtime.
    An error raised when the syntax is grammatically incorrect is called a syntax error,
    while an error that is grammatically correct but logically wrong is called an exception.

    Examples of exceptions include:
    ・TypeError: the operands have incompatible types
    EX. word/2 , 4*number

    ・ZeroDivisionError: division by zero
    EX. 3/0

    ・ValueError: the type is correct, but the value is not appropriate
    EX. int(“string”)

    You can prevent errors before they occur and write logically correct programs by using conditional branching, but another option is to handle errors with exception handling when they do occur.

    That is where try, except comes in.

    try: 
        実行したい処理(例外を含むかもしれない)
    except エラー名:
        例外発生時に行う処理

    You use it like this.
    For example,

    try:
        print(10 / 0)
    except ZeroDivisionError:
        print('できませんでした')
    #出力
    できませんでした

    This is the result.
    Note, however, that except only catches the errors you specify
    (in this program, only ZeroDivisionError), so any other error will still be reported as an error when the program runs.

    When you expect more than one kind of error, add another “except ErrorName:” clause.

    try:
        print(10 / 0)
    except ZeroDivisionError:
        print('できませんでした')
    except ValueError:
        print('値がうまく合致しませんでした')

    You can specify multiple except clauses.

    If you leave out the exception name in the except clause, it will catch every exception.

    try:
        print(10 / 0)
    except:
        print('できませんでした')

    However, because this catches every exception, it will also hide errors that the programmer never anticipated, so use it with great care.

    There are also related keywords in the try-except syntax that let you specify what happens after an exception occurs.
    I will list them briefly here.
    I plan to cover them in detail in another blog post.

    • raise: deliberately raise an exception
    • pass: do nothing after an exception occurs
    • else: run only when no exception occurred
    • finally: always run, whether or not an exception occurred

  • I Want to Control Arduino with Python!

    I Want to Control Arduino with Python!

    Arduino is normally controlled with the Arduino language.
    Sooner or later, though, you will want to do something more complex, or control an Arduino from within another program.

    The module introduced here, pySerial, lets you carry out serial communication with a Raspberry Pi or an Arduino.
    Through serial communication, you can send commands from a Python program to an Arduino or Raspberry Pi and control them from Python.

    Here I introduce the basic code for doing this.
    Just being able to use this will greatly expand what your programs can do.

    How to install

    pip install pyserial

    You can install it by opening Python in a terminal or command prompt.
    Alternatively, you can install it from a terminal inside an IDE such as PyCharm.

    Example code

    This example is a program that turns an LED on and off at one-second intervals.
    I will test it by writing output to pin 13, which drives the LED built into the Arduino.
    On the Python side:

    import serial, time
    
    def main():
        #  COMポートを開く
        print("Open Port")
        ser = serial.Serial("COM3", 9600)
        while True:
            #  LED点灯
            ser.write(b"1")
            time.sleep(1)
            #  LED消灯
            ser.write(b"0")
            time.sleep(1)
    
        print("Close Port")
        ser.close()
    
    if __name__ == '__main__':
        main()

    serial.Serial specifies the port and the serial communication settings.
    For the Arduino UNO, specify 9600.
    This differs from board to board, so check it in the Arduino IDE.

    The b in b”1″ plays a crucial role.
    With the serial.write() function, numbers and strings must be converted to byte sequences before they can be sent over serial.
    The b prefix is what marks the value as a byte sequence.

    Since this program runs in an infinite loop, the LED keeps switching on and off every second until you stop the program.

    Next is the program on the Arduino side.

    void setup() {
      Serial.begin(9600);
      pinMode(13, OUTPUT);
      digitalWrite(13, LOW);  //  初期化
    }
    
    void loop() {
      byte var;
      var = Serial.read();
      switch(var){
        case '0':
          digitalWrite(13, LOW);
          break;
        case '1':
          digitalWrite(13, HIGH);
          break;
        default:
          break;
      }
    }

    Here I use a switch-case statement.
    It makes the program easier to follow.
    For details, take a look at my previous blog post.

    In this code, sending 0 sets the pin LOW and sending 1 sets it HIGH.
    Using this program as a base, you can control an Arduino from a Python program in all sorts of ways.

  • I Don’t Really Know Much, But I Want to Program in Python! (1)

    I Don’t Really Know Much, But I Want to Program in Python! (1)

    When you start learning programming, I think the first hurdle is figuring out where to begin.
    You search online, but then what? What software are you even supposed to use to write your code?

    I struggled with exactly that myself.

    Broadly speaking, there are two kinds of tools for writing programs.
    One is interactive, and the other is script-based. (I’m putting it this way for clarity. I’m not entirely sure it’s the correct terminology, though…)

    With the interactive type, you type one line and get one response back
    —you write and run the program one line at a time, over and over.

    With the script type, it’s like writing a whole essay and then getting feedback on it
    —you run the entire program at once.
    This script type is what most people picture when they think of programming: page after page of cryptic-looking code.

    The problem is, when you set out to write a script-type program, what software should you actually use?

    On top of that, beginner-level programming lessons often use the interactive style, but at the intermediate level you’re suddenly writing script code, and what to write it in is either left unstated or differs completely from book to book.

    For the interactive style, you use something already built into your computer, like Command Prompt or Terminal, and get started by typing “Python”.

    But what do you write script-type programs in?

    Most script-type programs are written in an IDE (integrated development environment). (When in doubt, searching “Python IDE recommended” will turn up plenty of articles.)

    The tools I normally use for writing script-type programs are
    Jupyter Notebook and PyCharm.
    (Apparently people are split on whether “Jupyter” is pronounced “joo-pih-ter” or “joo-py-ter”.)

    I use Jupyter Notebook for statistical analysis and plotting graphs, and PyCharm for driving hardware or writing more complex programs.

    Let me describe what each one is like.

    First, Jupyter Notebook.
    It comes bundled when you download Anaconda.
    When you launch Jupyter Notebook, a browser such as Safari or Firefox opens first.

    A list of the files on your computer is then displayed.
    Open the folder you want, and click “New” in the upper right.
    Then click “Python 3” and a screen like the image below will open.
    Saving this file gives you a *.ipynb file.

    You write your code in individual cells, and by clicking the RUN button at the top you can execute the program cell by cell.

    Being able to run code one cell at a time is a huge advantage when making graphs or doing statistics.
    That’s why I use it so often.

    I use PyCharm when opening *.py files.
    Note that the two tools open different file types, so be careful.

    For PyCharm, please refer to other sites.
    (I haven’t used it much yet, so I plan to cover how to use it in detail in a future post.)

    The Anaconda site is here

    The Jupyter Notebook site is here

    The PyCharm site is here
    For the download, choose the gray Community edition rather than Professional—that’s the free one.
    I’d recommend starting with that.

    I’ve been using Jupyter Notebook a lot lately, so I plan to keep posting updates, both as notes on my own learning and for anyone who wants to learn to program.