Friday, July 8, 2022

Recent Questions - Stack Overflow

Recent Questions - Stack Overflow


How to fix "database is locked" when no concurrent threads are involved? golang, sqlite3

Posted: 08 Jul 2022 10:46 AM PDT

I'm running a list of sql files. The list of files comes from the same sqlite3 db that I'm going to execute sql against, hence there's only one db connection. If I don't kill the loop over sql files it will return "database is locked" error for every file starting with the first. Things that didn't work:

  1. No effect: Adding rows.Close() from Sqlite3 error: database is locked in golang

  2. Removing sqlx and using database/sql didn't change anything.

  3. No effect: Having Navicat open or closed.

  4. Restarting my laptop, no effect.

  5. No effect: Dumping database to a new file then running the process against the new file.

     package main     import (   "database/sql"   "fmt"   "io/ioutil"   "os"     _ "github.com/mattn/go-sqlite3"   )     func main() {   dbPath := "/Users/darianhickman/Documents/wc_study/history.db"     db, err := sql.Open("sqlite3", dbPath)   if err != nil {       println("db open failed.", err)       os.Exit(-1)   }   defer db.Close()   // qry := `select list_folder||'/'|| sql_file as script from run_list where 'order' > 0 ORDER BY 'order'; `   qry := `select list_folder||'/'|| sql_file as script from run_list    where run_order > 0    ORDER BY run_order;`     scripts, err := db.Query(qry)     if err != nil {       println("query failed ", qry)       os.Exit(-1)   }   defer scripts.Close()     var file string   for scripts.Next() {       err = scripts.Scan(&file)       if err != nil {           println(err)       }       println(file[len(file)-80:])       sqlScript, err := ioutil.ReadFile(file)         if err != nil {           println("reading script file failed\n", file)       }       if _, err := db.Exec(string(sqlScript)); err != nil {           fmt.Println(string(sqlScript[:80]))           fmt.Println(err)         } else {           fmt.Println("Success ", string(sqlScript[:80]))       }   }  

    }

AI for Counting Bats in Photos - (As they exit a cave for a population estimate)

Posted: 08 Jul 2022 10:46 AM PDT

AI to count bats in photos

I work at a U.S. National Monument where we have a cumbersome manual method of counting bats in photos we take every 30 seconds as they exit a cave, then extrapolate an estimate of their population. I'm looking for a better system, where we can:

  1. Train the AI to recognize the bats in images
  2. Have the AI then automate the counting of bats in 1000+ images and provide sum totals from all the images in an image set.

Can anyone recommend a no- or low-code solution to this? I'm sure something is out there, but I don't seem to be using the right Google keywords to find in; I'm an ecologist, not a computer scientist...

Thanks!

How create a domain for white label?

Posted: 08 Jul 2022 10:45 AM PDT

I have a subdomain that I have created but it must be accessible as a white label.

On Siteground, I have hosted subdomain.mydomain.com and it is the main site.

On Hostinger I have the domain whitelabel.com and I have created a CNAME record: Type: CNAME Name: system.whitelabel.com Target: subdomain.mydomain.com TTL: 43200

I want when people type system.whitelabel.com they can see the content of subdomain.mydomain.com but without redirecting or changing the URL.

But when I access system.whitelabel.com I get the message "This site's domain name is either not yet pointed or is still propagating." It's already been more than 72 hours.

Additionally, at subdomain.mydomain.com I tried to change the .htaccess file as follows: RewriteEngine on RewriteCond %{HTTP_HOST} ^system.whitelabel.com RewriteRule ^(.*)$ https://subdomain.mydomain.com/ [L,NC,QSA]

but it didn't work either. With or without this change, I get the same message described above.

What else do I need to configure? Or is there an error in what was done? Thanks for your help.

'DataFrame' object has no attribute 'text' with pyplot

Posted: 08 Jul 2022 10:45 AM PDT

I'm not getting to add text to the pyplot chart. 'm trying to do that ...

import matplotlib.pyplot as plt  ax = Teste.loc[:,['Q001', 'NU_NOTA_MT']]    plt.plot(ax['Q001'], ax['NU_NOTA_MT'], 'ro-', label = 'MTM')    #adicionando as legendas do questionário no gráfico  textdictQ002 = ""  for key,value in q001eq002Dicionario.items():      textdictQ002 = textdictQ002 + "{k} + {v}\n".format(k=key,v=value)    ax.text(0.03, 0.65, textdictQ002, transform=ax.transAxes, fontsize=12,          bbox={'boxstyle':'round', 'facecolor':'wheat', 'alpha':0.5})    plt.title("Notas x Escolaridade Pai", fontsize = 20)    plt.ylabel("Medias", size = 15)    plt.xlabel("Categorias", size = 15)    plt.legend()    plt.grid()    plt.show()  

