Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Sunday, 6 July 2025

Blazorise Datepicker with highlighting of dates

The Blazorise Datepicker is a date picker control for setting dates in Blazor WASM web assembly apps. It uses Flatpickr to provide a user friendly UI for selecting a date and navigate among months and years. It is used in web apps that use Blazor. The date picker is documented here :
https://blazorise.com/docs/components/date-picker
This date picker does not support highlighting certain dates. It do support enabling specified dates or disabling specified dates. But highlighting dates is not possible. I have added a Github repo where this is added support for :

https://github.com/toreaurstadboss/BlazoriseDatePickerWithHolidays

First off, the following custom Blazor component provides the custom date picker with the support for highlighting specified dates.

CustomDatePicker.razor




@using Blazorise
@using Blazorise.Localization
@using BlazoriseDatePickerWithHolidays.Service
@using System.Globalization
@using static BlazoriseDatePickerWithHolidays.Service.HolidayService
@inject IJSRuntime JS
@inject ITextLocalizerService TextLocalizerService

<Addons>
    <Addon AddonType="AddonType.Body">
        <DatePicker TValue="DateTime"
                    Date="@SelectedDate"
                    FirstDayOfWeek="@FirstDayOfWeek"
                    DateChanged="@OnDateChanged"
                    InputFormat="@InputFormat"   
                    InputMode="@InputMode"
                    DisplayFormat="@DisplayFormat"                    
                    Placeholder="@Placeholder"
                    TimeAs24hr="@TimeAs24hr"
                    @attributes="@AdditionalAttributes"
                    @ref="datePicker" />
    </Addon>
    <Addon AddonType="AddonType.End">
        <Button Class="addon-margin" Color="Color.Primary" Clicked="@(() => datePicker?.ToggleAsync())">
            <Icon Name="IconName.CalendarDay" />
        </Button>
    </Addon>
</Addons>

@code {
    /// <summary>
    /// List of dates to be highlighted in the date picker. Each date can have an annotation (e.g., holiday name) that will be shown as a tooltip.
    /// </summary>
    [Parameter]
    [EditorRequired]
    public List<AnnotatedDateTime> HighlightedDays { get; set; }

    /// <summary>
    /// The CSS class to apply to highlighted dates in the calendar.
    /// </summary>
    [Parameter]
    public string HighlightCssClass { get; set; } = "pink-day"; //Default CSS class pink-day is a custom CSS class defined in wwwroot/css/app.css

    /// <summary>
    /// The first day of the week in the date picker calendar. Defaults to Monday.
    /// </summary>
    [Parameter]
    public DayOfWeek FirstDayOfWeek { get; set; } = DayOfWeek.Monday;

    /// <summary>
    /// Whether to display time in 24-hour format. Defaults to true.
    /// </summary>
    [Parameter]
    public bool TimeAs24hr { get; set; } = true;

    /// <summary>   
    /// The locale to use for the date picker, which affects date formatting and localization. 
    /// Supported locales : https://blazorise.com/docs/helpers/localization
    /// </summary>
    [Parameter]
    public string Locale { get; set; } = "en-US";

    /// <summary>
    /// The input mode for the date picker, which can be either Date or DateTime or Month.
    /// </summary>
    [Parameter]
    public DateInputMode InputMode { get; set; } = DateInputMode.Date;

    /// <summary>
    /// The currently selected date in the date picker.
    /// </summary>
    [Parameter] 
    public DateTime SelectedDate { get; set; }

    /// <summary>
    /// Event callback triggered when the selected date changes.
    /// </summary>
    [Parameter] 
    public EventCallback<DateTime> SelectedDateChanged { get; set; }

    /// <summary>
    /// The input format string for displaying the date in the input field.
    /// </summary>
    [Parameter] 
    public string? InputFormat { get; set; } = "dd.MM.yyyy";

    /// <summary>
    /// The display format string for showing the date in the calendar.
    /// </summary>
    [Parameter] 
    public string? DisplayFormat { get; set; } = "dd.MM.yyyy";

    /// <summary>
    /// Placeholder text to display when no date is selected.
    /// </summary>
    [Parameter] 
    public string? Placeholder { get; set; }

    /// <summary>
    /// Whether to show the clear button in the date picker.
    /// </summary>
    [Parameter] 
    public bool ShowClearButton { get; set; } = true;

    /// <summary>
    /// Additional attributes to be splatted onto the underlying DatePicker component.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)] 
    public Dictionary<string, object>? AdditionalAttributes { get; set; }

    /// <summary>
    /// Handles component rendering and registers JS interop for month and year changes.
    /// </summary>
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _dotNetRef = DotNetObjectReference.Create(this);
            await JS.InvokeVoidAsync("registerFlatpickrMonthChange", "input.flatpickr-input", _dotNetRef);
            await JS.InvokeVoidAsync("registerFlatpickrYearChange", "input.flatpickr-input", _dotNetRef);
        }
        await ReloadDatesToHighlight();
    }

    protected override void OnParametersSet()
    {
        if (Locale != null){
            TextLocalizerService.ChangeLanguage(Locale);
        }
    }

    /// <summary>
    /// Invoked from JS when the month is changed in the calendar.
    /// </summary>
    [JSInvokable]
    public async Task OnMonthChanged()
    {
        await ReloadDatesToHighlight();
    }

    /// <summary>
    /// Invoked from JS when the year is changed in the calendar.
    /// </summary>
    [JSInvokable]
    public async Task OnYearChanged()
    {
        await ReloadDatesToHighlight();
    }

    /// <summary>
    /// Disposes JS interop references and the DatePicker component.
    /// </summary>
    public async ValueTask DisposeAsync()
    {
        _dotNetRef?.Dispose();
        if (datePicker != null)
            await datePicker.DisposeAsync();
    }
    
    /// <summary>
    /// Highlights the specified dates in the calendar using JS interop.
    /// </summary>
    private async Task ReloadDatesToHighlight()
    {
        if (HighlightedDays == null)
            return;

        string chosenLocale = this.Locale ?? "en-US";

        //Console.WriteLine("chosenLocale:" + chosenLocale);

        var datesToHighlight = HighlightedDays
            .Select(d => new { annotation = d.Annotation, date = d.Value.ToString("MMMM d, yyyy", new CultureInfo(chosenLocale)) })
            .ToArray();

        //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(datesToHighlight));

        await JS.InvokeVoidAsync(
            "highlightFlatpickrDates",
            ".flatpickr-calendar",
            datesToHighlight,
            HighlightCssClass // Pass the class as an extra argument
        );
    }

    /// <summary>
    /// Optionally provides a CSS class for a given date. Not used in this implementation.
    /// </summary>
    private string GetDateClass(DateTime date)
    {
        return string.Empty;
    }

    /// <summary>
    /// Handles the date change event from the DatePicker and propagates it to the parent component.
    /// </summary>
    private async Task OnDateChanged(DateTime newValue)
    {
        SelectedDate = newValue;
        await SelectedDateChanged.InvokeAsync(newValue);
    }

    private DatePicker<DateTime>? datePicker;
    private DotNetObjectReference<CustomDatePicker>? _dotNetRef;
}


Notice the trick done to capture unmatched values in case some parameters of DatePicker in the custom date picker is missing and we still want the user of this component to set a parameter of the date picker component :


    /// 
    /// Additional attributes to be splatted onto the underlying DatePicker component.
    /// 
    [Parameter(CaptureUnmatchedValues = true)] 
    public Dictionary<string, object>? AdditionalAttributes { get; set; }


The calls to IJSRunTime to register callback script handlers will register callbacks for the component, this is done in this line :

_dotNetRef = DotNetObjectReference.Create(this);
await JS.InvokeVoidAsync("registerFlatpickrMonthChange", "input.flatpickr-input", _dotNetRef); 
await JS.InvokeVoidAsync("registerFlatpickrYearChange", "input.flatpickr-input", _dotNetRef);           

The following client side script is registeret into the global window object to provide script event handlers that will be called when the end-user changes selected year or month, either via the UI controls or just entering the date.

datepicker-highlight.js


window.highlightFlatpickrDates = (selector, dates, highlightCssClass) => {
    //debugger
    const calendar = document.querySelector(selector); 
    if (!calendar) {
        return; //Wait for Flatpickr to render days
    }
    //debugger
    setTimeout(() => {
        //
        dates.forEach(date => {
            //debugger
            const dayElem = calendar.querySelector(`.flatpickr-day[aria-label="${date.date}"]`);
            if (dayElem) {
                dayElem.classList.add(highlightCssClass);
                dayElem.setAttribute('title', date.annotation);
            }
        });
    }, 50);
};

window.registerFlatpickrMonthChange = (selector, dotNetHelper) => {
    const fpInput = document.querySelector(selector);
    if (!fpInput || !fpInput._flatpickr) {
        setTimeout(() => window.registerFlatpickrMonthChange(selector, dotNetHelper), 50);
        return;
    }
    fpInput._flatpickr.config.onMonthChange.push(function () {
        dotNetHelper.invokeMethodAsync('OnMonthChanged');
    });
};

window.registerFlatpickrYearChange = (selector, dotNetHelper) => {
    const fpInput = document.querySelector(selector);
    if (!fpInput || !fpInput._flatpickr) {
        setTimeout(() => window.registerFlatpickrYearChange(selector, dotNetHelper), 50);
        return;
    }
    fpInput._flatpickr.config.onYearChange.push(function () {
        dotNetHelper.invokeMethodAsync('OnYearChanged');
    });
};

The following test page test out the component.

StyledDatePicker.razor



@page "/"
@using BlazoriseDatePickerWithHolidays.Components
@using BlazoriseDatePickerWithHolidays.Service
@using System.Globalization
@using static BlazoriseDatePickerWithHolidays.Service.HolidayService
@inject IHolidayService HolidayService

<h3>Custom Date Picker with Highlighted Days</h3>

<p>

    <b>The date picker used here is from Blazorise library and customized to allow setting days to highlight including tooltips annotating a description of the date being highlighted.</b>
    <br />
    Blazorise DatePicker component is described here:
    <br /><br />
    <a href="https://blazorise.com/docs/components/date-picker">https://blazorise.com/docs/components/date-picker</a>
    <br /><br />
    The highlighted dates selected for this demo are Christian Holidays and other public days off in Norway.
    In Norwegian, these days off are called 'Offentlige høytidsdager'.<br />
    These days are marked with a pink background, white foreground and rounded corners. <br />
    Tooltips are added showing the Holiday name <br/>

