Close Menu
    Trending
    • Mechanistic View of Transformers: Patterns, Messages, Residual Stream… and LSTMs
    • Iranian nuclear experts visited Russian labs with dual-use tech in 2024 – FT | by Defence Affairs & Analysis | Aug, 2025
    • MLPerf Releases AI Storage v2.0 Benchmark Results
    • DOE’s Advanced Nuclear Reactor Site 90-Day Challenge
    • Palantir hits $1 billion in quarterly sales for the first time, avoids DOGE cuts
    • Exploratory Data Analysis: Gamma Spectroscopy in Python (Part 3)
    • OpenAI has finally released open-weight language models
    • The First Learning Algorithms: Adaptive Filters | by Ryan Revilla | Aug, 2025
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Artificial Intelligence»Exploratory Data Analysis: Gamma Spectroscopy in Python (Part 3)
    Artificial Intelligence

    Exploratory Data Analysis: Gamma Spectroscopy in Python (Part 3)

    Team_AIBS NewsBy Team_AIBS NewsAugust 5, 2025No Comments11 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    objects round us will be barely radioactive. Americium in smoke detectors, radium in some classic watches, or uranium in classic glass; a full listing will be lengthy. Principally, these objects are secure and can’t trigger a well being danger. Additionally it is attention-grabbing to establish them and examine the matter on the atomic degree. And we will do that utilizing a radiation detector. In the first part, I did an exploratory information evaluation of the gamma spectroscopy information. Within the second part, I created a machine studying mannequin for detecting radioactive isotopes. That is the final third half, and it’s time so as to add a created mannequin to the actual app!

    On this story, I’ll check two approaches:

    • I’ll create a public Streamlit app that can be hosted totally free on Streamlit Cloud (the app hyperlink is added to the top of the article).
    • As a extra versatile and common resolution, I’ll create a Python HTMX-based app that may talk with actual {hardware} and make predictions in actual time.

    In the identical manner as within the earlier half, I’ll use a Radiacode scintillation detector to get the info (disclaimer: the system used on this check was supplied by the producer; I don’t get any business revenue from their gross sales, and I didn’t get any editorial enter about all of the assessments). Readers who don’t have a Radiacode {hardware} will be capable of check the app and the mannequin utilizing recordsdata out there on Kaggle.

    Let’s get began!

    1. Isotopes Classification Mannequin

    This mannequin was described within the previous part. It’s based mostly on XGBoost, and I skilled the mannequin utilizing totally different radioactive samples. I used samples that may be legally bought, like classic uranium glass or previous watches with radium dials made within the Fifties. As talked about earlier than, I additionally used a Radiacode scintillation detector, which permits me to get the gamma spectrum of the article. Solely 10-20 years in the past, these kinds of detectors have been out there solely in massive labs; immediately, they are often bought for the worth of a mid-range smartphone.

    The mannequin comprises three parts:

    • The XGBoost-based mannequin itself.
    • An inventory of radioactive isotopes (like Lead-214 or Actinium-228), on which the mannequin was skilled. The Radiacode scintillation detector returns 1024 spectrum values, and 23 of them have been used for the mannequin.
    • A label encoder to transform listing indexes into human-readable names.

    Let’s wrap all this right into a single Python class:

    from xgboost import XGBClassifier
    from sklearn.preprocessing import LabelEncoder
    
    
    class IsotopesClassificationModel:
        """ Gamma Spectrum Classification Mannequin """
    
        def __init__(self):
            """ Load fashions """
            path = self._get_models_path()
            self._classifier = self._load_model(path + "/XGBClassifier.json")
            self._isotopes = self._load_isotopes(path + "/isotopes.json")
            self._labels_encoder = self._load_labels_encoder(path + "/LabelEncoder.npy")
    
        def predict(self, spectrum: Spectrum) -> str:
            """ Predict the isotope """
            options = SpectrumPreprocessing.convert_to_features(
                spectrum, self._isotopes
            )
            preds = self._classifier.predict([features])
            preds = self._labels_encoder.inverse_transform(preds)
            return preds[0]
    
        @staticmethod
        def _load_model(filename: str) -> XGBClassifier:
            """ Load mannequin from file """
            bst = XGBClassifier()
            bst.load_model(filename)
            return bst
    
        @staticmethod
        def _load_isotopes(filename: str) -> Listing:
            with open(filename, "r") as f_in:
                return json.load(f_in)
    
        @staticmethod
        def _load_labels_encoder(filename: str) -> LabelEncoder:
            le = LabelEncoder()
            le.classes_ = np.load(filename)
            return le
    
        @staticmethod
        def _get_models_path() -> str:
            """ Get path to fashions. Mannequin recordsdata are saved in 
                'fashions/V1/' folder """
            parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
            return parent_dir + f"/fashions/{IsotopesClassificationModel.VERSION}"

    A Spectrum class comprises the spectrum information we get from a radiation detector:

    @dataclass
    class Spectrum:
        """ Radiation spectrum information """
    
        period: datetime.timedelta
        a0: float
        a1: float
        a2: float
        counts: listing[int]

    Right here, counts is a gamma spectrum, which is represented by an inventory of 1024 channel values. Spectrum information will be exported utilizing the official Radiacode Android app or retrieved immediately from a tool utilizing a radiacode Python library.

    To load the spectrum into the mannequin, I created a SpectrumPreprocessing class:

    class SpectrumPreprocessing:
        """ Gamma Spectrum Preprocessing """
    
        @staticmethod
        def convert_to_features(spectrum: Spectrum, isotopes: Listing) -> np.array:
            """ Convert the spectrum to the listing of options for prediction """
            sp_norm = SpectrumPreprocessing._normalize(spectrum)
            energies = [energy for _, energy in isotopes]
            channels = [SpectrumPreprocessing.energy_to_channel(spectrum, energy) for energy in energies]
            return np.array([sp_norm.counts[ch] for ch in channels])
    
        @staticmethod 
        def load_from_xml_file(file_path: str) -> Spectrum:
            """ Load spectrum from a Radiacode Android app file """

    Right here, I skip some code blocks that have been already revealed within the previous part. Extracting options from the gamma spectrum was additionally defined there, and I extremely advocate studying that half first.

    Now, let’s check the mannequin! I took a Radiacode detector and picked up a gamma spectrum inside 10 minutes:

    Radiacode radiation detector, Picture by writer

    This Chinese language pendant was marketed as “ion-generated,” and it’s barely radioactive. Its gamma spectrum, collected within the official Radiacode Android app, appears like this:

    Screenshot by writer

    After ready for ~10 minutes, I exported the spectrum into an XML file. Now, we will run the mannequin:

    from spectrum import SpectrumPreprocessing
    from ml_models import IsotopesClassificationModel
    
    sp = SpectrumPreprocessing.load_from_file("spectrum.xml")
    mannequin = IsotopesClassificationModel()
    end result = mannequin.predict(sp)
    print(end result)
    
    #> Thorium

    As we will see, the mannequin works properly. We will examine the peaks with spectra of recognized isotopes (for instance, here or here) and make sure that the spectrum belongs to thorium.

    2. Streamlit

    The mannequin works; nevertheless, we dwell within the XXI century, and nearly no one will run the console app to get the outcomes. As an alternative, we will make the app out there on-line, so all Radiacode customers will be capable of run it.

    There are a lot of Python frameworks for making browser-based apps, and Streamlit might be the preferred within the information science group. And what’s essential for us, a Streamlit Community Cloud platform permits everybody to publish their apps utterly totally free. To do that, let’s make the app first.

    2.1 Streamlit App

    A Streamlit framework is comparatively straightforward to make use of, no less than if a standard-looking app is nice for us. Personally, I’m not a fan of this method. These frameworks disguise all low-level implementation particulars from customers. It’s easy to make a prototype, however the UI logic can be tightly coupled with a really area of interest framework and can’t be reused anyplace else. Doing every little thing non-standard, which isn’t supported by the framework, will be nearly unattainable or exhausting to implement with out digging into tons of abstractions and pages of code. Nonetheless, in our case, the prototype is all we want.

    Usually, a Streamlit code is straightforward, and we simply want to explain the logical hierarchy of our web page:

    import streamlit as st
    import logging
    logger = logging.getLogger(__name__)
    
    
    def is_xml_valid(xml_data: str) -> bool:
        """ Verify if the XML has legitimate dimension and information """
        return len(xml_data) < 65535 and xml_data.startswith(" Optionally available[Spectrum]:
        """ Load spectrum from the StringIO stream """
        xml_data = stringio.learn()
        if is_xml_valid(xml_data):
            return SpectrumPreprocessing.load_from_xml(xml_data)
        return None
    
    def important():
        """ Major app """
        st.set_page_config(page_title="Gamma Spectrum")
        st.title("Radiacode Spectrum Detection")
        st.textual content(
            "Export the spectrum to XML utilizing the Radiacode app, and "
            "add it to see the outcomes."
        )
    
        # File Add
        uploaded_file = st.file_uploader(
            "Select the XML file", sort="xml", key="uploader",
        )
        if uploaded_file isn't None:
            stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
            if sp := get_spectrum(stringio):
                # Prediction
                mannequin = IsotopesClassificationModel()
                end result = mannequin.predict(sp)
                logger.data(f"Spectrum prediction: {end result}")
    
                # Present end result
                st.success(f"Prediction Consequence: {end result}")
                # Draw
                fig = get_spectrum_barchart(sp)
                st.pyplot(fig)
    
    
    if __name__ == "__main__":
        logger.setLevel(logging.INFO)
        important()

    As we will see, the total app requires a minimal quantity of Python code. Streamlit will render all HTML for us, with title, file add, and outcomes. As a bonus, I may even show a spectrum utilizing Matplotlib:

    def get_spectrum_barchart(sp: Spectrum) -> plt.Determine:
        """ Get Matplotlib's barchart """
        counts = SpectrumPreprocessing.get_counts(sp)
        vitality = [
           SpectrumPreprocessing.channel_to_energy(sp, x) for x in range(len(counts))
        ]
    
        fig, ax = plt.subplots(figsize=(9, 6))
        ax.spines["top"].set_color("lightgray")
        ax.spines["right"].set_color("lightgray")
        # Bars
        ax.bar(vitality, counts, width=3.0, label="Counts")
        # X values
        ticks_x = [SpectrumPreprocessing.channel_to_energy(sp, ch) for ch in range(0, len(counts), len(counts) // 20)]
        labels_x = [f"{int(ch)}" for ch in ticks_x]
        ax.set_xticks(ticks_x, labels=labels_x, rotation=45)
        ax.set_xlim(vitality[0], vitality[-1])
        ax.set_ylim(0, None)
        ax.set_title("Gamma spectrum")
        ax.set_xlabel("Power, keV")
        ax.set_ylabel("Counts")
        return fig

    Now we will run the app regionally:

    streamlit run st-app.py

    After that, our app is totally operational and will be examined in a browser:

    Screenshot by writer

    As talked about earlier than, I’m not a fan of very high-level frameworks and like to have a greater understanding of how issues work “below the hood.” Nonetheless, contemplating that I spent solely about 100 traces of code to make a completely practical net app, I can not complain – for prototyping, it really works properly.

    2.2 Streamlit Neighborhood Cloud

    When the app is examined regionally, it’s time to make it public! A Streamlit Cloud is a free service, and clearly, it has a variety of limitations:

    • The app runs in a Docker-like container. Your GitHub account have to be linked to Streamlit. When the container begins, it pulls your code from GitHub and runs it.
    • On the time of scripting this textual content, container sources are restricted to 2 cores and as much as 2,7 GB of RAM. It might be too constrained to run a 70B dimension LLM, however for a small XGBoost mannequin, it’s greater than sufficient.
    • Streamlit doesn’t present any everlasting storage. After shutdown or restart, all logs and momentary recordsdata can be misplaced (you should use API secrets and techniques and hook up with every other cloud storage out of your Python code if wanted).
    • After a interval of inactivity (about half-hour), the container can be stopped, and all momentary recordsdata may even be misplaced. If somebody opens the app hyperlink, it is going to run once more.

    As readers can guess, an inactive app prices Streamlit nearly nothing as a result of it shops solely a small configuration file. And it’s a good resolution for a free service – it permits us to publish the app with none prices and provides individuals a hyperlink to run it.

    To publish the app in Streamlit, we have to carry out three easy steps.

    First, we have to commit our Python app to GitHub. A necessities.txt file can be necessary. Streamlit container makes use of it to put in required Python dependencies. In my case, it appears like this:

    xgboost==3.0.2
    scikit-learn==1.6.1
    numpy==1.26.4
    streamlit==1.47.0
    pillow==11.1.0
    matplotlib==3.10.3
    xmltodict==0.14.2

    Server settings will be modified utilizing a .streamlit/config.toml file. In my case, I restricted the uploaded file dimension to 1 MB as a result of all spectra recordsdata are smaller:

    [server]
    # Max dimension, in megabytes, for recordsdata uploaded with the file_uploader.
    # Default: 200
    maxUploadSize = 1

    Second, we have to log in to share.streamlit.io utilizing a GitHub account and provides permission to entry the supply code.

    Lastly, we will create a brand new Streamlit challenge. Within the challenge settings, we will additionally choose the specified URL and atmosphere:

    Picture by writer

    If every little thing was achieved appropriately, we will see our app working:

    Picture by writer

    At this second, customers worldwide can even entry our app! In my case, I chosen a gammaspectrumdetection title, and the app is obtainable utilizing this URL.

    3. FastAPI + HTMX App

    As readers can see, Streamlit is a pleasant resolution for a easy prototype. Nonetheless, within the case of the radiation detector, I want to see information coming from actual Radiacode {hardware}. This is able to be unattainable to do in Streamlit; this library simply was not designed for that. As an alternative, I’ll use a number of production-grade frameworks:

    • An HTMX framework permits us to make a completely practical net interface.
    • FastAPI will run the server.
    • The ML mannequin will course of the info retrieved in real-time from a radiation detector utilizing a Radiacode library.

    As talked about earlier than, these readers who don’t have a Radiacode {hardware} will be capable of replay the info utilizing uncooked log recordsdata, saved from an actual system. A hyperlink to the app and all recordsdata is obtainable on the finish of the article.

    Let’s get into it!

    3.1 HTML/HTMX

    The app is linked to a Radiacode detector, and I made a decision to indicate a connection standing, radiation degree, and a spectrum graph on the web page. On the backside, a spectrum assortment time and an ML mannequin prediction can be displayed.

    An index.html file for this format appears like this:

    
    
        
        Gamma Spectrum & Monitoring
        
        
    
        

    Assortment Time:
    n/a

    Prediction:
    n/a



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleOpenAI has finally released open-weight language models
    Next Article Palantir hits $1 billion in quarterly sales for the first time, avoids DOGE cuts
    Team_AIBS News
    • Website

    Related Posts

    Artificial Intelligence

    Mechanistic View of Transformers: Patterns, Messages, Residual Stream… and LSTMs

    August 5, 2025
    Artificial Intelligence

    Hands-On with Agents SDK: Multi-Agent Collaboration

    August 5, 2025
    Artificial Intelligence

    On Adding a Start Value to a Waterfall Chart in Power BI

    August 5, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Mechanistic View of Transformers: Patterns, Messages, Residual Stream… and LSTMs

    August 5, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    How to Build a Business That Lasts a Century and Beyond

    January 7, 2025

    Get the 7 Best MS Office Programs for Only $20

    July 7, 2025

    What Even Is AI?. It’s Not Just Robots Trying to Kill Us | Shresthi Siddhant | Medium

    July 31, 2025
    Our Picks

    Mechanistic View of Transformers: Patterns, Messages, Residual Stream… and LSTMs

    August 5, 2025

    Iranian nuclear experts visited Russian labs with dual-use tech in 2024 – FT | by Defence Affairs & Analysis | Aug, 2025

    August 5, 2025

    MLPerf Releases AI Storage v2.0 Benchmark Results

    August 5, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

    Type above and press Enter to search. Press Esc to cancel.