How to deal with multicollinearity issue when analyzing survey results?

Question: I recently conducted my first study looking at parental education as a determinant of risk. I conducted a survey which measured risk in several contexts and also recorded information about the individuals such as gender, height and all the other usual suspects. Little work has been done on the effect of ethnicity on risk and also country of origin/residence on risk (an interesting secondary research question). The issue is that parents nationality are very highly correlated with their children (as one would expect) to the extent that Minitab has discarded various determinants for being highly correlated. Due to the nature of the sample (students in the UK) correlation is something that it unavoidable.
I don't wish to discard variables just because they are highly correlated, although I have seen a few suggestions to do so. Is there a way to overcome this issue? Otherwise how else would one look at the marginal effect of country of birth/parents nationality on their risk preferences? Am I overlooking something simple?
Comment: This is a very ad hoc method but one I've used before: If you have two collinear variables, regress one on the other and take the residuals from that model and using them as a predictor in the original model. This would be loosely interpreted as "the part of the second predictor not subsumed by the first predictor".
Answer: This is a great question, here's my intuition:
The sort of problem you're talking about has to do with identification: when we conduct causal analysis, we'd like to be able to make claims about the effects of several different variables.  However, in cases of high dependence amongst the variables (like this), our ability to make separate claims about the variables is heavily restricted.
I'll try to give an example using your data. (I may have the wrong idea, here, though – please correct me if I misstate your case):
Parents' ethnicity and children's ethnicity are very closely bound in your sample – another way of saying this is that you have very few (if any) observations where these are different.  This means that (statistically speaking) you'll be unable to differentiate the causal impact of parents vs. children's ethnicity.
The sort of data you'd need to make claims like this, would be observations where these variables are different from one another (perhaps by looking at multi-racial couples, or adopted children).  In the absence of this data, you'll be unable to differentiate these impacts.
In a sense, this is disheartening news (since we'd like to be able to estimate separate effects), but in another way, this result is entirely sensible. If we think of ethnicity being generally a stable trait at the family level, then it may make sense to think of ethnicity as sort of a bundle of effects – knowing a child's ethnicity may tell you something meaningful as well about their parents' ethnicity, so in a sense, it'd be unusual to report on a heavy distinction between these constructs.
In summary, you're not doing anything wrong (at least in my mind)… The sort of problem you've identified has to do with a property of the data that's tricky to get around, rather than a problem with the way the analysis is being conducted.
Comment: PS: The usual prescription in cases like this would be to drop one of the highly correlated variables (which your statistical program may automatically do).  The choice about which to drop won't really matter... and matters less the more correlated the variables are.
Comment: Thank you Mark for the rapid and in depth response. I cant explain the relief after countless hours without any further insight until now.
Comment: @George- No worries--  We've all got parts of this whole enterprise that give is the howling fantods.. happy to help!


Solve Improper Integrate using Residue theorem for this integral

Question: I am trying to solve this integral
$ \int_{-\infty}^\infty \frac{x\cdot sin(x)}{x^2+4} \cdot dx $
I have applied the residue theorem on a semicircle of radius $R> 2$, $\gamma$, so I have
$ \int_\gamma \frac{z\cdot sin(z)}{z^2+4} \cdot dz = \int_{-R}^R \frac{x\cdot sin(x)}{x^2+4} \cdot dx+  \int_{C_R} \frac{z\cdot sin(z)}{z^2+4} \cdot dz$
where $C_R = \{ z : z=R e^{i \theta} , \theta \in (0,\pi)\}$, but I cannot limit the second integral to eliminate this contribution when $R \to \infty$ and I don't know how I could do the integral in another way
Comment: @user, this is ***not*** a question that is for research mathematicians to answer, so MathOverflow is ***not*** the right site. I'm moving this to math.SE instead.
Comment: Welcome to Mathematica.SE. Are you sure you are posting on the right site? There is nothing in your question making it clear that it is concerned with Mathematica software.
Comment: Sorry, you are right. I have to post it on mathOverflow not mathematica. Thank you
Answer: Let's define a function $ f $ on $ \mathbb{R} $ as follows : $$ \left(\forall t\in\mathbb{R}\right),\ f\left(t\right)=\int_{-\infty}^{+\infty}{\frac{\mathrm{e}^{\mathrm{i}xt}}{x^{2}+4}\,\mathrm{d}x} $$
Fixing $ t\in\mathbb{R} $ and $ R> 2 $, then integrating $ f_{t}:z\mapsto\frac{\mathrm{e}^{\mathrm{i}zt}}{z^{2}+4} $ on $ \gamma_{R}=\left[-R,R\right]\cup\mathscr{C}_{R} $, where $ \mathscr{C}_{R}=\left\lbrace z:\left|z\right|=R,\ \mathcal{Im}\left(z\right)\geq 0\right\rbrace $, will give us a closed form for our $ f \cdot $
We have : $$ 2\pi\mathrm{i}\,\mathrm{Res}\left(f_{t},2\mathrm{i}\right)=\oint_{\gamma_{R}}{f_{t}\left(z\right)\mathrm{d}z}=\int_{-R}^{R}{f_{t}\left(x\right)\mathrm{d}x}+\int_{\mathscr{C}_{R}}{f_{t}\left(z\right)\mathrm{d}z} $$
Since : $$ \left|\int_{\mathscr{C}_{R}}{f_{t}\left(z\right)\mathrm{d}z}\right|\leq\int_{\mathscr{C}_{R}}{\left|f_{t}\left(z\right)\right|\left|\mathrm{d}z\right|}\leq\frac{1}{R^{2}-4}\int_{\mathscr{C}_{R}}{\left|\mathrm{d}z\right|}=\frac{1}{R^{2}-4}\int_{0}^{\pi}{R\,\mathrm{d}\theta}=\frac{\pi R}{R^{2}-4}\underset{n\to +\infty}{\longrightarrow }0 $$
And : $$ 2\pi\mathrm{i}\,\mathrm{Res}\left(f_{t},2\mathrm{i}\right)=2\pi\mathrm{i}\lim_{z\to 2\mathrm{i}}{\left(z-2\mathrm{i}\right)f_{t}\left(z\right)}=2\pi\mathrm{i}\lim_{z\to 2\mathrm{i}}{\frac{\mathrm{e}^{\mathrm{i}zt}}{z+2\mathrm{i}}}=\frac{\pi\,\mathrm{e}^{-2t}}{2} $$
Tending $ R $ to infinity, we get : $$ \int_{-\infty}^{+\infty}{f_{t}\left(x\right)\mathrm{d}x}=\frac{\pi\,\mathrm{e}^{-2t}}{2} $$
And hence, for any real $ t $, we have : $$ \int_{-\infty}^{+\infty}{\frac{x\,\mathrm{e}^{\mathrm{i}xt}}{x^{2}+4}\,\mathrm{d}x}=-\mathrm{i}\int_{-\infty}^{+\infty}{\frac{\partial f_{t}}{\partial t}\left(t,x\right)\mathrm{d}x}=-\mathrm{i}f'\left(t\right)=\pi\mathrm{i}\,\mathrm{e}^{-2t} $$
Taking the imaginary part, we get that $ \forall t\in\mathbb{R} $ : $$ \fbox{$\begin{array}{rcl}\displaystyle\int_{-\infty}^{+\infty}{\frac{x\sin{\left(xt\right)}}{x^{2}+4}\,\mathrm{d}x}=\pi\,\mathrm{e}^{-2t}\end{array}$} $$
Comment: The result is correct only for positive $t$ (the integral is odd in $t$).


Moving from LINQ2SQL to LINQ to Entities

Question: I'm using EF 4.1 in POCO mode.
LINQ2SQL generates partial methods on the datacontext that get called when inserting/updated/deleting an entity.  They're helpful for cases like:
<code>    partial void InsertCampaign( Campaign instance )
    {
        instance.CreatedAtUTC = instance.ModifiedAtUTC = DateTime.UtcNow;

        ExecuteDynamicInsert( instance );
    }

    partial void UpdateCampaign( Campaign instance )
    {
        instance.ModifiedAtUTC = DateTime.UtcNow;

        ExecuteDynamicUpdate( instance );
    }
</code>
So, my question is -- with EF, how do you hook into the inserting/updating/deleting of entities so that you can perform operations such as the above?  I would prefer not to have to do this manually every time I create/update an entity.
Thank you
Comment: What "flavor" of 4.1 are you using, code-, model- or db-first?  Are you asking how to generate or have EF generate your DbContext?
Comment: @Marc L -- I'm using db-first.  I've already generated POCO classes and have done several EF projects already.  I've just never learned how to automatically apply values when inserting/updating like you can do easily in LINQ2SQL.  Thanks...
Answer: Override SaveChanges() method in your DbContext and do something like:
<code>    public override int SaveChanges()
    {
        var modified = this.ChangeTracker.Entries().Where(e => e.State == System.Data.EntityState.Modified);
        // set whatever values you want on modified entities
        return base.SaveChanges();
    }
</code>


selective build with maven

Question: I have maven configured for my java project to build single jar with all the classes. Is it possible to select certain classes and build a separate jar out of them?
Comment: Alternatively you can use maven-assembly-plugin, but I'd keep the separate module approach.
Answer: On the usage page of the maven-jar-plugin, you can find a sample configuration for this under the subheading
Additional attached JAR.
Answer: Create a separate module for the 'lib' classes and set is as dependency of the project module.
Answer: Maven dictates one-to-one relationship between pom.xml and binary file it produces. In other words if you want to create several jar files you need several pom files: pom per jar.
So, just split your project. BTW sometimes it is a good practice. Really if some your classes can exist without others they do not depend on others and therefore can live in other project.
Projects can depend on others and can be hierarchical, i.e. have parent project. So, you can create parent project and several children under it.
Comment: thanks, splitting can be a good idea as well. Also it is possible to make maven module within the same project, but now I'm good with what Sean Patrick Floyd suggested.
Answer: This seems to be some kind of maven FAQ. You can find a detailed answer in the Sonatype (maven contributer) Blog:
How to Create Two JARs from One Project (…and why you shouldn't)

selective build with maven


MVC and EF Validation with added ValidationContext item

Question: I have a scenario where I'd like to add an item to the ValidationContext and check for it in the EF triggered entity validation.  I'm doing this in a wizard so I can only validate certain things on specific steps. (If there's a good pattern for that please do share it).
The problem is that the validation is triggered, twice actually, before the controller action is even hit.  I wish I understood why.  I'm not sure how to get the item in ValidationContext before that happens, so I can't tell the validation what step I'm on.
Furthermore, if I only do the custom validation when save changes is triggered by checking for the item as I have in my code below, then I get no automatic model validation errors displayed when the page refreshes.
In my custom context:
public WizardStep Step { get; set; }

protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
    items.Add("ValidationStep", Step);
    return base.ValidateEntity(entityEntry, items);
}

Service that sets the entity:
public void SaveChanges(WizardStep step)
{
    _context.Step = step;
    _context.SaveChanges();
}

In my entity
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    // Step will only be present when called from save changes.  Calls from model state validation won't have it
    if (validationContext.Items.ContainsKey("ValidationStep"))
    {
        var validationStep = (WizardStep)validationContext.Items["ValidationStep"];
        if (validationStep == WizardStep.Introduction)
        {
            if (criteria)
            {
                yield return new ValidationResult($"Error message  ", new[] { "field" });
            }
        }
    }
}

Controller:
public ActionResult MyAction(HomeViewModel vm)
{
    try
    {
        _incidentService.AddOrUpdate(vm.Enttiy);
        _incidentService.SaveChanges(WizardStep.Introduction);
    }
    catch (Exception ex)
    {
        return View(vm);
    }
    return RedirectToAction("Index");
}

Comment: Are you trying to do Business rules validation or data integrity checks like unique constraints and foreign key constraints? If you are trying to do the former I am pretty sure that some external lib like [FluentValidation](https://github.com/JeremySkinner/FluentValidation) is more suited to your needs.
Comment: As I understand the situation I think you should reverse your approach: Use a popular validation framework for complex business logic where some rules may internally query the database to complete.
Comment: @cleftheris It's a bit of both really.  I have reverse poco generator code that has data integrity attributes already, on the entities.  Then I have more complex business rules which I'm adding manually to the Validate method on entities, and for which I need to hit the database to check for error text (user configured).  That extra db check is why I need the context in the validationcontext.
Answer: The first validation is on the MVC created model that is passed to the controller.   MVC uses a ModelBinder class to construct, populate and validate the client http form data into the model.   Any failed validation will be returned to the client.  A valid model may then be changed by the controller, so a second validation is done by EF when saved.  I believe when saved, EF validation is only triggered if the property is new or has different data the original value.
Theoretically  it should be possible to have a custom MVC ModelValidator  and intercept the Validate method to set ValidationContext items.  However, I could NOT figure out how to do that.  I did however find a slightly different solution that works for me.  Perhaps it can be adapted to fit your needs.
In my case, I wanted the EF DbContext (In my code its named CmsEntities) available to the Validation methods so I can querying the database (and do rich complex business logic validation).  The controller has the DbContext, but the model validation is called by the ModelBinder before passing it to the controller's action.
My solution is to:
1) Add a DbContext property to my Entity (Using partial class, or in Base Entity that all entities inherit from)
2) Create a Custom ModelBinder that will get the DbContext from the controller and populate it to the model
3) Register the Custom ModelBinder in the Application_Start()
Now, inside any validation method, the model will have a populated DbContext.  
Custom ModelBinder
public class CmsModelBinder : DefaultModelBinder
{
    protected override bool OnModelUpdating(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // Copy CmsEntities from Controller to the Model (Before we update and validate the model)
        var modelPropertyInfo = bindingContext.Model.GetType().GetProperty("CmsEntities");
        if (modelPropertyInfo != null)
        {
            var controllerPropertyInfo = controllerContext.Controller.GetType().GetProperty("CmsEntities");
            if (controllerPropertyInfo != null)
            {
                CmsEntities cmsEntities = controllerPropertyInfo.GetValue(controllerContext.Controller) as CmsEntities;
                modelPropertyInfo.SetValue(bindingContext.Model, cmsEntities);
            }
        }
        return base.OnModelUpdating(controllerContext, bindingContext);
    }

Global.asax.cs
    protected void Application_Start()
    {
        ...
        ModelBinders.Binders.DefaultBinder = new CmsModelBinder();
    }

Comment: I'm not crazy about having the context be a property on the entities, but this is the only answer that has addressed the question so far, thank you!
Answer: Just share my solution for mvc validate:
public class TestController:Controller
{
    public ActionResult Action1(MyModel data)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Values.Where(c => c.Errors.Count > 0).SelectMany(c => c.Errors.Select(o => o.ErrorMessage));
                var errorMsg = String.Join("
", errors);
                return Json(new
                {
                    IsSuccess = false,
                    Message = errorMsg
                });
            }
            //deal business
            return Json(new { IsSuccess = true, Message = "success" });
        }
        catch (Exception ex)
        {
            return Json(new { IsSuccess = false, Message = "fail" });
        }
    }
}
public class MyModel : IValidatableObject
{
    [Required(ErrorMessage = "{0} is required")]
    public decimal TotalPrice { get; set; }
    [Required(ErrorMessage = "{0} is required")]
    public decimal TotalPriceWithoutCoupon { get; set; }
    public ContactStruct Contact { get; set; }
    public bool Condition{ get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var instance = validationContext.ObjectInstance as MyModel;
        if (instance == null)
        {
            yield break;
        }
        if (instance.Condition)
        {
            if (instance.Contact == null)
            {
                yield return new ValidationResult("contact is required", new string[] { "Contact" });
            }
            else
            {
                if (string.IsNullOrEmpty(instance.Contact.phone))
                {
                    yield return new ValidationResult("the phone of contact is required", new string[] { "Contact.phone" });
                }
            }
        }
    }
}