</p>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-2">
            <CustomDatePicker
                @bind-SelectedDate="selectedDate" 
                Locale="en-US"
                FirstDayOfWeek="DayOfWeek.Monday"
                InputMode="DateInputMode.DateTime"
                DisplayFormat="HH:mm dd.MM.yyyy"
                InputFormat="HH:mm dd.MM.yyyy"
                TimeAs24hr="true"
                HighlightedDays="_holidays"
                @ref="@_datePicker" />
        </div>
    </div>
</div>

@{
    var chosenLocale = new CultureInfo(_datePicker?.Locale ?? "en-US");
}

<p class="mt-3">Selected date <strong>@selectedDate.ToString("HH:mm dd.MM.yyyy", chosenLocale)</strong></p>

@code {
    private DateTime selectedDate = new DateTime(2025, 5, 4);
    private static int minYear = DateTime.Today.AddYears(-20).Year;
    private List<AnnotatedDateTime>? _holidays;

    private CustomDatePicker? _datePicker;

    protected override void OnParametersSet()
    {  
        _holidays = Enumerable.Range(minYear, 40)
            .SelectMany(y => HolidayService.GetHolidays(y))
            .ToList();
    }

}


The dates to select uses an implementation of IHolidayService. This implemented like this, note that this finds 'Offentlige høytidsdager' and (Christian-belief) holidays in Norway.

HolidayService.cs



namespace BlazoriseDatePickerWithHolidays.Service;

public interface IHolidayService
{
    DateTime AddWorkingDaysToDate(DateTime date, int days);
    List<string> GetHolidayNames();
    IEnumerable<HolidayService.AnnotatedDateTime> GetHolidays(int year);
    bool IsHoliday(DateTime date);
    bool IsWorkingDay(DateTime date);
}

public partial class HolidayService : IHolidayService
{

    private static MemoryCache<int, HashSet<AnnotatedDateTime>> holidays = new();

    private static readonly List<string> holidayNames = new List<string> {
            "1. nyttårsdag",
            "Palmesøndag",
            "Skjærtorsdag",
            "Langfredag",
            "1. påskedag",
            "2. påskedag",
            "Offentlig høytidsdag",
            "Grunnlovsdag",
            "Kristi Himmelfartsdag",
            "1. pinsedag",
            "2. pinsedag",
            "1. juledag",
            "2. juledag"
    };

    /// <summary>
    /// Adds the given number of working days to the given date. A working day is
    /// specified as a regular Norwegian working day, excluding weekends and all
    /// national holidays.
    /// 
    /// Example 1:
    /// - Add 5 working days to Wednesday 21.03.2007 -> Yields Wednesday
    /// 28.03.2007. (skipping saturday and sunday)
    /// 
    /// Example 2:
    /// - Add 5 working days to Wednesday 04.04.2007 (day before
    /// easter-long-weekend) -> yields Monday 16.04.2007 (skipping 2 weekends and
    /// 3 weekday holidays).
    /// </summary>
    /// <param name="date">The original date</param>
    /// <param name="days">The number of working days to add</param>
    /// <returns>The new date</returns>
    public DateTime AddWorkingDaysToDate(DateTime date, int days)
    {
        var localDate = date;
        for (var i = 0; i < days; i++)
        {
            localDate = localDate.AddDays(1);
            while (!IsWorkingDay(localDate))
            {
                localDate = localDate.AddDays(1);
            }
        }
        return localDate;
    }

    /// <summary>
    /// Will check if the given date is a working day. That is check if the given
    /// date is a weekend day or a national holiday.
    /// </summary>
    /// <param name="date">The date to check</param>
    /// <returns>true if the given date is a working day, false otherwise</returns>
    public bool IsWorkingDay(DateTime date)
    {
        return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday
               && !IsHoliday(date);
    }

    public List<string> GetHolidayNames()
    {
        return holidayNames;
    }

    /// <summary>
    /// Check if given Date object is a holiday.
    /// </summary>
    /// <param name="date">date to check if is a holiday</param>
    /// <returns>true if holiday, false otherwise</returns>
    public bool IsHoliday(DateTime date)
    {
        var year = date.Year;
        var holidaysForYear = GetHolidaySet(year);
        foreach (var holiday in holidaysForYear)
        {
            if (CheckDate(date, holiday.Value))
            {
                return true;
            }
        }
        return false;
    }

    /// <summary>
    /// Return a sorted array of holidays for a given year.
    /// </summary>
    /// <param name="year">The year to get holidays for</param>
    /// <returns>Holidays, sorted by date</returns>
    public IEnumerable<AnnotatedDateTime> GetHolidays(int year)
    {
        var days = GetHolidaySet(year);
        var listOfHolidays = new List<AnnotatedDateTime>(days);
        listOfHolidays.Sort((date1, date2) => date1.Value.CompareTo(date2.Value));
        return listOfHolidays;
    }

    /// <summary>
    /// Get a set of holidays for a given year
    /// </summary>
    /// <param name="year">The year to get holidays for</param>
    /// <returns>Holidays for year</returns>
    private IEnumerable<AnnotatedDateTime> GetHolidaySet(int year)
    {
        if (holidays == null)
        {
            holidays = new MemoryCache<int, HashSet<AnnotatedDateTime>>();
        }
        if (holidays.Get(year) == null)
        {
            var yearSet = new HashSet<AnnotatedDateTime>();

            // Add set holidays.
            yearSet.Add(new AnnotatedDateTime("1. nyttårsdag " + year, new DateTime(year, 1, 1)));
            yearSet.Add(new AnnotatedDateTime("Offentlig høytidsdag " + year, new DateTime(year, 5, 1)));
            yearSet.Add(new AnnotatedDateTime("Grunnlovsdag " + year, new DateTime(year, 5, 17)));
            yearSet.Add(new AnnotatedDateTime("1. juledag " + year, new DateTime(year, 12, 25)));
            yearSet.Add(new AnnotatedDateTime("2. juledag " + year, new DateTime(year, 12, 26)));

            // Add movable holidays - based on easter day.
            var easterDay = GetEasterDay(year);

            // Sunday before easter.
            yearSet.Add(new AnnotatedDateTime("Palmesøndag " + year, easterDay.AddDays(-7)));

            // Thurday before easter.
            yearSet.Add(new AnnotatedDateTime("Skjærtorsdag " + year, easterDay.AddDays(-3)));

            // Friday before easter.
            yearSet.Add(new AnnotatedDateTime("Langfredag " + year, easterDay.AddDays(-2)));

            // Easter day.
            yearSet.Add(new AnnotatedDateTime("1. påskedag " + year, easterDay));

            // Second easter day.
            yearSet.Add(new AnnotatedDateTime("2. påskedag " + year, easterDay.AddDays(1)));

            // "Kristi himmelfart" day.
            yearSet.Add(new AnnotatedDateTime("Kristi Himmelfartsdag " + year, easterDay.AddDays(39)));

            // "Pinse" day.
            yearSet.Add(new AnnotatedDateTime("1. pinsedag " + year, easterDay.AddDays(49)));

            // Second "Pinse" day.
            yearSet.Add(new AnnotatedDateTime("2. pinsedag " + year, easterDay.AddDays(50)));

            holidays.Add(year, yearSet);
        }
        return holidays.GetAsValue(year)!;
    }

    /// <summary>
    ///  Calculates easter day (sunday) by using Spencer Jones formula found here:
    ///  http://no.wikipedia.org/wiki/P%C3%A5skeformelen
    /// </summary>
    /// <param name="year">year</param>
    /// <returns>easterday for year</returns>
    private DateTime GetEasterDay(int year)
    {
        int a = year % 19;
        int b = year / 100;
        int c = year % 100;
        int d = b / 4;
        int e = b % 4;
        int f = (b + 8) / 25;
        int g = (b - f + 1) / 3;
        int h = (19 * a + b - d - g + 15) % 30;
        int i = c / 4;
        int k = c % 4;
        int l = (32 + 2 * e + 2 * i - h - k) % 7;
        int m = (a + 11 * h + 22 * l) / 451;
        int n = (h + l - 7 * m + 114) / 31; // This is the month number.
        int p = (h + l - 7 * m + 114) % 31; // This is the date minus one.

        return new DateTime(year, n, p + 1);
    }

    private bool CheckDate(DateTime date, DateTime other)
    {
        return date.Day == other.Day && date.Month == other.Month;
    }

}


Also, some CSS rules are added here for the custom control :

App.css



.flatpickr-day.pink-day {
    background: #ff69b4 !important; /* Hot pink */   
    color: white !important;
    border-radius: 50%;
}

.addon-margin {
    margin-left: 0.25rem !important; /* Adjust as needed */
}


Finally, a screenshot showing how the Custom date picker works. We have highlighted dates in May 2025 here, I chose to generate holidays for +- 20 years here. Obviously, an optimization would be the possibility to only pass in the chose year of the date picker and not a wide range of years. This is however looking to be fast anyways and I believe you can pass in a large number of dates to highlight here. Hopefully, this article has given you more inspiration how to add highlighting of dates into the Blazorise Datepicker component for Blazor !

Tuesday, 1 July 2025

Pluralizers in Entity Framework

In Entity Framework, you can implement a IPluralizationService to control how entities' names will be pluralized. In additional singularalized. The standard setup is an English pluralizer. In addition, a Spanish pluralizer is available. If you want to support another language for pluralization (and singularization), I have added a sample of this in the following Github repo of mine :

https://github.com/toreaurstadboss/BulkOperationsEntityFramework

Note that the Norwegian Pluralization service could also pluralize words in English and try to check if the word to pluralize is either English or Norwegian. It is about 500,000 English nouns and 100,000, so it might be hard to create a perfect pluralizer for both English or Norwegian. A list of about 40,000+ nouns are available here: https://gist.github.com/trag1c/f74b2ab3589bc4ce5706f934616f6195/ The Norwegian Pluralization service next could use that wordlist to check if the the word is English and use the standard built in English pluralizer service. I will give an updated version of the NorwegianPluralizationService at the end of the article.

NorwegianPluralizationService.cs



using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Pluralization;
using System.Diagnostics;
using System.Linq;

namespace BulkOperationsEntityFramework.Lib.Services
{

    /// <summary>
    /// Sources for the pluralization rules for Norwegian language:
    /// https://toppnorsk.com/2018/11/18/flertall-hovedregler/
    /// </summary>
    public class NorwegianPluralizationService : IPluralizationService
    {

