Skip to content

Commit

Permalink
Change hands-on: from Fit2D to PONI
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrepaleo committed Apr 3, 2024
1 parent 6d4f5af commit 4ec378c
Showing 1 changed file with 65 additions and 52 deletions.
117 changes: 65 additions & 52 deletions python/python/3_input_output.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -337,27 +337,52 @@
"source": [
"## Hands on\n",
"\n",
"Read an ascii spreadsheet written by FIT2D:\n",
"A scattering geometry can be entirely described with a Point of Normal Incidence (PONI). \n",
"This is the coordinates of the orthogonal projection of center of sample onto the detector.\n",
"\n",
"* The first non commented line looks like:\n",
" * `512 512 Start pixel = ( 1 1 )`\n",
" * Then 512 values per line, 512 lines\n",
"* Read the file as a list of lists and display as an image.\n",
"\n",
"If `data` is a list of lists (of float), this can be done using matlab with:\n",
"\n",
"``` python\n",
"%matplotlib inline\n",
"from matplotlib.pyplot import subplots\n",
"![sd](img/PONI.png)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"[pyFAI](https://pyfai.readthedocs.io/en/latest/) uses a PONI file to describe the geometry. \n",
"It looks like this:\n",
"\n",
"fig, ax = subplots()\n",
"ax.imshow(data)\n",
"```yaml\n",
"poni_version: 2.1\n",
"Detector: Eiger2_4M\n",
"Detector_config: {\"orientation\": 3}\n",
"Distance: 0.41826947072547654\n",
"Poni1: 0.1273645012076586\n",
"Poni2: 0.04721622971911041\n",
"Rot1: -0.006558391003187438\n",
"Rot2: -0.001319758222137916\n",
"Rot3: 4.987612019731912e-06\n",
"Wavelength: 1.6531226457760035e-11\n",
"```\n",
"\n",
"* Example file in : data/example.spr\n",
"\n",
"\n",
"![FIT2D image saved in ASCII](img/fit2d_ascii_file.png \"FIT2D image saved in ASCII\")\n"
"**Exercise** read the above information from the file `data/eiger.poni`, and store it in a dictionary. \n",
"It should look like: \n",
"```python\n",
"{'poni_version': '2.1',\n",
" 'Detector': 'Eiger2_4M',\n",
" 'Detector_config': ' {\"orientation\" 3}',\n",
" 'Distance': '0.41826947072547654',\n",
" 'Poni1': '0.1273645012076586',\n",
" 'Poni2': '0.04721622971911041',\n",
" 'Rot1': '-0.006558391003187438',\n",
" 'Rot2': '-0.001319758222137916',\n",
" 'Rot3': '4.987612019731912e-06',\n",
" 'Wavelength': '1.6531226457760035e-11'\n",
"}\n",
"```"
]
},
{
Expand All @@ -381,38 +406,31 @@
},
"outputs": [],
"source": [
"def readspr(filepath):\n",
" \"Read a FIT2D ascii spread file\"\n",
" result = []\n",
" xsize = 0\n",
" ysize = 0\n",
"def read_poni(filepath, comment=\"#\"):\n",
" \"Read a PONI file\"\n",
" poni_info = {}\n",
" with open(filepath, \"r\") as opened_file:\n",
" for idx, line in enumerate(opened_file):\n",
" strippedline = line.strip()\n",
" # if this is a commented line\n",
" if strippedline.startswith(\"#\"):\n",
" for line in opened_file:\n",
" # Sanitize current line: remove leading and trailing spaces\n",
" line = line.strip()\n",
" # Skip commented lines\n",
" if line.startswith(comment):\n",
" continue\n",
" words = strippedline.split()\n",
" if (len(words) == 8) and (words[2:6] == [\"Start\", \"pixel\", \"=\", \"(\"]):\n",
" xsize = int(words[0])\n",
" ysize = int(words[1])\n",
" print(\"Dimensions of the size are (%s, %s)\" % (xsize, ysize))\n",
" break\n",
" if xsize and ysize:\n",
" for line in opened_file:\n",
" words = line.split()\n",
" if len(words) != xsize:\n",
" print(\n",
" \"Error !!! Expected entries are %s, got %s\"\n",
" % (xsize, len(words))\n",
" )\n",
" return None\n",
" else:\n",
" result.append([float(i) for i in words])\n",
" return result\n",
"\n",
"\n",
"data = readspr(\"data/example.spr\")"
" # Split the line before and after the \":\" separator\n",
" fields = line.split(\":\")\n",
" name = fields[0]\n",
" if len(fields) == 2:\n",
" value = fields[1].strip()\n",
" elif len(fields) > 2:\n",
" value = \"\".join(fields[1:])\n",
" else:\n",
" continue\n",
" value = value.strip()\n",
" poni_info[name] = value\n",
" return poni_info\n",
"\n",
"\n",
"data = read_poni(\"data/eiger.poni\")"
]
},
{
Expand All @@ -425,12 +443,7 @@
},
"outputs": [],
"source": [
"# Display the image\n",
"%matplotlib inline\n",
"from matplotlib.pyplot import subplots\n",
"\n",
"fig, ax = subplots()\n",
"ax.imshow(data)"
"data"
]
},
{
Expand Down

0 comments on commit 4ec378c

Please sign in to comment.