<commit_before>﻿Imports System.Drawing
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.MarkupData.imzML
Imports BioNovoGene.Analytical.MassSpectrometry.Math.Ms1
Imports BioNovoGene.Analytical.MassSpectrometry.Math.Spectra
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
Imports Microsoft.VisualBasic.Imaging
Imports Microsoft.VisualBasic.Imaging.Drawing2D.Colors
Imports Microsoft.VisualBasic.Linq
Imports Microsoft.VisualBasic.Scripting.Runtime
Imports Microsoft.VisualBasic.Math
Imports Application = Microsoft.VisualBasic.Parallel
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel

''' <summary>
''' MS-imaging render canvas
''' </summary>
Public Class Drawer : Implements IDisposable

    Dim disposedValue As Boolean
    Dim pixels As ScanData()

    Public ReadOnly Property ibd As ibdReader
    Public ReadOnly Property dimension As Size

    Public ReadOnly Property UUID As String
        Get
            Return ibd.UUID
        End Get
    End Property

    Sub New(imzML As String)
        ibd = ibdReader.Open(imzML.ChangeSuffix("ibd"))
        pixels = XML.LoadScans(file:=imzML).ToArray
        dimension = New Size With {
            .Width = pixels.Select(Function(p) p.x).Max,
            .Height = pixels.Select(Function(p) p.y).Max
        }
    End Sub

    Public Function LoadMzArray(ppm As Double) As Double()
        Dim mzlist = pixels _
            .Select(Function(p) Application.DoEvents(Function() ibd.ReadArray(p.MzPtr))) _
            .IteratesALL _
            .Distinct _
            .ToArray
        Dim groups = mzlist _
            .GroupBy(Function(mz) mz, Tolerance.PPM(ppm)) _
            .Select(Function(mz) Val(mz.name)) _
            .ToArray

        Return groups
    End Function

    Public Iterator Function LoadPixels(mz As Double(), tolerance As Tolerance, Optional skipZero As Boolean = True) As IEnumerable(Of PixelData)
        Dim pixel As PixelData

        For Each point As ScanData In Me.pixels
            Dim msScan As ms2() = ibd.GetMSMS(point)
            Dim into As NamedCollection(Of ms2)() = msScan _
                .Where(Function(mzi) mz.Any(Function(dmz) tolerance(mzi.mz, dmz))) _
                .GroupBy(Function(a) a.mz, tolerance) _
                .ToArray

            Call Application.DoEvents()

            If skipZero AndAlso into.Length Then
                Continue For
            Else
                For Each mzi As NamedCollection(Of ms2) In into
                    pixel = New PixelData With {
                        .x = point.x,
                        .y = point.y,
                        .mz = Val(mzi.name),
                        .intensity = Aggregate x In mzi Into Max(x.intensity)
                    }

                    Yield pixel
                Next
            End If
        Next
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="pixels"></param>
    ''' <param name="dimension">the scan size</param><commit_msg>bugs fixed of load pixels data<commit_after>﻿Imports System.Drawing
Imports BioNovoGene.Analytical.MassSpectrometry.Assembly.MarkupData.imzML
Imports BioNovoGene.Analytical.MassSpectrometry.Math.Ms1
Imports BioNovoGene.Analytical.MassSpectrometry.Math.Spectra
Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
Imports Microsoft.VisualBasic.Imaging
Imports Microsoft.VisualBasic.Imaging.Drawing2D.Colors
Imports Microsoft.VisualBasic.Linq
Imports Microsoft.VisualBasic.Scripting.Runtime
Imports Microsoft.VisualBasic.Math
Imports Application = Microsoft.VisualBasic.Parallel
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel

''' <summary>
''' MS-imaging render canvas
''' </summary>
Public Class Drawer : Implements IDisposable

    Dim disposedValue As Boolean
    Dim pixels As ScanData()

    Public ReadOnly Property ibd As ibdReader
    Public ReadOnly Property dimension As Size

    Public ReadOnly Property UUID As String
        Get
            Return ibd.UUID
        End Get
    End Property

    Sub New(imzML As String)
        ibd = ibdReader.Open(imzML.ChangeSuffix("ibd"))
        pixels = XML.LoadScans(file:=imzML).ToArray
        dimension = New Size With {
            .Width = pixels.Select(Function(p) p.x).Max,
            .Height = pixels.Select(Function(p) p.y).Max
        }
    End Sub

    Public Function LoadMzArray(ppm As Double) As Double()
        Dim mzlist = pixels _
            .Select(Function(p) Application.DoEvents(Function() ibd.ReadArray(p.MzPtr))) _
            .IteratesALL _
            .Distinct _
            .ToArray
        Dim groups = mzlist _
            .GroupBy(Function(mz) mz, Tolerance.PPM(ppm)) _
            .Select(Function(mz) Val(mz.name)) _
            .ToArray

        Return groups
    End Function

    Public Iterator Function LoadPixels(mz As Double(), tolerance As Tolerance, Optional skipZero As Boolean = True) As IEnumerable(Of PixelData)
        Dim pixel As PixelData

        For Each point As ScanData In Me.pixels
            Dim msScan As ms2() = ibd.GetMSMS(point)
            Dim into As NamedCollection(Of ms2)() = msScan _
                .Where(Function(mzi) mz.Any(Function(dmz) tolerance(mzi.mz, dmz))) _
                .GroupBy(Function(a) a.mz, tolerance) _
                .ToArray

            Call Application.DoEvents()

            If skipZero AndAlso into.Length = 0 Then
                Continue For
            Else
                For Each mzi As NamedCollection(Of ms2) In into
                    pixel = New PixelData With {
                        .x = point.x,
                        .y = point.y,
                        .mz = Val(mzi.name),
                        .intensity = Aggregate x In mzi Into Max(x.intensity)
                    }

                    Yield pixel
                Next
            End If
        Next
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="pixels"></param>
    ''' <param name="dimension">the scan size</param>
{'fraction_non_alphanumeric': 0.08290241868223519, 'fraction_numerical': 0.001834862385321101, 'mean_word_length': 2.1131879543094496, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3847985', 'n_tokens_mistral': 1755, 'n_tokens_neox': 1654, 'n_words': 435}