        public static List<string> PluralizedWords = new List<string>();

        public string Pluralize(string word)
        {
            if (PluralizedWords.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                return word; // Return the already pluralized word
            }

//#if DEBUG
//            Debugger.Break();
//            Debugger.Launch(); // Uncomment this line to break into the debugger when this method is called, for example when database migrations are made with EF Code First
//#endif

            word = NormalizeWord(word);

            string pluralizedWord;

            if (_specialCases.ContainsKey(word))
            {
                pluralizedWord = _specialCases[word];
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsChangingVowelToÆ.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                if (word.Equals("Håndkle", StringComparison.OrdinalIgnoreCase))
                {
                    pluralizedWord = "Håndklær";
                    PluralizedWords.Add(pluralizedWord);
                    return pluralizedWord;
                }
                pluralizedWord = word.Replace("å", "æ").Replace("e", "æ") + "r";
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsForUnits.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsForRelatives.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                switch (word.ToLower())
                {
                    case "far": pluralizedWord = "Fedre"; break;
                    case "mor": pluralizedWord = "Mødre"; break;
                    case "datter": pluralizedWord = "Døtre"; break;
                    case "søster": pluralizedWord = "Søstre"; break;
                    case "fetter": pluralizedWord = "Fettere"; break;
                    case "onkel": pluralizedWord = "Onkler"; break;
                    case "svigerbror": pluralizedWord = "Svigerbrødre"; break;
                    case "svigerfar": pluralizedWord = "Svigerfedre"; break;
                    case "svigersøster": pluralizedWord = "Svigersøstre"; break;
                    case "svigermor": pluralizedWord = "Svigermødre"; break;
                    case "bror": pluralizedWord = "Brødre"; break;
                    default: pluralizedWord = word; break;
                }
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsNeutralGenderEndingWithEumOrIum.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                if (word.EndsWith("eum"))
                    pluralizedWord = word.Substring(0, word.Length - 3) + "eer";
                else if (word.EndsWith("ium"))
                    pluralizedWord = word.Substring(0, word.Length - 3) + "ier";
                else
                    pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsNoPluralizationForNeutralGenderOneSyllable.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordChangingVowelsInPluralFemale.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = NormalizeWord(word.ToLower().Replace("å", "e").Replace("a", "e") + "er");
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_wordsChangingVowelsInPluralMale.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                string rewrittenWord = NormalizeWord(word.Replace("o", "ø"));
                if (rewrittenWord.Equals("føt", StringComparison.OrdinalIgnoreCase))
                    pluralizedWord = rewrittenWord + "ter";
                else if (rewrittenWord.EndsWith("e"))
                    pluralizedWord = rewrittenWord + "r";
                else if (!rewrittenWord.EndsWith("er"))
                    pluralizedWord = rewrittenWord + "er";
                else
                    pluralizedWord = rewrittenWord;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_nonEndingWordsInPlural.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = word;
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            // General rules
            if (word.EndsWith("er"))
                pluralizedWord = word.Substring(0, word.Length - 2) + "ere";
            else if (word.EndsWith("el"))
                pluralizedWord = word.Substring(0, word.Length - 2) + "ler";
            else if (word.EndsWith("e"))
                pluralizedWord = word + "r";
            else if (word.EndsWith("en"))
                pluralizedWord = word + "er";
            else
                pluralizedWord = word + "er";

            PluralizedWords.Add(pluralizedWord);
            return pluralizedWord;
        }

        public string Singularize(string word)
        {
            word = NormalizeWord(word);

            // Reverse special cases
            var specialSingular = _specialCases.FirstOrDefault(kvp => kvp.Value.Equals(word, StringComparison.OrdinalIgnoreCase));
            if (!specialSingular.Equals(default(KeyValuePair<string, string>)))
                return specialSingular.Key;

            // Words that are the same in singular and plural
            if (_nonEndingWordsInPlural.Contains(word, StringComparer.OrdinalIgnoreCase) ||
                _wordsNoPluralizationForNeutralGenderOneSyllable.Contains(word, StringComparer.OrdinalIgnoreCase) ||
                _wordsForUnits.Contains(word, StringComparer.OrdinalIgnoreCase))
                return word;

            // Irregulars and vowel changes (expand as needed)
            if (word.Equals("Bøker", StringComparison.OrdinalIgnoreCase)) return "Bok";
            if (word.Equals("Føtter", StringComparison.OrdinalIgnoreCase)) return "Fot";
            if (word.Equals("Brødre", StringComparison.OrdinalIgnoreCase)) return "Bror";
            if (word.Equals("Menn", StringComparison.OrdinalIgnoreCase)) return "Mann";
            if (word.Equals("Kvinner", StringComparison.OrdinalIgnoreCase)) return "Kvinne";
            if (word.Equals("Gutter", StringComparison.OrdinalIgnoreCase)) return "Gutt";
            if (word.Equals("Netter", StringComparison.OrdinalIgnoreCase)) return "Natt";
            if (word.Equals("Tær", StringComparison.OrdinalIgnoreCase)) return "Tå";
            if (word.Equals("Tenner", StringComparison.OrdinalIgnoreCase)) return "Tann";
            if (word.Equals("Trær", StringComparison.OrdinalIgnoreCase)) return "Tre";
            if (word.Equals("Knær", StringComparison.OrdinalIgnoreCase)) return "Kne";
            if (word.Equals("Bønder", StringComparison.OrdinalIgnoreCase)) return "Bonde";
            if (word.Equals("Hender", StringComparison.OrdinalIgnoreCase)) return "Hand";
            if (word.Equals("Døtre", StringComparison.OrdinalIgnoreCase)) return "Datter";
            if (word.Equals("Fedre", StringComparison.OrdinalIgnoreCase)) return "Far";
            if (word.Equals("Mødre", StringComparison.OrdinalIgnoreCase)) return "Mor";
            if (word.Equals("Søstre", StringComparison.OrdinalIgnoreCase)) return "Søster";
            if (word.Equals("Øyne", StringComparison.OrdinalIgnoreCase)) return "Øye";

            // "ler" ending (from "el")
            if (word.EndsWith("ler"))
            {
                return word.Substring(0, word.Length - 2);
            }
            if (word.EndsWith("ter"))
            {
                return word.Substring(0, word.Length - 1);
            }

            // "ere" ending (from "er" ending in singular, e.g. "Lærere" -> "Lærer")
            if (word.EndsWith("ere"))
                return word.Substring(0, word.Length - 1);

            // "er" ending (general case, e.g. "Biler" -> "Bil", "Stoler" -> "Stol", "Jenter" -> "Jente")
            if (word.EndsWith("er"))
                return word.Substring(0, word.Length - 2);

            // "r" ending (from "e" ending in singular, e.g. "Jenter" -> "Jente" already handled above)
            if (word.EndsWith("r"))
            {
                var possibleSingular = word.Substring(0, word.Length - 1);
                return possibleSingular;
            }

            // Default: return as is
            return word;
        }

        /// <summary>
        /// Make the world normalized, i.e. first letter upper case and rest lower case letters, the word is trimmed.
        /// Not considering using invariant culture here, as this is a Norwegian pluralization service.
        /// </summary>
        /// <remarks>In case an empty word (null or empty) is passed in, just return the word.
        /// Edge case: In case just One non-empty letter was passed in, make the word also uppercase.</remarks>
        private string NormalizeWord(string word)
        {
            word = word?.Trim();
            if (string.IsNullOrEmpty(word) || word.Trim().Length <= 1) {
                return word?.ToUpper();
            }
            return word.Substring(0, 1).ToUpper() + word.Trim().ToLower().Substring(1);
        }

        private string[] _nonEndingWordsInPlural = new string[] {
            "mus", "sko", "ski", "feil", "ting" }; // Add more non-ending words in plural as needed

        private string[] _wordsChangingVowelsInPluralMale = new string[]
        {
            "bonde", "fot", "bok", "bot", "rot"
        };

        private Dictionary<string, string> _specialCases = new Dictionary<string, string>
        {
            { "Mann", "Menn" } , // 'mann' => 'menn'
            { "Barn", "Barn" }, // 'barn' => 'barn' (no pluralization)
            { "Øye", "Øyne" }, // 'øye' => 'øyne' (plural form of 'eye') //consider adding more special cases here in case all the other pluralization rules do not cover the given word
        };

        private string[] _wordsChangingVowelToÆ = new string[]
        {
            "Håndkle", "Kne", "Tre", "Tå"
        };

        private string[] _wordsForUnits = new string[]
        {
            "meter", "centimeter", "millimeter", "kilometer", "gram", "kilogram", "tonn", "liter", "desiliter", "centiliter", "dollar", "lire",
            "pesetas", "euro", "yen", "franc", "pund", "rupee", "ringgit", "peso", "real", "won", "yuan"
        };

        private string[] _wordChangingVowelsInPluralFemale = new string[]
        {
            "and", "hand", "hånd", "natt", "stang", "strand", "tang", "tann"
        };

        private string[] _wordsForRelatives = new string[]
        {
            "far", "mor", "datter", "fetter", "onkel", "bror", "svigerbror", "svigerfar", "svigermor", "svigersøster", "søster"
        };

        private string[] _wordsNoPluralizationForNeutralGenderOneSyllable = new string[]
        {
            "hus", "fjell", "blad"
        };

        private string[] _wordsNeutralGenderEndingWithEumOrIum = new string[]
        {
            "museum", "Jubileum", "kjemikalium"
        };

    }
}


The following DbConfiguration set up for the DbContext sets up the pluralization service to use for Entity Framework.

ApplicationDbConfiguration.cs



using BulkOperationsEntityFramework.Lib.Services;
using System;
using System.Data.Entity;
using System.Data.Entity.SqlServer;

namespace BulkOperationsEntityFramework
{
    public class ApplicationDbConfiguration : DbConfiguration
    {