Answer: First of all you should consider if WizardStep does belong to context or the object which is modified during separate steps?
Other thing is why not to use ie. Strategy to handle validation logic on separate steps?
About validation, I see you are mixing 2 things.
One is validation on context, where you should handle validation logic for every entity type you have in context.
The other one is implementation of IValidatableObject.Validate which should be called automatically for entity on SaveChanges.
I would decide and choose one way to go and from info you gave us I think having only IValidatableObject.Validate makes more sense, but then you would have to either put step into entity that's being validated or somehow inject that step other way only for validation.
Answer: You can do this in this way:
try
{
  //write code

}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                var outputLines = new List<string>();
                foreach (var eve in ex.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines);
            }


Could npm link cause the cannot find module problem?

Question: I'm trying to learn the concept of how to use TypeScript modules from plain JavaScript projects, and it seems to me that I can only use a npm linked module, but not a module that npm link to others. Let me explain with an example:
$ cat index1.js
const { add, multiply, divide } = require('module-a')

const newfunc = (a, b) =>
       divide(multiply(add(a, b), 6), 2);

const result = newfunc(1, 2)
console.log(result);

$ node index1.js
9

The module-a is a TypeScript module that I npm linked to from my JavaScript project. And it works fine. Now:
$ diff -wU 1 index1.js index2.js
--- index1.js   2019-01-01 16:25:50.000000000 -0500
+++ index2.js   2019-01-01 16:37:33.000000000 -0500
@@ -1,2 +1,3 @@
 const { add, multiply, divide } = require('module-a')
+const { myfunc } = require('module-b')

@@ -7 +8,3 @@
 console.log(result);
+
+console.log(myfunc(1, 2));

$ node index2.js
internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'module-b'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
    at Function.Module._load (internal/modules/cjs/loader.js:529:25)
    at Module.require (internal/modules/cjs/loader.js:657:17)
    at require (internal/modules/cjs/helpers.js:22:18)
...

$ ls -l node_modules/
total 0
lrwxrwxrwx 1 tong tong 30 2019-01-01 16:17 module-a -> /usr/lib/node_modules/module-a
lrwxrwxrwx 1 tong tong 30 2019-01-01 16:28 module-b -> /usr/lib/node_modules/module-b

$ ls -l /usr/lib/node_modules/module-b
lrwxrwxrwx 1 root root 83 2019-01-01 16:32 /usr/lib/node_modules/module-b -> /paths/to/ts-modules-test/module-b

I.e., to me module-b looks nothing different than module-a. but why it is OK to require('module-a') but not to require('module-b')?
Is it really because my module-b npm linked to module-a?
The whole npm link setup from module-b to module-a, and all the code, can be found at this repo.
UPDATE. I don't have a project's package.json for either module-a or module-b, but why module-a works? Moreover, having created module-c/package.json, the problem remains the same:
$ find .
.
./node_modules
./node_modules/module-a
./node_modules/module-b
./index1.js
./index2.js

$ npm init --force --yes
Wrote to /paths/to/ts-modules-test/module-c/package.json:

{
  "name": "module-c",
  "version": "1.0.0",
  "description": "",
  "main": "index1.js",
  "dependencies": {
    "module-a": "^1.0.0",
    "module-b": "^1.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

$ node index2.js
internal/modules/cjs/loader.js:605
    throw err;
    ^

Error: Cannot find module 'module-b'

Comment: Module-b needs to be in project's package.json. please share content of file.
Comment: do both `module-a` and `module-b` have a file `index.js`?
Comment: no.. `module-b` is not defined correctly. please look at "main" in `package.json` for `module-b` vs. `module-a`.    module-a defines it correctly `build/index.js` while module-b does not.
Answer: When nodejs requires a folder, it will try to find a main file.
By default the main file is index.js. Since you are using TypeScript, you do not have index.js, but instead you have index.ts.
To define a main file, you will need to define it in package.json. I can see you have done so in module-a.
{
   "main": "build/index.js"
}

This means that at some point during installation you have compiled the ts to js and the output from the compiler was placed in folder build.
Looking in module-b, your compiler for this module is also pointing to build folder, but the package.json "main" property has value "index.js". I assume that if you point it to build/index.js like you did in module-a it will work.
Comment: Oh thanks a lot Mr. Mograbi, I'm new to this and `package.json` of `module-b` was forked from somebody else's place. I would never have been able to figure this one out on my own, thanks a lot for helping!!!
Comment: This fixes my issue in Angular 10 module


Centos 5.4 (x86_64) DNS resolution woes

Question: I've setup a Centos 5.4 (x86_64) box in a VM recently; the trouble is I just can't get internet to work on it.
Pinging the resolved google.com ips, nameserver ips works fine but DNS lookup queries don't.
Here's some of the digging around that I did:
dig google.com
; <<>> DiG 9.3.6-P1-RedHat-9.3.6-4.P1.el5 <<>> google.com
;; global options:  printcmd
;; connection timed out; no servers could be reached

nslookup google.com
;; connection timed out; no servers could be reached

I didn't observe any UDP/TCP packets during dig and nslookup either
cat /etc/resolv.conf
; generated by /sbin/dhclient-script
nameserver 126.96.36.199
nameserver 22.214.171.124

Also my /etc/nsswitch.conf is in order and contains the "hosts: files dns"
What could be wrong?
Comment: I'll definitely update you you guys on this as soon as I get a chance.
Comment: I had openSUSE 11.1 installed in a VM as well, DNS worked fine in it.
Comment: UPDATE:

I tried with FreeBSD8 as well and DNS query didn't work. This is beginning to look more and more like an issue with my UTStarcom WA3002G4 ADSL2+ Router and its NAT or the lack of it.

My setup and the ip addresses:
Router         192.168.1.1
Windows      192.168.1.2
Centos 5.4   192.168.1.3
FreeBSD8    192.168.1.4

The windows host works fine with no issues with DNS queries.
Comment: Would be glad to email you the packet dump file, if you're interested.
Answer:
Rule out iptables:
service iptables stop

Do the nslookup. If it works then inspect your ruleset and retry. Don't forget to restart iptables.

Try telnetting to port 53 of those ip addresses:
telnet 126.96.36.199 53

You should see:
$   telnet 126.96.36.199 53
Trying 18.104.22.168...
Connected to 184.108.40.206.
Escape character is '^]'.

Comment: nslookup didn't work, but telnet did.
Answer: Can you ping the DNS servers that were specified in resolv.conf?
Comment: yes I can ping them
Answer: I had the exact same issue and this question pointed me to the spot to fix it.
After banging my head against a wall for three days it turned out that my modem was the issue. DHCP was set locally which was causing havoc for my firewall.
After turning off DHCP on the modem and restarting it my name server was working perfectly.

Centos 5.4 (x86_64) DNS resolution woes


Proof of Extreme Value Theorem. Modus operandi. (S. Abbott pp 115 t4.4.3)

Question: By pp 115 Abbott Theorem 4.4.2, we know that $f([a, b])$ is bounded above and below.
$f([a, b])$ contains [a,b] ergo it is clearly nonempty).
By the agency of the Axiom of Completeness, it has a supremum. Name it $s$.
Now for each natural number n, the number $s - 1/n$ is not an upper bound.
Thus there is a number in $f([a, b])$, call it $f(x_n)$, such that $s - 1/n < f(x_n) ≤ s.$
Ergo the sequence $\color{seagreen}{f(x_n) \text{ converges to } s.}$

1. Is the last sentence by dint of Squeeze Theorem? If not, why does $f(x_n)$ converges to s?

$\{ x_n \} $ is a sequence in $[a, b]$, so by the Bolzano–Weierstrass Theorem,
$\{ x_n \} $  has a subsequence $\{x_{n_k} \}$ that converges to an
element in [a, b], call it $x_0$.
Then since f is continuous, the image of this subsequence, $f( x_{n(k)} )$ must converge to $f(x_0)$.
But since $f( x_{n(k)} )$ is a subsequence of $\color{seagreen}{\text{a sequence that converges to } s}$, $f( x_n ) $ must converge to s. Thus $s = f(x_0)$. So the supremum is a function value.
Now note that since f is continuous, so is –f.
Since –f has a maximum (by above) f has a minimum at the same point.

2. Is the nub to the proof $\max f = \sup f $? How can we presage this before proving?
3. I acquiesce to the proof's steps, but I don't understand the modus operandi?
How can we presage the need of subsequences? Why chagrin about subsequences?
Comment: Tucker-- Just to let you know I saw your comment and added something for question 2 in my answer below.
Comment: @coffeemath thanks. i try to understand it.
Comment: "By the agency of/by dint of/the nub to/presage/acquiesce to/chagrin about": For your information, I NEVER encountered ANY of these formulations in a mathematical context (and I believe I have read quite a few pages). This might make you think.
Comment: @Did yes i will think, but english is not my first language so i translate from french. sorry...
Comment: https://translate.google.fr/?hl=fr&tab=wT#fr/en/
Answer: 1. It is by the squeeze theorem on the "lower" sequence $a_n=s-1/n$ and the constant "upper" sequence $b_n=s.$ You then have $a_n < f(x_n) \le b_n$ so the squeeze theorem applies, since the length $b_n-a_n=1/n\to 0.$ This then gives $f(x_n) \to s.$
3. This is a construction of an initial sequence $x_n$ where each term is somewhere in $[a,b]$. But one wants a single element $x_0$ of $[a,b]$ for which $f(x_0)=s.$ That is the reason for using a subsequence of the already constructed sequence $x_n$. Any subsequence of $x_n$ will have its $f$ values converging to $s$.
Then since the points $x_n$ are in the bounded interval $[a,b]$ one can find a limit point $x_0$ of these $x_n$, which is a point for which any neighborhood contains $x_n$ for infinitely many subscripts $n$. Now, there is no way around constructing  a subsequence of the $x_n$ which converge to $x_0$, and then one shows that $f(x_0)=s$ as in the quoted argument.
In summary: The first sequence $x_n$ is to make the $f$ values approach $s$, while the need for a subsequence of that is so one can have also the $x_n$ values approach some $x_0$ in $[a,b]$. Note there may be a way to do this proof without sequences at all, but this sequence approach isn't very complicated. I'd also be curious to see a non-sequence proof.
To address question 2: The theorem is saying that in this situation:
$\max f(x)=\sup f(x).$
One might "presage" this before proving it, after looking at several cases. However one would not know it for sure until a proof was obtained or else looked at from some source. Often one is given an exercise to prove something, and doesn't really know in advance that it must be true in the first place. It's of course always good to try for a counterexample first in terms of understanding what's going on.
Some more explanation: (for the OP, who requested that some things be explained in the answer part, rather than in comments.)
By "This is a construction..." I was referring to the quoted part of the proof in the posted question, or basically to the sequence and subsequence constructed in the proof.
By saying "there is no way around" I only meant to imply that, once one had gotten that far, it would seem clear that one should next construct the subsequence. "no way around" is not meant to mean that there is no other way, only that the subsequence step is an obvious way that works, so no need for other ways to do it.
Comment: Usually the squeeze theorem is stated in terms of inclusive inequalities, which is why I initially had $a_n \le f(x_n) \le b_n.$ (Here it makes no difference since $a_n=s-1/n$ is strictly increasing.) I'll add something in answer about question 2.
Comment: sorry...i believed the inequality was a flub. please add your comment into the answer?  thanks. for **2**, is there any more intuition to presaging $\max f(x)$? it feels fey.
Comment: +upvote. i will upvote you. thanks. very sorry for long wait. for 3, what does 'this' mean in 'This is a construction of an initial sequence'? also, what does it mean: "Now, there is no way around constructing a subsequence of the $x_n$ which converge to $x_0$" why 'no way around' ?


Add values from a column when two other columns match

Question: I have an ecology data table with about 12,000 rows.  There are three columns: site, species, and value.  I need to add up the values for each set of matching site and species - for example, all "red maple" values at "site A".  I have the data sorted by site and species, so I can do it by hand, but it's slow going.  The number of site/species matches varies, so I can't just add up the values in sets of three or anything.
Similar types of questions have talked about pivot tables, but none have needed to match two columns and add a third column, and I haven't been able to figure out how to extrapolate to my situation.
I'm reasonably comfortable coding and would like to do something that looks like this pseudocode, but I'm not clear on the syntax in VBA:
For each row
if a(x) = a(x+1) and b(x) = b(x+1) then
sum = sum + c(x)
else
d(x) = sum
sum = 0
next

Any ideas?
Comment: pivot table is the way to go, just learn how to properly use it
Answer: In a PivotTable, put site in Row Labels and species in Column Labels (or vice versa) and Sum of  value in Σ Values:


Migrate Module and AddressField Locality/City Lookup in PrepareRow Fails

Question: I'm running a migration to move volunteers from a csv file to drupal.  I have written the following code.
<code><?php
/**
    * @file
    * Migration for Volunteer File
*/

ini_set('auto_detect_line_endings', TRUE);

class VolunteerMigration extends Migration {

public function __construct($arguments) {
    parent::__construct($arguments);

    $this->description = t('Imports Volunteers from CSV file ');

    // Create a source object
    $columns = array(
        0 => array('pkid', 'PKID'),
        1 => array('fname', 'First Name'),
        2 => array('lname', 'Last Name'),
        3 => array('email','Email'),
        5 => array('dayphone','DayPhone'),
        6 => array('eveningphone','EveningPhone'),
        7 => array('address','Address'),
        8 => array('city', 'City'),
        9 => array('state','State'),
        10 => array('postalcode','zip'),
        11 => array('occupation','Occupation'),
        12 => array('interests','Interests'),
        13 => array('created','Timestamp'),
    );
    $address_arguments = array(
        'first_name' => array('source_field' => 'fname'),
        'last_name' => array('source_field' => 'lname'),
        'thoroughfare' => array('source_field' => 'address'),
        'locality' => array('source_field' => 'city'),
        'administrative_area' => array('source_field'=>'state'),
        'postal_code' => array('source_field' => 'postalcode'),
    );
    $this->source = new MigrateSourceCSV('/mnt/www/cylindrical/theoregonproject.com/data/drvollist.csv', $columns, array('header_rows' => 1));

    // Create a destination object
    $this->destination = new MigrateDestinationNode('volunteers');

    // Create a map
    $this->map = new MigrateSQLMap(
        $this->machineName,
        array(
            'pkid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
            ),
        ),
        MigrateDestinationNode::getKeySchema(),
        'campaign_migration'
    );

    // Create Mappings
    $this->addFieldMapping('title', NULL)->DefaultValue('Volunteer');
    $this->addFieldMapping('field_volunteer_address')
        ->arguments($address_arguments);
    $this->addFieldMapping('field_volunteer_id', 'pkid');
    $this->addFieldMapping('field_email', 'email');
    $this->addFieldMapping('field_dayphone', 'dayphone');
    $this->addFieldMapping('field_eveningphone', 'eveningphone');
    $this->addFieldMapping('field_occupation', 'occupation');
    $this->addFieldMapping('field_interests', 'interests')
            ->separator(',');
    $this->addFieldMapping('created','created');

}

public function prepareRow($row) {

    if ( $row->zip ) {
        $row->city = zipcity($row->zip);
    }
}

public function zipcity($zip) {
    $ch = curl_init('http://ziptasticapi.com/' . $zip);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $result = curl_exec($ch);
    curl_close($ch);

    $json = json_decode($result,true);

    return $json['city'];
}
}
?>
</code>
I'm am using the ziptastic api to fill in the cities using the zipcode.  Running the migration returns all except the address portion of the migration.  The zipcity function works as expected (returns city given a zipcode) but none of the address arguments are migrated.
I have adapted this code from a previous and successful migration minus the preparerow / zipcity additions.
Mark
Answer: Found the issue.  When using the addressfield module, you should have field mappings that allow for preparerow to work.  I thought that the address_arguments array was enough to create the field maps.  Also I was using the csv column title 'Zip' instead of the the mapped field title 'postalcode'
Under my address field mapping I added the following code.
<code>    $this->addFieldMapping(NULL, 'fname');
    $this->addFieldMapping(NULL, 'lname');
    $this->addFieldMapping(NULL, 'address');
    $this->addFieldMapping(NULL, 'city');
    $this->addFieldMapping(NULL, 'state');
    $this->addFieldMapping(NULL, 'postalcode');
