Skip to content

Commit

Permalink
more robust text processing for custom_loader_one
Browse files Browse the repository at this point in the history
function to generate random test data for custom_loader_one
  • Loading branch information
pranabdas committed Feb 6, 2023
1 parent 081badf commit 0c59dcd
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 40 deletions.
7 changes: 5 additions & 2 deletions docs/data-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ sidebar_label: Importing data
keywords: ["arpes data", "ses", "scienta", "scienta-omicron"]
---
At the moment, this module can only import data produced by Scienta-Omicron SES
program. For the spectral scans, we will use the plaintext (`.txt`) output file
as input, and in case of Fermi map data, we will need the ZIP files as input
program. However, you can implement custom loader specific to your data file,
and use other modules. Here is an example of [custom loader](
https://github.com/pranabdas/arpespythontools/blob/main/src/custom_loader_one.py).
For the spectral scans, we will use SES produced plaintext (`.txt`) output file
as input, while in case of Fermi map data, we will need the ZIP files as input
format.

First thing first, import `arpespythontools` in your program:
Expand Down
10 changes: 6 additions & 4 deletions docs/gs.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ pip install --upgrade -r requirements.txt
```

### Importing arpespythontools

We can import the module by `import arpespythontools as arp` so that later in
the code we can refer to the module as `arp` in short.
```python
import sys
sys.path.append("/parent/arpespythontools/path/")
sys.path.append("/workspaces/projects/")
# module is path is `/workspaces/projects/arpespythontools`
import arpespythontools as arp
```

Expand All @@ -58,9 +60,9 @@ two lines above.

### Run Python and Jupyter notebook in Docker container

I have a [Dockerfile](
https://github.com/pranabdas/arpespythontools/blob/master/Dockerfile), you can
adjust according to your needs.
If you want to setup and run python in a container, I have a
[Dockerfile](https://github.com/pranabdas/arpespythontools/blob/master/Dockerfile),
please adjust according to your needs.
```dockerfile title="Dockerfile" showLineNumbers
# Start from Ubuntu 22.04 LTS
FROM ubuntu:jammy
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
astropy
autopep8
jupyterlab
matplotlib
numpy
Expand Down
91 changes: 63 additions & 28 deletions src/custom_loader_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,86 @@
# -*- coding: utf-8 -*-
"""
Program: custom loader to suit specific data format as requested by a user
Version: 20220203
Version: 20220204
@author: Pranab Das (GitHub: @pranabdas)
"""


def custom_loader_one(fname):
import warnings
def custom_loader_one(fname, polar_start=None, polar_end=None):
import itertools
import numpy as np
import math
import warnings

# suppress numpy warning related to max_rows behavior
with warnings.catch_warnings():
warnings.filterwarnings(action='ignore')
warnings.filterwarnings(action="ignore")
# read only one line for theta values
theta = np.loadtxt(fname, dtype="float64", max_rows=1)
data = np.loadtxt(fname, dtype="float64", skiprows=2)

data = itertools.islice(open(fname), None)
# exclude blank and comment lines, comments must begin with `#`
data = itertools.dropwhile(
lambda x: x.strip() == "" or x.strip()[0] == "#", data)
# skip first/theta row
data = np.loadtxt(data, dtype="float64", skiprows=1)

energy = np.unique(data[:, 0])
polar_len = int(len(data) / len(energy))
polar_start = input(
"Enter polar angle start value (if unknown, leave empty): ")
polar_end = input(
"Enter polar angle end value (if unknown, leave empty): ")

try:
polar_start = np.float64(polar_start)
except:
polar_start = np.nan

try:
polar_end = np.float64(polar_end)
except:
polar_end = np.nan

# check dimension agree
# check dimension agree after integer conversion
# note: assert can be switched off globally using `-O` or `-OO` flag
assert polar_len * len(energy) == len(data), "dimension mismatch"

phi = range(polar_len)

if (np.isnan(polar_start)) or np.isnan(polar_end):
pass
else:
if (polar_start and polar_end):
phi = np.linspace(polar_start, polar_end, num=polar_len)
else:
phi = np.arange(polar_len)

intensity = np.zeros((len(energy), len(theta), polar_len), dtype="float64")

for i in range(len(data)):
assert data[i, 0] == energy[i %
len(energy)], "energy value irregularity"
assert math.isclose(data[i, 0], energy[i % len(energy)]), \
"energy value irregularity"
intensity[i % len(energy), :, i // len(energy)] = data[i, 1:]

return intensity, energy, theta, phi


def gen_test_data():
"""
generates fake/random data for testing `custom_loader_one`
You can run:
python3 -c 'from src.custom_loader_one import *; gen_test_data()' > out.txt
alternatively, execute python code interactively:
python3 -i src/custom_loader_one.py
or enter python interpreter and run python code:
python3
>>> exec(open("src/custom_loader_one.py").read())
and save data:
>>> from contextlib import redirect_stdout
>>> with open('out.txt', 'w') as f:
... with redirect_stdout(f):
... gen_test_data()
"""
import numpy as np

print("# generated fake/random data to test `custom_loader_one`")
print("\n\t", end="")

th = np.linspace(-5.8, 5.8, 10, dtype="float64")

for i in range(len(th)):
print(th[i], end=" ")
print()

ph = np.linspace(-3, 3, 5, dtype="float64")
e = np.linspace(10, 15, 15, dtype="float64")

for i in range(len(ph)*len(e)):
print(e[i % len(e)], end=" ")
for _ in range(len(th)):
print(np.random.rand()*50, end=" ")
print()

0 comments on commit 0c59dcd

Please sign in to comment.