Tag: プログラミング

  • 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 specific reason otherwise, this is the one to use.

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

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

    Configuring the command line environment

    After installing on macOS, all you get is Anaconda-Navigator in your applications list — as is, you can’t use Anaconda or Python from the terminal.

    If you want to use the conda command to install packages and so on, you need to activate Anaconda.
    To activate Anaconda (i.e., 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 シェル名

    Run the above.
    Use the name of the shell you actually use. If you have no idea what that means and have never changed it, you’re probably 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 above in the terminal.

  • Arithmetic Operations in Bonsai

    Arithmetic Operations in Bonsai

    Bonsai is widely used for designing experiments in neuroscience and beyond.
    With Bonsai you can build sophisticated programs without writing code, and run them with precise synchronization.

    Because it is so simple, however, operations that would be trivial in ordinary programming—such as basic arithmetic—have to be done in a somewhat unintuitive way.
    Here I explain how to perform arithmetic operations, using MouseMove as an example.

    Below are the arithmetic operations in action: from top to bottom, addition, subtraction, multiplication, and division.

    For clarity, I used the X and Y values from MouseMove as an example here.
    In practice, though, you can perform calculations on values obtained from other sensors and the like.

    The key point in Bonsai is that you need to combine the two values with “Zip” before performing the operation.

    The contents of “Zip” take the form (value 1, value 2); its role is to bundle the two values at that moment into one.
    Each operation then computes value 1 and value 2 together.

    It is hard to explain in words, so try to get a rough intuition for it.
    Without “Zip”, the program would not know which value to multiply with which, so the calculation could not be performed. That is why the X and Y values must first be combined so that they correspond to each other.

    The names of the operators for each arithmetic operation are listed below.

    • Addition => Add
    • Subtraction => Subtract
    • Multiplication => Multiply
    • Division => Divide

    I have included a demonstration below for your reference.

    <Example in action>

  • I Don’t Really Know What I’m Doing, But I Want to Program in Python! (Part 1)

    I Don’t Really Know What I’m Doing, But I Want to Program in Python! (Part 1)

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

    I struggled with exactly that myself.

    There are broadly 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 technically correct terminology…)

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

    The script-based type is more like writing a whole essay and then getting feedback on it—you run the entire program at once.
    What most people picture when they think of a program is the script-based type, which looks like a long list of incomprehensible code.

    But when you decide to write a script-based program, the problem becomes: which software do you actually use?

    On top of that, beginner-level programming lessons often use the interactive style, and then at the intermediate level you’re suddenly writing scripts—yet what to write them in is often 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-based programs in?

    Most script-based programs are written in an IDE (integrated development environment). (When you’re stuck, searching for “Python IDE recommendations” turns up plenty of articles.)

    The ones I normally use for writing script-based programs are
    Jupyter Notebook and PyCharm.
    (Apparently some people pronounce “Jupyter” as “joo-pih-ter” and others as “joo-py-ter”.)

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

    Let me explain 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.

    Then a listing of the files on your computer appears.
    Open the folder you want, and click “New” in the upper right.
    Then click Python3, and a screen like the image below opens.
    Saving this file gives you a *.ipynb file.

    You write code into individual cells, and by clicking the RUN button at the top you can execute the program one cell at a time.

    Being able to run code cell by cell is a huge advantage when working with graphs and statistics.
    That’s why I use it so often.

    I use PyCharm for opening *.py files.
    Note that the two tools open different types of files.

    For PyCharm, please refer to other sites.
    (I haven’t used it much yet, so I’d like 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 downloading that one to start with.

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

  • Bonsai as a Visual Programming Language

    Bonsai as a Visual Programming Language

    Bonsai is a visual programming language introduced in a 2015 paper.
    It lets you acquire data from sensors and process it at the same time.

    One of Bonsai’s strengths is that you can build complex processing pipelines much like assembling a puzzle, and then check the results of that processing in real time.

    Bonsai can also be combined with other analysis software (DeepLabCut, Open Ephys, BonVision, and so on) and with hardware (cameras, controllers, microcontrollers, etc.), which makes it highly extensible.

    Because you can perform complex analysis and processing intuitively without having to learn a seemingly daunting programming language, anyone can use it easily, and it broadens the range of experiments you can run.

    The paper on Bonsai is available here.
    You can install Bonsai from here.

    Image taken from the paper.
    Each circle is called a node and represents an individual processing step.

    This is what the screen looks like when Bonsai is actually running (taken from the paper).
    On the left you can search for nodes and add the one you want; in the center you arrange nodes to build your pipeline while visualizing how it works; and on the right are the detailed settings for each node.

    The pop-up windows on the main screen show the execution status of each node — for a camera capture node, for example, it can display the live camera image.

    It may look difficult at first glance, but seen this way it turns out to be surprisingly simple.

    Now let’s actually install it.
    That said, all you have to do is download it from this site.
    Just open the downloaded “Bonsai-*.*.*.exe” file and run the installer.
    Note that security software such as Virus Buster may sometimes prevent the installation.

    Once Bonsai is installed, both Bonsai and Bonsai (x86) will appear in your list of applications.
    Either one is fine for normal use, but occasionally one of them will not run; when that happens, trying the other one often works.

    One drawback is that Bonsai currently runs only on Windows and does not support Linux or macOS.
    Support may come in the future, but this is something to keep in mind.

    From next time, I’ll start actually using Bonsai.