</code>
I also moved the function zipcity to my helper function file to allow other migrations to use it if needed.
This post https://drupal.org/node/1326044 was key into figuring out the missing link.


looping / chaining multiple Function.applyThen() or Function.apply() using for or for-each loop

Question: i have a generic method that takes a var-args type Function and an input and i want to process the first Function and input then the output will be chained by Function.apply(Function.apply(input)) or chained by andThen() like Function.andThen(Function) and the output function will take the input and process it.
private static <Input,Output> Output flow(Input input, Function<Input,Output>... functions) {
    Function<Input,Output> outputFunction;
    for (int i =0; i<functions.length; i++) {
        outputFunction = functions[i].andThen((Function<? super Output, ? extends Output>) functions[i+1]);
    }
}

Q1: is it possible using function.apply(input);
Q2: is it possible using function.andThen(function);
Q3: any other solution available for this same problem?
Q4: what would be the best practice if i want to solve this type of chaining issue?
Comment: I think that you'll run into generic type mismatches very quickly. That's because the result of the first function is the input to the second, so these need to match. For two functions you need not one but three generic types: ` O flow(I input, Function f1, Function f2)`. The more functions, the more generic types are needed. My advice: just chain the functions where they come from, because at that point their generic types are still known.
Comment: Aside from not compiling, this function doesn't make type sense: to chain functions, you have to be able to pass the output of one as the input to the next. This means that `Input` and `Output` have to be the same types.
Comment: "just chain the functions where they come from" yes, this. But then, if you're composing the functions outside this method, you may as well also _apply_ the composed function outside this method, making it redundant.
Comment: This method also doesn't make sense because you would end up with `outputFunction` being the composition of the penultimate and last function (ignoring the off-by-one indexing error), rather than a chain of all the functions.
Comment: It is not possible to chain a bunch of `Function` together. Think about it: the first function would produce `Output`, and the second function would need to take in an `Output` (not `Input`!) for this to work! In general, it is possible to chain `Function`, `Function`, `Function` and so on together to produce `Function`, but it is not possible to chain a list of functions of the same type `Function`, unless `T1 ` and `T2` are the same.
Comment: @AndyTurner its an example what i want to achieve.
Comment: @MohibulHassan I can only go off what you show us.
Comment: @RobSpoor thank you. That's a nice advice
Comment: @Sweeper thank you for the explanation. Now i understand clearly
Comment: It's not possible with functions, but the overall idea isn't absolutely pointless. You can combine in such a way unary operators, predicates, even consumers.
Answer: As already pointed out in the comments, there is not an easy way to produce chained functions without specifically defining the input-output type for each of them and keeping the solution flexible.
A solution asks for a compromise by keeping the Function raw that would not enforce the types at the compile time and does not prevent runtime errors.
private static Object flow(Object input, Function... functions) {
    Object temp = input;
    for (Function function : functions) {
        temp = function.apply(temp);
    }
    return temp;
}

Usage:
final int input = 1;
final String result = (String) flow(input,
    (Function<Integer, Integer>) integer -> integer * 10,
    integer -> ((double) integer / 4),
    decimal -> "c" + decimal,
    string -> List.of("a", "b", string),
    (Function<List<Double>, Set<Double>>) HashSet::new,
    (Function<Set<String>, String>) set -> String.join("-", set)
);

Result:

a-b-c2.5


Broken if statement, can't figure out why this won't work

Question: The code keeps getting stuck even if the directory does exist in the list. I'm not sure what's going on here. When I comment out the code I wrote to re-ask the user for a directory if the directory doesn't exist, the code works perfectly.
def path_sel():
path = raw_input("Select desired working directory: ")
b = []
ftp.retrlines('LIST', b.append)
if path not in b:
    print "ERROR- Directory does not exist.\n"
    path_sel()
else:
    print '\nChanging to '+ path, '\n'
    ftp.cwd(path)
    print path
    ftp.retrlines('LIST')
    c = []
    ftp.retrlines('LIST', c.append)
    if 'd' in str(c[0]):
            path_sel()
file_dl()

Comment: you have indentation problem, I guess it is only here but you probably want to fix it.
Comment: Don't start again by calling `path_sel` from within `path_sel`. Use a loop instead.
Answer: This seems to work for me (I picked a random FTP server):
from ftplib import FTP

def path_sel():
   path = raw_input("Select desired working directory: ")
   b = []
   ftp.retrlines('LIST', b.append)

   found = False
   for entry in b:
      if path in entry:
         found = True

   if not found:
      print "ERROR- Directory does not exist.\n"
      path_sel()
   else:
      print '\nChanging to '+ path, '\n'
      ftp.cwd(path)
      print path
      ftp.retrlines('LIST')
      # After printing the directory, you probably
      # want to ask the user to enter a new file/path
      # rather than just assuming that they are interested
      # in the first entry in the ftp.retrlines() printout
      c = []
      ftp.retrlines('LIST', c.append)
      if 'd' in str(c[0]):
         path_sel()
   # ---------------------------------------------------
   # Once you've extracted a filename, run your function
   # ---------------------------------------------------
   print 'Running file_dl()'
   #file_dl(filename)

ftp = FTP('ftp.debian.org')
ftp.login()
path_sel()

It continually returns "ERROR- Directory does not exist" and asks for new input until a valid path is entered.  Once you get down to a file (something that does not start with 'drwxrwxrwx ....'), you want to execute your file_dl() function.
Here's what this looks like in my console:
Comment: Yeah exactly, that's my problem. I don't understand why it gets stuck in that part of the statement.
Comment: Sorry, I thought you were having issues with the recursive bit.  My guess is you need to check individual elements in the list returned by ftp.retrlines().
Comment: Yeah I've printed the list to see what was in there, it's just very strange that it will work perfectly for find the "d" in the list to determine there is a directory, and it needs to ask for a directory again or if the "d" isn't there and it runs file_dl. Yet, as soon as I try to add the part to determine if the directory's name is real it gets stuck


saving number field with a value of zero in Laravel 5.5

Question: I have a form that accepts delivery of products which I noticed if I enter 0 in the quantity field it doesn't save in the database even if I add data in the calendar or in Notes field.