================================================================== And error ...

enter image description here

My files "Teste" and "q001eq002Dicionario" are:

"Teste" Q001 SG_UF_RESIDENCIA NU_NOTA_MT 0 A SC 482.720461 1 B SC 505.414095 2 H SC 520.755467 3 C SC 522.308284 4 D SC 533.886299 5 E SC 556.726944 6 F SC 606.302340 7 G SC 630.829501

"q001eq002Dicionario" {'A': 'Nunca Estudou', 'B': 'Não completou a 4º série/5º ano do ensino fundamental', 'C': 'Completou a 4º série/5º ano, mas não completou a 8º série/9º ano do ensino fundamental', 'D': 'Completou a 8º série/9º ano do ensino fundamental, mas não completou o ensino médio', 'E': 'Completou o ensino médio, mas não completou a Faculdade', 'F': 'Completou a Faculdade, mas não completou a Pós-Graduação', 'G': 'Completou a Pós-Graduação', 'H': 'Não sei'}

I appreciate any help in resolving this error

Thanks!

How to automatically close cmd window after when i choose the option?

Posted: 08 Jul 2022 10:45 AM PDT

@echo off  cls  :start  echo.  echo 1. Multiplayer  echo 2. Singeplayer  echo.  echo.  set /p x=Pick:  IF '%x%' == '%x%' GOTO Item_%x%    :Item_1  start /MIN /D"D:\games\Battlefield 2142 - Deluxe Edition\mods\bf2142\Tools\BF2142Unlocker64" BF2142Unlocker.exe  GOTO Start    :Item_2  start /MIN /D"D:\games\Battlefield 2142 - Deluxe Edition" BF2142_offline_4gb.exe  GOTO Start  

i'm trying to solve it but i am noob shortcuts take up a lot of space on the desktop so I'm trying to run it with bat file

Filtering in Mongoose / MongoDB 'find' with non-existent value returns all results

Posted: 08 Jul 2022 10:45 AM PDT

I am using Mongoose v6.3.8. I might have a basic misunderstanding about how Mongoose / MongoDB works, but if I do...

model.find({ nonExistentKey: 'anyValue' });  

it returns all documents, even though nonExistentKey doesn't show up anywhere in my database.

I was expecting it to return no documents since no documents use nonExistentKey.

Can someone please explain this behavior?

Thank you.

Puppeteer wait for navigation causing errors on two of the same websites

Posted: 08 Jul 2022 10:44 AM PDT

I'm having a problem with Puppeteer where I run one set of code and it works fine for a website, but then if I run the same code for another website running the same platform it doesn't work as expected. I'm using the two websites https://www.toyotaofcedarpark.com/ and https://www.toyotaofnaperville.com/ both of which are made with Dealer Inspire

This code works on https://www.toyotaofnaperville.com/ but not https://www.toyotaofcedarpark.com/ (I receive a timeout error)

index.js

var link = await scraperFunctions.getInventoryLink(page, category);  console.log("Found inventory link:", link);  await page.goto(link);  await page.waitForNavigation();  var postedVehicles = await scraperFunctions.postedVehicles(page, category);  console.log(postedVehicles, ": NUM VEHICLES");  

postedVehicles.js

async function postedVehicles(page, category) {    switch (category) {      case 0:        return category0(page);      case 1:        return category1(page);      case 2:        return category2(page);    }  }  async function category0(page) {    return await page.$eval("span#results-title", (elem) =>      elem.innerText.replace(/\D/g, "")    );  }  module.exports = postedVehicles;  

Yet, when I remove the await page.waitForNavigation(); line from index.js it works for https://www.toyotaofcedarpark.com/ but not https://www.toyotaofnaperville.com/ (I receive a Error: Execution context was destroyed, most likely because of a navigation.

How is the output() function different than, better than, a simple printf or fprintf statement?

Posted: 08 Jul 2022 10:43 AM PDT

A book that I am reading [1] says something that I need help understanding.

The book first describes the input(), output(), and unput() functions. Then it says this:

The use of output() allows Lex to be used as a tool for stand-alone data "filters" for transforming a stream of data.

Below is a lexer I created. It is a stand-alone data filter for transforming a stream of data (it finds all occurrences of a word and outputs the word and its line number). It does not use output(), it uses fprintf. How would my lexer be different if it used output()? Is it recommended to use output()? Please help me to understand the significance of what the book says.

%option noyywrap  %option always-interactive  %option yylineno  %{  #include <stdlib.h>  #include <string.h>  #include <stdio.h>  #define DESIRED_STRING "FORMAT"  %}  %%   [^\n]+      {                  char *ret;                 ret = strstr(yytext, DESIRED_STRING);                 if (ret) {                    fprintf(stdout, "%d: %s\n", yylineno, yytext);                 }               }  \n          { }  %%  int main()  {yylex();}  

[1] Crafting a Compiler with C, page 69.

Iterating over rows to find mean of a data frame in Python

Posted: 08 Jul 2022 10:46 AM PDT

I have a dataframe of 100 random numbers and I would like to find the mean as follows:

mean0 should have mean of 0,5,10,... rows

mean1 should have mean of 1,6,11,16,.... rows

.

.

. mean4 should have mean of 4,9,14,... rows.

So far, I am able to find the mean0 but I am not able to figure out a way to iterate the process in order to obtain the remaining means.

My code is as follows:

import numpy as np  import pandas as pd  import csv    data = np.random.randint(1, 100, size=100)  df = pd.DataFrame(data)    print(df)    df.to_csv('example.csv', index=False)    df1 = df[::5]  print("Every 12th row is:\n",df1)    df2 = df1.mean()  print(df2)  

How to trace variable values when using os.system() or using `subprocess.run()` to run python file?

Posted: 08 Jul 2022 10:45 AM PDT

How to trace variable values using sys.settrace() when using os.system() or using subprocess.run() to run python file?

My current implementation goes like this:

sys.settrace(traceit)  os.system(f"python {fname} < {inp_fp}")  sys.settrace(None)  

But I am getting no traces at all, any clue?

List is not in order

Posted: 08 Jul 2022 10:45 AM PDT

img1

img2

Hi! I'm new to springboot and Java. apologies if I have wrong terminologies.

I converted UUID to string(String id) and put the conversion inside a method, I also declare other Strings variables such as First Name etc and put in on a Array List

the code does work but I'm confused why the string email was showing second on the list. I'm expecting data to be on this order

ID , FirstName , LastName , email

public class StudentController {    @Autowired  StudentService studentService = new StudentService();    @GetMapping  public List<Student> displayStudent(){      return studentService.getStudent();  }  

}

public class StudentService {      Student student = new Student();    private List<Student> studentList = Arrays.asList(          new Student(student.genID(),"Elvis" , "Presley" ,"Elvis@gmail.com")  );    public List<Student> getStudent(){      return studentList;  }  

}

public class Student {    UUID uuid = UUID.randomUUID();  private String id;  private String FirstName;  private String LastName;  private String email;        public Student() {  }      //Method Converting UUID into string  public String genID(){      id = uuid.toString();      return id;  }    public Student(String id) {      this.id = id;  }  public Student(String id, String firstName, String lastName, String email) {      this.id = id;      FirstName = firstName;      LastName = lastName;      this.email = email;  }    public String getId() {      return id;  }    public void setId(String id) {      this.id = id;  }    public String getFirstName() {      return FirstName;  }    public void setFirstName(String firstName) {      FirstName = firstName;  }    public String getLastName() {      return LastName;  }    public void setLastName(String lastName) {      LastName = lastName;  }    public String getEmail() {      return email;  }    public void setEmail(String email) {      this.email = email;  }  

}

Any wstring/wchar_t gets blocked by Defender as Trojan:Win32/Wacatac.B!ml

Posted: 08 Jul 2022 10:45 AM PDT

Whenever i use a wstring/wchar_t in a cpp program, zip the binary, send it over to another Win machine and unzip the program, it gets blocked as a "dangerous" Win32/Wacatac.B!ml trojan - Which is obviously wrong. How do i solve this? This is completely acceptable :(

Sample 1:

int main()  {      const string s = "Hello World!";      const wstring ws(s.begin(), s.end());      const wchar_t *wc = ws.c_str();      wcout << wc << endl;  }  

Sample 2:

wchar_t* c2wc(const char* c) {      size_t size = strlen(c) + 1;      wchar_t *wc = new wchar_t[size];      size_t outSize;      mbstowcs_s(&outSize, wc, size, c, size - 1);      return wc;  }    int main()  {      const string s = "Hello World!";      const wchar_t *wc = c2wc(s.c_str());      wcout << wc << endl;  }  

How to add security key to github with firefox?

Posted: 08 Jul 2022 10:44 AM PDT

Github refused to clone a private repo:

$ git clone https://github.com/jamesbond/secretmission  Cloning into 'secretmission'...  Username for 'https://github.com': kilojoules  Password for 'https://kilojoules@github.com':   remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.  remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.  fatal: Authentication failed for 'https://github.com/jamesbond/secretmission/'  

In my opinion, the blog they post to is not clear about what I can do here. I am trying to add a security key to see if that helps. When I try to enter a security key on firefox on my mac, I see this: github.com wants to register an account with one of your security keys. You can connect and authorize one now, or cancel. I have no calls actions available I am aware of past this point.

I'm out of my element. How do I connect and authorize a security key? When I use the firefox plugin, it asked for the issuer and secret, of which I have neither. Can I use an ethereum wallet as an authentication?

enter image description here

How to force Event.target for dispatched events on global listeners in javascript? [duplicate]

Posted: 08 Jul 2022 10:45 AM PDT

I got a question about dispatching events in Javascript.

Here is a little code section providing the intro, which works as expected. The event.currentTarget is the same as if I directly clicked on the button itself.

var button1 = document.querySelector('.button1');  button1.addEventListener('click', function (e) {      alert(`Button1 was clicked!\nEvent currentTarget: ${e.currentTarget} .${e.currentTarget.className}`);  });    var event = new Event('click');  button1.dispatchEvent(event);
<p>if you load the page a button click will be dispatched</p>    <button class="button1">Button1: If you click me I wil open an alert</button>    <p>Expected Output: "Button1 was clicked! [object HTMLButtonElement] .button1"</p>

Question: Why is different if I have a listener on the document.

Event.currentTarget seems to be the element from where the event was dispatched from, not the element where dispatch is called on.

var button1 = document.querySelector('.button1');  document.addEventListener('click', function (e) {      alert(`Button1 was clicked!\nEvent.currentTarget: ${e.currentTarget} .${e.currentTarget.className}`);  });    var event = new Event('click');  button1.dispatchEvent(event);
<p>Click anywhere besides the button itself to see the difference!</p>    <button class="button1">Button1: If you click me I wil open an alert</button>    <p>Expected Output: "Button1 was clicked! [object HTMLButtonElement] .button1"</p>  <p>Actual Output: "Button1 was clicked! [object HTMLHtmlElement]"</p>

Is there a way to force the event target?

Ideally I want to use a global listener handling multiple purposes and check per event.target if that button was clicked.

One slightly more detailed demo can be found on codepen!

Using audioplayers to play a single note from a button

Posted: 08 Jul 2022 10:45 AM PDT

I am doing an outdated tutorial on the audioplayers package and just trying to play a single note from when the button is pressed. I am not able to get it to work, can someone please help

import 'package:flutter/material.dart';  import 'package:audioplayers/audioplayers.dart';    void main() => runApp(XylophoneApp());    class XylophoneApp extends StatelessWidget {    @override    Widget build(BuildContext context) {      return MaterialApp(        home: Scaffold(          body: SafeArea(            child: Center(              child: TextButton(                onPressed: () {                  final player = AudioCache();                  player.play('note1.wav'); //ERROR THAT 'play' method is not valid                },                child: Text('Click Me'),              ),            ),          ),        ),      );    }  }  

how can i blur song cover in background of the player activity?

Posted: 08 Jul 2022 10:45 AM PDT

I have this player screen in my app, and I want to put the album cover of the song on the background of the screen with a blurred effect, how can i do that?

Images:

my player activity

what i want

Should I use Gatsby.js to create a website like medium.com?

Posted: 08 Jul 2022 10:43 AM PDT

I am going to create a website similar to Medium.com where people can register and write blogs. As far as I know Gatsby can be a great for speed and SEO but I am not sure if it is the case for such website. Is it really better than pure react for my case ?

And I google about Gatsby.js and could not find a good answer about whether I need to use any other framework for backend or I just need to choose database to use with gatsby.js. Can you provide anything about it as well?

Thanks!

How to conduct joint significance test in seemingly unrelated regression

Posted: 08 Jul 2022 10:44 AM PDT

I'm trying to conduct a joint test of significance in a seemingly unrelated regression setup with robust standard errors. I have three outcomes Y1, Y2, and Y3 and I want to conduct a joint hypothesis test against the null that the average effect of the treatment Z is zero on all three outcomes.

I think that I have the model set up correctly, but I don't think that I have the hypothesis.matrix set correctly in car::linearHypothesis.

Here's some data:

library(tibble)  library(car)  library(systemfit)    set.seed(343)  N = 800  dat <-    tibble(      U = rnorm(N),      Z = rbinom(N, 1, 0.5),      Y = 0.2 * Z + U,      Y1 = Y + rnorm(N, sd = 0.3),      Y2 = Y + rnorm(N, sd = 0.5),      Y3 = Y + rnorm(N, sd = 0.5)    )  

Here's the seemingly unrelated regression fit:

sur <- systemfit(list(Y1 ~ Z, Y2 ~ Z, Y3 ~ Z), method = "SUR", data = dat)  summary(sur)  

Which is identical to the ols fit in this case:

ols <- lm(cbind(Y1, Y2, Y3) ~ Z, data = dat)  summary(ols)  

Which is useful, because I need to estimate robust standard errors for this test:

linearHypothesis(ols, hypothesis.matrix = "Z = 0", white.adjust = "hc2")  

This last line is the one that I think is incorrect. I think it's incorrect because the individual coefficients all have lower p-values than the joint test, but I could be wrong?

How can i show/hide navbar based on scroll position?

Posted: 08 Jul 2022 10:44 AM PDT

I am looking for a solution to apply a scroll based animation to my navbar. When i scroll down or up, i want to hide/show the navbar with a scroll animation. After some research i always find only solutions like the following.

var lastScrollTop; // This Varibale will store the top position    navbar = document.getElementById('navbar'); // Get The NavBar    window.addEventListener('scroll',function(){   //on every scroll this funtion will be called        var scrollTop = window.pageYOffset || document.documentElement.scrollTop;    //This line will get the location on scroll        if(scrollTop > lastScrollTop){ //if it will be greater than the previous      navbar.style.top='-80px';      //set the value to the negetive of height of navbar     }        else{      navbar.style.top='0';    }        lastScrollTop = scrollTop; //New Position Stored  });
body{    margin:0;    padding:0;  }  .container{    background:url('https://images.unsplash.com/photo-1497250681960-ef046c08a56e') no-repeat center center;    height:170vh;  }  #navbar{    position:fixed;    top:0;    left:0;    width:100%;    background:blue;    border-radius:0 0 30px 30px;    color:white;    text-align:center;        /*Define a height for NavBar*/    height:80px;    transition: 0.5s;    /*and a transition time for a smooth appearence*/      }    footer{    text-align:center;    position:relative;    bottom:0;    left:0;  }
<div class="container">    <navbar id="navbar">      <h1>NAVBAR<h1>    </navbar>  </div>  <footer>    <h3>End of the Page</h3>  </footer>

If you scroll down or up, a css class or a style definition is applied to the navbar. But i want to show/hide the navbar, based on scroll distance. Say you open the page, the navbar is visible top: 0px, so you need to scroll maybe 80px down to hide it and when you scroll up, you also need to scroll 80px up to show it completly.

How can i calculcate this? Any ideas?

How do you get the length of an array that is in the state of an ngrx component store?

Posted: 08 Jul 2022 10:44 AM PDT

If I have a state object that contains the state below, what is the correct way to get the length of the users array in the state?

Do I need to create a selector for this?

userLength$ = this.select(state => state.users.length);   

Basically, on my view I would like to be able to show the number of users;

 <span>There are {{userLength}} users</span>   //or something like   <span>There are {{users$.length}} users</span>    
export const initialState: UserState = {    users: [      {        userId: 1,        name: 'Dean'      },      {        userId: 2,        name: 'John'      }    ],    selectedUserId: null  }    //selector for the users in the state  users$ = this.select(state => state.users);  

Thanks!

Python - remove spaces and indents from string

Posted: 08 Jul 2022 10:44 AM PDT

I have a sql query string

query_for_update =   f'''  update {db_name}.{schema_name}.{self.table_name}  set {self.updated_field} = {self.updated_field}  where {self.key_field} in ({ids});  '''  

But when I try to write this query to file f.write(query_for_update) I get following result:

update store_1.dbo.[my_table]              set [Trusted Plan] = [Trusted Plan]              where [Entry No_] in (1472371,  1472375,  1472377,  1472379,  1472373,  );  

Code that creates string:

ids_string = ',\n'.join(["'" + str(item) + "'" for item in result.id])  query_for_update = mssql_table.get_update_query('dbo', mssql_db_name, ids_string).strip()  with open(mssql_server_name + '.sql', 'a') as f:      f.write(query_for_update)  

How can i remove indents for strings in this case?

How to get the inner control in a ValueControlAccessor in the correct validation satet in Angular

Posted: 08 Jul 2022 10:45 AM PDT

We started with implementation of a component library for our Angular (version 12 at the moment) project.

At the moment we implement the first component as a blueprint. The component should encapsulate a Material input (matInput) with label, mat-errors, some validators etc. so that the developer just needs to write s.th. like

<our-input max-length="5" required label="fancy field" [formControlName]="fancyField">  

to get a fully designed input field. Btw. I'm aware that mixing validations from FormControl and template should not be done, but that is only for the sake of the example; in real life the validation will be defined by the component like a datepicker.

At the moment our template is realy simple:

<mat-form-field class="example-full-width" appearance="fill">    <input      matInput      [formControl]="formControl"      (input)="onInputChange($event)"      (blur)="onInputBlur()"    />    <mat-error *ngFor="let error of outerControl.errors | keyvalue">      <ng-container [ngSwitch]="error.key">        <ng-container *ngSwitchCase="'required'">          Field required error        </ng-container>  ...      </ng-container>    </mat-error>  </mat-form-field>  

We are stuck at the part with the validation. If the validation happens only for the "outer control" (the control fancyField in the example) the inner matInput neither get the styling nor not show the validation errors because it simply does not know that it is invalid.

Because we register some validators in a similar way Netanel Basal described in his blog post Adding Integrated Validation to Custom Form Controls in Angular we already have access to the outer NgControl:

  constructor(@Self() private ngControl: NgControl) {      controlDirective.valueAccessor = this;    }   

We already tried to call the outer validators as an inner validator

 this.formControl.addValidators((control) => this.ngControl.control.validator?.(control));   // the same for asyncValidators?  

or inspired by Christian Lüdemann's post Form validation with ControlValueAccessor to assign the outer form control to the inner form control on view-init

public ngAfterViewInit(): void {      // syncing with validators on host element      this.formControl = this.ngControl.control as FormControl;  }  

but both methods trigger the validators twice. Additionally I have a strange feeling about connecting a FormControl to two template controls.

Here is an example at StckBlitz https://stackblitz.com/edit/angular-ivy-swiydl

I guess there some other solutions, too.

I already thought about trying to mirror the outer state but did not find any feasible way to have change detection for properties of an object which is not in a template. The only solution I could found was to create an inner component to bind properties of this.ngControl.control but the felt strange, too.

Which way would you got or is there any better way?

Flutter 3 : Warning : Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null , getting this after upgrading to flutter 3 [duplicate]

Posted: 08 Jul 2022 10:45 AM PDT

I just upgraded to Flutter 3 and my flutter project console is full of warnings. Did anyone know how to fix these warnings or should I wait for the stable update from flutter itself.

enter image description here

Pandas Groupby Weighted Standard Deviation

Posted: 08 Jul 2022 10:44 AM PDT

I have a dataframe:

             Type Weights Value  ....      0         W     0.5    15        1         C     1.2    19        2         W     12     25        3         C     7.1    15    .....      .......      .......  

I want to group on type and then calculate weighted mean and weighted standard deviation.

There seem to be solution available for weighted mean (groupby weighted average and sum in pandas dataframe) but none for weighted standard deviation.

Is there a simple way to do it.

Spring boot application failing to start with The port may already be in use error

Posted: 08 Jul 2022 10:44 AM PDT

Spring boot application fails to start with below error. Tried with multiple ports but same issue

`org.springframework.boot.web.embedded.tomcat.ConnectorStartFailedException: Connector configured to listen on port 9330 failed to start  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.checkConnectorHasStarted(TomcatWebServer.java:232) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.checkThatConnectorsHaveStarted(TomcatWebServer.java:224) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:202) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:311) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:164) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]  at com.itm.test.xyz.report.XYZReportApiApplication.main(XYZReportApiApplication.java:15) [classes/:?]`  

Non Linear Seemingly Unrelated Regressions (SUR) in R imposing restrictions

Posted: 08 Jul 2022 10:44 AM PDT

I'm trying to estimate a non-linear Seemingly Unrelated Regressions (SUR) model with 5 equations in R, and I was working over the package systemfit. Everything goes well until it needs to set some restrictions on my equations. using the package systemfit, the function nlsystemfit() it works for non-linear equations. But the option/parameter restrict.matrix it's not allowed for nlsystemfit() (it works for linear equations in the function systemfit()).

A simplified example is (I think show the data is irrelevant here):

EQ_1 <- Y1 ~ (c1 - x)*Q + c11*G11 + c12*G12 + c13*G13  EQ_2 <- Y2 ~ (c2 - x)*Q + c21*G11 + c22*G12 + c23*G13  EQ_3 <- Y3 ~ (c3 - x)*Q + c31*G11 + c32*G12 + c33*G13      start.values <- c(c1 = 0,c2= 0,c3 = 0,                    c11 = 0,c12 = 0,c13 = 0,                    c21 = 0,c22 = 0,c23 = 0,                    c31 = 0,c32 = 0,c33 = 0)    model <- list(EQ_1 ,EQ_2 ,EQ_3)    model.sur <- nlsystemfit(method = "SUR",                           eqns = model,                           startvals = start.values,                           data = as.data.frame(dat))  

The estimation works perfectly so far. But now, I need to set the following constraints:

Rest_1 <- c11 + c12 + c13 = 0  Rest_2 <- c21 + c22 + c23 = 0  Rest_3 <- c31 + c32 + c33 = 0    Rest_4 <- c1 + c2 + c3 = -1  

Obviously, the model here is linear with 3 equations, but it's because I'm trying to simplify the idea. But the current model has 5 nonlinear equations and more parameters.

Anyone can guide me, please, about how to perform a Non-Linear SUR estimation with restrictions in R?

Thanks a lot in advance.

How to reliably detect a barcode's 4 corners?

Posted: 08 Jul 2022 10:45 AM PDT

I'm trying to detect this Code128 barcode with Python + zbar module:

(Image download link here).

This works:

import cv2, numpy  import zbar  from PIL import Image   import matplotlib.pyplot as plt    scanner = zbar.ImageScanner()  pil = Image.open("000.jpg").convert('L')  width, height = pil.size      plt.imshow(pil); plt.show()  image = zbar.Image(width, height, 'Y800', pil.tobytes())  result = scanner.scan(image)    for symbol in image:      print symbol.data, symbol.type, symbol.quality, symbol.location, symbol.count, symbol.orientation  

but only one point is detected: (596, 210).

If I apply a black and white thresholding:

pil = Image.open("000.jpg").convert('L')  pil = pil .point(lambda x: 0 if x<100 else 255, '1').convert('L')      

it's better, and we have 3 points: (596, 210), (482, 211), (596, 212). But it adds one more difficulty (finding the optimal threshold - here 100 - automatically for every new image).

Still, we don't have the 4 corners of the barcode.

Question: how to reliably find the 4 corners of a barcode on an image, with Python? (and maybe OpenCV, or another library?)

Notes:

  • It is possible, this is a great example (but sadly not open-source as mentioned in the comments):

    Object detection, very fast and robust blurry 1D barcode detection for real-time applications

    The corners detection seems to be excellent and very fast, even if the barcode is only a small part of the whole image (this is important for me).

  • Interesting solution: Real-time barcode detection in video with Python and OpenCV but there are limitations of the method (see in the article: the barcode should be close up, etc.) that limit the potential use. Also I'm more looking for a ready-to-use library for this.

  • Interesting solution 2: Detecting Barcodes in Images with Python and OpenCV but again, it does not seem like a production-ready solution, but more a research in progress. Indeed, I tried their code on this image but the detection does not yield successful result. It has to be noted that it doesn't take any spec of the barcode in consideration for the detection (the fact there's a start/stop symbol, etc.)

    import numpy as np  import cv2  image = cv2.imread("000.jpg")  gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  gradX = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 1, dy = 0, ksize = -1)  gradY = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 0, dy = 1, ksize = -1)  gradient = cv2.subtract(gradX, gradY)  gradient = cv2.convertScaleAbs(gradient)  blurred = cv2.blur(gradient, (9, 9))  (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)  kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))  closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)  closed = cv2.erode(closed, None, iterations = 4)  closed = cv2.dilate(closed, None, iterations = 4)  (_, cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  c = sorted(cnts, key = cv2.contourArea, reverse = True)[0]  rect = cv2.minAreaRect(c)  box = np.int0(cv2.boxPoints(rect))  cv2.drawContours(image, [box], -1, (0, 255, 0), 3)  cv2.imshow("Image", image)  cv2.waitKey(0)  

Seemingly unrelated regression in R with imputed data-Pooling results

Posted: 08 Jul 2022 10:45 AM PDT

I am trying to complete seemingly unrelated regressions (SUR) using the systemfit package in R. However, it is not straightforward to complete these analyses with multiply imputed data (with mice package).

Upon googling this question, I see that there was a deleted post about the identical question, which seems to have utilized the following example (credit to poster, minor edits)

library(systemfit)  library(mice)  nhanes2    r1 <- bmi ~ hyp     r2 <- bmi ~ age    system <- list( r1, r2 )    imp <- mice(nhanes2, m = 5)    m=5    completed=lapply(1:5,function(i)complete(imp,i))    fit.model <- systemfit(system, data= completed[[1]])  

Above yields complete systemfit output for each imputed dataset. This is great but I am left with the task of pooling the entire output generated by SUR.

I have also unsuccessfully attempted running the function in zelig:

  completed.mi=do.call(Zelig:mi,completed)    system=list(r1= bmi ~ hyp,r2=bmi~age)    z.out=zelig(formula= system,model="sur",data=completed.mi)    >Error: sur is not a supported model type.  

Finally, calling the long form of the imputed data yields large degrees of freedom which does not reflect the actual number of cases in each imputed dataset (example not included)

My questions are:

1) Does the systemfit package support SUR for MI data?

2) If so, are you able to pool the output across all imputed datasets?

3) Are there alternative package options (other than systemfit) for completing SUR in R?

4) If 3 is a no, is there a similar analysis that will accomplish the same objectives and is there a different package(e.g., rms) that might support the pooling of MI data?

How to get App Groups name programmatically?

Posted: 08 Jul 2022 10:45 AM PDT

I am using extensions in my application. For sharing data between app and extension using app Groups. How Can I read App Group name programmatically so no need create extra configuration file for storing App Group name.

WM_INPUT is defective

Posted: 08 Jul 2022 10:44 AM PDT

I want to catch the key presses send to my window in order to store each key's state, and distinguish between the left and right instances of the key, like WM_LCONTROL and WM_RCONTROL.

As WM_KEYUP/DOWN doesn't offer this functionality so i moved to raw input and i need to process WM_INPUT.

The problem is that the flags from the RAWKEYBOARD structure, described here, don't work as they should.

When i press right control key, RI_KEY_E0 is set which is for the left version of the key, and when i press left control, neither RI_KEY_E0 nor RI_KEY_E1 are set. The alt key performs similarly, but i can workaround this problem, since left still provides different flags form the right key.

But for the left and right shift keys there aren't any flags set to distinguish the left version from the right version, so i can't do anything about them.

The virtual key code given when WM_INPUT arrives doesn't distinguish between left and right keys, and using MapVirtualKey on the scan code doesn't work either.

What's up with this unorthodox behavior of raw inputs?

Some code:

Registering:

RAWINPUTDEVICE rid;  rid.usUsagePage=0x01;  rid.usUsage=0x06;  rid.dwFlags=0; // I also tried RIDEV_APPKEYS,RIDEV_NOHOTKEYS,RIDEV_NOLEGACY, none fixed the problem  rid.hwndTarget=hwnd;  if(!RegisterRawInputDevices(&rid,1,sizeof(RAWINPUTDEVICE)))      ExitError("Failed to register raw input device",true); //displays error and exits  

Processing WM_INPUT:

case WM_INPUT:  {          RAWINPUT rw;      UINT sz=sizeof(rw);      u_char vk; //used to make code shorter      USHORT flag; //used to make code shorter        if(!GetRawInputData((HRAWINPUT)lparam,RID_INPUT,&rw,&sz,sizeof(RAWINPUTHEADER)))          break;        vk=rw.data.keyboard.VKey;      flag=rw.data.keyboard.Flags;        ...Process flags and save the actual key pressed in vk...        keys[vk]=!(flag&RI_KEY_BREAK); //save key's state      break;  }  

No comments:

Post a Comment