        public ApplicationDbConfiguration()
        {
        
            SetPluralizationService(new NorwegianPluralizationService());  //Set up the NorwegianPluralizationService as the Pluralizer         
            
            //more code etc..
        }

    }

}


I have also created a Schema attribute to control schema names of tables convention previously in the solution, so the Norwegian pluralizer is also being used there.

SchemaConvention.cs



using BulkOperationsEntityFramework.Attributes;
using BulkOperationsEntityFramework.Lib.Services;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Reflection;

namespace BulkOperationsEntityFramework.Conventions
{
    public class SchemaConvention : Convention
    {
        public SchemaConvention()
        {
            var pluralizer = new NorwegianPluralizationService();

            Types().Configure(c =>
            {
                var schemaAttr = c.ClrType.GetCustomAttribute<SchemaAttribute>(false);
                var tableName = pluralizer.Pluralize(c.ClrType.Name);

                if (schemaAttr != null && !string.IsNullOrEmpty(schemaAttr.SchemaName))
                {
                    c.ToTable(tableName, schemaAttr.SchemaName ?? "dbo");
                }
                else
                {
                    c.ToTable(tableName);
                }
            });
        }
    }
}


The DbContext will use the IPluralizationService. Consider first this example DbContext :

ApplicationDbContext



using BulkOperationsEntityFramework.Conventions;
using BulkOperationsEntityFramework.Models;
using BulkOperationsEntityFramework.Test;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;

namespace BulkOperationsEntityFramework
{

    [DbConfigurationType(typeof(ApplicationDbConfiguration))]
    public class ApplicationDbContext : DbContext
    {       

        public ApplicationDbContext(DbConnection connection) : base(connection, false)
        {
        }

        public ApplicationDbContext() : base("name=App")
        {
        }

        public virtual DbSet Bruker { get; set; }

        public DbSet ArkivertBruker { get; set; }

        public DbSet ArkivertGjest { get; set; }

        public DbSet Sesjon { get; set; }

        public DbSet Jubileum { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {            
            modelBuilder.Conventions.Add(new SchemaConvention());
            //more code etc
          
        }

    }

}


The following test cases checks how good the pluralizer works.

ApplicationDbContextTests.cs



using Bogus;
using BulkOperationsEntityFramework.Lib.Services;
using BulkOperationsEntityFramework.Models;
using FluentAssertions;
using Moq;
using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure.Interception;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace BulkOperationsEntityFramework.Test
{

    [TestFixture]
    public class ApplicationDbContextTests
    {

       
        [Test]
        [TestCaseSource(nameof(NorwegianPluralizationCases))]
        public void CanUsePluralizationService(string word, string expected)
        {
            var norwegianPluralizationService = new NorwegianPluralizationService();
            string pluralizedWord = norwegianPluralizationService.Pluralize(word);
            pluralizedWord.Should().Be(expected, "Norwegian Pluralization service should return the correct plural form of the word.");
        }

        [Test, TestCaseSource(nameof(NorwegianSingularizationCases))]
        public void NorwegianPluralizationService_CanSingularize(string plural, string expectedSingular)
        {
            var norwegianPluralizationService = new NorwegianPluralizationService();

            var actual = norwegianPluralizationService.Singularize(plural);
            Assert.That(actual, Is.EqualTo(expectedSingular), $"Expected singular of '{plural}' to be '{expectedSingular}', but got '{actual}'.");
        }

        public static IEnumerable<TestCaseData> NorwegianPluralizationCases
        {
            get
            {
                yield return new TestCaseData("Bil", "Biler");
                yield return new TestCaseData("Bok", "Bøker");
                yield return new TestCaseData("Hund", "Hunder");
                yield return new TestCaseData("Stol", "Stoler");
                yield return new TestCaseData("Jente", "Jenter");
                yield return new TestCaseData("Gutt", "Gutter");
                yield return new TestCaseData("Lærer", "Lærere");
                yield return new TestCaseData("Barn", "Barn");
                yield return new TestCaseData("Fjell", "Fjell");
                yield return new TestCaseData("Sko", "Sko");
                yield return new TestCaseData("Ting", "Ting");
                yield return new TestCaseData("Mann", "Menn");
                yield return new TestCaseData("Kvinne", "Kvinner");
                yield return new TestCaseData("Bror", "Brødre");
                yield return new TestCaseData("Far", "Fedre");
                yield return new TestCaseData("Mor", "Mødre");
                yield return new TestCaseData("Datter", "Døtre");
                yield return new TestCaseData("Søster", "Søstre");
                yield return new TestCaseData("Øye", "Øyne");
                yield return new TestCaseData("Hand", "Hender");
                yield return new TestCaseData("Fot", "Føtter");
                yield return new TestCaseData("Tå", "Tær");
                yield return new TestCaseData("Tann", "Tenner");
                yield return new TestCaseData("Natt", "Netter");
                yield return new TestCaseData("Tre", "Trær");
                yield return new TestCaseData("Kne", "Knær");
                yield return new TestCaseData("Bonde", "Bønder");

                // _nonEndingWordsInPlural
                yield return new TestCaseData("Mus", "Mus");
                yield return new TestCaseData("Ski", "Ski");
                yield return new TestCaseData("Feil", "Feil");

                // _wordsChangingVowelsInPluralMale
                yield return new TestCaseData("Bot", "Bøter");
                yield return new TestCaseData("Rot", "Røter");

                // _wordsChangingVowelToÆ
                yield return new TestCaseData("Håndkle", "Håndklær");
                yield return new TestCaseData("Kne", "Knær");

                // _wordsForUnits (should not pluralize)
                yield return new TestCaseData("Meter", "Meter");
                yield return new TestCaseData("Gram", "Gram");
                yield return new TestCaseData("Dollar", "Dollar");

                // _wordChangingVowelsInPluralFemale
                yield return new TestCaseData("And", "Ender");
                yield return new TestCaseData("Hånd", "Hender");
                yield return new TestCaseData("Stang", "Stenger");
                yield return new TestCaseData("Strand", "Strender");
                yield return new TestCaseData("Tang", "Tenger");
                yield return new TestCaseData("Tann", "Tenner");

                // _wordsForRelatives (some already covered, but add missing)
                yield return new TestCaseData("Fetter", "Fettere");
                yield return new TestCaseData("Onkel", "Onkler");
                yield return new TestCaseData("Svigerbror", "Svigerbrødre");
                yield return new TestCaseData("Svigerfar", "Svigerfedre");
                yield return new TestCaseData("Svigermor", "Svigermødre");
                yield return new TestCaseData("Svigersøster", "Svigersøstre");

                // _wordsNoPluralizationForNeutralGenderOneSyllable
                yield return new TestCaseData("Hus", "Hus");
                yield return new TestCaseData("Blad", "Blad");

                // _wordsNeutralGenderEndingWithEumOrIum
                yield return new TestCaseData("Museum", "Museer");
                yield return new TestCaseData("Jubileum", "Jubileer");
                yield return new TestCaseData("Kjemikalium", "Kjemikalier");
            }
        }

        public static IEnumerable<TestCaseData> NorwegianSingularizationCases
        {
            get
            {
                yield return new TestCaseData("Biler", "Bil");
                yield return new TestCaseData("Bøker", "Bok");
                yield return new TestCaseData("Hunder", "Hund");
                yield return new TestCaseData("Stoler", "Stol");
                yield return new TestCaseData("Jenter", "Jente");
                yield return new TestCaseData("Gutter", "Gutt");
                yield return new TestCaseData("Lærere", "Lærer");
                yield return new TestCaseData("Barn", "Barn");
                yield return new TestCaseData("Fjell", "Fjell");
                yield return new TestCaseData("Sko", "Sko");
                yield return new TestCaseData("Ting", "Ting");
                yield return new TestCaseData("Menn", "Mann");
                yield return new TestCaseData("Kvinner", "Kvinne");
                yield return new TestCaseData("Brødre", "Bror");
                yield return new TestCaseData("Fedre", "Far");
                yield return new TestCaseData("Mødre", "Mor");
                yield return new TestCaseData("Døtre", "Datter");
                yield return new TestCaseData("Søstre", "Søster");
                yield return new TestCaseData("Øyne", "Øye");
                yield return new TestCaseData("Hender", "Hand");
                yield return new TestCaseData("Føtter", "Fot");
                yield return new TestCaseData("Tær", "Tå");
                yield return new TestCaseData("Tenner", "Tann");
                yield return new TestCaseData("Netter", "Natt");
                yield return new TestCaseData("Trær", "Tre");
                yield return new TestCaseData("Knær", "Kne");
                yield return new TestCaseData("Bønder", "Bonde");
            }
        }

    }
}


The pluralization is used when creating migrations and map entites to table names. The following migration shows how the Norwegian pluralization has pluralized the table names.

202506292302222_Init.cs



namespace BulkOperationsEntityFramework.Migrations
{
    using System.Data.Entity.Migrations;

    public partial class Init : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "Archive.Arkivertbrukere",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Email = c.String(maxLength: 255),
                    FirstName = c.String(maxLength: 255),
                    LastName = c.String(maxLength: 255),
                    PhoneNumber = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "Arkiv.Gjester",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Email = c.String(maxLength: 255),
                    FirstName = c.String(maxLength: 255),
                    LastName = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Brukere",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Email = c.String(maxLength: 255),
                    FirstName = c.String(maxLength: 255),
                    LastName = c.String(maxLength: 255),
                    PhoneNumber = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Jubileer",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Date = c.DateTime(),
                    Description = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Id);

            CreateTable(
                "dbo.Sesjoner",
                c => new
                {
                    Key = c.Guid(nullable: false),
                    CreatedAt = c.DateTime(nullable: false),
                    ExpiresAt = c.DateTime(),
                    IpAddress = c.String(maxLength: 255),
                    UserAgent = c.String(maxLength: 255),
                })
                .PrimaryKey(t => t.Key);

        }

        public override void Down()
        {
            DropTable("dbo.Sesjoner");
            DropTable("dbo.Jubileer");
            DropTable("dbo.Brukere");
            DropTable("Arkiv.Gjester");
            DropTable("Archive.Arkivertbrukere");
        }
    }
}


The table names above are pluralized into their Norwegian pluralization. In Norwegian we call these words "ubestemt flertall", indefinite plural. It is possible to add detection if the passed in noun is an English noun. But beware that there are several Norwegian nouns overlapping these English nouns. So in fairness, a Norwegian wordlist should also be checked. However, the following update only shows how an English dictionary can be checked. The English noun list is put into a static variable into memory for quick access. The world list I have tested with got about 40,0000+ noun as mentioned previously in this article.

NorwegianPluraizationService.cs