I already commented out the \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,iin kernel.php still doesn't work.
how can I forced laravel to save my data even if I want to put 0 in quantity? thanks in advance!
update
public function store(Request $request)
{
    $input = $request->all();

    $items = [];

    for ($i = 0; $i <= count($input['order_id']); $i++) {

        if (empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;

        $acceptItem = [
            'order_id'      => $input['order_id'][$i],
            'product_id'    => $input['product_id'][$i],
            'order_item_id' => $input['order_item_id'][$i],
            'delivery_date' => $input['delivery_date'][$i],
            'company_id'    => $input['company_id'][$i],
            // 'stock_in_qty' => intval($input['stock_in_qty'])[$i],
            'stock_in_qty'  => $input['stock_in_qty'][$i],
            // 'stock_out_qty' => $input['stock_out_qty'][$i],
            // 'transfer_to' => $input['transfer_to'][$i],
            'delivery_note' => $input['delivery_note'][$i],
            'user_id'       => $input['user_id'][$i],
        ];
        array_push($items, Warehouse1stocks::create($acceptItem));

        $stockSummary = Warehouse1StockSummaries::firstOrCreate(
            ['product_id' => $input['product_id'][$i]],
            ['qty_in'  => $input['stock_in_qty'][$i],
             'qty_out' => null,
            ]);

        if (!$stockSummary->wasRecentlyCreated) {
            $stockSummary->increment('qty_in', $input['stock_in_qty'][$i]);
        }
    }

    if ($input['rd'] == $input['stock_in_qty'] || $input['rd'] == 0) {
        $order_idAcceptedItem = $acceptItem['order_id'];
        $setStatus = \App\Orders::where('id', '=', $order_idAcceptedItem)->first();
        if ($setStatus) {
            $setStatus->status_id = 4;
        }
        $setStatus->save();
    } else {
        $order_idAcceptedItem = $acceptItem['order_id'];
        $setStatus = \App\Orders::where('id', '=', $order_idAcceptedItem)->first();
        if ($setStatus) {
            $setStatus->status_id = 3;
        }
        $setStatus->save();
    }

    return redirect()->route('orders.index');
}

Comment: 0 is probably being converted to `false`, you should be explicit about types in cases like this, i.e: `(int) $request->quantity` (after validating that it's a number).
Comment: what is the column type of quantity field?
Comment: Hi @PrafullaKumarSahu its  $table->integer('stock_in_qty')->unsigned();
Comment: How's your controller's update method? You can add a null check there like `$model->quantity = $request->quantity ?? 0;`
Comment: hi @TahaPaksu actually I have no update method I only have the store method.
Comment: @Lito try what sam is telling as unsigned should allow 0.
Comment: If you don't know how to do that then please share your code for the `store` method and we can guide you.
Comment: yes @PrafullaKumarSahu I just thinking how should I do that?
Comment: share your code of store method.
Comment: ok @sam I'll just update my question
Comment: I just update my question
Comment: @Lito Can you var_dump `$input['stock_in_qty']` ?
Comment: or just make it (int)$input['stock_in_qty'] and try.
Comment: dd($input['stock_in_qty']);   -- >    array:3 [▼
  0 => "0"
  1 => "213"
  2 => "21"
]
Comment: see your `0` is string, not an int
Comment: I also think there is some syntax mistake on `['qty_in'     => $input['stock_in_qty'][$i],` where the first square bracket ends? any way make it `(int)$input['stock_in_qty']` try to save and let me know what happens.
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190167/discussion-between-prafulla-kumar-sahu-and-lito).
Answer: empty() will return true with 0 or '0' which will mean that if you try to change the quantity to 0 the for loop will just continue on to the next loop. If you need to check if the value exists you can instead use isset().
Changing your first if statement to the following should be all you need:
 if(!isset($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;

Comment: exactly @RossWilson that is what happening.
Comment: I just applied your suggestion @RossWilson. I am receiving Undefined offset: 3
Comment: the zero is now saving in my database but I am having  ErrorException (E_NOTICE)
Undefined offset: 3? and it is pointing to  if(!is_numeric($input['stock_in_qty'][$i])) continue;
Comment: @Lito I've updated my answer. If you need to check if the value exists then use `isset` instead of `empty`. I would also suggest looking at using https://laravel.com/docs/5.7/validation#available-validation-rules instead of inline validation.
Comment: thanks you @RossWilson! I'll take note of that. I believe you already save me twice! :)


Printer Management

Question: On our network we are  getting an error every time we attempt to add a printer or sometimes print to printers. The error states that new drivers are needed to installed to print however we are using Xerox global driver. On my computer for example, I can print to the printer I have always been connected to, however I can not connect to any other printer. So far we found that when we remove windows update KB5005030 on the computer it will work again however now there are computers that do not have the update that are prompting for the driver update. If anyone has seen this issue before or can offer any advice it would be greatly appreciated!
Comment: Just as a point of clarification, were the Xerox global drivers signed by Microsoft?  I know that the more recent versions of Windows (e.g. >= Win8, maybe as far back as Vista) look for drivers that have been digitally signed, and it has been known to cause problems if the driver in use is not signed.
Answer: With newer update you need to set that registry key to 0 to allow user to install print driver.
The registry is: RestrictDriverInstallationToAdministrators
The location is: HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows NT\Printers\PointAndPrint
You need to set it to 0 (DWORD)
With SCCM or other method you can push print driver, or keep it mind that registry key don't need a reboot, so as it's a security risk you can set it, push the printer and unset it after. (Or just leave it at zero if you are confident for the security risk it bring)
Comment: Thank you for the help, when I go to that path on regedit I hit printers and don't see PointAndPrint. In the printer section could I make a registry named RestrictDriverInstallationToAdministrators and set that to 0? Or should I look somewhere else?
Comment: @starman Hi, you need the key in the correct location, in the folder PointAndPrint. It's normal you don't see all key there, in the policy si folder it's policy push to your computer. That just mean you don't have PointAndPrint settings push
Comment: Is this a setting that I can change to enable users to update these drivers? Also, when I log in with an administrator account it allows the update driver bar to work and complete however it does not actually have an effect on the computer printing
Comment: @starman Yes, it allow them to update driver from your print server. Not from a package from the internet in example.
Comment: Thank you, how can we enable this without the PointAndPrint setting? Should I check on my print servers regedit?
Comment: I was able to add the registry key on our domain controllers group policies that mirrored what was listed and this was then applied to all of our computers. Printing appears to be working now and I will answer this question after a few days of monitoring.

Printer Management


Ajax post to RESTful web service

Question: I am having an issue with an Ajax post to a RESTful web service in Java. The project utilizes a single servlet mvc model, with the Ajax post data being sent as JSON to the web service. The specific issue that is occuring is that I a unable to pull the data out of a HttpServletRequest object on the web service side. The POST goes directly to the web service, and I attempted to pull the data out with the following:
@Path(Ajax)
public AjaxResource(){
@Context
HttpServletRequest request;

@POST
@Produces("application/json")
@Consumes("application/json")
public Response postMethod(){

BufferedReader reader = request.getReader();
// additional code
}
}

I receive an IllegalStateException on the getReader() call on the request; from what I understand the input stream/reader can only be called once. I am unsure if this is due to the doPost method in the servlet doing a request.getParameter call as it seems to ago I'd hitting the servlet before this web service. Is there any other way to retrieve this data other than implementing HttpServletRequestWrapper in the servlet?
Comment: What is the stack trace? What is "request"? It is nowhere declared. What framework do you use (those annotations are not a part of JRE)? People here are willing to help but they can't read in mind.
Comment: Looks like Java6 to me.  Also stated request is HttpServletRequest. Don't know the answer though ;-)
Comment: We use a proprietary framework within our company.
Comment: The annotations are jersey (for rest)
Answer: You should use @Context HttpServletRequest request as an argument of the resource method.
So it should be something like this:
public Response postMethod(@Context HttpServletRequest request){

    // rest of the code

}

Ajax post to RESTful web service


Evaluation of model without training of model in the execution of cnn in pytorch

Question: I am new to pytorch as I have just started today, I saw few examples in kaggle and try to adapt my cnn from tensorflow in torch to enable better gpu allocation.
I want to balance the data in train and test dataset so I have used train_test_split. I have two problems:

I could not evaluate the model before training the model. I want to just see the val_loss and val_accuracy without training the model. But that is not possible

I was just testing for 2 epoch but the validation accuracy shows abnormal value, why is it so?

Could someone help please?
I have defined the customdataset in other python file and importing it in the main module

import os
import cv2
import random
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import torchvision
from torchsummary import summary

class CustomDataset(Dataset):

        def __init__(self, root_folder_path):

            self.root_folder_path = root_folder_path
            self.image_files = []
            self.labels = []

            # Collect image paths and corresponding labels

            folders = sorted([f for f in os.listdir(root_folder_path) if os.path.isdir(os.path.join(root_folder_path, f))])
            self.label_dict = {folder: i for i, folder in enumerate(folders)}

            for folder in folders:

                folder_path = os.path.join(root_folder_path, folder)
                image_files = sorted([f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f)) and f.endswith('.jpg')])
                self.image_files.extend([os.path.join(folder_path, img) for img in image_files])
                self.labels.extend([self.label_dict[folder]] * len(image_files))

            self.transform = transforms.Compose([
                transforms.ToPILImage(),
                transforms.Resize((900, 300)),
                transforms.Grayscale(),
                transforms.ToTensor(),
                transforms.Normalize(mean=[0.5], std=[0.5])
            ])

        def __len__(self):

            return len(self.image_files)

        def __getitem__(self, idx):

            image_path = self.image_files[idx]
            label = self.labels[idx]
            image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
            image = self.transform(image)
            #print("Image shape:", image.shape)  # Print the shape of the image
            one_hot_label = torch.zeros(len(self.label_dict))
            one_hot_label[label] = 1

            return image, one_hot_label

this is my main script

if __name__ == '__main__':

    # Instantiate your custom dataset and dataloaders
    root_folder_path = r'W:\MASTER_BAGCHI_SCHALDACH\THESIS\code and dataset\image_dataset_300_900_10_classes'
    dataset = CustomDataset(root_folder_path)

    print("Labels:", sorted(dataset.label_dict.keys()))
    print("Total number of labels:", len(dataset.label_dict))

    # Display some images from each folder
    n_images_to_display = 4
    n_folders = len(dataset.label_dict)
    fig, ax = plt.subplots(n_images_to_display, n_folders, figsize=(n_folders * 4, n_images_to_display * 4))

    for i, (folder, label) in enumerate(dataset.label_dict.items()):
        folder_images = [dataset[i][0] for i, lbl in enumerate(dataset.labels) if lbl == label]
        indices_to_display = random.sample(range(len(folder_images)), min(n_images_to_display, len(folder_images)))
        for j, ind in enumerate(indices_to_display):
            ax[j, i].imshow(folder_images[ind].squeeze(), cmap='gray')  # Squeeze to remove the channel dimension for grayscale images
            ax[j, i].axis('off')
        ax[0, i].set_title(folder, fontsize=30)

    plt.show()
    fig.tight_layout(pad=0, w_pad=0, h_pad=0)

    from torch.utils.data import DataLoader, Subset
    from sklearn.model_selection import train_test_split

    TEST_SIZE = 0.2
    BATCH_SIZE = 64
    SEED = 42

    # Get the labels from the dataset
    labels = np.array([label for _, label in dataset])

    # generate indices: instead of the actual data we pass in integers instead
    train_indices, test_indices, _, _ = train_test_split(
        range(len(dataset)),
        labels,
        stratify=labels,
        test_size=TEST_SIZE,
        random_state=SEED
    )

    # generate subset based on indices
    train_split = Subset(dataset, train_indices)
    test_split = Subset(dataset, test_indices)
    print('Length of train_batch:',len(train_split))
    print('Length of test_batch:',len(test_split))

    # create batches
    train_loader = DataLoader(train_split, batch_size=BATCH_SIZE, num_workers=6,shuffle=True,pin_memory=True)
    test_loader = DataLoader(test_split, batch_size=BATCH_SIZE,num_workers=6,pin_memory=True)

    class ImageClassificationBase(nn.Module):

        def training_step(self, batch):
            images, labels = batch
            out = self(images)                  # Generate predictions
            loss = F.cross_entropy(out, labels) # Calculate loss
            return loss

        def accuracy(self,outputs, labels):
            #_, preds = torch.max(outputs, dim=1)
            preds = torch.argmax(outputs, dim=1)
            preds_one_hot = F.one_hot(preds, num_classes=labels.shape[1])  # Convert predictions to one-hot encoding
            print("Shape of preds:", preds_one_hot.shape)  # Check the shape of preds
            return torch.sum(preds_one_hot == labels).float().mean()

        def validation_step(self, batch):
            images, labels = batch
            out = self(images)                    # Generate predictions
            loss = F.cross_entropy(out, labels)   # Calculate loss
            acc = self.accuracy(out, labels)           # Calculate accuracy
            return {'val_loss': loss.detach(), 'val_acc': acc}

        def validation_epoch_end(self, outputs):
            batch_losses = [x['val_loss'] for x in outputs]
            epoch_loss = torch.stack(batch_losses).mean()   # Combine losses
            batch_accs = [x['val_acc'] for x in outputs]
            epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies
            return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}

        def epoch_end(self, epoch, result):
            print("Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}".format(
                epoch, result['train_loss'], result['val_loss'], result['val_acc']))

    import torch.nn.init as init
    class ImageClassification(ImageClassificationBase):
        def __init__(self):
            super().__init__()
            self.network = nn.Sequential(
                #image size is [1,900,300] as [channel, height,width]
                nn.Conv2d(1, 32, kernel_size = 3, padding = 1),
                nn.LeakyReLU(0.01),
                nn.BatchNorm2d(32),
                nn.AvgPool2d(kernel_size=2, stride=2),

                nn.Conv2d(32,32, kernel_size = 3,  padding = 1),
                nn.LeakyReLU(0.01),
                nn.BatchNorm2d(32),
                nn.AvgPool2d(kernel_size=2, stride=2),

                nn.Conv2d(32, 64, kernel_size = 3, padding = 1),
                nn.LeakyReLU(0.01),
                nn.BatchNorm2d(64),
                nn.AvgPool2d(kernel_size=2, stride=2),

                nn.Conv2d(64 ,64, kernel_size = 3, padding = 1),
                nn.LeakyReLU(0.01),
                nn.BatchNorm2d(64),
                nn.AvgPool2d(kernel_size=2, stride=2),

                nn.Flatten(),
                nn.Dropout(0.3),

                nn.Linear(64 * 56 * 18, 64),  # Assuming input size after convolutional layers is 64 * 56 * 18
                nn.LeakyReLU(0.01),
                nn.BatchNorm1d(64),
                nn.Dropout(0.2),

                nn.Linear(64, 64),
                nn.LeakyReLU(0.01),
                nn.BatchNorm1d(64),
                nn.Dropout(0.2),

                nn.Linear(64, 10)  # Output layer
            )
            # Initialize the weights of convolutional layers
            self._initialize_weights()

        def _initialize_weights(self):
            for m in self.modules():
                if isinstance(m, nn.Conv2d):
                    init.kaiming_uniform_(m.weight, mode='fan_in', nonlinearity='leaky_relu')

        def forward(self, xb):
            return self.network(xb)

    def get_default_device():
        """ Set Device to GPU or CPU"""
        if torch.cuda.is_available():
            return torch.device('cuda')
        else:
            return torch.device('cpu')

    def to_device(data, device):
        "Move data to the device"
        if isinstance(data,(list,tuple)):
            return [to_device(x,device) for x in data]
        return data.to(device,non_blocking = True)

    class DeviceDataLoader():
        """ Wrap a dataloader to move data to a device """

        def __init__(self, dl, device):
            self.dl = dl
            self.device = device

        def __iter__(self):
            """ Yield a batch of data after moving it to device"""
            for b in self.dl:
                yield to_device(b,self.device)

        def __len__(self):
            """ Number of batches """
            return len(self.dl)

    device = get_default_device()
    device

    torch.cuda.empty_cache()
    model = ImageClassification()

    random_seed = 99
    torch.manual_seed(random_seed)

    train_loader = DeviceDataLoader(train_loader, device)
    test_loader = DeviceDataLoader(test_loader, device)

    to_device(model, device)

    @torch.no_grad()
    def evaluate(model, val_loader):
        model.eval()
        outputs = [model.validation_step(batch) for batch in val_loader]
        return model.validation_epoch_end(outputs)

    def accuracy(outputs, labels):
        _, preds = torch.max(outputs, dim=1)
        return torch.tensor(torch.sum(preds == labels).item() / len(preds))

    def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.RMSprop):
        history = []
        optimizer = opt_func(model.parameters(), lr)
        for epoch in range(epochs):
            # Training Phase
            model.train()
            train_losses = []
            for batch in train_loader:
                loss = model.training_step(batch)
                train_losses.append(loss)
                loss.backward()
                optimizer.step()
                optimizer.zero_grad()
            # Validation phase
            result = evaluate(model, val_loader)
            result['train_loss'] = torch.stack(train_losses).mean().item()
            model.epoch_end(epoch, result)
            history.append(result)
        return history

    model=to_device(ImageClassification(),device)

    #initial evaluation of the model
    evaluate(model,test_loader)

 #set the no. of epochs, optimizer funtion and learning rate
    num_epochs = 2
    opt_func = torch.optim.RMSprop
    lr = 0.0001

    #fitting the model on training data and record the result after each epoch
    history = fit(num_epochs, lr, model, train_loader, test_loader, opt_func)

