From 86a2a6b1ae0dd3e75f80ca675721e3829f8ec34f Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 15 Apr 2021 15:47:53 +0100 Subject: [PATCH 01/30] Create pylint.yml --- .github/workflows/pylint.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/pylint.yml diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000000..c16ba223d6 --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,22 @@ +name: Pylint + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.8 + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + - name: Analysing the code with pylint + run: | + pylint `ls -R|grep .py$|xargs` From c53df7922aaf7901e48063276f20f07fc45359a6 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 15 Apr 2021 15:55:06 +0100 Subject: [PATCH 02/30] Create python-package.yml --- .github/workflows/python-package.yml | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/workflows/python-package.yml diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml new file mode 100644 index 0000000000..4fd3d3d290 --- /dev/null +++ b/.github/workflows/python-package.yml @@ -0,0 +1,39 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python package + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.7, 3.8, 3.9] + + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest From 6dc0e6aa94ee92af2bcc2080e6332585f931ea7f Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 15 Apr 2021 16:06:06 +0100 Subject: [PATCH 03/30] Update pylint.yml --- .github/workflows/pylint.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index c16ba223d6..e557530cba 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -17,6 +17,7 @@ jobs: run: | python -m pip install --upgrade pip pip install pylint + pip install -e . - name: Analysing the code with pylint run: | pylint `ls -R|grep .py$|xargs` From 0aa7171c42575073885bfd10e0362ea296a558fc Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 22 Apr 2021 15:20:30 +0100 Subject: [PATCH 04/30] Update python-package.yml --- .github/workflows/python-package.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 4fd3d3d290..0be53adf5c 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -31,9 +31,9 @@ jobs: - name: Lint with flake8 run: | # stop the build if there are Python syntax errors or undefined names - flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + flake8 src --count --select=E9,F63,F7,F82 --show-source --statistics # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + flake8 src --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest From ca63efda054e54968c9a94f2db825523a9bbf1e9 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 22 Apr 2021 17:02:18 +0100 Subject: [PATCH 05/30] Update pylint.yml --- .github/workflows/pylint.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index e557530cba..abf6e7496b 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -17,7 +17,8 @@ jobs: run: | python -m pip install --upgrade pip pip install pylint - pip install -e . + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + # pip install -e . - name: Analysing the code with pylint run: | pylint `ls -R|grep .py$|xargs` From 2edc2959a9e3c1a4ed907711b23c455e87e4626a Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 22 Apr 2021 17:07:07 +0100 Subject: [PATCH 06/30] Update pylint.yml --- .github/workflows/pylint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index abf6e7496b..fd3c176b07 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -17,7 +17,7 @@ jobs: run: | python -m pip install --upgrade pip pip install pylint - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi # pip install -e . - name: Analysing the code with pylint run: | From 8a64e2e7740f8d66d95f9963cf30a779cf5bd7dc Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 15:58:59 +0100 Subject: [PATCH 07/30] Update pylint.yml --- .github/workflows/pylint.yml | 66 +++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index fd3c176b07..ff48711fd3 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -1,24 +1,72 @@ name: Pylint -on: [push] +on: + push: + branches: + - main + pull_request: + branches: + - main jobs: build: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} + + strategy: + matrix: + py-version: + - 3.5 + - 3.6 + - 3.7 + - 3.8 + - 3.9 + py-arch: + - x64 + mpi: + #- mpich + - openmpi + os: + - ubuntu-latest + #- ubuntu-18.04 + #- ubuntu-20.04 + #- macos-10.15 + #- macos-latest steps: - uses: actions/checkout@v2 + - name: Install MPI (${{ matrix.mpi }}) + run: apt install -y openmpi-bin libopenmpi-dev - name: Set up Python 3.8 - uses: actions/setup-python@v1 + uses: actions/setup-python@v2 with: - python-version: 3.8 + python-version: ${{ matrix.py-versionn }} + architecture: ${{ matrix.py-arch }} + - name: Install prerequisites + run: | + apt install -y + build-essential gfortran libgsl-dev cmake + libfftw3-3 libfftw3-dev + libgmp3-dev libmpfr6 libmpfr-dev + libhdf5-serial-dev hdf5-tools + libblas-dev liblapack-dev - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pylint - # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - # pip install -e . - - name: Analysing the code with pylint + python -m pip install + pylint + pycodestyle + pydocstyle + flake8 + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Install AMUSE run: | - pylint `ls -R|grep .py$|xargs` + pip install -e . + - name: Analyse the code with flake8 + run: flake8 src/amuse + - name: Analyse the code with pycodestyle + run: pycodestyle src/amuse + - name: Analyse the code with pydocstyle + run: pydocstyle src/amuse + - name: Analysing the code with pylint + run: pylint src/amuse From 85ef0f8644268ed7a4684bd8403d467199d62202 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:01:01 +0100 Subject: [PATCH 08/30] Update pylint.yml --- .github/workflows/pylint.yml | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index ff48711fd3..2bd9df3e6d 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -16,11 +16,11 @@ jobs: strategy: matrix: py-version: - - 3.5 - - 3.6 + #- 3.5 + #- 3.6 - 3.7 - - 3.8 - - 3.9 + #- 3.8 + #- 3.9 py-arch: - x64 mpi: @@ -35,21 +35,21 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install MPI (${{ matrix.mpi }}) - run: apt install -y openmpi-bin libopenmpi-dev + #- name: Install MPI (${{ matrix.mpi }}) + # run: apt install -y openmpi-bin libopenmpi-dev - name: Set up Python 3.8 uses: actions/setup-python@v2 with: python-version: ${{ matrix.py-versionn }} architecture: ${{ matrix.py-arch }} - - name: Install prerequisites - run: | - apt install -y - build-essential gfortran libgsl-dev cmake - libfftw3-3 libfftw3-dev - libgmp3-dev libmpfr6 libmpfr-dev - libhdf5-serial-dev hdf5-tools - libblas-dev liblapack-dev + #- name: Install prerequisites + # run: | + # apt install -y + # build-essential gfortran libgsl-dev cmake + # libfftw3-3 libfftw3-dev + # libgmp3-dev libmpfr6 libmpfr-dev + # libhdf5-serial-dev hdf5-tools + # libblas-dev liblapack-dev - name: Install dependencies run: | python -m pip install --upgrade pip @@ -59,9 +59,9 @@ jobs: pydocstyle flake8 if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - - name: Install AMUSE - run: | - pip install -e . + #- name: Install AMUSE + # run: | + # pip install -e . - name: Analyse the code with flake8 run: flake8 src/amuse - name: Analyse the code with pycodestyle From ebe8e9ed3c539bf39256c6991c647704fc7294e0 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:02:45 +0100 Subject: [PATCH 09/30] Update pylint.yml --- .github/workflows/pylint.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 2bd9df3e6d..201bf27c59 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -53,12 +53,8 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install - pylint - pycodestyle - pydocstyle - flake8 - if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + python -m pip install pylint pycodestyle pydocstyle flake8 + # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi #- name: Install AMUSE # run: | # pip install -e . From a5005e23e4a0a6424e16015aff9d6da37993fe0b Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:18:02 +0100 Subject: [PATCH 10/30] Update pylint.yml --- .github/workflows/pylint.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 201bf27c59..898089b36a 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -58,11 +58,11 @@ jobs: #- name: Install AMUSE # run: | # pip install -e . - - name: Analyse the code with flake8 - run: flake8 src/amuse + #- name: Analyse the code with flake8 + # run: flake8 src/amuse - name: Analyse the code with pycodestyle run: pycodestyle src/amuse - - name: Analyse the code with pydocstyle - run: pydocstyle src/amuse - - name: Analysing the code with pylint - run: pylint src/amuse + #- name: Analyse the code with pydocstyle + # run: pydocstyle src/amuse + #- name: Analysing the code with pylint + # run: pylint src/amuse From 8d9efc2abf49b4fd0b05ae2ba742dd33530780ed Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:29:25 +0100 Subject: [PATCH 11/30] Update pylint.yml --- .github/workflows/pylint.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 898089b36a..4fc58eefb0 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -40,7 +40,7 @@ jobs: - name: Set up Python 3.8 uses: actions/setup-python@v2 with: - python-version: ${{ matrix.py-versionn }} + python-version: ${{ matrix.py-version }} architecture: ${{ matrix.py-arch }} #- name: Install prerequisites # run: | @@ -59,10 +59,10 @@ jobs: # run: | # pip install -e . #- name: Analyse the code with flake8 - # run: flake8 src/amuse - - name: Analyse the code with pycodestyle - run: pycodestyle src/amuse + # run: flake8 `git diff --name-only --diff-filter=ACMRTUXB master | grep -E "(.py$)"` + #- name: Analyse the code with pycodestyle + # run: pycodestyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` #- name: Analyse the code with pydocstyle # run: pydocstyle src/amuse - #- name: Analysing the code with pylint - # run: pylint src/amuse + - name: Analysing the code with pylint + run: pylint `git diff --name-only --diff-filter=ACMRTUXB master | grep -E "(.py$)"` From 9ad96237207021af31db81a143d82b7c7a0a703f Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:31:04 +0100 Subject: [PATCH 12/30] Update pylint.yml --- .github/workflows/pylint.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 4fc58eefb0..a92a84d346 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -59,10 +59,10 @@ jobs: # run: | # pip install -e . #- name: Analyse the code with flake8 - # run: flake8 `git diff --name-only --diff-filter=ACMRTUXB master | grep -E "(.py$)"` + # run: flake8 `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` #- name: Analyse the code with pycodestyle # run: pycodestyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` #- name: Analyse the code with pydocstyle - # run: pydocstyle src/amuse + # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - name: Analysing the code with pylint - run: pylint `git diff --name-only --diff-filter=ACMRTUXB master | grep -E "(.py$)"` + run: pylint `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` From f2eac55de8ff59a13463f27d50887c546390432c Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:34:29 +0100 Subject: [PATCH 13/30] Update pylint.yml --- .github/workflows/pylint.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index a92a84d346..f2eec6bb61 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -65,4 +65,7 @@ jobs: #- name: Analyse the code with pydocstyle # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - name: Analysing the code with pylint - run: pylint `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` + run: | + for file in `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"`; do + pylint $file; + done From 942c9f4ba9311969d085d2eaeb431cb05fcd8424 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:35:57 +0100 Subject: [PATCH 14/30] Test change --- src/amuse/plot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index d40e4fe2e3..f2168102e8 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -1,3 +1,4 @@ +#! /env/bin python3 try: import matplotlib.pyplot as native_plot from mpl_toolkits.mplot3d import Axes3D From 4820e85490b412a89b842561ec70e25c87cd08a8 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 16:48:55 +0100 Subject: [PATCH 15/30] Update and rename pylint.yml to lint.yml --- .github/workflows/lint.yml | 31 ++++++++++++++++ .github/workflows/pylint.yml | 71 ------------------------------------ 2 files changed, 31 insertions(+), 71 deletions(-) create mode 100644 .github/workflows/lint.yml delete mode 100644 .github/workflows/pylint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000000..0a1137df94 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,31 @@ +name: Lint + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + run-linters: + name: Run linters + runs-on: ubuntu-latest + + steps: + - name: Check out Git repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v1 + with: + python-version: 3.8 + + - name: Install Python dependencies + run: pip install flake8 + + - name: Run linters + uses: wearerequired/lint-action@v1 + with: + flake8: true diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml deleted file mode 100644 index f2eec6bb61..0000000000 --- a/.github/workflows/pylint.yml +++ /dev/null @@ -1,71 +0,0 @@ -name: Pylint - -on: - push: - branches: - - main - pull_request: - branches: - - main - -jobs: - build: - - runs-on: ${{ matrix.os }} - - strategy: - matrix: - py-version: - #- 3.5 - #- 3.6 - - 3.7 - #- 3.8 - #- 3.9 - py-arch: - - x64 - mpi: - #- mpich - - openmpi - os: - - ubuntu-latest - #- ubuntu-18.04 - #- ubuntu-20.04 - #- macos-10.15 - #- macos-latest - - steps: - - uses: actions/checkout@v2 - #- name: Install MPI (${{ matrix.mpi }}) - # run: apt install -y openmpi-bin libopenmpi-dev - - name: Set up Python 3.8 - uses: actions/setup-python@v2 - with: - python-version: ${{ matrix.py-version }} - architecture: ${{ matrix.py-arch }} - #- name: Install prerequisites - # run: | - # apt install -y - # build-essential gfortran libgsl-dev cmake - # libfftw3-3 libfftw3-dev - # libgmp3-dev libmpfr6 libmpfr-dev - # libhdf5-serial-dev hdf5-tools - # libblas-dev liblapack-dev - - name: Install dependencies - run: | - python -m pip install --upgrade pip - python -m pip install pylint pycodestyle pydocstyle flake8 - # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - #- name: Install AMUSE - # run: | - # pip install -e . - #- name: Analyse the code with flake8 - # run: flake8 `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - #- name: Analyse the code with pycodestyle - # run: pycodestyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - #- name: Analyse the code with pydocstyle - # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - - name: Analysing the code with pylint - run: | - for file in `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"`; do - pylint $file; - done From 4a70e6455ccc3fa41b7b8c0d256ca1ebe275ec03 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 18:45:20 +0100 Subject: [PATCH 16/30] Update lint.yml --- .github/workflows/lint.yml | 77 +++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0a1137df94..db89e79762 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: Lint +name: Pylint on: push: @@ -9,23 +9,66 @@ on: - main jobs: - run-linters: - name: Run linters - runs-on: ubuntu-latest + build: - steps: - - name: Check out Git repository - uses: actions/checkout@v2 + runs-on: ${{ matrix.os }} - - name: Set up Python - uses: actions/setup-python@v1 - with: - python-version: 3.8 + strategy: + matrix: + py-version: + #- 3.5 + #- 3.6 + - 3.7 + #- 3.8 + #- 3.9 + py-arch: + - x64 + mpi: + #- mpich + - openmpi + os: + - ubuntu-latest + #- ubuntu-18.04 + #- ubuntu-20.04 + #- macos-10.15 + #- macos-latest - - name: Install Python dependencies - run: pip install flake8 + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: '0' - - name: Run linters - uses: wearerequired/lint-action@v1 - with: - flake8: true + #- name: Install MPI (${{ matrix.mpi }}) + # run: apt install -y openmpi-bin libopenmpi-dev + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.py-version }} + architecture: ${{ matrix.py-arch }} + #- name: Install prerequisites + # run: | + # apt install -y + # build-essential gfortran libgsl-dev cmake + # libfftw3-3 libfftw3-dev + # libgmp3-dev libmpfr6 libmpfr-dev + # libhdf5-serial-dev hdf5-tools + # libblas-dev liblapack-dev + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install pylint pycodestyle pydocstyle flake8 + # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + #- name: Install AMUSE + # run: | + # pip install -e . + #- name: Analyse the code with flake8 + # run: flake8 `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` + #- name: Analyse the code with pycodestyle + # run: pycodestyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` + #- name: Analyse the code with pydocstyle + # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` + - name: Analysing the code with pylint + run: | + for file in `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"`; do + pylint $file; + done From 05087ec39ea489712a592d29108b5679912f675e Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 18:46:36 +0100 Subject: [PATCH 17/30] Innocent change --- src/amuse/plot.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index f2168102e8..d40e4fe2e3 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -1,4 +1,3 @@ -#! /env/bin python3 try: import matplotlib.pyplot as native_plot from mpl_toolkits.mplot3d import Axes3D From a52c2ee05778294806aa7ea9b9ad7ffc95285e96 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 18:48:14 +0100 Subject: [PATCH 18/30] Update plot.py --- src/amuse/plot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index d40e4fe2e3..ab27f0f092 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -1,3 +1,4 @@ +#! /bin/env python3 try: import matplotlib.pyplot as native_plot from mpl_toolkits.mplot3d import Axes3D From 3ac3b6b8f922643657279ddee5c8ab3fc0440d5e Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:08:04 +0100 Subject: [PATCH 19/30] Update lint.yml --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index db89e79762..df48f33fa2 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -69,6 +69,6 @@ jobs: # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - name: Analysing the code with pylint run: | - for file in `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"`; do + for file in `git diff --name-only --diff-filter=ACMRTUXB HEAD | grep -E "(.py$)"`; do pylint $file; done From 4a1bfb911f61f058e6e3dc3613308f124a660472 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:08:56 +0100 Subject: [PATCH 20/30] Update lint.yml --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index db89e79762..df48f33fa2 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -69,6 +69,6 @@ jobs: # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - name: Analysing the code with pylint run: | - for file in `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"`; do + for file in `git diff --name-only --diff-filter=ACMRTUXB HEAD | grep -E "(.py$)"`; do pylint $file; done From a3adec062beac2d33e1e671c95fda7bd0a27ad2e Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:23:33 +0100 Subject: [PATCH 21/30] Update lint.yml --- .github/workflows/lint.yml | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index df48f33fa2..690055258c 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -37,38 +37,20 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: '0' - - #- name: Install MPI (${{ matrix.mpi }}) - # run: apt install -y openmpi-bin libopenmpi-dev - - name: Set up Python 3.8 + - uses: jitterbit/get-changed-files@v1 + id: files + with: + format: 'csv' + - name: Set up Python uses: actions/setup-python@v2 with: python-version: ${{ matrix.py-version }} architecture: ${{ matrix.py-arch }} - #- name: Install prerequisites - # run: | - # apt install -y - # build-essential gfortran libgsl-dev cmake - # libfftw3-3 libfftw3-dev - # libgmp3-dev libmpfr6 libmpfr-dev - # libhdf5-serial-dev hdf5-tools - # libblas-dev liblapack-dev - name: Install dependencies - run: | - python -m pip install --upgrade pip - python -m pip install pylint pycodestyle pydocstyle flake8 - # if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - #- name: Install AMUSE - # run: | - # pip install -e . - #- name: Analyse the code with flake8 - # run: flake8 `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - #- name: Analyse the code with pycodestyle - # run: pycodestyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` - #- name: Analyse the code with pydocstyle - # run: pydocstyle `git diff --name-only --diff-filter=ACMRTUXB main | grep -E "(.py$)"` + run: python -m pip install pylint - name: Analysing the code with pylint run: | - for file in `git diff --name-only --diff-filter=ACMRTUXB HEAD | grep -E "(.py$)"`; do - pylint $file; + mapfile -d ',' -t added_modified_files < <(printf '%s,' '${{ steps.files.outputs.added_modified }}') + for added_modified_file in "${added_modified_files[@]}"; do + pylint ${added_modified_file}; done From b7af2a7f208fb58aa8459493b4baaaff56b7a9e2 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:25:31 +0100 Subject: [PATCH 22/30] Update lint.yml --- .github/workflows/lint.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 690055258c..adb1270295 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -39,8 +39,8 @@ jobs: fetch-depth: '0' - uses: jitterbit/get-changed-files@v1 id: files - with: - format: 'csv' + with: + format: csv - name: Set up Python uses: actions/setup-python@v2 with: From 3a4ab424e7473eeaa8caa9cba726065cf542d3e5 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:28:59 +0100 Subject: [PATCH 23/30] Update lint.yml --- .github/workflows/lint.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index adb1270295..589045ee66 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -35,8 +35,6 @@ jobs: steps: - uses: actions/checkout@v2 - with: - fetch-depth: '0' - uses: jitterbit/get-changed-files@v1 id: files with: From 62398f5d0cc1cfedc3e16d8c37d10ac6728e68b8 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:31:17 +0100 Subject: [PATCH 24/30] Update plot.py --- src/amuse/plot.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index ab27f0f092..5765873496 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -1,4 +1,6 @@ #! /bin/env python3 +"""Plotting things +""" try: import matplotlib.pyplot as native_plot from mpl_toolkits.mplot3d import Axes3D From c705c569339875e02bd809733b82d35a05afdc6d Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:35:41 +0100 Subject: [PATCH 25/30] Update lint.yml --- .github/workflows/lint.yml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 589045ee66..d69ee78dff 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -38,7 +38,7 @@ jobs: - uses: jitterbit/get-changed-files@v1 id: files with: - format: csv + format: space-delimited - name: Set up Python uses: actions/setup-python@v2 with: @@ -48,7 +48,4 @@ jobs: run: python -m pip install pylint - name: Analysing the code with pylint run: | - mapfile -d ',' -t added_modified_files < <(printf '%s,' '${{ steps.files.outputs.added_modified }}') - for added_modified_file in "${added_modified_files[@]}"; do - pylint ${added_modified_file}; - done + pylint ${{ steps.files.outputs.added_modified }} From ab783eec773cab07b319a1024304e9166e2627a3 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:39:11 +0100 Subject: [PATCH 26/30] Update plot.py --- src/amuse/plot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index 5765873496..12d5e8cbe4 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -8,10 +8,10 @@ class FakePlotLibrary(object): def stub(self, *args, **kwargs): raise Exception("No plot library available") - + def __getattr__(self, name): raise Exception("matplotlib not present") - + native_plot = FakePlotLibrary() import numpy @@ -248,10 +248,10 @@ def circle_with_radius(x, y, radius, **kwargs): circle = native_plot.Circle((x, y), radius, **kwargs) return native_plot.gca().add_artist(circle) -def fix_xyz_axes(X, Y, Z): - if not (X.shape == Z.shape and Y.shape == Z.shape): - X, Y = numpy.meshgrid(X, Y) - return X, Y, Z +def fix_xyz_axes(x, y, z): + if not (x.shape == z.shape and y.shape == z.shape): + x, y = numpy.meshgrid(x, y) + return x, y, z def log_norm(Z, vmin, vmax): # for log scale, 0 is considered a missing value From 6ef715a6fe95cec7265508a224a1e8813564cc0b Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:41:30 +0100 Subject: [PATCH 27/30] Update plot.py --- src/amuse/plot.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index 12d5e8cbe4..fbb9181183 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -320,12 +320,11 @@ def imshow_color_plot(x, y, z, label=None, add_colorbar=False, **kwargs): native_plot.ylabel(UnitlessArgs.y_label()) if add_colorbar: - bar = native_plot.colorbar(cax) - bar.set_label(UnitlessArgs.z_label(label)) + colorbar = native_plot.colorbar(cax) + colorbar.set_label(UnitlessArgs.z_label(label)) - return cax, bar - else: - return cax + return cax, colorbar + return cax def pcolor(*args, **kwargs): stripped_args = UnitlessArgs.strip(*args) @@ -423,9 +422,9 @@ def sph_particles_plot(particles, u_range = None, min_size = 100, max_size = 100 current_axes = native_plot.gca() try: - current_axes.set_facecolor('#101010') + current_axes.set_facecolor('#101010') except: - current_axes.set_axis_bgcolor('#101010') + current_axes.set_axis_bgcolor('#101010') if width is not None: view = width * [-0.5, 0.5, -0.5, 0.5] From 356d4449d75399cc9762d673e1150721d9080575 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:48:33 +0100 Subject: [PATCH 28/30] Update plot.py --- src/amuse/plot.py | 278 +++++++++++++++++++++++++++++----------------- 1 file changed, 177 insertions(+), 101 deletions(-) diff --git a/src/amuse/plot.py b/src/amuse/plot.py index fbb9181183..f99bc0f9b7 100644 --- a/src/amuse/plot.py +++ b/src/amuse/plot.py @@ -1,6 +1,3 @@ -#! /bin/env python3 -"""Plotting things -""" try: import matplotlib.pyplot as native_plot from mpl_toolkits.mplot3d import Axes3D @@ -35,14 +32,20 @@ def __getattr__(self, name): auto_label = "{0}" custom_label = "{0} {1}" -class UnitlessArgs(object): + +class UnitlessArgs: current_plot = None @classmethod def strip(self, *args, **kwargs): if self.current_plot is native_plot.gca(): - args = [arg.as_quantity_in(unit) if quantities.is_quantity(arg) else arg - for arg, unit in map(lambda *x : tuple(x), args, self.arg_units)] + args = [ + arg.as_quantity_in(unit) + if quantities.is_quantity(arg) + else arg for arg, unit in map( + lambda *x: tuple(x), args, self.arg_units + ) + ] self.clear() self.current_plot = native_plot.gca() for arg in args: @@ -59,7 +62,6 @@ def strip(self, *args, **kwargs): return self.stripped_args - @classmethod def clear(self): self.stripped_args = [] @@ -116,12 +118,14 @@ def label(self, s, unit_name): else: return custom_label.format(s, unit_name) + def latex_support(): from matplotlib import rc - #rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) - #rc('font',**{'family':'serif','serif':['Palatino']}) + # rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) + # rc('font',**{'family':'serif','serif':['Palatino']}) rc('text', usetex=True) + def plot(*args, **kwargs): args = UnitlessArgs.strip(*args, **kwargs) result = native_plot.plot(*args, **kwargs) @@ -129,12 +133,14 @@ def plot(*args, **kwargs): native_plot.ylabel(UnitlessArgs.y_label()) return result + def plot3(*args, **kwargs): args = UnitlessArgs.strip(*args, **kwargs) fig = native_plot.figure() ax = fig.gca(projection='3d') return ax.plot(*args, **kwargs) + def semilogx(*args, **kwargs): args = UnitlessArgs.strip(*args, **kwargs) result = native_plot.semilogx(*args, **kwargs) @@ -142,6 +148,7 @@ def semilogx(*args, **kwargs): native_plot.ylabel(UnitlessArgs.y_label()) return result + def semilogy(*args, **kwargs): args = UnitlessArgs.strip(*args, **kwargs) result = native_plot.semilogy(*args, **kwargs) @@ -149,6 +156,7 @@ def semilogy(*args, **kwargs): native_plot.ylabel(UnitlessArgs.y_label()) return result + def loglog(*args, **kwargs): args = UnitlessArgs.strip(*args, **kwargs) result = native_plot.loglog(*args, **kwargs) @@ -156,27 +164,31 @@ def loglog(*args, **kwargs): native_plot.ylabel(UnitlessArgs.y_label()) return result + def scatter(x, y, **kwargs): - args = UnitlessArgs.strip(x,y) + args = UnitlessArgs.strip(x, y) result = native_plot.scatter(*UnitlessArgs.stripped_args, **kwargs) native_plot.xlabel(UnitlessArgs.x_label()) native_plot.ylabel(UnitlessArgs.y_label()) return result + def fill_between(x, y1, y2, **kwargs): - x, y1 = UnitlessArgs.strip(x,y1) + x, y1 = UnitlessArgs.strip(x, y1) y2 = UnitlessArgs.value_in_y_unit(y2) result = native_plot.fill_between(x, y1, y2, **kwargs) native_plot.xlabel(UnitlessArgs.x_label()) native_plot.ylabel(UnitlessArgs.y_label()) return result + def hist(x, **kwargs): args = UnitlessArgs.strip(x) result = native_plot.hist(args[0], **kwargs) UnitlessArgs.unitnames_of_args.append("") return result + def errorbar(*args, **kwargs): for label in ['yerr', 'xerr']: if label in kwargs: @@ -197,20 +209,24 @@ def errorbar(*args, **kwargs): native_plot.ylabel(UnitlessArgs.y_label()) return result + def text(x, y, s, **kwargs): strp_x, strp_y = UnitlessArgs.strip(x, y) return native_plot.text(strp_x, strp_y, s, **kwargs) + def xlabel(s, *args, **kwargs): - if not '[' in s: + if '[' not in s: s = UnitlessArgs.x_label(s) return native_plot.xlabel(s, *args, **kwargs) + def ylabel(s, *args, **kwargs): - if not '[' in s: + if '[' not in s: s = UnitlessArgs.y_label(s) return native_plot.ylabel(s, *args, **kwargs) + def xlim(*args, **kwargs): if len(UnitlessArgs.arg_units) == 0: raise AmuseException("Cannot call xlim function before plotting") @@ -222,6 +238,7 @@ def xlim(*args, **kwargs): native_plot.xlim(*args, **kwargs) + def ylim(*args, **kwargs): if len(UnitlessArgs.arg_units) == 0: raise AmuseException("Cannot call ylim function before plotting") @@ -233,25 +250,30 @@ def ylim(*args, **kwargs): native_plot.ylim(*args, **kwargs) + def axvline(x, **kwargs): x_number = UnitlessArgs.value_in_x_unit(x) return native_plot.axvline(x_number, **kwargs) + def axhline(y, **kwargs): y_number = UnitlessArgs.value_in_y_unit(y) return native_plot.axhline(y_number, **kwargs) + def circle_with_radius(x, y, radius, **kwargs): - x, y = UnitlessArgs.strip(x, y)[:2] + x, y = UnitlessArgs.strip(x, y)[:2] radius = UnitlessArgs.value_in_x_unit(radius) circle = native_plot.Circle((x, y), radius, **kwargs) return native_plot.gca().add_artist(circle) -def fix_xyz_axes(x, y, z): - if not (x.shape == z.shape and y.shape == z.shape): - x, y = numpy.meshgrid(x, y) - return x, y, z + +def fix_xyz_axes(X, Y, Z): + if not (X.shape == Z.shape and Y.shape == Z.shape): + X, Y = numpy.meshgrid(X, Y) + return X, Y, Z + def log_norm(Z, vmin, vmax): # for log scale, 0 is considered a missing value @@ -262,21 +284,22 @@ def log_norm(Z, vmin, vmax): from matplotlib.colors import LogNorm return masked_Z, LogNorm(vmin=vmin, vmax=vmax) + def fix_pcolor_norm(args, kwargs): args = [a for a in args] if 'vlog' in kwargs and kwargs['vlog']: zmin = kwargs.pop("vmin", None) zmax = kwargs.pop("vmax", None) - args[2], kwargs['norm']= log_norm(args[2], zmin, zmax) + args[2], kwargs['norm'] = log_norm(args[2], zmin, zmax) del kwargs['vlog'] else: for name in ("vmin", "vmax"): if name in kwargs: kwargs[name] = UnitlessArgs.value_in_z_unit(kwargs[name]) - return args, kwargs + def has_log_scaling(array): diff = numpy.diff(array) if numpy.all(diff - diff[0] < diff[0]/10.): @@ -288,6 +311,7 @@ def has_log_scaling(array): raise AmuseException("This method cannot be used for non regular arrays") + def imshow_color_plot(x, y, z, label=None, add_colorbar=False, **kwargs): """ Plot a density matrix as a color map using imshow, @@ -297,10 +321,10 @@ def imshow_color_plot(x, y, z, label=None, add_colorbar=False, **kwargs): X, Y, Z = UnitlessArgs.strip(x, y, z) X, Y, Z = fix_xyz_axes(X, Y, Z) - xlow = X[0,0] - xhigh = X[-1,-1] - ylow = Y[0,0] - yhigh = Y[-1,-1] + xlow = X[0, 0] + xhigh = X[-1, -1] + ylow = Y[0, 0] + yhigh = Y[-1, -1] extent = (xlow, xhigh, ylow, yhigh) (X, Y, Z), kwargs = fix_pcolor_norm((X, Y, Z), kwargs) @@ -311,20 +335,22 @@ def imshow_color_plot(x, y, z, label=None, add_colorbar=False, **kwargs): cax = native_plot.imshow(Z, **kwargs) - if has_log_scaling(X[0,:]): + if has_log_scaling(X[0, :]): native_plot.gca().set_xscale('log') - if has_log_scaling(Y[:,0]): + if has_log_scaling(Y[:, 0]): native_plot.gca().set_yscale('log') native_plot.xlabel(UnitlessArgs.x_label()) native_plot.ylabel(UnitlessArgs.y_label()) if add_colorbar: - colorbar = native_plot.colorbar(cax) - colorbar.set_label(UnitlessArgs.z_label(label)) + bar = native_plot.colorbar(cax) + bar.set_label(UnitlessArgs.z_label(label)) + + return cax, bar + else: + return cax - return cax, colorbar - return cax def pcolor(*args, **kwargs): stripped_args = UnitlessArgs.strip(*args) @@ -337,6 +363,7 @@ def pcolor(*args, **kwargs): return result + def pcolormesh(*args, **kwargs): stripped_args = UnitlessArgs.strip(*args) stripped_args, kwargs = fix_pcolor_norm(stripped_args, kwargs) @@ -348,8 +375,9 @@ def pcolormesh(*args, **kwargs): return result + def contour(*args, **kwargs): - if len(args)%2 == 0: + if len(args) % 2 == 0: stripped_args = UnitlessArgs.strip(*args[:-1]) levels = args[-1] @@ -360,7 +388,6 @@ def contour(*args, **kwargs): else: stripped_args = UnitlessArgs.strip(*args) - if 'levels' in kwargs: levels = kwargs['levels'] z_unit = UnitlessArgs.arg_units[-1] @@ -375,19 +402,23 @@ def contour(*args, **kwargs): return result + def smart_length_units_for_vector_quantity(quantity): - length_units = [units.Mpc, units.kpc, units.parsec, units.AU, units.RSun, units.km] + length_units = [units.Mpc, units.kpc, + units.parsec, units.AU, units.RSun, units.km] total_size = max(quantity) - min(quantity) for length_unit in length_units: if total_size > (1 | length_unit): return length_unit return units.m -def sph_particles_plot(particles, u_range = None, min_size = 100, max_size = 10000, - alpha = 0.1, gd_particles=None, width=None, view=None): + +def sph_particles_plot(particles, u_range=None, min_size=100, max_size=10000, + alpha=0.1, gd_particles=None, width=None, view=None): """ Very simple and fast procedure to make a plot of the hydrodynamics state of - a set of SPH particles. The particles must have the following attributes defined: + a set of SPH particles. The particles must have the following attributes + defined: position, u, h_smooth. For a more accurate plotting procedure, see for example: examples/applications/christmas_card_2010.py @@ -397,11 +428,12 @@ def sph_particles_plot(particles, u_range = None, min_size = 100, max_size = 100 :argument min_size: minimum size to use for plotting particles, in pixel**2 :argument max_size: maximum size to use for plotting particles, in pixel**2 :argument alpha: the opacity of each particle - :argument gd_particles: non-SPH particles can be indicated with white circles + :argument gd_particles: non-SPH particles can be indicated with white + circles :argument view: the (physical) region to plot [xmin, xmax, ymin, ymax] """ positions = particles.position - us = particles.u + us = particles.u h_smooths = particles.h_smooth x, y, z = positions.x, positions.y, positions.z z, x, y, us, h_smooths = z.sorted_with(x, y, us, h_smooths) @@ -411,14 +443,18 @@ def sph_particles_plot(particles, u_range = None, min_size = 100, max_size = 100 else: u_min, u_max = min(us), max(us) log_u = numpy.log((us / u_min)) / numpy.log((u_max / u_min)) - clipped_log_u = numpy.minimum(numpy.ones_like(log_u), numpy.maximum(numpy.zeros_like(log_u), log_u)) + clipped_log_u = numpy.minimum(numpy.ones_like( + log_u), numpy.maximum(numpy.zeros_like(log_u), log_u)) - red = 1.0 - clipped_log_u**4 - blue = clipped_log_u**4 + red = 1.0 - clipped_log_u**4 + blue = clipped_log_u**4 green = numpy.minimum(red, blue) colors = numpy.transpose(numpy.array([red, green, blue])) - n_pixels = native_plot.gcf().get_dpi() * native_plot.gcf().get_size_inches() + n_pixels = ( + native_plot.gcf().get_dpi() + * native_plot.gcf().get_size_inches() + ) current_axes = native_plot.gca() try: @@ -429,18 +465,23 @@ def sph_particles_plot(particles, u_range = None, min_size = 100, max_size = 100 view = width * [-0.5, 0.5, -0.5, 0.5] if view: - current_axes.set_aspect("equal", adjustable = "box") + current_axes.set_aspect("equal", adjustable="box") length_unit = smart_length_units_for_vector_quantity(view) - current_axes.set_xlim(view[0].value_in(length_unit), + current_axes.set_xlim( + view[0].value_in(length_unit), view[1].value_in(length_unit), emit=True, auto=False) - current_axes.set_ylim(view[2].value_in(length_unit), + current_axes.set_ylim( + view[2].value_in(length_unit), view[3].value_in(length_unit), emit=True, auto=False) - phys_to_pix2 = n_pixels[0]*n_pixels[1] / ((view[1]-view[0])**2 + (view[3]-view[2])**2) + phys_to_pix2 = n_pixels[0]*n_pixels[1] / \ + ((view[1]-view[0])**2 + (view[3]-view[2])**2) else: - current_axes.set_aspect("equal", adjustable = "datalim") + current_axes.set_aspect("equal", adjustable="datalim") length_unit = smart_length_units_for_vector_quantity(x) - phys_to_pix2 = n_pixels[0]*n_pixels[1] / ((max(x)-min(x))**2 + (max(y)-min(y))**2) - sizes = numpy.minimum(numpy.maximum((h_smooths**2 * phys_to_pix2), min_size), max_size) + phys_to_pix2 = n_pixels[0]*n_pixels[1] / \ + ((max(x)-min(x))**2 + (max(y)-min(y))**2) + sizes = numpy.minimum(numpy.maximum( + (h_smooths**2 * phys_to_pix2), min_size), max_size) x = x.as_quantity_in(length_unit) y = y.as_quantity_in(length_unit) @@ -450,7 +491,9 @@ def sph_particles_plot(particles, u_range = None, min_size = 100, max_size = 100 xlabel('x') ylabel('y') -def convert_particles_to_pynbody_data(particles, length_unit=units.kpc, pynbody_unit="kpc"): + +def convert_particles_to_pynbody_data( + particles, length_unit=units.kpc, pynbody_unit="kpc"): if not HAS_PYNBODY: raise AmuseException("Couldn't find pynbody") @@ -460,59 +503,76 @@ def convert_particles_to_pynbody_data(particles, length_unit=units.kpc, pynbody_ pynbody_data = new(dm=len(particles)) pynbody_data._filename = "AMUSE" if hasattr(particles, "mass"): - pynbody_data['mass'] = SimArray(particles.mass.value_in(units.MSun), "Msol") + pynbody_data['mass'] = SimArray( + particles.mass.value_in(units.MSun), "Msol") if hasattr(particles, "position"): - pynbody_data['x'] = SimArray(particles.x.value_in(length_unit), pynbody_unit) - pynbody_data['y'] = SimArray(particles.y.value_in(length_unit), pynbody_unit) - pynbody_data['z'] = SimArray(particles.z.value_in(length_unit), pynbody_unit) + pynbody_data['x'] = SimArray( + particles.x.value_in(length_unit), pynbody_unit) + pynbody_data['y'] = SimArray( + particles.y.value_in(length_unit), pynbody_unit) + pynbody_data['z'] = SimArray( + particles.z.value_in(length_unit), pynbody_unit) if hasattr(particles, "velocity"): - pynbody_data['vx'] = SimArray(particles.vx.value_in(units.km / units.s), "km s^-1") - pynbody_data['vy'] = SimArray(particles.vy.value_in(units.km / units.s), "km s^-1") - pynbody_data['vz'] = SimArray(particles.vz.value_in(units.km / units.s), "km s^-1") + pynbody_data['vx'] = SimArray( + particles.vx.value_in(units.km / units.s), "km s^-1") + pynbody_data['vy'] = SimArray( + particles.vy.value_in(units.km / units.s), "km s^-1") + pynbody_data['vz'] = SimArray( + particles.vz.value_in(units.km / units.s), "km s^-1") if hasattr(particles, "h_smooth"): - pynbody_data['smooth'] = SimArray(particles.h_smooth.value_in(length_unit), pynbody_unit) + pynbody_data['smooth'] = SimArray( + particles.h_smooth.value_in(length_unit), pynbody_unit) if hasattr(particles, "rho"): - pynbody_data['rho'] = SimArray(particles.rho.value_in(units.g / units.cm**3), - "g cm^-3") + pynbody_data['rho'] = SimArray( + particles.rho.value_in(units.g / units.cm**3), "g cm^-3") if hasattr(particles, "temp"): pynbody_data['temp'] = SimArray(particles.temp.value_in(units.K), "K") elif hasattr(particles, "u"): -# pynbody_data['u'] = SimArray(particles.u.value_in(units.km**2 / units.s**2), "km^2 s^-2") temp = 2.0/3.0 * particles.u * mu() / constants.kB pynbody_data['temp'] = SimArray(temp.value_in(units.K), "K") return pynbody_data -def mu(X = None, Y = 0.25, Z = 0.02, x_ion = 0.1): + +def mu(X=None, Y=0.25, Z=0.02, x_ion=0.1): """ - Compute the mean molecular weight in kg (the average weight of particles in a gas) - X, Y, and Z are the mass fractions of Hydrogen, of Helium, and of metals, respectively. + Compute the mean molecular weight in kg (the average weight of particles in + a gas) X, Y, and Z are the mass fractions of Hydrogen, of Helium, and of + metals, respectively. x_ion is the ionisation fraction (0 < x_ion < 1), 1 means fully ionised """ if X is None: X = 1.0 - Y - Z elif abs(X + Y + Z - 1.0) > 1e-6: - raise AmuseException("Error in calculating mu: mass fractions do not sum to 1.0") - return constants.proton_mass / (X*(1.0+x_ion) + Y*(1.0+2.0*x_ion)/4.0 + Z*x_ion/2.0) + raise AmuseException( + "Error in calculating mu: mass fractions do not sum to 1.0") + return ( + constants.proton_mass + / (X*(1.0+x_ion) + Y*(1.0+2.0*x_ion)/4.0 + Z*x_ion/2.0) + ) + def _smart_length_units_for_pynbody_data(length): length_units = [(units.Gpc, "Gpc"), (units.Mpc, "Mpc"), (units.kpc, "kpc"), - (units.parsec, "pc"), (units.AU, "au"), (1.0e9*units.m, "1.0e9 m"), - (1000*units.km, "1000 km"), (units.km, "km")] + (units.parsec, "pc"), (units.AU, + "au"), (1.0e9*units.m, "1.0e9 m"), + (1000*units.km, "1000 km"), (units.km, "km")] for length_unit, pynbody_unit in length_units: if length > (1 | length_unit): return length_unit, pynbody_unit return units.m, "m" + def pynbody_column_density_plot(particles, width=None, qty='rho', units=None, - sideon=False, faceon=False, **kwargs): + sideon=False, faceon=False, **kwargs): if not HAS_PYNBODY: raise AmuseException("Couldn't find pynbody") if width is None: width = 2.0 * particles.position.lengths_squared().amax().sqrt() length_unit, pynbody_unit = _smart_length_units_for_pynbody_data(width) - pyndata = convert_particles_to_pynbody_data(particles, length_unit, pynbody_unit) - UnitlessArgs.strip([1]|length_unit, [1]|length_unit) + pyndata = convert_particles_to_pynbody_data( + particles, length_unit, pynbody_unit) + UnitlessArgs.strip([1] | length_unit, [1] | length_unit) if sideon: function = pynbody_sph.sideon_image @@ -524,28 +584,31 @@ def pynbody_column_density_plot(particles, width=None, qty='rho', units=None, if units is None and qty == 'rho': units = 'm_p cm^-2' - result = function(pyndata, width=width.value_in(length_unit), qty=qty, units=units, **kwargs) + result = function(pyndata, width=width.value_in( + length_unit), qty=qty, units=units, **kwargs) UnitlessArgs.current_plot = native_plot.gca() return result + def effective_iso_potential_plot(gravity_code, - omega, - center_of_rotation = [0, 0]|units.AU, - xlim = [-1.5, 1.5] | units.AU, - ylim = [-1.5, 1.5] | units.AU, - resolution = [1000, 1000], - number_of_contours = 20, - fraction_screen_filled = 0.5, - quadratic_contour_levels = True, - contour_kwargs = dict(), - omega2 = None, - center_of_rotation2 = [0, 0]|units.AU, - fraction_screen_filled2 = 0.2, - projection3D=False): + omega, + center_of_rotation=[0, 0] | units.AU, + xlim=[-1.5, 1.5] | units.AU, + ylim=[-1.5, 1.5] | units.AU, + resolution=[1000, 1000], + number_of_contours=20, + fraction_screen_filled=0.5, + quadratic_contour_levels=True, + contour_kwargs=dict(), + omega2=None, + center_of_rotation2=[0, 0] | units.AU, + fraction_screen_filled2=0.2, + projection3D=False): """ - Create a contour plot of the effective potential of particles in a gravity code. - The code needs to support 'get_potential_at_point' only, so it can also be an - instance of Bridge. + Create a contour plot of the effective potential of particles in a gravity + code. + The code needs to support 'get_potential_at_point' only, so it can also be + an instance of Bridge. :argument gravity_code: an instance of a gravity code :argument omega: The angular velocity of the system @@ -568,16 +631,20 @@ def effective_iso_potential_plot(gravity_code, y = (y_num | UnitlessArgs.arg_units[1]).flatten() zeros = x.aszeros() potential = gravity_code.get_potential_at_point(zeros, x, y, zeros) - potential -= omega**2 * ((x-center_of_rotation[0])**2 + (y-center_of_rotation[1])**2) / 2.0 + potential -= omega**2 * \ + ((x-center_of_rotation[0])**2 + (y-center_of_rotation[1])**2) / 2.0 if projection3D: from matplotlib import cm ax = native_plot.gca(projection='3d') Z = potential.number.reshape(resolution[::-1]) - levels = set_contour_levels(potential, number_of_contours, fraction_screen_filled, quadratic_contour_levels) + levels = set_contour_levels( + potential, number_of_contours, fraction_screen_filled, quadratic_contour_levels) Z = numpy.maximum(Z, levels[0]) - ax.plot_surface(x_num, y_num, Z, rstride=1, cstride=1, cmap=cm.spectral, - linewidth=0, antialiased=False, vmin=levels[0], vmax=3*levels[-1]-2*levels[0]) + ax.plot_surface( + x_num, y_num, Z, rstride=1, cstride=1, cmap=cm.spectral, + linewidth=0, antialiased=False, vmin=levels[0], vmax=3*levels[-1]-2 + * levels[0]) ax.set_xlabel('X') ax.set_xlim(-1, 1) @@ -587,26 +654,35 @@ def effective_iso_potential_plot(gravity_code, ax.set_zlim(levels[0], levels[-1]) return potential - levels = set_contour_levels(potential, number_of_contours, fraction_screen_filled, quadratic_contour_levels) - CS = native_plot.contour(x_num, y_num, potential.number.reshape(resolution[::-1]), levels, **contour_kwargs) - #~native_plot.clabel(CS, inline=1, fontsize=10) + levels = set_contour_levels( + potential, number_of_contours, fraction_screen_filled, + quadratic_contour_levels + ) + CS = native_plot.contour(x_num, y_num, potential.number.reshape( + resolution[::-1]), levels, **contour_kwargs) if omega2 is None: return potential - potential2 = potential - omega2**2 * ((x-center_of_rotation2[0])**2 + (y-center_of_rotation2[1])**2) / 2.0 - #~levels = set_contour_levels(potential, number_of_contours2, fraction_screen_filled2, quadratic_contour_levels2) - levels = set_contour_levels(potential2, number_of_contours, fraction_screen_filled2, quadratic_contour_levels) - CS = native_plot.contour(x_num, y_num, potential2.number.reshape(resolution[::-1]), levels, **contour_kwargs) + potential2 = potential - omega2**2 * \ + ((x-center_of_rotation2[0])**2 + (y-center_of_rotation2[1])**2) / 2.0 + # ~levels = set_contour_levels(potential, number_of_contours2, fraction_screen_filled2, quadratic_contour_levels2) + levels = set_contour_levels( + potential2, number_of_contours, fraction_screen_filled2, + quadratic_contour_levels) + CS = native_plot.contour( + x_num, y_num, potential2.number.reshape( + resolution[::-1]), levels, **contour_kwargs) return potential.reshape(resolution[::-1]), potential2.reshape(resolution[::-1]) + def set_contour_levels(potential, number_of_contours, fraction_screen_filled, quadratic_contour_levels): uniform = numpy.linspace(0.0, 1.0, number_of_contours) V_max = potential.amax().number - V_min = potential.sorted().number[int(len(potential)*(1-fraction_screen_filled))] + V_min = potential.sorted().number[int( + len(potential)*(1-fraction_screen_filled))] if quadratic_contour_levels: levels = V_min + (V_max-V_min) * uniform * (2 - uniform) else: levels = V_min + (V_max-V_min) * uniform return levels - From 764031319238afd4ead5bfc2bc1eda100e244edc Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:50:13 +0100 Subject: [PATCH 29/30] Delete plot.py --- src/amuse/plot.py | 688 ---------------------------------------------- 1 file changed, 688 deletions(-) delete mode 100644 src/amuse/plot.py diff --git a/src/amuse/plot.py b/src/amuse/plot.py deleted file mode 100644 index f99bc0f9b7..0000000000 --- a/src/amuse/plot.py +++ /dev/null @@ -1,688 +0,0 @@ -try: - import matplotlib.pyplot as native_plot - from mpl_toolkits.mplot3d import Axes3D -except ImportError: - class FakePlotLibrary(object): - def stub(self, *args, **kwargs): - raise Exception("No plot library available") - - def __getattr__(self, name): - raise Exception("matplotlib not present") - - native_plot = FakePlotLibrary() - -import numpy -try: - from pynbody.array import SimArray - from pynbody.snapshot import SimSnap - try: - from pynbody.snapshot import new - except: - from pynbody.snapshot import _new as new - import pynbody.plot.sph as pynbody_sph - HAS_PYNBODY = True -except ImportError: - HAS_PYNBODY = False - -from amuse.support.exceptions import AmuseException -from amuse.units import units, constants -from amuse.units import quantities -from amuse.support import console - -auto_label = "{0}" -custom_label = "{0} {1}" - - -class UnitlessArgs: - current_plot = None - - @classmethod - def strip(self, *args, **kwargs): - if self.current_plot is native_plot.gca(): - args = [ - arg.as_quantity_in(unit) - if quantities.is_quantity(arg) - else arg for arg, unit in map( - lambda *x: tuple(x), args, self.arg_units - ) - ] - self.clear() - self.current_plot = native_plot.gca() - for arg in args: - if quantities.is_quantity(arg): - arg = console.current_printing_strategy.convert_quantity(arg) - - self.stripped_args.append(arg.value_in(arg.unit)) - self.arg_units.append(arg.unit) - self.unitnames_of_args.append("["+str(arg.unit)+"]") - else: - self.stripped_args.append(arg) - self.arg_units.append(None) - self.unitnames_of_args.append("") - - return self.stripped_args - - @classmethod - def clear(self): - self.stripped_args = [] - self.arg_units = [] - self.unitnames_of_args = [] - - @classmethod - def value_in(self, unit, *args): - if len(args) < 1: - return args - - if unit is not None: - args = [arg.value_in(unit) for arg in args] - - if len(args) > 1: - return args - else: - return args[0] - - @classmethod - def value_in_x_unit(self, *args): - return self.value_in(UnitlessArgs.arg_units[0], *args) - - @classmethod - def value_in_y_unit(self, *args): - return self.value_in(UnitlessArgs.arg_units[1], *args) - - @classmethod - def value_in_z_unit(self, *args): - return self.value_in(UnitlessArgs.arg_units[2], *args) - - @classmethod - def x_label(self, s=None): - unit_name = self.unitnames_of_args[0] - - return self.label(s, unit_name) - - @classmethod - def y_label(self, s=None): - unit_name = self.unitnames_of_args[1] - - return self.label(s, unit_name) - - @classmethod - def z_label(self, s=None): - unit_name = self.unitnames_of_args[2] - - return self.label(s, unit_name) - - @classmethod - def label(self, s, unit_name): - if s is None: - return auto_label.format(unit_name) - else: - return custom_label.format(s, unit_name) - - -def latex_support(): - from matplotlib import rc - # rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']}) - # rc('font',**{'family':'serif','serif':['Palatino']}) - rc('text', usetex=True) - - -def plot(*args, **kwargs): - args = UnitlessArgs.strip(*args, **kwargs) - result = native_plot.plot(*args, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def plot3(*args, **kwargs): - args = UnitlessArgs.strip(*args, **kwargs) - fig = native_plot.figure() - ax = fig.gca(projection='3d') - return ax.plot(*args, **kwargs) - - -def semilogx(*args, **kwargs): - args = UnitlessArgs.strip(*args, **kwargs) - result = native_plot.semilogx(*args, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def semilogy(*args, **kwargs): - args = UnitlessArgs.strip(*args, **kwargs) - result = native_plot.semilogy(*args, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def loglog(*args, **kwargs): - args = UnitlessArgs.strip(*args, **kwargs) - result = native_plot.loglog(*args, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def scatter(x, y, **kwargs): - args = UnitlessArgs.strip(x, y) - result = native_plot.scatter(*UnitlessArgs.stripped_args, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def fill_between(x, y1, y2, **kwargs): - x, y1 = UnitlessArgs.strip(x, y1) - y2 = UnitlessArgs.value_in_y_unit(y2) - result = native_plot.fill_between(x, y1, y2, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def hist(x, **kwargs): - args = UnitlessArgs.strip(x) - result = native_plot.hist(args[0], **kwargs) - UnitlessArgs.unitnames_of_args.append("") - return result - - -def errorbar(*args, **kwargs): - for label in ['yerr', 'xerr']: - if label in kwargs: - args += (kwargs.pop(label),) - else: - args += (None,) - - yerr, xerr = args[2:4] - - args1 = UnitlessArgs.strip(*args[:2]) - if xerr is not None: - xerr = UnitlessArgs.value_in_x_unit(xerr) - if yerr is not None: - yerr = UnitlessArgs.value_in_y_unit(yerr) - args = args1 + [yerr, xerr] - result = native_plot.errorbar(*args, **kwargs) - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - return result - - -def text(x, y, s, **kwargs): - strp_x, strp_y = UnitlessArgs.strip(x, y) - return native_plot.text(strp_x, strp_y, s, **kwargs) - - -def xlabel(s, *args, **kwargs): - if '[' not in s: - s = UnitlessArgs.x_label(s) - return native_plot.xlabel(s, *args, **kwargs) - - -def ylabel(s, *args, **kwargs): - if '[' not in s: - s = UnitlessArgs.y_label(s) - return native_plot.ylabel(s, *args, **kwargs) - - -def xlim(*args, **kwargs): - if len(UnitlessArgs.arg_units) == 0: - raise AmuseException("Cannot call xlim function before plotting") - - args = UnitlessArgs.value_in_x_unit(*args) - for name in ("xmin", "xmax"): - if name in kwargs: - kwargs[name] = UnitlessArgs.value_in_x_unit(kwargs[name]) - - native_plot.xlim(*args, **kwargs) - - -def ylim(*args, **kwargs): - if len(UnitlessArgs.arg_units) == 0: - raise AmuseException("Cannot call ylim function before plotting") - - args = UnitlessArgs.value_in_y_unit(*args) - for name in ("ymin", "ymax"): - if name in kwargs: - kwargs[name] = UnitlessArgs.value_in_y_unit(kwargs[name]) - - native_plot.ylim(*args, **kwargs) - - -def axvline(x, **kwargs): - x_number = UnitlessArgs.value_in_x_unit(x) - return native_plot.axvline(x_number, **kwargs) - - -def axhline(y, **kwargs): - y_number = UnitlessArgs.value_in_y_unit(y) - return native_plot.axhline(y_number, **kwargs) - - -def circle_with_radius(x, y, radius, **kwargs): - x, y = UnitlessArgs.strip(x, y)[:2] - radius = UnitlessArgs.value_in_x_unit(radius) - - circle = native_plot.Circle((x, y), radius, **kwargs) - return native_plot.gca().add_artist(circle) - - -def fix_xyz_axes(X, Y, Z): - if not (X.shape == Z.shape and Y.shape == Z.shape): - X, Y = numpy.meshgrid(X, Y) - return X, Y, Z - - -def log_norm(Z, vmin, vmax): - # for log scale, 0 is considered a missing value - masked_Z = numpy.ma.masked_equal(Z, 0.0, copy=False) - vmin = UnitlessArgs.value_in_z_unit(vmin) if vmin else masked_Z.min() - vmax = UnitlessArgs.value_in_z_unit(vmax) if vmax else masked_Z.max() - - from matplotlib.colors import LogNorm - return masked_Z, LogNorm(vmin=vmin, vmax=vmax) - - -def fix_pcolor_norm(args, kwargs): - args = [a for a in args] - if 'vlog' in kwargs and kwargs['vlog']: - zmin = kwargs.pop("vmin", None) - zmax = kwargs.pop("vmax", None) - args[2], kwargs['norm'] = log_norm(args[2], zmin, zmax) - del kwargs['vlog'] - else: - for name in ("vmin", "vmax"): - if name in kwargs: - kwargs[name] = UnitlessArgs.value_in_z_unit(kwargs[name]) - - return args, kwargs - - -def has_log_scaling(array): - diff = numpy.diff(array) - if numpy.all(diff - diff[0] < diff[0]/10.): - return False - - logdiff = numpy.diff(numpy.log10(array)) - if numpy.all(logdiff - logdiff[0] < logdiff[0]/10.): - return True - - raise AmuseException("This method cannot be used for non regular arrays") - - -def imshow_color_plot(x, y, z, label=None, add_colorbar=False, **kwargs): - """ - Plot a density matrix as a color map using imshow, - this gives a smoother image then pcolor(mesh) - It only works if x and y are regular (linear or logarithmic). - """ - X, Y, Z = UnitlessArgs.strip(x, y, z) - X, Y, Z = fix_xyz_axes(X, Y, Z) - - xlow = X[0, 0] - xhigh = X[-1, -1] - ylow = Y[0, 0] - yhigh = Y[-1, -1] - extent = (xlow, xhigh, ylow, yhigh) - - (X, Y, Z), kwargs = fix_pcolor_norm((X, Y, Z), kwargs) - - kwargs['origin'] = 'lower' - kwargs['aspect'] = 'auto' - kwargs['extent'] = extent - - cax = native_plot.imshow(Z, **kwargs) - - if has_log_scaling(X[0, :]): - native_plot.gca().set_xscale('log') - if has_log_scaling(Y[:, 0]): - native_plot.gca().set_yscale('log') - - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - - if add_colorbar: - bar = native_plot.colorbar(cax) - bar.set_label(UnitlessArgs.z_label(label)) - - return cax, bar - else: - return cax - - -def pcolor(*args, **kwargs): - stripped_args = UnitlessArgs.strip(*args) - stripped_args, kwargs = fix_pcolor_norm(stripped_args, kwargs) - - result = native_plot.pcolor(*stripped_args, **kwargs) - - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - - return result - - -def pcolormesh(*args, **kwargs): - stripped_args = UnitlessArgs.strip(*args) - stripped_args, kwargs = fix_pcolor_norm(stripped_args, kwargs) - - result = native_plot.pcolormesh(*stripped_args, **kwargs) - - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - - return result - - -def contour(*args, **kwargs): - if len(args) % 2 == 0: - stripped_args = UnitlessArgs.strip(*args[:-1]) - - levels = args[-1] - z_unit = UnitlessArgs.arg_units[-1] - - if quantities.is_quantity(levels): - stripped_args.append(levels.value_in(z_unit)) - else: - stripped_args = UnitlessArgs.strip(*args) - - if 'levels' in kwargs: - levels = kwargs['levels'] - z_unit = UnitlessArgs.arg_units[-1] - - if quantities.is_quantity(levels): - kwargs['levels'] = levels.value_in(z_unit) - - result = native_plot.contour(*stripped_args, **kwargs) - - native_plot.xlabel(UnitlessArgs.x_label()) - native_plot.ylabel(UnitlessArgs.y_label()) - - return result - - -def smart_length_units_for_vector_quantity(quantity): - length_units = [units.Mpc, units.kpc, - units.parsec, units.AU, units.RSun, units.km] - total_size = max(quantity) - min(quantity) - for length_unit in length_units: - if total_size > (1 | length_unit): - return length_unit - return units.m - - -def sph_particles_plot(particles, u_range=None, min_size=100, max_size=10000, - alpha=0.1, gd_particles=None, width=None, view=None): - """ - Very simple and fast procedure to make a plot of the hydrodynamics state of - a set of SPH particles. The particles must have the following attributes - defined: - position, u, h_smooth. - For a more accurate plotting procedure, see for example: - examples/applications/christmas_card_2010.py - - :argument particles: the SPH particles to be plotted - :argument u_range: range of internal energy for color scale [umin, umax] - :argument min_size: minimum size to use for plotting particles, in pixel**2 - :argument max_size: maximum size to use for plotting particles, in pixel**2 - :argument alpha: the opacity of each particle - :argument gd_particles: non-SPH particles can be indicated with white - circles - :argument view: the (physical) region to plot [xmin, xmax, ymin, ymax] - """ - positions = particles.position - us = particles.u - h_smooths = particles.h_smooth - x, y, z = positions.x, positions.y, positions.z - z, x, y, us, h_smooths = z.sorted_with(x, y, us, h_smooths) - - if u_range: - u_min, u_max = u_range - else: - u_min, u_max = min(us), max(us) - log_u = numpy.log((us / u_min)) / numpy.log((u_max / u_min)) - clipped_log_u = numpy.minimum(numpy.ones_like( - log_u), numpy.maximum(numpy.zeros_like(log_u), log_u)) - - red = 1.0 - clipped_log_u**4 - blue = clipped_log_u**4 - green = numpy.minimum(red, blue) - - colors = numpy.transpose(numpy.array([red, green, blue])) - n_pixels = ( - native_plot.gcf().get_dpi() - * native_plot.gcf().get_size_inches() - ) - - current_axes = native_plot.gca() - try: - current_axes.set_facecolor('#101010') - except: - current_axes.set_axis_bgcolor('#101010') - if width is not None: - view = width * [-0.5, 0.5, -0.5, 0.5] - - if view: - current_axes.set_aspect("equal", adjustable="box") - length_unit = smart_length_units_for_vector_quantity(view) - current_axes.set_xlim( - view[0].value_in(length_unit), - view[1].value_in(length_unit), emit=True, auto=False) - current_axes.set_ylim( - view[2].value_in(length_unit), - view[3].value_in(length_unit), emit=True, auto=False) - phys_to_pix2 = n_pixels[0]*n_pixels[1] / \ - ((view[1]-view[0])**2 + (view[3]-view[2])**2) - else: - current_axes.set_aspect("equal", adjustable="datalim") - length_unit = smart_length_units_for_vector_quantity(x) - phys_to_pix2 = n_pixels[0]*n_pixels[1] / \ - ((max(x)-min(x))**2 + (max(y)-min(y))**2) - sizes = numpy.minimum(numpy.maximum( - (h_smooths**2 * phys_to_pix2), min_size), max_size) - - x = x.as_quantity_in(length_unit) - y = y.as_quantity_in(length_unit) - scatter(x, y, s=sizes, c=colors, edgecolors="none", alpha=alpha) - if gd_particles: - scatter(gd_particles.x, gd_particles.y, c='w', marker='o') - xlabel('x') - ylabel('y') - - -def convert_particles_to_pynbody_data( - particles, length_unit=units.kpc, pynbody_unit="kpc"): - if not HAS_PYNBODY: - raise AmuseException("Couldn't find pynbody") - - if hasattr(particles, "u"): - pynbody_data = new(gas=len(particles)) - else: - pynbody_data = new(dm=len(particles)) - pynbody_data._filename = "AMUSE" - if hasattr(particles, "mass"): - pynbody_data['mass'] = SimArray( - particles.mass.value_in(units.MSun), "Msol") - if hasattr(particles, "position"): - pynbody_data['x'] = SimArray( - particles.x.value_in(length_unit), pynbody_unit) - pynbody_data['y'] = SimArray( - particles.y.value_in(length_unit), pynbody_unit) - pynbody_data['z'] = SimArray( - particles.z.value_in(length_unit), pynbody_unit) - if hasattr(particles, "velocity"): - pynbody_data['vx'] = SimArray( - particles.vx.value_in(units.km / units.s), "km s^-1") - pynbody_data['vy'] = SimArray( - particles.vy.value_in(units.km / units.s), "km s^-1") - pynbody_data['vz'] = SimArray( - particles.vz.value_in(units.km / units.s), "km s^-1") - if hasattr(particles, "h_smooth"): - pynbody_data['smooth'] = SimArray( - particles.h_smooth.value_in(length_unit), pynbody_unit) - if hasattr(particles, "rho"): - pynbody_data['rho'] = SimArray( - particles.rho.value_in(units.g / units.cm**3), "g cm^-3") - if hasattr(particles, "temp"): - pynbody_data['temp'] = SimArray(particles.temp.value_in(units.K), "K") - elif hasattr(particles, "u"): - temp = 2.0/3.0 * particles.u * mu() / constants.kB - pynbody_data['temp'] = SimArray(temp.value_in(units.K), "K") - return pynbody_data - - -def mu(X=None, Y=0.25, Z=0.02, x_ion=0.1): - """ - Compute the mean molecular weight in kg (the average weight of particles in - a gas) X, Y, and Z are the mass fractions of Hydrogen, of Helium, and of - metals, respectively. - x_ion is the ionisation fraction (0 < x_ion < 1), 1 means fully ionised - """ - if X is None: - X = 1.0 - Y - Z - elif abs(X + Y + Z - 1.0) > 1e-6: - raise AmuseException( - "Error in calculating mu: mass fractions do not sum to 1.0") - return ( - constants.proton_mass - / (X*(1.0+x_ion) + Y*(1.0+2.0*x_ion)/4.0 + Z*x_ion/2.0) - ) - - -def _smart_length_units_for_pynbody_data(length): - length_units = [(units.Gpc, "Gpc"), (units.Mpc, "Mpc"), (units.kpc, "kpc"), - (units.parsec, "pc"), (units.AU, - "au"), (1.0e9*units.m, "1.0e9 m"), - (1000*units.km, "1000 km"), (units.km, "km")] - for length_unit, pynbody_unit in length_units: - if length > (1 | length_unit): - return length_unit, pynbody_unit - return units.m, "m" - - -def pynbody_column_density_plot(particles, width=None, qty='rho', units=None, - sideon=False, faceon=False, **kwargs): - if not HAS_PYNBODY: - raise AmuseException("Couldn't find pynbody") - - if width is None: - width = 2.0 * particles.position.lengths_squared().amax().sqrt() - length_unit, pynbody_unit = _smart_length_units_for_pynbody_data(width) - pyndata = convert_particles_to_pynbody_data( - particles, length_unit, pynbody_unit) - UnitlessArgs.strip([1] | length_unit, [1] | length_unit) - - if sideon: - function = pynbody_sph.sideon_image - elif faceon: - function = pynbody_sph.faceon_image - else: - function = pynbody_sph.image - - if units is None and qty == 'rho': - units = 'm_p cm^-2' - - result = function(pyndata, width=width.value_in( - length_unit), qty=qty, units=units, **kwargs) - UnitlessArgs.current_plot = native_plot.gca() - return result - - -def effective_iso_potential_plot(gravity_code, - omega, - center_of_rotation=[0, 0] | units.AU, - xlim=[-1.5, 1.5] | units.AU, - ylim=[-1.5, 1.5] | units.AU, - resolution=[1000, 1000], - number_of_contours=20, - fraction_screen_filled=0.5, - quadratic_contour_levels=True, - contour_kwargs=dict(), - omega2=None, - center_of_rotation2=[0, 0] | units.AU, - fraction_screen_filled2=0.2, - projection3D=False): - """ - Create a contour plot of the effective potential of particles in a gravity - code. - The code needs to support 'get_potential_at_point' only, so it can also be - an instance of Bridge. - - :argument gravity_code: an instance of a gravity code - :argument omega: The angular velocity of the system - :argument center_of_rotation: The (2D) center around which the system rotates, usually the center of mass - :argument xlim: Range in x coordinate; width of window - :argument ylim: Range in y coordinate; width of window - :argument resolution: Number of points to sample potential for x and y direction - :argument number_of_contours: How many contour lines to plot - :argument fraction_screen_filled: Lowest contour will enclose this fraction of the screen - :argument quadratic_contour_levels: Quadratic or linear scaling between contour levels - :argument contour_kwargs: Optional keyword arguments for pyplot.contour - """ - UnitlessArgs.strip(xlim, ylim) - xlim, ylim = UnitlessArgs.stripped_args - - x_num = numpy.linspace(xlim[0], xlim[1], resolution[0]) - y_num = numpy.linspace(ylim[0], ylim[1], resolution[1]) - x_num, y_num = numpy.meshgrid(x_num, y_num) - x = (x_num | UnitlessArgs.arg_units[0]).flatten() - y = (y_num | UnitlessArgs.arg_units[1]).flatten() - zeros = x.aszeros() - potential = gravity_code.get_potential_at_point(zeros, x, y, zeros) - potential -= omega**2 * \ - ((x-center_of_rotation[0])**2 + (y-center_of_rotation[1])**2) / 2.0 - - if projection3D: - from matplotlib import cm - ax = native_plot.gca(projection='3d') - Z = potential.number.reshape(resolution[::-1]) - levels = set_contour_levels( - potential, number_of_contours, fraction_screen_filled, quadratic_contour_levels) - Z = numpy.maximum(Z, levels[0]) - ax.plot_surface( - x_num, y_num, Z, rstride=1, cstride=1, cmap=cm.spectral, - linewidth=0, antialiased=False, vmin=levels[0], vmax=3*levels[-1]-2 - * levels[0]) - - ax.set_xlabel('X') - ax.set_xlim(-1, 1) - ax.set_ylabel('Y') - ax.set_ylim(-1, 1) - ax.set_zlabel('Z') - ax.set_zlim(levels[0], levels[-1]) - return potential - - levels = set_contour_levels( - potential, number_of_contours, fraction_screen_filled, - quadratic_contour_levels - ) - CS = native_plot.contour(x_num, y_num, potential.number.reshape( - resolution[::-1]), levels, **contour_kwargs) - - if omega2 is None: - return potential - - potential2 = potential - omega2**2 * \ - ((x-center_of_rotation2[0])**2 + (y-center_of_rotation2[1])**2) / 2.0 - # ~levels = set_contour_levels(potential, number_of_contours2, fraction_screen_filled2, quadratic_contour_levels2) - levels = set_contour_levels( - potential2, number_of_contours, fraction_screen_filled2, - quadratic_contour_levels) - CS = native_plot.contour( - x_num, y_num, potential2.number.reshape( - resolution[::-1]), levels, **contour_kwargs) - return potential.reshape(resolution[::-1]), potential2.reshape(resolution[::-1]) - - -def set_contour_levels(potential, number_of_contours, fraction_screen_filled, quadratic_contour_levels): - uniform = numpy.linspace(0.0, 1.0, number_of_contours) - V_max = potential.amax().number - V_min = potential.sorted().number[int( - len(potential)*(1-fraction_screen_filled))] - if quadratic_contour_levels: - levels = V_min + (V_max-V_min) * uniform * (2 - uniform) - else: - levels = V_min + (V_max-V_min) * uniform - return levels From 7ceaefd29b2034a52fb3c1ab834b2c61d8b0bc0c Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Tue, 11 May 2021 19:59:57 +0100 Subject: [PATCH 30/30] Update codes.py --- src/amuse/codes.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/amuse/codes.py b/src/amuse/codes.py index 7a4093d25e..75db52a230 100644 --- a/src/amuse/codes.py +++ b/src/amuse/codes.py @@ -1,32 +1,34 @@ - +"""AMUSE codes +""" import sys _CODES = [ -'athena', 'capreole', 'cachedse', 'gadget2', 'mesa', 'octgrav', 'twobody', -'capreole', 'hermite0', 'mocassin', 'phiGRAPE', -'athena', 'evtwin', 'hop', 'seba', -'bhtree', 'evtwin2sse', 'interface', 'smallN', -'bse', 'fi', 'mercury', 'sse', + 'athena', 'capreole', 'cachedse', 'gadget2', 'mesa', + 'octgrav', 'twobody', 'capreole', 'hermite0', 'mocassin', + 'phiGRAPE', 'athena', 'evtwin', 'hop', 'seba', + 'bhtree', 'evtwin2sse', 'interface', 'smallN', 'bse', + 'fi', 'mercury', 'sse', ] __all__ = [] + def _import_modules(): - for x in _CODES: - modulename = 'amuse.legacy.' + x + '.interface' + for code in _CODES: + modulename = 'amuse.legacy.' + code + '.interface' try: __import__(modulename) - globals()[x] = sys.modules[modulename] - __all__.append(x) + globals()[code] = sys.modules[modulename] + __all__.append(code) except ImportError as ex: - modulename = 'amuse.legacy.' + x + '.' + x + modulename = 'amuse.legacy.' + code + '.' + code try: __import__(modulename) - globals()[x] = sys.modules[modulename] - __all__.append(x) + globals()[code] = sys.modules[modulename] + __all__.append(code) except ImportError as ex: pass + _import_modules() - \ No newline at end of file