(updated with a check against a English noun list and using the default EnglishPluralizationService that Entity Framework contains to add support for English noun pluralization (and singularization)


using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure.Pluralization;
using System.IO;
using System.Linq;
using System.Reflection;

namespace BulkOperationsEntityFramework.Lib.Services
{

    /// <summary>
    /// Sources for the pluralization rules for Norwegian language:
    /// https://toppnorsk.com/2018/11/18/flertall-hovedregler/
    /// </summary>
    public class NorwegianPluralizationService : IPluralizationService
    {
        private EnglishPluralizationService _englishPluralizationService = new EnglishPluralizationService();
        
        public static List<string> PluralizedWords = new List<string>();

        private static List<string> EnglishNounsWordList = null;

        public string Pluralize(string word)
        {
            if (PluralizedWords.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                return word; // Return the already pluralized word
            }

            //#if DEBUG
            //            Debugger.Break();
            //            Debugger.Launch(); // Uncomment this line to break into the debugger when this method is called, for example when database migrations are made with EF Code First
            //#endif

            word = NormalizeWord(word);

            if (EnglishNounsWordList == null)
            {
                EnglishNounsWordList = new List<string>();
                var assembly = typeof(NorwegianPluralizationService).Assembly;
                // Adjust the resource name to match your project's default namespace and folder structure
                var resourceName = "BulkOperationsEntityFramework.Lib.Services.EnglishNouns.txt";
                using (var stream = assembly.GetManifestResourceStream(resourceName))
                using (var reader = new StreamReader(stream))
                {
                    EnglishNounsWordList.AddRange(reader.ReadToEnd().Split('\n').Select(l => l.Trim()));
                }
            }

            string pluralizedWord;

            if (EnglishNounsWordList.Contains(word, StringComparer.OrdinalIgnoreCase))
            {
                pluralizedWord = _englishPluralizationService.Pluralize(word);
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }

            if (_specialCases.ContainsKey(word))
            {
                pluralizedWord = _specialCases[word];
                PluralizedWords.Add(pluralizedWord);
                return pluralizedWord;
            }
            
            //more code


To sum up, we can customize Entity Framework pluralization to support other languages. But please note that there is a lot of work to make a good pluralization service. Also, you probably want to support English nouns too. The last version above did not use a Norwegian wordlist. A better pluralization service could directly try to lookup pluralized and singularized nouns of both languages instead of all the general and specific rules presented in these code samples. The English pluralization service do not do this, instead it relies on rule sets, such as the first version of
the Norwegian pluralizer shown earlier in this article is using.

Monday, 5 May 2025

Using MatPlotLib from .NET

MatPlotLib is a powerful library for data visualization. It provides graphing for scientific computing. It can be used for doing both mathematical calculations and statistics. Together with additional libraries like NumPy or Numerical Python, it is clear that Python as a programming language and ecosystem provides a lot of powerful functionality that is also free to use. MatplotLib has a BSD license, which means it can be ued for personal, academic or commercial purposes without restrictions. This article will look at using MatplotLib from .NET. First off an image that displays the demo and example of using MatplotLib.

The source code shown in this article is available on Github here:

https://github.com/toreaurstadboss/SeabornBlazorVisualizer



Using MatPlotLib from .NET

First off, install Anaconda. Anaconda is a Python distribution that contains a large collection of data visualization libraries. A compatible version with the lastest version of Python.net Nuget library. The demo displayed here uses Anaconda version 2023.03.

Anaconda archived versions 2023.03 can be installed from here. Windows users can download the file: https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Windows-x86_64.exe

https://repo.anaconda.com/archive/

Next up, install also Python 3.10 version. It will be used together with Anaconda. A 64-bit installer can be found here:

Python 3.10 installer (Windows 64-bits) The correct versions of NumPy and MatPlotLib can be checked against this list :

https://github.com/toreaurstadboss/SeabornBlazorVisualizer/blob/main/SeabornBlazorVisualizer/conda_list_loading_matplotlib_working_1st_May_2025.txt

Calculating the determinite integral of a function

The demo in this article show in the link at the top has got an appsettings.json file, you can adjust to your environment.

appsettings.json Application configuration file




{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "PythonConfig": {
    "PythonDllPath": "C:\\Python310\\Python310.dll",
    "PythonHome": "C:\\Programdata\\anaconda3",
    "PythonSitePackages":  "C:\\Programdata\\anaconda3\\lib\\site-packages",
    "PythonVersion": "3.10"
  },
  "AllowedHosts": "*"
}


Clone the source code and run the application. It is a Blazor server app. You can run it from VS 2022 for example. The following code shows how Python.net is set up to start using Python. Both Python 3.10 and Anaconda site libs are used here. The Python runtime and engine is set up using this helper class.

PythonInitializer.cs



using Microsoft.Extensions.Options;
using Python.Runtime;

namespace SeabornBlazorVisualizer.Data
{

    /// <summary>
    /// Helper class to initialize the Python runtime
    /// </summary>
    public static class PythonInitializer
    {

        private static bool runtime_initialized = false;

        /// <summary>
        /// Perform one-time initialization of Python runtime
        /// </summary>
        /// <param name="pythonConfig"></param>
        public static void InitializePythonRuntime(IOptions<PythonConfig> pythonConfig)
        {
            if (runtime_initialized)
                return;
            var config = pythonConfig.Value;

            // Set environment variables
            Environment.SetEnvironmentVariable("PYTHONHOME", config.PythonHome, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONPATH", config.PythonSitePackages, EnvironmentVariableTarget.Process);
            Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", config.PythonDllPath);
            Environment.SetEnvironmentVariable("PYTHONNET_PYVER", config.PythonVersion);

            PythonEngine.Initialize();

            PythonEngine.PythonHome = config.PythonHome ?? Environment.GetEnvironmentVariable("PYTHONHOME", EnvironmentVariableTarget.Process)!;
            PythonEngine.PythonPath = config.PythonDllPath ?? Environment.GetEnvironmentVariable("PYTHONNET_PYDLL", EnvironmentVariableTarget.Process)!;

            PythonEngine.BeginAllowThreads();
            AddSitePackagesToPythonPath(pythonConfig);
            runtime_initialized = true;
        }

        private static void AddSitePackagesToPythonPath(IOptions<PythonConfig> pythonConfig)
        {
            if (!runtime_initialized)
            {
                using (Py.GIL())
                {
                    dynamic sys = Py.Import("sys");
                    sys.path.append(pythonConfig.Value.PythonSitePackages);
                    Console.WriteLine(sys.path);

                    //add folders in solution this too with scripts
                    sys.path.append(@"Data/");
                }
            }
        }

    }
}



The following helper class sets up the site libraries we will use.

PythonHelper.cs



using Python.Runtime;

namespace SeabornBlazorVisualizer.Data
{

    /// <summary>
    /// Helper class to initialize the Python runtime
    /// </summary>
    public static class PythonHelper
    {

        /// <summary>
        /// Imports Python modules. Returned are the following modules:
        /// <para>np (numpy)</para>
        /// <para>os (OS module - standard library)</para>
        /// <para>scipy (scipy)</para>
        /// <para>mpl (matplotlib)</para>
        /// <para>plt (matplotlib.pyplot </para>
        /// </summary>
        /// <returns>Tuple of Python modules</returns>
        public static (dynamic np, dynamic os, dynamic scipy, dynamic mpl, dynamic plt) ImportPythonModules()
        {

            dynamic np = Py.Import("numpy");
            dynamic os = Py.Import("os");
            dynamic mpl = Py.Import("matplotlib");
            dynamic plt = Py.Import("matplotlib.pyplot");
            dynamic scipy = Py.Import("scipy");

            mpl.use("Agg");

            return (np, os, scipy, mpl, plt);
        }

    }
}



The demo is a Blazor server app. The following service will generate the plot of a determinite integral using MatPlotLib. The service saves the plot into a PNG file. This PNG file is saved into the folder wwwroot. The Blazor server app displays the image that was generated and saved.

MatPlotImageService.cs



using Microsoft.Extensions.Options;
using Python.Runtime;

namespace SeabornBlazorVisualizer.Data
{
    public class MatplotPlotImageService
    {

        private IOptions<PythonConfig>? _pythonConfig;

        private static readonly object _lock = new object();

        public MatplotPlotImageService(IOptions<PythonConfig> pythonConfig)
        {
            _pythonConfig = pythonConfig;
            PythonInitializer.InitializePythonRuntime(_pythonConfig);
        }

        public Task<string> GenerateDefiniteIntegral(string functionExpression, int lowerBound, int upperBound)
        {

            string? result = null;

            using (Py.GIL()) // Ensure thread safety for Python calls
            {
                dynamic np = Py.Import("numpy");
                dynamic plt = Py.Import("matplotlib.pyplot");

                dynamic patches = Py.Import("matplotlib.patches"); // Import patches module

                // Create a Python execution scope
                using (var scope = Py.CreateScope())
                {
                    // Define the function inside the scope
                    scope.Exec($@"
import numpy as np
def func(x):
    return {functionExpression}
");

                    // Retrieve function reference from scope
                    dynamic func = scope.Get("func");

                    // Define integration limits
                    double a = lowerBound, b = upperBound;

                    // Generate x-values
                    dynamic x = np.linspace(0, 10, 100); //generate evenly spaced values in range [0, 20], 100 values (per 0.1)
                    dynamic y = func.Invoke(x);

                    // Create plot figure
                    var fig = plt.figure();
                    var ax = fig.add_subplot(111);

                    // set title to function expression
                    plt.title(functionExpression);

                    ax.plot(x, y, "r", linewidth: 2);
                    ax.set_ylim(0, null);

                    // Select range for integral shading
                    dynamic ix = np.linspace(a, b, 100);
                    dynamic iy = func.Invoke(ix);

                    // **Fix: Separate x and y coordinates properly**
                    List<double> xCoords = new List<double> { a }; // Start at (a, 0)
                    List<double> yCoords = new List<double> { 0 };

                    int length = (int)np.size(ix);
                    for (int i = 0; i < length; i++)
                    {
                        xCoords.Add((double)ix[i]);
                        yCoords.Add((double)iy[i]);
                    }

                    xCoords.Add(b); // End at (b, 0)
                    yCoords.Add(0);

                    // Convert x and y lists to NumPy arrays
                    dynamic npVerts = np.column_stack(new object[] { np.array(xCoords), np.array(yCoords) });

                    // **Correctly Instantiate Polygon Using NumPy Array**
                    dynamic poly = patches.Polygon(npVerts, facecolor: "0.6", edgecolor: "0.2");
                    ax.add_patch(poly);

                    // Compute integral area
                    double area = np.trapezoid(iy, ix);
                    ax.text(0.5 * (a + b), 30, "$\\int_a^b f(x)\\mathrm{d}x$", ha: "center", fontsize: 20);
                    ax.text(0.5 * (a + b), 10, $"Area = {area:F2}", ha: "center", fontsize: 12);

                    plt.show();


                    result = SavePlot(plt, dpi: 150);
                }
            }
            return Task.FromResult(result);
        }

        public Task<string> GenerateHistogram(List<double> values, string title = "Provide Plot title", string xlabel = "Provide xlabel title", string ylabel = "Provide ylabel title")
        {
            string? result = null;
            using (Py.GIL()) //Python Global Interpreter Lock (GIL)
            {
                var (np, os, scipy, mpl, plt) = PythonHelper.ImportPythonModules();

                var distribution = np.array(values.ToArray());

                //// Ensure clearing the plot
                //plt.clf();

                var fig = plt.figure(); //create a new figure
                var ax1 = fig.add_subplot(1, 2, 1);
                var ax2 = fig.add_subplot(1, 2, 2);

                // Add style
                plt.style.use("ggplot");

                var counts_bins_patches = ax1.hist(distribution, edgecolor: "black");

                // Normalize counts to get colors 
                var counts = counts_bins_patches[0];
                var patches = counts_bins_patches[2];

                var norm_counts = counts / np.max(counts);

                int norm_counts_size = Convert.ToInt32(norm_counts.size.ToString());

                // Apply colors to patches based on frequency
                for (int i = 0; i < norm_counts_size; i++)
                {
                    plt.setp(patches[i], "facecolor", plt.cm.viridis(norm_counts[i])); //plt.cm is the colormap module in MatPlotlib. viridis creates color maps from normalized value 0 to 1 that is optimized for color-blind people.
                }

                // **** AX1 Histogram first - frequency counts ***** 

                ax1.set_title(title);
                ax1.set_xlabel(xlabel);
                ax1.set_ylabel(ylabel);

                string cwd = os.getcwd();

                // Calculate average and standard deviation
                var average = np.mean(distribution);
                var std_dev = np.std(distribution);
                var total_count = np.size(distribution);

                // Format average and standard deviation to two decimal places
                var average_formatted = np.round(average, 2);
                var std_dev_formatted = np.round(std_dev, 2);

                //Add legend with average and standard deviation
                ax1.legend(new string[] { $"Total count: {total_count}\n Average: {average_formatted} cm\nStd Dev: {std_dev_formatted} cm" }, framealpha: 0.5, fancybox: true);



                //***** AX2 : Set up ax2 = Percentage histogram next *******

                ax2.set_title("Percentage distribution");
                ax2.set_xlabel(xlabel);
                ax2.set_ylabel(ylabel);
                // Fix for CS1977: Cast the lambda expression to a delegate type
                ax2.yaxis.set_major_formatter((PyObject)plt.FuncFormatter(new Func<double, int, string>((y, _) => $"{y:P0}")));

                ax2.hist(distribution, edgecolor: "black", weights: np.ones(distribution.size) / distribution.size);

                // Format y-axis to show percentages
                ax2.yaxis.set_major_formatter(plt.FuncFormatter(new Func<double, int, string>((y, _) => $"{y:P0}")));

                // tight layout to prevent overlap 
                plt.tight_layout();

                // Show the plot with the two subplots at last (render to back buffer 'Agg', see method SavePlot for details)
                plt.show();

                result = SavePlot(plt, theme: "bmh", dpi: 150);
            }

            return Task.FromResult(result);
        }

        public Task<string> GeneratedCumulativeGraphFromValues(List<double> values)
        {
            string? result = null;
            using (Py.GIL()) //Python Global Interpreter Lock (GIL)
            {
                var (np, os, scipy, mpl, plt) = PythonHelper.ImportPythonModules();

                dynamic pythonValues = np.cumsum(np.array(values.ToArray()));

                // Ensure clearing the plot
                plt.clf();

                // Create a figure with increased size
                dynamic fig = plt.figure(figsize: new PyTuple(new PyObject[] { new PyFloat(6), new PyFloat(4) }));

                // Plot data
                plt.plot(values, color: "green");

                string cwd = os.getcwd();

                result = SavePlot(plt, theme: "ggplot", dpi: 200);

            }

            return Task.FromResult(result);
        }

        public Task<string> GenerateRandomizedCumulativeGraph()
        {
            string? result = null;
            using (Py.GIL()) //Python Global Interpreter Lock (GIL)
            {

                dynamic np = Py.Import("numpy");

                //TODO : Remove imports of pandas and scipy and datetime if they are not needed

                Py.Import("pandas");
                Py.Import("scipy");
                Py.Import("datetime");
                dynamic os = Py.Import("os");

                dynamic mpl = Py.Import("matplotlib");
                dynamic plt = Py.Import("matplotlib.pyplot");

                // Set dark theme
                plt.style.use("ggplot");

                mpl.use("Agg");


                // Generate data
                //dynamic x = np.arange(0, 10, 0.1);
                //dynamic y = np.multiply(2, x); // Use NumPy's multiply function

                dynamic values = np.cumsum(np.random.randn(1000, 1));


                // Ensure clearing the plot
                plt.clf();

                // Create a figure with increased size
                dynamic fig = plt.figure(figsize: new PyTuple(new PyObject[] { new PyFloat(6), new PyFloat(4) }));

                // Plot data
                plt.plot(values, color: "blue");

                string cwd = os.getcwd();

                result = SavePlot(plt, theme: "ggplot", dpi: 200);

            }

            return Task.FromResult(result);
        }

        /// <summary>
        /// Saves the plot to a PNG file with a unique name based on the current date and time
        /// </summary>
        /// <param name="plot">Plot, must be a PyPlot plot use Python.net Py.Import("matplotlib.pyplot")</param>
        /// <param name="theme"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        public string? SavePlot(dynamic plt, string theme = "ggplot", int dpi = 200)
        {
            string? plotSavedImagePath = null;
            //using (Py.GIL()) //Python Global Interpreter Lock (GIL)
            //{
            dynamic os = Py.Import("os");
            dynamic mpl = Py.Import("matplotlib");
            // Set dark theme
            plt.style.use(theme);
            mpl.use("Agg"); //set up rendering of plot to back-buffer ('headless' mode)

            string cwd = os.getcwd();
            // Save plot to PNG file
            string imageToCreatePath = $@"GeneratedImages\{DateTime.Now.ToString("yyyyMMddHHmmss")}{Guid.NewGuid().ToString("N")}_plotimg.png";
            string imageToCreateWithFolderPath = $@"{cwd}\wwwroot\{imageToCreatePath}";
            plt.savefig(imageToCreateWithFolderPath, dpi: dpi); //save the plot to a file (use full path)
            plotSavedImagePath = imageToCreatePath;

            CleanupOldGeneratedImages(cwd);
            //}
            return plotSavedImagePath;
        }

        private static void CleanupOldGeneratedImages(string cwd)
        {
            lock (_lock)
            {

                Directory.GetFiles(cwd + @"\wwwroot\GeneratedImages", "*.png")
                 .OrderByDescending(File.GetLastWriteTime)
                 .Skip(10)
                 .ToList()
                 .ForEach(File.Delete);
            }
        }

}



The code above shows some additional examples of using MatPlotLib.
  • Histogram example
  • Line graph using cumulative sum by making use of NumPy or a helper method in .NET
These examples demonstrates also that MatPlotLib can be used for statistics, which today for .NET is mostly crunched with the help of Excel or EP Plus library for example. Since Python is considered as the home of data visualization with its vast ecosystem of data science libraries, this article and demos shows how you can get started with using this ecosystem from .NET. Note, using Python.net to create these plots in MatPlotLib is best prepared using Jupyter Notebook. When the plot displayed looks okay, it is time to integrate that Python script into .NET and C# using Python.Net library. Make note that there will be some challenges to get the Python code to work in C# of course. When passing in values to a function, sometimes you must use
for example NumPy to create compatible data types. Also note the usage of the Pystatic class here from Python.net , which offers the GIL Global Interpreter lock and a way to import Python modules.

https://jupyter.org/ A screenshot showing histogram in the demo is shown below. As we can see, MatPlotLib can be used from many different data visualizations and domains.

Tuesday, 22 April 2025

Predicting variable using Regression with ML.net

This article will look at regression with ML.net In the example, the variable "Poverty rate" measured as a percentage against amount "teenage pregnancies" per 1,000 birth. The data is fetched from a publicly available CSV file. The data is obtained from Jeff Prosise repos of ML.net here on Github:

https://github.com/jeffprosise/ML.NET/blob/master/MLN-SimpleRegression/MLN-SimpleRegression/Data/poverty.csv



In this article, Linqpad 8 will be used. First off, the following two Nuget packages are added :
  • Microsoft.ML
  • Microsoft.ML.Mkl.Components
The following method will plot a scatter graph from provided MLContext data, and add a standard linear trendline, which will work with the example in this article.

Plotutils.cs



void PlotScatterGraph<T>(MLContext mlContext, IDataView trainData, Func<T, PointItem> pointCreator, string chartTitle) where T : class, new()
{
	//Convert the IDataview to an enumerable collection
	var data = mlContext.Data.CreateEnumerable<T>(trainData, reuseRowObject: false).Select(x => pointCreator(x)).ToList();

	// Calculate trendline (simple linear regression)
	double avgX = data.Average(d => d.X);
	double avgY = data.Average(d => d.Y);
	double slope = data.Sum(d => (d.X - avgX) * (d.Y - avgY)) / data.Sum(d => (d.X - avgX) * (d.X - avgX));
	double intercept = avgY - slope * avgX;
	var trendline = data.Select(d => new { X = d.X, Y = slope * d.X + intercept }).ToList();

	//Plot the scatter graph
	var plot = data.Chart(d => d.X)
		.AddYSeries(d => d.Y, LINQPad.Util.SeriesType.Point, chartTitle)
		.AddYSeries(d => trendline.FirstOrDefault(t => t.X == d.X)?.Y ?? 0, Util.SeriesType.Line, "Trendline")
		.ToWindowsChart();
		
	plot.AntiAliasing = System.Windows.Forms.DataVisualization.Charting.AntiAliasingStyles.All;
	plot.Dump();
}



Let's look at the code for loading the CSV data and into the MLContext and then used the method TrainTestSplit to split the data into training data and testing data. Note also the classes Input and Output and the usage of LoadColumn and ColumnName

Program.cs



void Main()
{

	string inputFile = Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath)!, @"Sampledata\poverty2.csv"); //linqpad tech

	var context = new MLContext(seed: 0);

	//Train the model 
	var data = context.Data
		.LoadFromTextFile<Input>(inputFile, hasHeader: true, separatorChar: ';');
	
	// Split data into training and test sets 
	var split = context.Data.TrainTestSplit(data, testFraction: 0.2);
	var trainData = split.TrainSet;
	var testData = split.TestSet;

	var pipeline = context
		.Transforms.NormalizeMinMax("PovertyRate")
		.Append(context.Transforms.Concatenate("Features", "PovertyRate"))
		.Append(context.Regression.Trainers.Ols());

	var model = pipeline.Fit(trainData);
	// Use the model to make a prediction
	var predictor = context.Model.CreatePredictionEngine<Input, Output>(model);
	var input = new Input { PovertyRate = 8.4f };

	var actual = 36.8f;

	var prediction = predictor.Predict(input);
	Console.WriteLine($"Input poverty rate: {input.PovertyRate} . Predicted birth rate per 1000: {prediction.TeenageBirthRate:0.##}");
	Console.WriteLine($"Actual birth rate per 1000: {actual}");

	// Evaluate the regression model 
	var predictions = model.Transform(testData);
	var metrics = context.Regression.Evaluate(predictions);
	Console.WriteLine($"R-squared: {metrics.RSquared:0.##}");
	Console.WriteLine($"Root Mean Squared Error: {metrics.RootMeanSquaredError:0.##}");
	Console.WriteLine($"Mean Absolute Error: {metrics.MeanAbsoluteError:0.##}");
	Console.WriteLine($"Mean Squared Error: {metrics.MeanSquaredError:0.##}");


	PlotScatterGraph<Input>(context, trainData, (Input input) => 
		new PointItem { X = (float) Math.Round(input.PovertyRate, 2), Y = (float) Math.Round(input.TeenageBirthRate, 2) },
		"Poverty rate (%) vs Teenage Pregnancies per 1,000 birth");

}

public class PointItem {
	public float X { get; set; }
	public float Y { get; set; }
}

void PlotScatterGraph<T>(MLContext mlContext, IDataView trainData, Func<T, PointItem> pointCreator, string chartTitle) where T : class, new()
{
	//Convert the IDataview to an enumerable collection
	var data = mlContext.Data.CreateEnumerable<T>(trainData, reuseRowObject: false).Select(x => pointCreator(x)).ToList();

	// Calculate trendline (simple linear regression)
	double avgX = data.Average(d => d.X);
	double avgY = data.Average(d => d.Y);
	double slope = data.Sum(d => (d.X - avgX) * (d.Y - avgY)) / data.Sum(d => (d.X - avgX) * (d.X - avgX));
	double intercept = avgY - slope * avgX;
	var trendline = data.Select(d => new { X = d.X, Y = slope * d.X + intercept }).ToList();

	//Plot the scatter graph
	var plot = data.Chart(d => d.X)
		.AddYSeries(d => d.Y, LINQPad.Util.SeriesType.Point, chartTitle)
		.AddYSeries(d => trendline.FirstOrDefault(t => t.X == d.X)?.Y ?? 0, Util.SeriesType.Line, "Trendline")
		.ToWindowsChart();
		
	plot.AntiAliasing = System.Windows.Forms.DataVisualization.Charting.AntiAliasingStyles.All;
	plot.Dump();
}



public class Input
{

	[LoadColumn(1)]
	public float PovertyRate;

	[LoadColumn(5), ColumnName("Label")]
	public float TeenageBirthRate { get; set; }

}
public class Output
{
	[ColumnName("Score")]
	public float TeenageBirthRate;

}


A pipeline is defined for the machine learning here consisting of the following :
  • The method NormalizeMinMax will transform the poverty rate into a normalized scale between 0 and 1. The Concatenate method will be used to specify the "Features", in this case only the column Poverty rate is the feature of which we want to predict a score, this is the rate of teenage pregnancy births per 1,000 births. Note that our CSV data set contains more columns, but this is a simple regression where only one variable is taken into account.
  • The trainers used to train the machine learning algorithm is Ols, the Ordinary Least Squares.
  • The method fit will train using the training data defined from the method TrainTestSplit.
  • The resulting model is used to create a prediction engine.
  • Using the prediction engine, it is possible to predict a value value using the Predict method given one input item. Our prediction engine expects input objects of type Input and Output.
  • Using the testdata, the method Transform using the model gives us multiple predictions and it is possible to evalute the regression analysis from the predictions to check how accurate the regression model is.
  • Returning from this evaluation, we get the R-squared for example. This is a value from 0 to 1.0 where it describes how accurate the regression is in in describing the total variation of the residues of the model, the amount the data when plotted in a scatter graph where residue is the offset between the actual data and what the regression model predicts.
  • Other values such as RMSE and MSE are the root and mean squared error, which are absolute values.
  • Using the code above we got a fairly accurate regression model, but more accuracy would be achieved by taking in additional factors.


  • Output from the Linqpad 8 application shown in this article :
    
        
    Input poverty rate: 8,4 . Predicted birth rate per 1000: 35,06
    Actual birth rate per 1000: 36,8
    R-squared: 0,59
    Root Mean Squared Error: 8,99
    Mean Absolute Error: 8,01
    Mean Squared Error: 80,83
        
      
    
    Please note that there are some standard column names used for machine learning.
    
    Label: Represents the target variable (the value to predict).
    
    Features: Contains the input features used for training.
    
    Score: Stores the predicted value in regression models.
    
    PredictedLabel: Holds the predicted class in classification models.
    
    Probability: Represents the probability of a predicted class.
    
    FeatureContributions: Shows how much each feature contributes to a prediction.
    
    
    In the code above, the column names "Label", "Features" and "Score" was used to instruct the regression being calculated in the code here for ML.Net context model. The attribute ColumnName was being used here together with the Concatenate method.

Tuesday, 15 April 2025

Adding plugins to use for Semantic Kernel

With Microsoft Semantic Kernel, it is possible to consume AI services such as Azure AI and OpenAI with less code, as this framework provides simplification and standardization for consuming these services. A repo on Github with the code shown in this article is provided here :

https://github.com/toreaurstadboss/SemanticKernelPluginDemov4

The demo code is a Blazor server app. It demonstrates how to use Microsoft Semantic Kernel with plugins. I have decided to provide the Northwind database as the extra data the plugin will use. Via debugging and seeing the output, I see that the plugin is successfully called and used. It is also easy to add plugins, which provides additional data to the AI model. This is suitable for providing AI powered solutions with private data that you want to provide to the AI model. For example, when using OpenAI Chat GPT-4, providing a plugin will make it possible to specify which data are to be presented and displayed. It is a convenient way to provide a natural language interface for doing data reporting such as listing up results from this plugin. The plugin can provide kernel functions, using attributes on the method. Let's first look at the Program.cs file for wiring up the semantic kernel for a Blazor Server demo app.

Program.cs



using Microsoft.EntityFrameworkCore;
using Microsoft.SemanticKernel;
using SemanticKernelPluginDemov4.Models;
using SemanticKernelPluginDemov4.Services;


namespace SemanticKernelPluginDemov4
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddRazorPages();
            builder.Services.AddServerSideBlazor();

            // Add DbContext
            builder.Services.AddDbContextFactory<NorthwindContext>(options =>
                options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

            builder.Services.AddScoped<IOpenAIChatcompletionService, OpenAIChatcompletionService>();

            builder.Services.AddScoped<NorthwindSemanticKernelPlugin>();

            builder.Services.AddScoped(sp =>
            {
                var kernelBuilder = Kernel.CreateBuilder();
                kernelBuilder.AddOpenAIChatCompletion(modelId: builder.Configuration.GetSection("OpenAI").GetValue<string>("ModelId")!,
                    apiKey: builder.Configuration.GetSection("OpenAI").GetValue<string>("ApiKey")!);

                var kernel = kernelBuilder.Build();

                var dbContextFactory = sp.GetRequiredService<IDbContextFactory<NorthwindContext>>();
                var northwindSemanticKernelPlugin = new NorthwindSemanticKernelPlugin(dbContextFactory);
                kernel.ImportPluginFromObject(northwindSemanticKernelPlugin);

                return kernel;
            });

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseRouting();

            app.MapBlazorHub();
            app.MapFallbackToPage("/_Host");

            app.Run();
        }
    }
}



In the code above, note the following:
  • The usage of IDbContextFactory for creating a db context, injected into the plugin. This is a Blazor server app, so this service is used to create db contet, since a Blazor server will have a durable connection between client and the server over Signal-R and there needs to use this interface to create dbcontext instances as needed
  • Using the method ImportPluginFromObject to import the plugin into the semantic kernel built here. Note that we register the kernel as a scoped service here. Also the plugin is registered as a scoped service here.
The plugin looks like this.

NorthwindSemanticKernelplugin.cs



using Microsoft.EntityFrameworkCore;
using Microsoft.SemanticKernel;
using SemanticKernelPluginDemov4.Models;
using System.ComponentModel;

namespace SemanticKernelPluginDemov4.Services
{

    public class NorthwindSemanticKernelPlugin
    {
        private readonly IDbContextFactory<NorthwindContext> _northwindContext;

        public NorthwindSemanticKernelPlugin(IDbContextFactory<NorthwindContext> northwindContext)
        {
            _northwindContext = northwindContext;
        }

        [KernelFunction]
        [Description("When asked about the suppliers of Nortwind database, use this method to get all the suppliers. Inform that the data comes from the Semantic Kernel plugin called : NortwindSemanticKernelPlugin")]
        public async Task<List<string>> GetSuppliers()
        {
            using (var dbContext = _northwindContext.CreateDbContext())
            {
                return await dbContext.Suppliers.OrderBy(s => s.CompanyName).Select(s => "Kernel method 'NorthwindSemanticKernelPlugin:GetSuppliers' gave this: " + s.CompanyName).ToListAsync();
            }
        }

        [KernelFunction]
        [Description("When asked about the total sales of a given month in a year, use this method. In case asked for multiple months, call this method again multiple times, adjusting the month and year as provided. The month and year is to be in the range 1-12 for months and for year 1996-1998. Suggest for the user what the valid ranges are in case other values are provided.")]
        public async Task<decimal> GetTotalSalesInMontAndYear(int month, int year)
        {
            using (var dbContext = _northwindContext.CreateDbContext())
            {
                var sumOfOrders = await (from Order o in dbContext.Orders
                             join OrderDetail od in dbContext.OrderDetails on o.OrderId equals od.OrderId
                             where o.OrderDate.HasValue && (o.OrderDate.Value.Month == month
                             && o.OrderDate.Value.Year == year) 
                             select (od.UnitPrice * od.Quantity) * (1 - (decimal)od.Discount)).SumAsync();

                return sumOfOrders;
            }
        }

    }
}


In the code above, note the attributes used. KernelFunction tells that this is a method the Semantic kernel can use. The description attribute instructs the AI LLM model how to use the method, how to provide parameter values if any and when the method is to be called. Let's look at the OpenAI service next.

OpenAIChatcompletionService.cs



using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

namespace SemanticKernelPluginDemov4.Services
{

    public class OpenAIChatcompletionService : IOpenAIChatcompletionService
    {
        private readonly Kernel _kernel;

        private IChatCompletionService _chatCompletionService;

        public OpenAIChatcompletionService(Kernel kernel)
        {
            _kernel = kernel;
            _chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
        }

        public async IAsyncEnumerable<string?> RunQuery(string question)
        {
            var chatHistory = new ChatHistory();

            chatHistory.AddSystemMessage("You are a helpful assistant, answering only on questions about Northwind database. In case you got other questions, inform that you only can provide questions about the Northwind database. It is important that only the provided Northwind database functions added to the language model through plugin is used when answering the questions. If no answer is available, inform this.");

            chatHistory.AddUserMessage(question);

            await foreach (var chatUpdate in _chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory, CreateOpenAIExecutionSettings(), _kernel))
            {
                yield return chatUpdate.Content;
            }
        }

        private OpenAIPromptExecutionSettings? CreateOpenAIExecutionSettings()
        {
            return new OpenAIPromptExecutionSettings
            {
                ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
            };
        }

    }
}



In the code above, the kernel is injected into this service. The kernel was registered in Program.cs as a scoped service, so it is injected here. The method GetRequiredService is similar to the method with same name of IServiceProvider used inside Program.cs. Note the use of ToolCallBehavior set to AutoInvokeKernelFunctions. Extending the AI powered functionality with plugins requires little extra code with Microsoft Semantic kernel. A screenshot of the demo is shown below.

Monday, 31 March 2025

Generating Dall-e-3 images using Microsoft Semantic Kernel

In this demo, Dall-e-3 images are generated from a console app using Microsoft Semantic Kernel. The semantic kernel is a library that offers different plugins for different AI services. It is supported for multiple languages, these are C#, Java and Python. Its goal is to ease the use of consuming AI services and building a shared infrastructure for these services and offer a way to conceptualize and abstract the consumption of these services. It can also be seen as a middleware for the services and offering a framework where consuming AI services becomes a more standardized process. A Github repo has been created with the code for this demo here:

Github repo for this demo
Dall-e-3 image generator with semantic kernel

The demo contains two steps, first building the semantic kernel itself and then the image generation. First off, the .csproj file has package references to the latest as of March 2025 nuget package of Microsoft Semantic Kernel.
DalleImageGeneratorWithSemanticKernel.csproj


<Project Sdk="Microsoft.NET.Sdk"> 

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <NoWarn>$(NoWarn);CS8618,IDE0009,CA1051,CA1050,CA1707,CA1054,CA2007,VSTHRD111,CS1591,RCS1110,RCS1243,CA5394,SKEXP0001,SKEXP0010,SKEXP0020,SKEXP0040,SKEXP0050,SKEXP0060,SKEXP0070,SKEXP0101,SKEXP0110</NoWarn>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.SemanticKernel" Version="1.44.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
  </ItemGroup>

</Project>


Note that multiple warnings are marked as no warning as semantic kernel is open for change in the future and thus flags multiple different warnings. The image generation demo is set up like this in the class ImageGeneration. Note how the Kernel object is built up here. It got a builder that offers many methods to add AI services. In this case we add an ITextToImageService. The modelName used here is "dall-e-3".
ImageGeneration.cs


using DalleImageGeneratorWithSemanticKernel;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.TextToImage;
using OpenAI.Images;
using System;
using System.Diagnostics;

namespace UseSemanticKernelFromNET;

public class ImageGeneration
{
    public async Task GenerateBasicImage(string modelName)
    {
        Kernel kernel = Kernel
            .CreateBuilder()
            .AddOpenAITextToImage(modelId:modelName, apiKey: Environment.GetEnvironmentVariable("OPENAI_API_KEY")!).Build();

        ITextToImageService imageService = kernel.GetRequiredService<ITextToImageService>();

        Console.WriteLine("##### SEMANTIC KERNEL - IMAGE GENERATOR DALL-E-3 CONSOLE APP #####\n\n");


        string prompt =
           """
            In the humorous image, Vice President JD Vance and his wife are seen stepping out of their plane onto the icy runway of
            Thule Air Base. Just as they set foot on the frozen ground, a bunch of playful polar bears greet them enthusiastically, much like 
            overzealous fans welcoming celebrities. The surprised expressions on their faces are priceless as the couple finds 
            themselves being "chased" by these bundles of fur and excitement. JD Vance, with a mix of amusement and alarm, has one 
            shoe comically left behind in the snow, while his wife, holding onto her hat against the chilly wind, can't suppress a laugh.
            The scene is completed with members of the Air Base
            staff in the background, chuckling and capturing the moment on their phones, adding to the light-heartedness of the unexpected encounter.  
            The plane should carry the AirForce One Colors and read "United States of America". 
         """;

        Console.WriteLine($"\n ### STORY FOR THE IMAGE TO GENERATE WITH DALL-E-3 ### \n{prompt}\n\n");

        Console.WriteLine("\n\nStarting generation of dall-e-3 image...");

        var cts = new CancellationTokenSource();
        var cancellationToken = cts.Token;

        var rotationTask = Task.Run(() => ConsoleUtil.RotateDash(cancellationToken), cts.Token);

        var image = await imageService.GetOpenAIImageContentAsync(prompt,
            kernel: kernel,
            size: (1024, 1024), //for Dall-e-2 images, use: 256x256, 512x512, or 1024x1024. For dalle-3 images, use: 1024x1024, 1792x1024, 1024x1792. 
            style: "vivid",
            quality: "hd", //high
            responseFormat: "b64_json", // bytes
            cancellationToken: cancellationToken);       
        
        cts.Cancel(); //cancel to stop animating the waiting indicator

        var imageTmpFilePng = Path.ChangeExtension(Path.GetTempFileName(), "png");
        image?.FirstOrDefault()?.WriteToFile(imageTmpFilePng);

        Console.WriteLine($"Wrote image to location: {imageTmpFilePng}");

        Process.Start(new ProcessStartInfo
        {
            FileName = "explorer.exe",
            Arguments = imageTmpFilePng,
            UseShellExecute = true
        });

    }

}


A helper extension method has been added for the Open AI Dall-e-3 image creation. Please note that one should stick to not too many extension methods of semantic kernel itself as this defeats the purpose of a standardized way of using the semantic kernel. But in this case, it is just a helper method to customize the generation of particularly dall-e-3 (and dall-e-2) images from Open AI using the Semantic kernel. The code is shown below
TextToImageServiceExtensions.cs


using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TextToImage;

namespace UseSemanticKernelFromNET;

public static class TextToImageServiceExtensions
{


    /// <summary>
    /// Generates OpenAI image content asynchronously based on the provided text input and settings.
    /// </summary>
    /// <param name="imageService">The image service used to generate the image content.</param>
    /// <param name="input">The text input used to generate the image.</param>
    /// <param name="kernel">An optional kernel instance for additional processing.</param>
    /// <param name="size">
    /// The desired size of the generated image. For DALL-E 2 images, use: 256x256, 512x512, or 1024x1024. 
    /// For DALL-E 3 images, use: 1024x1024, 1792x1024, or 1024x1792.
    /// </param>
    /// <param name="style">The style of the image. Must be "vivid" or "natural".</param>
    /// <param name="quality">The quality of the image. Must be "standard", "hd", or "high".</param>
    /// <param name="responseFormat">
    /// The format of the response. Must be one of the following: "url", "uri", "b64_json", or "bytes".
    /// </param>
    /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
    /// <returns>
    /// A task that represents the asynchronous operation. The task result contains a read-only list of 
    /// <see cref="ImageContent"/> objects representing the generated images.
    /// </returns>
    public static Task<IReadOnlyList<ImageContent>> GetOpenAIImageContentAsync(this ITextToImageService imageService,
        TextContent input,
        Kernel? kernel = null,
        (int width, int height) size = default((int, int)), // for Dall-e-2 images, use: 256x256, 512x512, or 1024x1024. For dalle-3 images, use: 1024x1024, 1792x1024, 1024x1792. 
        string style = "vivid",
        string quality = "hd",
        string responseFormat = "b64_json",        
        CancellationToken cancellationToken = default)
    {
        
        string? currentModelId = imageService.GetModelId();

        if (currentModelId != "dall-e-3" && currentModelId != "dall-e-2")
        {
            throw new NotSupportedException("This method is only supported for the DALL-E 2 and DALL-E 3 models.");
        }

        if (size.width == 0 || size.height == 0)
        {
            size = (1024, 1024); //defaulting here to (1024, 1024).
        }

        if (currentModelId == "dall-e-2"){
            var supportedSizes = new[]{
                (256, 256),
                (512, 512),
                (1024, 1024)
            };
            if (!supportedSizes.Contains(size))
            {
                throw new ArgumentException("For DALL-E 2, the size must be one of: 256x256, 512x512, or 1024x1024.");
            }
        }
        else if (currentModelId == "dall-e-3")
        {
            var supportedSizes = new[]{
                (1024, 1024),
                (1792, 1024),
                (1024, 1792)
            };
            if (!supportedSizes.Contains(size))
            {
                throw new ArgumentException("For DALL-E 3, the size must be one of: 256x256, 512x512, or 1024x1024.");
            }
        }

        return imageService.GetImageContentsAsync(
            input,
            new OpenAITextToImageExecutionSettings
                {
                    Size = size,
                    Style = style, //must be "vivid" or "natural"
                    Quality = quality, //must be "standard" or "hd" or "high"
                    ResponseFormat = responseFormat // url or uri or b64_json or bytes
                },
            kernel,
            cancellationToken);

    }
}


Screenshot of this demo, console app running:
The console app will generate the dall-e-3 image using OpenAI service for this and save the image as a PNG image and save it into file saved into a temporary location and then open this image using Windows default image viewer application. Example image generated :