data looks like this and my size of the image in the model is [1,900,300] where 1 stands for graychannel, 900 - height of the image in pixels, 300 - width of the image in pixels
Sample data
and the output is:
  Labels: ['120', '144', '168', '192', '216', '24', '240', '48', '72', '96']
Total number of labels: 10
Length of train_batch: 1835
Length of test_batch: 459
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Train batch size: torch.Size([11, 1, 900, 300])
Shape of labels array: torch.Size([11, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([32, 1, 900, 300])
Shape of labels array: torch.Size([32, 10])
Test batch size: torch.Size([11, 1, 900, 300])
Shape of labels array: torch.Size([11, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([11, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([11, 10])
Epoch [0], train_loss: 1.5568, val_loss: 1.6037, val_acc: 267.2000
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([32, 10])
Shape of preds: torch.Size([11, 10])
Epoch [1], train_loss: 0.9959, val_loss: 1.6849, val_acc: 273.4667

Here is the drive link for the dataset: https://drive.google.com/drive/folders/1PsT9_HWX4snfgnhlwC6xM4rNjcoqXdk5?usp=drive_link
As @Ivan suggested, I have tried but error is still there
output of the code
PS C:\Users\smjobagc> & C:/Users/smjobagc/AppData/Local/miniconda3/envs/FSV/python.exe "w:/MASTER_BAGCHI_SCHALDACH/THESIS/code and dataset/10 class cropped 300_900 runs/10_class_torch.py"
Labels: ['120', '144', '168', '192', '216', '24', '240', '48', '72', '96']
Total number of labels: 10
Length of train_batch: 1835
Length of test_batch: 459
Traceback (most recent call last):
  File "w:\MASTER_BAGCHI_SCHALDACH\THESIS\code and dataset\10 class cropped 300_900 runs\10_class_torch.py", line 236, in <module>
    evaluate(model,test_loader)
  File "C:\Users\smjobagc\AppData\Local\miniconda3\envs\FSV\lib\site-packages\torch\autograd\grad_mode.py", line 28, in decorate_context
    return func(*args, **kwargs)
  File "w:\MASTER_BAGCHI_SCHALDACH\THESIS\code and dataset\10 class cropped 300_900 runs\10_class_torch.py", line 206, in evaluate
    outputs = [model.validation_step(batch) for batch in val_loader]
  File "w:\MASTER_BAGCHI_SCHALDACH\THESIS\code and dataset\10 class cropped 300_900 runs\10_class_torch.py", line 206, in <listcomp>
    outputs = [model.validation_step(batch) for batch in val_loader]
  File "w:\MASTER_BAGCHI_SCHALDACH\THESIS\code and dataset\10 class cropped 300_900 runs\10_class_torch.py", line 92, in validation_step
    acc = accuracy(out, labels)           # Calculate accuracy
  File "w:\MASTER_BAGCHI_SCHALDACH\THESIS\code and dataset\10 class cropped 300_900 runs\10_class_torch.py", line 212, in accuracy
    return torch.sum(preds.argmax(1) == labels).float().mean()
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Answer: Laying out the situation may help you:

a data loader outputting tensors of shape (64, 1, 900, 300), ie. on 900 by 300 grayscale image per batch element;
and the corresponding ground truth label of shape (64,);
a model which outputs class logits of shape (64, 10), ie. 10 logits per batch element: q1, q2, ..., q10;
you are trying to compare two tensors of different shapes: preds == labels, preds is (64, 10) while labels is (64,).

Hence the error, which you can rewrite as

The size of tensor labels (64) must match the size of tensor preds (10) at non-singleton dimension 1

You're trying to compare the estimated probability of the image with an integer class, how can you expect this to work? You must first get the arg max of the estimated logits (ie. the class number associated with the highest scoring logit), only then will it make sense to compare with the ground truth to measure the accuracy:
(preds.argmax(1) == labels).float().mean()

Comment: Thankyou @Ivan for the reply. Unfortunately, the problem is still there. I have posted the error in the question above.
Comment: What are the shapes of `preds` and `labels` just before this line?
Comment: i chose batch size as 16
Shape of labels tensor: torch.Size([16, 10])
i think I cannot see the shape of **preds**
Comment: How come, you should look at the shape of `preds`, otherwise you won't know what the problem is!
Comment: i am sorry @Ivan, I thought since it is giving error at the accuracy so it won't print the shape of preds
Shape of preds: torch.Size([32])
Comment: So if `preds` is 1D, you don't need to apply the `argmax`. Why is `labels` shaped `(16, 10)`, what does it contain? Are you sure you haven't swapped them around? This new error message you get suggest that the argmax has already been applied.
Comment: oh, i was checking for different batch size(16/32) so labels shape is [batch_size,num_classes] and preds shape is 1D but it should be also equal to the shape of labels, right? any idea, how to implement it?
Comment: Are you labels one hot encoding?
Comment: Thankyou very much @Ivan for the help. Yes, I have onehotencoded the labels so I changed the preds shape to be equal to labels. But now, I could not evaluate the model without training of model first and even the validation accuracy is showing some abnormal results. Could you please help? Do I have to post another question for that?
Comment: No worries, consider accepting the answer as valid (green checkmark on the left). You can create a new thread, just ping me here so I can have a look.
Comment: Thankyou @Ivan for the help. Although I am trying by my own first, if I do not get then I will surely post a thread.


How to allow users to customize text field with styling

Question: I am currently working on a project for school and am required to create a blog. Part of our assignment is to create an input field that would allow users to customize the styling of their text within the field. Although I have looked around, I have had no luck in finding what it is that I am looking for. For clarification, the goal is to create a field that resembles the field on here when creating a post (Photo included below).
I am assuming that Javascript and/or JQuery is required, although I am unsure where to begin. Features would include text styling, addition of links, keeping track of [return]s (to create new paragraphs) and any other HTML/CSS elements.
Below, I will include examples of what it is exactly that I am wanting in case I am in any way unclear.
Thank you in advance for any help/guidance!
Example 1:

Example 2:
Answer: Just use tinymce - unless your assignment is to create the actual control itself, which is far more complicated than even creating a blog unfortunately.
Get TinyMCE here:
https://github.com/tinymce
or at their site:
https://www.tinymce.com/
Then create a textarea element and initiate the control:
Include TinyMCE at the bottom of your page and include this script below that include.
Example here @ the TinyMCE site: https://www.tinymce.com/docs/demo/basic-example/#liveexample
HTML:
<textarea></textarea>

Javascript:
tinymce.init({
  selector: 'textarea',
  height: 500,
  plugins: [
    'advlist autolink lists link image charmap print preview anchor',
    'searchreplace visualblocks code fullscreen',
    'insertdatetime media table contextmenu paste code'
  ],
  toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
  content_css: [
    '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
    '//www.tinymce.com/css/codepen.min.css'
  ]
});

Comment: Thank you very much Ryan, I certainly appreciate the help!
Answer: Use a text editor for this. There are lot of text editor plugins are available.
This may help you : http://ckeditor.com/
Note: Have a look in to the Licence also before using the editor.
Comment: Thank you very much! This turned out to be easier than I thought.
Answer: You could create it yourself!
I threw this together in a couple of minutes. It's pretty rudimentary, but if you want to build it yourself, this could be a starting point for you:

var areas = document.getElementsByClassName('augmented-textarea');

var commands = [{
  display: "B",
  f: function(text) {
    return "**" + text + "**";
  },
}, {
  display: "I",
  f: function(text) {
    return "_" + text + "_";
  },
}, ];

Array.prototype.forEach.call(areas, initTextarea);

function initTextarea(t) {
  var wrapper = document.createElement('div');
  wrapper.classList.add('textarea-wrapper');
  var bar = document.createElement('div');
  bar.classList.add('textarea-bar');

  commands.forEach(function(c) {
    var b = document.createElement('button');
    b.type = 'button';
    b.innerHTML = c.display;
    b.addEventListener('click', function() {
      if (t.selectionStart === t.selectionEnd)
        return;

      var v = t.value,
        ss = t.selectionStart,
        se = t.selectionEnd,
        prefix = v.slice(0, ss),
        suffix = v.slice(se),
        target = v.substring(ss, se),
        changed = c.f(target);
      t.value = prefix + changed + suffix;
      t.selectionStart = ss;
      t.selectionEnd = ss + changed.length;
      t.focus();
    });
    bar.appendChild(b);
  });

  t.parentNode.insertBefore(wrapper, t);
  t.parentNode.removeChild(t);
  wrapper.appendChild(bar);
  wrapper.appendChild(t);
}
.textarea-wrapper {
  display: flex;
  overflow: hidden;
  width: 300px;
  flex-direction: column;
  border: 1px solid gray;
}
.textarea-bar {
  background-color: #ddd;
  min-height: 30px;
  line-height: 30px;
}
.textarea-bar button {
  background-color: #ddd;
  font-family: serif;
  font-size: 20px;
  width: 30px;
  display: inline-block;
  border: 1px solid gray;
  border-radius: 3px;
  margin: 3px;
  cursor: pointer;
}
.augmented-textarea {
  width: 100%;
  max-width: 100%;
  height: 70px;
  box-sizing: border-box;
  border: 0;
}
<textarea class="augmented-textarea">Select some text and press one of the buttons above!</textarea>

Each command is registered in the commands array, with HTML for a button, and an instruction to do with the selected text.
Comment: Thank you very much for this, I'll look to expand this more.
Answer: A free and open source option is https://quilljs.com/.
From their quickstart guide:

<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">

Hello World!

Some initial bold text


Prove that x{n} is convergent

Question: So I'm currently studying for my midterms and I found the following question from my practice set which I'm unable to solve:
Prove that the following sequence $(x_{n})$ is convergent. Let
$$x_{n} = \frac{\sin 1}{2} + \frac{\sin 2}{2^2} + \frac{\sin 3}{2^3} + ... + \frac{\sin n}{2^n}$$
This is the first time I've seen such a sequence, how does one go about proving such questions? Do I need to first prove that the sequence is monotonic?
Comment: $\dfrac{|\sin{n}|}{2^n}\leq \dfrac{1}{2^n}$
Answer: The series converges absolutely and thus it converges, since
$$\left|\frac{\sin k}{2^k}\right|\le\frac1{2^k}$$
Apply now the comparison test and we're done.
Comment: Thanks alot! looks like i'm a little slow on math here >< i can't seem to get the hang of proving-type questions at all :/
Comment: My pleasure....

Prove that x{n} is convergent


How to compute conditional density function in this particular case?

Question:
Suppose we have two random variables $\xi,\eta$, which are defined on the same probability space and $\xi \sim \mathcal{U}[0,1]$. Moreover, $\forall \lambda\in[0,1]:(\eta|\xi=\lambda)\sim \mathcal{Be}(\lambda)$.

I need to find the distribution of $\eta$. In order to do so, I need to calculate $f_{\xi}(\lambda|\eta=t)$, but how does one compute it? In the question conditional probability combining discrete and continous random variables user Wio computes it using  joint probability density, but is it really possible, given that one of the variables is absolutely continuous and the other is discrete?
Answer: $$\begin{aligned}P(\eta \leq t)&=E[P(\eta\leq t|\xi)]=\\
&=\int_0^1P(\eta\leq t|\xi=y)dy=\\
&=\int_0^1(y \mathbf{1}_{\{0\leq t<1\}}(t)+\mathbf{1}_{\{t\geq 1\}}(t))dy=\\
&=\frac{1}{2}\mathbf{1}_{\{0\leq t<1\}}(t)+\mathbf{1}_{\{t\geq 1\}}(t)\end{aligned}$$
We may conclude $\eta \sim \textrm{Bernoulli}(1/2)$.
Comment: Why $\mathbb{P}[\eta\leq t] = \mathbb{E}[\mathbb{P}[\eta\leq t|\xi]]$? Is this some kind of generalization of the total probability formula?
Comment: @Egor [Total expectation](https://en.wikipedia.org/wiki/Law_of_total_expectation): $P(\eta \leq t)=E[\mathbf{1}_{\{\eta \leq t\}}]=E[E[\mathbf{1}_{\{\eta \leq t\}}|\xi]]=E[P(\eta\leq t|\xi)]$
Comment: So we didn't use the distribution of $\xi$ in this solution? It seems that the only relevant thing was $(\eta|\xi = \lambda)\sim \mathrm{Be}(\lambda)$...
Comment: Oh, it seems like we used it to argue that $dF_{\xi} = f_{\xi}dy = dy?$
Comment: @Egor, indeed, we took the expectation wrt to the law of $\xi$, which is just uniform over the unit interval.


JUNG animate image along an edge

Question: So I'm using JUNG, which is something new for me. I've already implemented a simple GUI, which displays vertexes and edges and through the use of Transformers I can change shapes and all that.
But how would I animate an image along an edge?
If the edges were straight lines it'd be easy as I know start and end X and Y coords, but the edges can also be BentLines, CubicCurves or QuadCurves. How would I make, say, a car move along the line that has been drawn?
I've looked at the docs for the PathIterator, but to be honest, I haven't got a clue what it actually does and whether it's apropriate for what I want.
Any pointers in the right direction would be appreciated!
Answer: This, in fact, is a bit tricky.
First of all, some contortions are necessary in order to obtain the real edge shape that is painted on the screen. Fortunately, the relevant code is already contained in the ShapePickSupport.java class from JUNG.
Then, one has to compute a point on this shape (which is therefore implicitly assumed to be a line). This involves playing around with the PathIterator for computing the total length, and the current position on the line.
I tried to implement this (in a very basic and simple form) and encapsulate this in an ImageAtEdgePainter class: It receives the VisualizationViewer for the edge shape computations, as well as the edge and the image that should be painted. It has a setImageLocation method that accepts a value between 0.0 and 1.0, where 0.0 means that the image should be at the start of the edge, and 1.0 means that the image is at the end of the edge, respectively.
Using a dummy graph and a dummy image, the result looks like this:

where the image oscillates between the end points of the edge. Here is the code, as an MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Context;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.visualization.Layer;
import edu.uci.ics.jung.visualization.MultiLayerTransformer;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class JUNGEdgePathTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static BufferedImage createDummyImage()
    {
        int w = 100;
        int h = 30;
        BufferedImage image =
            new BufferedImage(w,h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0,0,w,h);
        g.setColor(Color.WHITE);
        g.drawString("Image", 10, 20);
        g.dispose();
        return image;
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        final Graph<String, String> graph = getGraph();
        final VisualizationViewer<String, String> vv =
            new VisualizationViewer<String, String>(
                new FRLayout<String, String>(graph));
        final BufferedImage image = createDummyImage();

        String edge = graph.getEdges().iterator().next();
        final ImageAtEdgePainter<String, String> imageAtEdgePainter =
            new ImageAtEdgePainter<String, String>(vv, edge, image);

        Timer t = new Timer(20, new ActionListener()
        {
            long prevMillis = 0;
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (prevMillis == 0)
                {
                    prevMillis = System.currentTimeMillis();
                }
                long dtMs = System.currentTimeMillis() - prevMillis;
                double dt = dtMs / 1000.0;
                double phase = 0.5 + Math.sin(dt) * 0.5;
                imageAtEdgePainter.setImageLocation(phase);
                vv.repaint();
            }
        });
        t.start();

        vv.addPostRenderPaintable(imageAtEdgePainter);

        f.getContentPane().add(vv);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    static class ImageAtEdgePainter<V, E> implements VisualizationViewer.Paintable
    {
        private final VisualizationViewer<V, E> vv;
        private final E edge;
        private final BufferedImage image;
        private double imageLocation;

        ImageAtEdgePainter(
            VisualizationViewer<V, E> vv,
            E edge,
            BufferedImage image)
        {
            this.vv = vv;
            this.edge = edge;
            this.image = image;
        }

        public void setImageLocation(double imageLocation)
        {
            this.imageLocation = imageLocation;
        }

        @Override
        public void paint(Graphics gr)
        {
            Graphics2D g = (Graphics2D)gr;
            Shape shape = getTransformedEdgeShape(vv, vv.getGraphLayout(), edge);
            Point2D p = computePointAt(shape, 0.2, imageLocation);
            //g.setColor(Color.BLUE);
            //g.draw(shape);
            //System.out.println(p);
            gr.drawImage(image, (int)p.getX(), (int)p.getY(), null);
        }
        @Override
        public boolean useTransform()
        {
            return true;
        }

    }

    private static double computeLength(Shape shape, double flatness)
    {
        double length = 0;
        PathIterator pi = shape.getPathIterator(null, flatness);
        double[] coords = new double[6];
        double previous[] = new double[2];
        while (!pi.isDone())
        {
            int segment = pi.currentSegment(coords);
            switch (segment)
            {
                case PathIterator.SEG_MOVETO:
                    previous[0] = coords[0];
                    previous[1] = coords[1];
                    break;

                case PathIterator.SEG_LINETO:
                    double dx = previous[0]-coords[0];
                    double dy = previous[1]-coords[1];
                    length += Math.sqrt(dx*dx+dy*dy);
                    previous[0] = coords[0];
                    previous[1] = coords[1];
                    break;
            }
            pi.next();
        }
        return length;
    }

    public static Point2D computePointAt(
        Shape shape, double flatness, double alpha)
    {
        alpha = Math.min(1.0, Math.max(0.0, alpha));
        double totalLength = computeLength(shape, flatness);
        double targetLength = alpha * totalLength;
        double currentLength = 0;
        PathIterator pi = shape.getPathIterator(null, flatness);
        double[] coords = new double[6];
        double previous[] = new double[2];
        while (!pi.isDone())
        {
            int segment = pi.currentSegment(coords);
            switch (segment)
            {
                case PathIterator.SEG_MOVETO:
                    previous[0] = coords[0];
                    previous[1] = coords[1];
                    break;

                case PathIterator.SEG_LINETO:
                    double dx = previous[0]-coords[0];
                    double dy = previous[1]-coords[1];
                    double segmentLength = Math.sqrt(dx*dx+dy*dy);
                    double nextLength = currentLength + segmentLength;
                    if (nextLength >= targetLength)
                    {
                        double localAlpha =
                            (currentLength - targetLength) / segmentLength;
                        //System.out.println("current "+currentLength+" target "+targetLength+" seg "+segmentLength);
                        double x = previous[0] + localAlpha * dx;
                        double y = previous[1] + localAlpha * dy;
                        return new Point2D.Double(x,y);
                    }
                    previous[0] = coords[0];
                    previous[1] = coords[1];
                    currentLength = nextLength;
                    break;
            }
            pi.next();
        }
        return null;
    }

    // This method is take from JUNG ShapePickSupport.java
    private static <V, E>  Shape getTransformedEdgeShape(
        VisualizationViewer<V, E> vv, Layout<V, E> layout, E e) {
        Pair<V> pair = layout.getGraph().getEndpoints(e);
        V v1 = pair.getFirst();
        V v2 = pair.getSecond();
        boolean isLoop = v1.equals(v2);
        RenderContext<V, E> rc = vv.getRenderContext();
        MultiLayerTransformer multiLayerTransformer =
            rc.getMultiLayerTransformer();
        Point2D p1 = multiLayerTransformer.transform(
            Layer.LAYOUT, layout.transform(v1));
        Point2D p2 = multiLayerTransformer.transform(
            Layer.LAYOUT, layout.transform(v2));
        if(p1 == null || p2 == null)
            return null;
        float x1 = (float) p1.getX();
        float y1 = (float) p1.getY();
        float x2 = (float) p2.getX();
        float y2 = (float) p2.getY();
        AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);
        Shape edgeShape =
            rc.getEdgeShapeTransformer().transform(
                Context.<Graph<V,E>,E>getInstance(
                    vv.getGraphLayout().getGraph(),e));
        if(isLoop) {
            Shape s2 = rc.getVertexShapeTransformer().transform(v2);
            Rectangle2D s2Bounds = s2.getBounds2D();
            xform.scale(s2Bounds.getWidth(),s2Bounds.getHeight());
            xform.translate(0, -edgeShape.getBounds2D().getHeight()/2);
        } else {
            float dx = x2 - x1;
            float dy = y2 - y1;
            double theta = Math.atan2(dy,dx);
            xform.rotate(theta);
            float dist = (float) Math.sqrt(dx*dx + dy*dy);
            xform.scale(dist, 1.0f);
        }
        edgeShape = xform.createTransformedShape(edgeShape);
        return edgeShape;
    }

    public static Graph<String, String> getGraph()
    {
        Graph<String, String> g = new DirectedSparseGraph<String, String>();
        g.addVertex("v0");
        g.addVertex("v1");
        g.addVertex("v2");
        g.addVertex("v3");
        g.addVertex("v4");
        g.addEdge("e0", "v0", "v1");
        g.addEdge("e1", "v1", "v2");
        g.addEdge("e2", "v2", "v3");
        g.addEdge("e3", "v3", "v4");
        g.addEdge("e4", "v4", "v0");
        g.addEdge("e5", "v1", "v3");
        g.addEdge("e6", "v2", "v4");
        return g;
    }
}

When you say that you want to move a car along the line, I can imagine that you also want to align the image of the car with the edge - that is, to rotate the image so that the car always points towards the end of the edge. This would not be sooo difficult. But if this is an issue, you should probably first have a look at other questions (like Java: Rotate image towards mouse position? ) to see whether the answers there can... "inspire" you, or ask it as a separate (not-JUNG-specific) question.
Comment: I hadn't actually considered having to align the car, although now you've mentioned, I feel it's something I would'veprobably realized I needed later on. But thanks anyway ;D
Regarding your answer, it looks quite helpful, but it's late so I've just taken a short look at it. More feedback later asap.
Comment: Works beautifully, exactly what I intended and thanks to you I've now figured out how to work with the PathIterator! Thanks!

JUNG animate image along an edge