https://github.com/mrsamsonn/monolithic-polylithic-crystal-segmentation
A grid segmentation algorithm for clustering crystal structures using diffraction patterns. Useful in material science and nanotechnology, this code enables detailed analysis of crystals for research and industrial applications.
https://github.com/mrsamsonn/monolithic-polylithic-crystal-segmentation
clustering crystal-structure crystallography data-analysis diffraction-patterns grid-segmentation image-processing k-means machine-learning matertial-science nanotechnology python research-project research-tools scientific-computing
Last synced: 6 months ago
JSON representation
A grid segmentation algorithm for clustering crystal structures using diffraction patterns. Useful in material science and nanotechnology, this code enables detailed analysis of crystals for research and industrial applications.
- Host: GitHub
- URL: https://github.com/mrsamsonn/monolithic-polylithic-crystal-segmentation
- Owner: mrsamsonn
- Created: 2024-08-11T15:22:09.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-06T22:42:45.000Z (6 months ago)
- Last Synced: 2025-04-06T23:25:48.222Z (6 months ago)
- Topics: clustering, crystal-structure, crystallography, data-analysis, diffraction-patterns, grid-segmentation, image-processing, k-means, machine-learning, matertial-science, nanotechnology, python, research-project, research-tools, scientific-computing
- Language: Jupyter Notebook
- Homepage:
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Monolithic-Polylithic Crystal Segmentation
This project provides a grid-based segmentation approach to identify monolithic and polylithic crystal structures from diffraction patterns. The method partitions diffraction data into grids, detects peaks in each tile, extracts features, and applies k-means clustering for grouping similar diffraction patterns.
## Table of Contents
- [Overview](#overview)
- [Screenshots](#screenshots)
- [Algorithm Explanation](#algorithm-explanation)
- [Setup Instructions](#setup-instructions)
- [Usage](#usage)
- [Output](#output)
- [Project Structure](#project-structure)
- [Future Work](#future-work)
- [License](#license)## Overview
This project segments diffraction patterns into square grids, each analyzed for peak detection. The algorithm then clusters the grids using k-means and visualizes the segmented diffraction patterns to identify monolithic vs. polylithic structures.
## Screenshots
### Clustered Output Examples
## Example Output

![]()
## Algorithm Explanation
The segmentation algorithm follows a structured approach:
### 1. **Grid Partitioning**
First, the diffraction pattern is divided into square grids. Each grid cell is treated as a separate region for analysis.```python
import numpy as npdef create_grid(data, grid_size):
"""
Divides the diffraction data into square grids based on the given grid size.
"""
rows, cols = data.shape
grid_rows = rows // grid_size
grid_cols = cols // grid_size
grids = []for i in range(grid_rows):
for j in range(grid_cols):
grid = data[i*grid_size:(i+1)*grid_size, j*grid_size:(j+1)*grid_size]
grids.append(grid)
return grids```
2. Peak Detection
For each grid, local maxima are identified using a peak detection algorithm. The scipy.signal.find_peaks function is often employed for this purpose.
from scipy.signal import find_peaks
```python
def detect_peaks(grid):
"""
Detects peaks in the grid data using scipy's find_peaks function.
"""
peaks, _ = find_peaks(grid)
return peaks
```3. Feature Extraction
For each grid, features such as peak intensity and spatial distribution are extracted into a feature vector. These features are essential for clustering.
```python
def extract_features(grid, peaks):
"""
Extracts features from the grid based on the detected peaks.
"""
features = []
for peak in peaks:
intensity = grid[peak] # Peak intensity
position = peak # Peak position (x, y)
features.append([intensity, position])
return np.array(features)
```4. Clustering
The features are clustered using k-means. The optimal number of clusters is determined using the elbow method.
```python
from sklearn.cluster import KMeansdef cluster_features(features, n_clusters=3):
"""
Clusters the feature vectors using k-means clustering.
"""
kmeans = KMeans(n_clusters=n_clusters)
clusters = kmeans.fit_predict(features)
return clusters
```5. Visualization
Finally, the clusters are visualized using matplotlib. Each grid cell is colored based on the cluster it belongs to.
```python
import matplotlib.pyplot as pltdef plot_clusters(clusters, grid_size, data_shape):
"""
Visualizes the clustered data with color coding.
"""
rows, cols = data_shape
grid_rows = rows // grid_size
grid_cols = cols // grid_size# Create an empty image for the cluster visualization
cluster_image = np.zeros((rows, cols))idx = 0
for i in range(grid_rows):
for j in range(grid_cols):
cluster_image[i*grid_size:(i+1)*grid_size, j*grid_size:(j+1)*grid_size] = clusters[idx]
idx += 1plt.imshow(cluster_image, cmap='viridis')
plt.title('Cluster Visualization')
plt.colorbar()
plt.show()
```Setup Instructions
Prerequisites
• Python 3.x
• Jupyter NotebookInstall Dependencies
```
pip install numpy matplotlib scikit-learn tifffile
```Clone and Run
```
git clone https://github.com/mrsamsonn/Monolithic-Polylithic-Crystal-Segmentation.git
cd Monolithic-Polylithic-Crystal-Segmentation
jupyter notebook Grid_Segmentation.ipynb
```Usage
1. Open Grid_Segmentation.ipynb in Jupyter Notebook.
2. Follow the instructions to load diffraction data, define grid parameters, and run the segmentation algorithm.Output
The segmentation process outputs:
• Animated GIFs showing the clustering process.
• Static images with color-coded clusters.
• Legends and clustering metrics.Project Structure
```
Monolithic-Polylithic-Crystal-Segmentation/
│
├── Grid_Segmentation.ipynb # Main analysis notebook
├── animated_plot.gif # Example output GIF
├── *.png # Sample output images
├── README.md # Project documentation
└── data/ # Input diffraction files (user-provided)
```Future Work
• Dynamic Grid Resizing: Implement adaptive grid resizing based on the size of detected structures.
• Advanced Clustering: Incorporate DBSCAN or spectral clustering for better handling of noisy diffraction data.
• Real-Time Analysis: Support live data processing for real-time diffraction pattern segmentation.