Thursday, 31 December 2020

Modify python array using graphical interaction (Bokeh or Javascript)

I have a 1-D numpy array eg.[1,0,6,3,2,1,6,4,3,2,1] which I have been able to graphically display as a histogram graph using the Bokeh Library.

I would like to be able to modify the array using graphical interaction.

Ideally I would like to be able to shift 1 value at a time.

Any ideas would be appreciated :)

Example enter image description here

import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource, Grid, LinearAxis, Plot, Quad,Range1d,HoverTool, Panel, Tabs,Legend, LegendItem,FixedTicker,NumeralTickFormatter,SingleIntervalTicker
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.io import curdoc, show

#-------------------------------------------------------------------------------------------------
# Create Data
#-------------------------------------------------------------------------------------------------
Values= np.random.randint(0,6000,800)


def Values_to_Bins(Values, Bin_size): #takes in a numpy array of values, and the Bin size
    Min_Value = np.amin(Values)
    Max_Value =np.amax(Values)
    bins = np.arange(Min_Value, Max_Value + 2 * (Bin_size), Bin_size)  # Define bin edges
    hist_array, edges = np.histogram(Values,bins=bins)  # Create 2 arrays, hist_array represents the bin counts, edges is the bin edges
    return hist_array,edges #Returns the histogram array, and edges
# -------------------------------------------------------------------------------------------------
# STEP X: Create Bokeh Histogram Plots
# -------------------------------------------------------------------------------------------------
hist_array,edges = Values_to_Bins(Values, 15)

# Create a Pandas Dataframe 
Bokeh_Bins = pd.DataFrame({'Values': hist_array, 'left': edges[:-1], 'right': edges[1:]})
Max_Initial_Bin = Bokeh_Bins['Values'].max()

# Create a column showing the extent of each interval
Bokeh_Bins['Bin_interval'] = ['%d to %d ms' % (left, right) for left, right in
                              zip(Bokeh_Bins['left'], Bokeh_Bins['right'])]

tick_vals = np.arange(Max_Initial_Bin + 1)

# Convert dataframe to column data source
src1 = ColumnDataSource(Bokeh_Bins)

plot = figure(y_range=Range1d(start=0, end=Max_Initial_Bin), sizing_mode="scale_width", width=3000, height=600,
              title="Want to create interactive Histogram data",
              x_axis_label="Time (ms)",
              y_axis_label="Number of occurances")
plot.yaxis.ticker = FixedTicker(ticks=list(tick_vals))

plot.quad(bottom=0, top='Values', left='left',
          right='right', source=src1, fill_color="#939393",
          line_color='black', fill_alpha=0.5,
          hover_fill_alpha=1.0, hover_fill_color='#00E400')

# Add hover tool for when mouse is over data
hover1 = HoverTool(tooltips=[('Timeframe', '@Bin_interval'), ('Count', '@Values')])
plot.add_tools(hover1)
original_script, original_div = components(plot)
show(plot)


from Modify python array using graphical interaction (Bokeh or Javascript)

How to import a service-like singleton-class with System.js?

I have a Singleton-Class FooService that is loaded via an import-map. I'd like to (a)wait for it and use it in various async functions like so:

declare global {
  interface Window {
    System: System.Module
  }
}

const module = window.System.import('@internal/foo-service')
const fooService = module.FooService

async function func1() {
  await fooService.doBar()
  .
  .
}

async function func2() {
  await fooService.doBar2()
  .
  .
}

But I could only get it to work like this:

declare global {
  interface Window {
    System: System.Module
  }
}

async function getfooService() {
  const module = await window.System.import('@internal/foo-service')
  return module.FooService
}

function func1() {
  getfooService().then(fooService => fooService .doBar())
  .
  .
}

function func2() {
  getfooService().then(fooService => fooService.doBar2())
  .
  .
}

How can I achieve this without loading it anew every time I want to use it?



from How to import a service-like singleton-class with System.js?

Python - urllib3 cannot parse response if the server answers before the POST is complete

I'm using the requests (which uses urllib3) library to upload a file from a Python script. My backend starts by inspecting the headers of the request and if it doesn't comply with the needed prerequisites, it stops the request right away and respond with a valid 400 response.

This behavior works fine in Postman, or with Curl; i.e. the client is able to parse the 400 response even though it hasn't completed the upload and the server answers prematurely. However, while doing so in Python with requests/urllib3, the library is unable to process the backend response :

response : None
request : <PreparedRequest [POST]>
2020-12-29 11:53:25,316 - ERROR - ('Connection aborted.', ConnectionResetError(10054, 'Une connexion existante a dû être fermée par l’hôte distant', None, 10054, None))

Because the server answers before the transfer is complete, it considers that the connection has been aborted, even though the server DOES return a valid response.

Is there a way to avoid this and parse the response nonetheless ?

The code for sending the POST request is pretty standard (I tried without the MultipartEncoder, using vanilla requests, the result is the same) :

mpEncoder = MultipartEncoder(fields={'file': (open(args.filepath, 'rb'))})
headers = { "Authorization": "Bearer " + jwt_token }

try:
    r = requests.post(endpoint, headers=headers, data=mpEncoder, verify=False)
except requests.exceptions.RequestException as e:
    for property, value in vars(e).items():
        print(property, ":", value)
    fatal(str(e))


from Python - urllib3 cannot parse response if the server answers before the POST is complete

Regex module error when join a set of regex using pipes

For performance purposes, I've tried to transform a set of regexes into only one using pipes.

   self.regexes_token = [
                {'descricao':'site www.', 'regex': r'^www\.(.+?)$'},
                {'descricao':'apenas pontuacao','regex':r'^[[:punct:]]+?$'},
                {'descricao':'palavra com sinal negativo', 'regex': r'^(-)(.*?)$', 'grupo': r'\2'},
                {'descricao':'pronomes e títulos', 'regex': r'^(sra?|exm[º|°|o]|dr[a|ª]?|(v\.)?ex\.?(a|ª)\.?)\.??$'},
                {'descricao':'oab sigla', 'regex': r'^oab\/[a-z]{2}$'},
                {'descricao':'termos irrelevantes', 'regex': r'^(s\/n|e\/ou|e-?mail|cep|rj|tel\.?(\/fax|efone)?|anos?|rua|cpf|www)\.?$'},
                {'descricao':'chassi (VIN)', 'regex': r'^[A-Za-z0-9]{1}[A-Za-z]{2}[A-Za-z0-9]{9}[\d+]{5}$'},
                {'descricao':'data_br', 'regex': r'^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$'},
                {'descricao':'um char e ponto', 'regex': r'^\w[[:punct:]]$'},
                {'descricao':'rg','regex': r'^\d{2}\.\d{3}\.\d{3}-\d(\/.*)?'},
                {'descricao':'unidades de medidas', 'regex': r'^(\d{1,2},?x?)+(cm|m(l|²|2|m)?|k(g|m))$'},
                {'descricao':'zero seguido de qualquer coisa', 'regex': r'^0(.*)$'},
                {'descricao':'::punct:: seguido de qualquer coisa','regex':r'^[[:punct:]](.+?)$'},
                {'descricao':'telefone avulso', 'regex': r'^\d{4,5}-\d{4}$'},
                {'descricao':'ano', 'regex': r'\b(19|20)\d{2}\.?\b'},
                {'descricao':'contém char especial', 'regex': r'^.*?(~|\^|¿|¡|>|<|»|#|£|\?|»|·|#|\*|=|\+|¥|€|\||µ|®)+.*?$'}
            ]
            
            
            self.regexes_token_union = r'('+'|'.join([d['regex'] for d in self.regexes_token])+r')'
            print(self.regexes_token_union)

Follow regex:

(^www\.(.+?)$|^[[:punct:]]+?$|^(-)(.*?)$|^(sra?|exm[º|°|o]|dr[a|ª]?|(v\.)?ex\.?(a|ª)\.?)\.??$|^oab\/[a-z]{2}$|^(s\/n|e\/ou|e-?mail|cep|rj|tel\.?(\/fax|efone)?|anos?|rua|cpf|www)\.?$|^[A-Za-z0-9]{1}[A-Za-z]{2}[A-Za-z0-9]{9}[\d+]{5}$|^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^\w[[:punct:]]$|^\d{2}\.\d{3}\.\d{3}-\d(\/.*)?|^(\d{1,2},?x?)+(cm|m(l|²|2|m)?|k(g|m))$|^0(.*)$|^[[:punct:]](.+?)$|^\d{4,5}-\d{4}$|\b(19|20)\d{2}\.?\b|^.*?(~|\^|¿|¡|>|<|»|#|£|\?|»|·|#|\*|=|\+|¥|€|\||µ|®)+.*?$)

But when I've tried to run (compile), python's regex module (not re) got an error:

regex._regex_core.error: cannot refer to an open group at position 272

I've used notepad++ to see the "col" position but even then I was not able to detect what opened group is this

The "confusing thing" is that when I run each regex on a loop, it's works fine (but the performance is not good)

So, how can I fix this?



from Regex module error when join a set of regex using pipes

Blur Material UI Select component on the onChange event

By default MaterialUI's Select component leaves itself focused after selecting an option. This behaviour can be seen in all their examples in their docs

I would like the element to blur once something is selected. Here is what my code currently looks like:

const App = () => {
    const [selectedValue, setSelectedValue] = useState('')

    const selectElement = useRef(null);

  return (
        <Select
            native
            ref={selectElement}
            value={selectedValue}
            onChange={(evt) => {
                setSelectedValue(evt.target.value)

                // Attempt at blurring the element upon selection using the useRef:
                selectElement.current.blur(); // Nothing happens

                // Attempt at blurring the element upon selection using activeElement:
                document.activeElement.blur(); // Get Error: "Property 'blur' does not exist on type 'Element'."
            }}
        >
            <option value='option 1'>Option 1</option>
            <option value='option 2'>Option 2</option>
            <option value='option 3'>Option 3</option>
        </Select>
  );
};

As you can see in the code, I've tried to do this with two different methods I've found:

  • Through useRef(): this does nothing, no errors or anything, but does not blur my element

  • Through the document.activeElement: this gives me an error, apparently property blur does not exist on type element.

What is the proper method of blurring my Select component upon selecting an option?



from Blur Material UI Select component on the onChange event

How to perform an ND coordinate sweep using numpy meshgrid

I want to create a grid of sampling points in 4 dimensions. I want the points to span from 0 to 10 with equal spacing in each direction. I tried a few iterations of np.meshgrid calls, but I am sure I am doing something wrong. What is the proper way to create these points?

import numpy as np
XL, XU = (0, 10) # lower/upper bounds
MD = 4 # 4 dimensions
x = np.linspace(XL, XU, 20)
np.meshgrid(*[x for ____ in range(MD)])[0].reshape(MD, x.size ** MD // MD).T

array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       ...,
       [10., 10., 10., 10.],
       [10., 10., 10., 10.],
       [10., 10., 10., 10.]])

My current best solution has repeated elements, so I'm sure I must have done something wrong. What has gone wrong here? How can I create the desired grid of equally spaced points?



from How to perform an ND coordinate sweep using numpy meshgrid

react-scpring clean scale and translateX transition

The react-swipeable-views library is providing example usages. I want to reuse the coverflow example, but in a react functional comp. way. I almost managed to get it working. However on my implementation, the swipeable element can get stuck during swipe, if you swipe slowly (the scale is not applied anymore). See screenshot:

enter image description here

In the demo of react-swipeable views this is somehow not happening. This example is using react-spring for the animation transitions. I provided a stackblitz demo that is reproducable and maybe you can find out the issue.

component

const useStyles = makeStyles((theme) => ({
  root: {
    background: theme.palette.background.paper,
    padding: theme.spacing(0, 6),
  },
  img: {
    width: 180,
    height: 180,
    display: "block",
    marginBottom: theme.spacing(2),
  },
  container: {
    padding: theme.spacing(2),
    borderRadius: 4,
    justifyContent: "center",
    maxWidth: 320,
    margin: "auto",
  },
  slide: {
    padding: theme.spacing(3, 2),
    color: theme.palette.text.primary,
    alignItems: "center",
    justifyContent: "center",
    flexDirection: "column",
    display: "flex",
  },
}));

const albums = [
  {
    name: "Abbey Road",
    src: "https://picsum.photos/200/300",
  },
  {
    name: "Bat Out of Hell",
    src: "https://picsum.photos/200/300",
  },
  {
    name: "Homogenic",
    src: "https://picsum.photos/200/300",
  },
  {
    name: "Number of the Beast",
    src: "https://picsum.photos/200/300",
  },
  {
    name: "It's Blitz",
    src: "https://picsum.photos/200/300",
  },
  {
    name: "The Man-Machine",
    src: "https://picsum.photos/200/300",
  },
];

export function StatisticSelector() {
  const classes = useStyles();

  const [index, setIndex] = useState(0);
  const [props, start] = useSpring(() => ({
    from: { position: 0 },
  }));

  function handleChangeIndex(indexNum) {
    setIndex(indexNum);
  }

  function handleSwitch(index, type) {
    if (type === "end") {
      start({
        from: { position: props.position.value },
        to: { position: Math.round(index) },
      });
      return;
    }
    props.position.setValue(index);
  }

  function interpolatePositionProps(range, output) {
    return props.position.interpolate({
      range,
      output,
    });
  }

  return (
    <div className={classes.container}>
      <SwipeableViews
        index={index}
        className={classes.root}
        onChangeIndex={handleChangeIndex}
        onSwitching={handleSwitch}
        enableMouseEvents
      >
        {albums.map((album, currentIndex) => {
          const inputRange = albums.map((_, i) => i);
          const scale = interpolatePositionProps(
            inputRange,
            inputRange.map((i) => (currentIndex === i ? 1 : 0.7))
          ).interpolate((x) => `scale(${x})`);

          const opacity = interpolatePositionProps(
            inputRange,
            inputRange.map((i) => (currentIndex === i ? 1 : 0.3))
          );

          const translateX = interpolatePositionProps(
            inputRange,
            inputRange.map((i) => (100 / 2) * (i - currentIndex))
          ).interpolate((x) => `translateX(${x}px)`);

          const scaleAndTranslateX = interpolate(
            [scale, translateX],
            (scale, translateX) => `${scale} ${translateX}`
          );
          return (
            <animated.div
              key={String(currentIndex)}
              className={classes.slide}
              style={Object.assign({
                opacity,
                transform: scaleAndTranslateX,
              })}
            >
              <img className={classes.img} src={album.src} alt="cover" />
              <Button variant="contained" color="primary" size="small">
                Select
              </Button>
            </animated.div>
          );
        })}
      </SwipeableViews>
    </div>
  );
}


from react-scpring clean scale and translateX transition

Difference between similar FFT code on complete signal or averaged on chunks

I have 2 snippets of code that should produce the same result. In the first one, I consider a 2500 sample signal on 64 channels sampled at 500 Hz; and in the second one, I cut down this signal into 5 chunks of 500 samples each.

# Snippet 1: 
data = get_data() # data shape is 64x2500

# Snippet 2:
data = get_data().reshape(64, 5, 500) # data shape is 64x5x500

Where get_data() is just a placeholder for this example, feel free to generate a random sample array to use with both pieces of code. In practice, to test my code I duplicated a sample signal with copy.deepcopy().

My goal is to extract the frequency power on 2 bands by averaging on the band and across all 64 channels.

  • alpha: (8, 13) Hz
  • delta: (1, 4) Hz

To do so, I apply a fast Fourier transform through numpy: (The code below should work on both snippets with both array dimensions.)

import numpy as np

alpha = (8, 13)
delta = (1, 4)
fs = 500. # sampling frequency

frequencies = np.fft.rfftfreq(data.shape[-1], 1.0/fs)
alpha_band = np.where(np.logical_and(frequencies>=alpha[0], 
                                     frequencies<=alpha[1]))[0]
delta_band = np.where(np.logical_and(frequencies>=delta[0], 
                                     frequencies<=delta[1]))[0]

fftval = np.abs(np.fft.rfft(data, axis=-1) / fs)

The absolute values are then squared. Moreover, each of the 64 channels also has a weight applied to it via multiplication. To test the code, I set all weights to 1.

weights = np.ones(shape=(64,))
alpha = np.average(np.multiply(np.square(fftval[..., alpha_band]).T, weights))
delta = np.average(np.multiply(np.square(fftval[..., delta_band]).T, weights))

My understanding was that averaging across chunks should not change the result. Yet, here are the 2 outputs I got on the same data:

# Snippet 1, 64x2500
alpha
Out: 5.294149596657551e-13

delta
Out: 9.372696436349552e-13

alpha/delta
Out: 0.564848081084284

# Snippet 2: 64x5x500
alpha
Out: 6.326672955916193e-12

delta
Out: 7.584706602278469e-13

alpha/delta
Out: 8.34135489699185

Anyone knows what I am doing wrong here and why both results are completely different? What did I miss?



from Difference between similar FFT code on complete signal or averaged on chunks

Why am I unable to generate a query using relationships?

I'm experimenting with relationship functionality within SQLAlchemy however I've not been able to crack it. The following is a simple MRE:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey, Integer, create_engine
from sqlalchemy.orm import relationship, sessionmaker

Base = declarative_base()


class Tournament(Base):

    __tablename__ = "tournament"
    __table_args__ = {"schema": "belgarath", "extend_existing": True}

    id_ = Column(Integer, primary_key=True)
    tournament_master_id = Column(Integer, ForeignKey("belgarath.tournament_master.id_"))

    tournament_master = relationship("TournamentMaster", back_populates="tournament")


class TournamentMaster(Base):

    __tablename__ = "tournament_master"
    __table_args__ = {"schema": "belgarath", "extend_existing": True}

    id_ = Column(Integer, primary_key=True)
    tour_id = Column(Integer, index=True)

    tournament = relationship("Tournament", back_populates="tournament_master")


engine = create_engine("mysql+mysqlconnector://root:root@localhost/")
Session = sessionmaker(bind=engine)
session = Session()

qry = session.query(Tournament.tournament_master.id_).limit(100)

I was hoping to be able to query the id_ field from the tournament_master table through a relationship specified in the tournament table. However I get the following error:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Tournament.tournament_master has an attribute 'id_'

I've also tried replacing the two relationship lines with a single backref line in TournamentMaster:

tournament = relationship("Tournament", backref="tournament_master")

However I then get the error:

AttributeError: type object 'Tournament' has no attribute 'tournament_master'

Where am I going wrong?

(I'm using SQLAlchemy v1.3.18)



from Why am I unable to generate a query using relationships?

Next Js combined with an external REST API Authentication and atuhorization

I have already an application built with Node Js and express , also I have a front end using create-react-app with redux , but now I would like to move it to next Js, but I've got stuck because I do not the right way to authenticate and authorize using my rest Full API, I want to mention that my API already handle this using JWT (saving it within a cookie)



from Next Js combined with an external REST API Authentication and atuhorization

Using ES6 Module Libraries in Angular Applications and Unexpected token 'export' Error

I have a JavaScript library I'm trying use within an Angular application. It is written with ES6 syntax and exports itself as a module.

export { Q as Library };

Imported into Angular.json as you do:

architect: {
build: {
options: {
   "scripts": [
      "src/assets/libs/library.min.js"
   ]

Angular loads the script but with a module syntax error.

scripts.js:1 Uncaught SyntaxError: Unexpected token 'export'

Here is my tsconfig

compilerOptions
  "target": "es2020",
  "module": "es2020",
  "lib": [
      "es2020",
      "dom"
  ]

How can I properly import this module into the application?



from Using ES6 Module Libraries in Angular Applications and Unexpected token 'export' Error

Adjust array using graphic interaction (Bokeh)

I have a 1-D numpy array eg.[1,0,6,3,2,1,6,4,3,2,1] which I have been able to graphically display as a histogram graph using the Bokeh Library.

I would like to be able to modify the array using graphical interaction.

Ideally I would like to be able to shift 1 value at a time.

Any ideas would be appreciated :)

Example enter image description here

import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource, Grid, LinearAxis, Plot, Quad,Range1d,HoverTool, Panel, Tabs,Legend, LegendItem,FixedTicker,NumeralTickFormatter,SingleIntervalTicker
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.io import curdoc, show

#-------------------------------------------------------------------------------------------------
# Create Data
#-------------------------------------------------------------------------------------------------
Values= np.random.randint(0,6000,800)


def Values_to_Bins(Values, Bin_size): #takes in a numpy array of values, and the Bin size
    Min_Value = np.amin(Values)
    Max_Value =np.amax(Values)
    bins = np.arange(Min_Value, Max_Value + 2 * (Bin_size), Bin_size)  # Define bin edges
    hist_array, edges = np.histogram(Values,bins=bins)  # Create 2 arrays, hist_array represents the bin counts, edges is the bin edges
    return hist_array,edges #Returns the histogram array, and edges
# -------------------------------------------------------------------------------------------------
# STEP X: Create Bokeh Histogram Plots
# -------------------------------------------------------------------------------------------------
hist_array,edges = Values_to_Bins(Values, 15)

# Create a Pandas Dataframe 
Bokeh_Bins = pd.DataFrame({'Values': hist_array, 'left': edges[:-1], 'right': edges[1:]})
Max_Initial_Bin = Bokeh_Bins['Values'].max()

# Create a column showing the extent of each interval
Bokeh_Bins['Bin_interval'] = ['%d to %d ms' % (left, right) for left, right in
                              zip(Bokeh_Bins['left'], Bokeh_Bins['right'])]

tick_vals = np.arange(Max_Initial_Bin + 1)

# Convert dataframe to column data source
src1 = ColumnDataSource(Bokeh_Bins)

plot = figure(y_range=Range1d(start=0, end=Max_Initial_Bin), sizing_mode="scale_width", width=3000, height=600,
              title="Want to create interactive Histogram data",
              x_axis_label="Time (ms)",
              y_axis_label="Number of occurances")
plot.yaxis.ticker = FixedTicker(ticks=list(tick_vals))

plot.quad(bottom=0, top='Values', left='left',
          right='right', source=src1, fill_color="#939393",
          line_color='black', fill_alpha=0.5,
          hover_fill_alpha=1.0, hover_fill_color='#00E400')

# Add hover tool for when mouse is over data
hover1 = HoverTool(tooltips=[('Timeframe', '@Bin_interval'), ('Count', '@Values')])
plot.add_tools(hover1)
original_script, original_div = components(plot)
show(plot)


from Adjust array using graphic interaction (Bokeh)

Closing StreamConn with user input

I've been playing around with the API from alpaca.markets here https://github.com/alpacahq/alpaca-trade-api-python and I'm trying stream data but also let the user close the connection with an input however I'm not entirely sure how to go about this. I've included an attempt to exit with a keyboard interrupt which doesn't seem to work. Looking for someone to point me in the right direction.

EDIT: I've tried to do this with threading however I'm not sure if this is the right way to go about it as the endstream() doesn't seem to run.

What the code looks like.

import alpaca_trade_api as tradeapi
import threading

conn = tradeapi.StreamConn(xxx,xxx,xxx)

def streamorders(conn):
    t1 = threading.Thread(target = startstream(conn))
    t2 = threading.Thread(target = endstream(conn))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    return

def startstream(conn):
    @conn.on(r'trade_updates')
    async def on_msg(conn, data, symbols):
        print('Order completed')
    conn.run(['trade_updates'])

def endstream(conn):
    time.sleep(0.2)
    userAction = int(input('Press 0 to stop streaming: '))
    if userAction == 0:
        conn.close()
        return

streamorders(conn)

Ideally I want it to behave like this

order completed
order completed
order completed

Press 0 to exit: 


from Closing StreamConn with user input

Cordova camera plugin works on emulator and not on Android device

I'm building an app that allows the user to take photos. I'm using Cordova - JS/CSS/HTML - to write the script. The Cordova camera plugin works perfectly on my Android emulator in Android Studio, but I can't get it to work on my device. Here's what I'm working with:

IRL Phone: Samsung Galaxy S9+

Cordova version: 10.0.0

Cordova camera plugin version: 5.0.1

I'm not sure if it has something to do with an inconsistency in the build (I heard Cordova versions can have an effect on plugins in certain environments) or if my code is just not friendly for a real Android device.

Here's the plugin code:

let app = {
    init: function(){
        document.getElementById('btn').addEventListener('click', app.takephoto);
    },
    takephoto: function(){
        let opts = {
            quality: 80,
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.CAMERA,
            mediaType: Camera.MediaType.PICTURE,
            encodingType: Camera.EncodingType.JPEG,
            cameraDirection: Camera.Direction.BACK,
            targetWidth: 300,
            targetHeight: 400
        };

        navigator.camera.getPicture(app.ftw, app.wtf, opts);
    },
    ftw: function(imgURI){
        document.getElementById('msg').textContent = imgURI;
        document.getElementById('photo').src = imgURI;

    },
    wtf: function(msg){
        document.getElementById('msg').textContent = msg;
    }
};

document.addEventListener('deviceready', app.init);

Here's the HTML that runs the script on the app's page from the file camera.js in a separate folder:

        <div class="page">
            <p class="code"><img src="img/logo.png" alt="image" id="photo" /></p>
            <p class="node"><button id="btn">Take Picture</button></p>
            <p id="msg"></p>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script src="js/camera.js"></script>

And here's the information in my head tag:

<meta http-equiv="Content-Security-Policy" 
content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; 
style-src 'self' 'unsafe-inline'; 
media-src *; 
script-src 'self' 'unsafe-inline';
img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">

<link rel="stylesheet" type="text/css" href="css/index.css" /> 
<title>4-Point Inspection</title>

Maybe this has something to do with the XML or build files? This is my first Cordova app so I'm a little new to this. Any ideas why it works on emulator and not on my Galaxy S9+?



from Cordova camera plugin works on emulator and not on Android device

Android set orientation

I have an oddball android device which has no gravity sensors, and on that device, my "set orientaion" function does nothing. On all other devices, including some without sensors, the following works fine.

        Activity act = AndroidNativeUtil.getActivity();
    int newo = portrait 
        ?   (reverse
                ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
                : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
        :   (reverse
                ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    act.setRequestedOrientation(
            newo

is there some additional API that can lock the screen orientation? Some other apps successfully change the screen orientation.



from Android set orientation

How to create a layout like instagram explore with flexboxlayoutmaanger in android?

I am trying to create a recyclerview that shows images like instagram tab. I have checked this Implement Asymmetrical Grid Layout Manager Like Instagram Search question but it does not seem to do the work. I tried using gridlayoutmanager but I can't make it work. How to recreate it with google's FlexboxLayoutManager or a custom layout manager? Hope you'll answer. Regards.



from How to create a layout like instagram explore with flexboxlayoutmaanger in android?

Wednesday, 30 December 2020

Where to find list of algorithms implemented in TensorFlow, PyTorch and Keras?

According to TensorFlow Github,

TensorFlow is an end-to-end open source platform for machine learning.

It's possible to use algorithm like SVM with tf.contrib.learn.SVM, Linear Regression with a simple neuron, clustering with Keras...
I'd like to know which algo are available.

Is there something like the ScikitLearn list of algo available for TensorFlow, Keras, and PyTorch? (although the last is more focused on DNN)



from Where to find list of algorithms implemented in TensorFlow, PyTorch and Keras?

Find Guild ID in on_ready() - Discord.py

I'm trying to make a random fact send at certain times. It worked before I implemented a dict to hold the random facts and it should work now, the only problem I'm running into is I can't access the guild id inside the on_ready() event. Here is what I have:

    async def func(self):
        await bot.wait_until_ready()
        with open('guild_settings.json', 'r') as file:
            guild_settings = json.loads(file.read())
------> guild_id = NEED
        channel = bot.get_channel(guild_settings[guild_id]["random_facts_channel_id"])

        random_messages = guild_settings[guild_id]["random_facts"].value()
        if random_messages is None:
            await channel.send(f"You need to add a fact first.")
        else:
            random_messages = random.choice(random_messages)
            await channel.send(random_messages)

    @commands.Cog.listener()
    async def on_ready(self):
        print("Bot is ready.")

        # Initializing scheduler
        scheduler = AsyncIOScheduler()

        # Sends "Your Message" at given times
        scheduler.add_job(self.func, CronTrigger(hour="0, 19", minute="10", second="0"))

        # Starting the scheduler
        scheduler.start()

If I were able to somehow get the guild id then I'm sure it would work correctly, I just can't figure out how.



from Find Guild ID in on_ready() - Discord.py

How to make menu hierarchy flowchart in ReactJS or HTML/CSS

Figma design

Hey, I am working on a side project in which I am want to develop react component which can render details on the top bar with text and two buttons and at the bottom, a hierarchy styled like a menu flowchart. each menu option will expand further when the plus sign is clicked and collapse when the minus sign is clicked.

I am a beginner in reactjs, I don't know how to render such components, can you help me here?

If you have any question please comment below and thanks in advance.



from How to make menu hierarchy flowchart in ReactJS or HTML/CSS

ERROR: (gcloud.beta.ai-platform.versions.create) argument VERSION: Must be specified

When deploying a model to GCP Predict using Custom Prediction Routine, I'm getting this error:

ERROR: (gcloud.beta.ai-platform.versions.create) argument VERSION: Must be specified.

However, I do have the version specified like this:

!gcloud beta ai-platform versions create $VERSION_NAME_ \
    --model=$MODEL_NAME \
    --runtime-version=1.15 \
    --python-version=3.7 \
    --config=$CONFIG \
    --origin=gs://$BUCKET_NAME/$PROJECT_NAME/$VERSION_NAME/model/ \
    --package-uris=gs://$BUCKET_NAME/$PROJECT_NAME/$VERSION_NAME/$PACKAGE_NAME \
    --prediction-class predict.Predictor

Also, see image below:

enter image description here

I tried upgrading the sdk to the latest version, but still getting the same error. Version info:

enter image description here



from ERROR: (gcloud.beta.ai-platform.versions.create) argument VERSION: Must be specified

How to complete a Kotlin Flow in Android Worker

I'm investigating the use of Kotlin Flow within my current Android application

My application retrieves its data from a remote server via Retrofit API calls.

Some of these API's return 50,000 data items in 500 item pages.

Each API response contains an HTTP Link header containing the Next pages complete URL.

These calls can take up to 2 seconds to complete.

In an attempt to reduce the elapsed time I have employed a Kotlin Flow to concurrently process each page of data while also making the next page API call.

My flow is defined as follows:

private val persistenceThreadPool = Executors.newFixedThreadPool(3).asCoroutineDispatcher()
private val internalWorkWorkState = MutableStateFlow<Response<List<MyPage>>?>(null)
private val workWorkState = internalWorkWorkState.asStateFlow()

private val myJob: Job

init {
    myJob = GlobalScope.launch(persistenceThreadPool) {
        workWorkState.collect { page ->
            if (page == null) {
            } else managePage(page!!)
        }
    }
}

My Recursive function is defined as follows that fetches all pages:-

    private suspend fun managePages(accessToken: String, response: Response<List<MyPage>>) {
        when {
            result != null -> return
            response.isSuccessful -> internalWorkWorkState.emit(response)
            else -> {
                manageError(response.errorBody())
                result = Result.failure()
                return
            }
        }

        response.headers().filter { it.first == HTTP_HEADER_LINK && it.second.contains(REL_NEXT) }.forEach {
            val parts = it.second.split(OPEN_ANGLE, CLOSE_ANGLE)
            if (parts.size >= 2) {
                managePages(accessToken, service.myApiCall(accessToken, parts[1]))
            }
        }
    }

   private suspend fun managePage(response: Response<List<MyPage>>) {
        val pages = response.body()

        pages?.let {
            persistResponse(it)
        }
    }

    private suspend fun persistResponse(myPage: List<MyPage>) {
        val myPageDOs = ArrayList<MyPageDO>()

        myPage.forEach { page ->
            myPageDOs.add(page.mapDO())
        }

        database.myPageDAO().insertAsync(myPageDOs)
    }
    

My numerous issues are

  1. This code does not insert all data items that I retrieve

  2. How do complete the flow when all data items have been retrieved

  3. How do I complete the GlobalScope job once all the data items have been retrieved and persisted

UPDATE

By making the following changes I have managed to insert all the data

 private val persistenceThreadPool = Executors.newFixedThreadPool(3).asCoroutineDispatcher()
    private val completed = CompletableDeferred<Int>()

    private val channel = Channel<Response<List<MyPage>>?>(UNLIMITED)
    private val channelFlow = channel.consumeAsFlow().flowOn(persistenceThreadPool)

    private val frank: Job

    init {
        frank = GlobalScope.launch(persistenceThreadPool) {
            channelFlow.collect { page ->
                if (page == null) {
                    completed.complete(totalItems)
                } else managePage(page!!)
            }
        }
    }


...
...
...

   channel.send(null)
   completed.await()

   return result ?: Result.success(outputData)

I do not like having to rely on a CompletableDeferred, is there a better approach than this to know when the Flow has completed everything?



from How to complete a Kotlin Flow in Android Worker

Angular - child component in new window has very odd behavior

I am using Angular's DomPortalHost to open a child component in a new browser window. It works for the most part.

Here is a working prototype of opening a child component in a new browser window:

https://stackblitz.com/edit/portalfun

I am able to pass data to and from child component - works great right?

Well.. when I have let's say, a map in the new browser window some of the map events seem to think the map exists in the original browser window. See:

https://www.youtube.com/watch?v=0387htqmXZw

Hilarity ensues:

I am able to click once in the new window to begin a selection (circle, etc) but not a second time. Using the view +/- controls seems to work fine. However, the map navigation/scrolling and the selection events think they are controlled from the original window.

The endgame for this little innovation project was to pass whatever data was selected in the map (syncing the grid you see in the background and the map). This works fine when I don't use DomPortalHost (that area below the grid is where the map was originally and it functions correctly).

I think that because *cdkPortal (see the stackblitz) is technically inside the original browser window:

      <window *ngIf="showPortal" [dataFromParent]="dataToPass" (dataToParent)="this.onClosed($event)">
      </window>
selector: 'window'...
    <ng-container *cdkPortal>
      <h1>Child component window</h1>
      <div></div>
      <a class="btn-3" (click)="emitClose()">Close Window</a>
      You can also close the parent window, or navigate to a different route. It will close the child window component.
    </ng-container>

It's as if the map think's its been rendered in the original window, but is actually rendered in the new window.

The closest.. discovery that i've had was that some of the event handling has a [[Scope]] property set to the original window - not the new on. However I don't see how to overwrite this property.

If anyone has any ideas, resources, or a better approach please feel free to let me know. This is part of an innovation sprint, but it'd be cool to get it working as i'm sure it has a lot of use cases for others out there.

Imagine opening child components and passing data back and forth in new windows.. and not having to run a second cloned app or deal with the overheard of state management :)



from Angular - child component in new window has very odd behavior

How to create custom eval metric for catboost?

Similar SO questions:

Catboost tutorials

Question

In this question, I have a binary classification problem. After modelling we get the test model predictions y_pred and we already have true test labels y_true.

I would like to get the custom evaluation metric defined by following equation:

profit = 400 * truePositive - 200*fasleNegative - 100*falsePositive

Also, since higher profit is better I would like to maximize the function instead of minimize it.

How to get this eval_metric in catboost?

Using sklearn

def get_profit(y_true, y_pred):
    tn, fp, fn, tp = sklearn.metrics.confusion_matrix(y_true,y_pred).ravel()
    loss = 400*tp - 200*fn - 100*fp
    return loss

scoring = sklearn.metrics.make_scorer(get_profit, greater_is_better=True)

Using catboost

class ProfitMetric(object):
    def get_final_error(self, error, weight):
        return error / (weight + 1e-38)

    def is_max_optimal(self):
        return True

    def evaluate(self, approxes, target, weight):
        assert len(approxes) == 1
        assert len(target) == len(approxes[0])

        approx = approxes[0]

        error_sum = 0.0
        weight_sum = 0.0

        ** I don't know here**

        return error_sum, weight_sum

Question

How to complete the custom eval metric in catboost?

UPDATE

My update so far

import numpy as np
import pandas as pd
import seaborn as sns
import sklearn

from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split

def get_profit(y_true, y_pred):
    tn, fp, fn, tp = sklearn.metrics.confusion_matrix(y_true,y_pred).ravel()
    profit = 400*tp - 200*fn - 100*fp
    return profit


class ProfitMetric:
    def is_max_optimal(self):
        return True # greater is better

    def evaluate(self, approxes, target, weight):
        assert len(approxes) == 1
        assert len(target) == len(approxes[0])

        approx = approxes[0]

        y_pred = np.rint(approx)
        y_true = np.array(target).astype(int)

        output_weight = 1 # weight is not used

        score = get_profit(y_true, y_pred)
 
        return score, output_weight

    def get_final_error(self, error, weight):
        return error


df = sns.load_dataset('titanic')
X = df[['survived','pclass','age','sibsp','fare']]
y = X.pop('survived')

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=100)


model = CatBoostClassifier(metric_period=50,
  n_estimators=200,
  eval_metric=ProfitMetric()
)

model.fit(X, y, eval_set=(X_test, y_test)) # this fails


from How to create custom eval metric for catboost?

Why is an AppBar adding squares to the background of my app bar

Good afternoon all,

I have been digging at this for a long time now and its time to ask for help.

I have the following AppBar

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/app_bar_constrained"
    android:layout_height="?android:attr/actionBarSize"
    android:layout_width="match_parent"
    android:background="@drawable/appbar_background_2020">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/campfire_app_bar"
        android:elevation="0dp"
        android:background="@drawable/appbar_background_2020"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@id/app_bar_constrained">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/app_bar_collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="0dp">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar_2020"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:elevation="0dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="Title" />

                    <TextView
                        android:id="@+id/toolbar_sub_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:visibility="gone"
                        tools:text="Subtitle"
                        tools:visibility="visible" />
                </LinearLayout>
            </androidx.appcompat.widget.Toolbar>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_bar_campfire"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fadingEdge="horizontal"
            android:fadingEdgeLength="24dp"
            android:visibility="gone"
            android:layout_marginStart="24dp"
            android:layout_marginEnd="@24dp"
            android:elevation="0dp"
            android:background="@drawable/appbar_background_2020"
            app:tabBackground="?attr/selectableItemBackground"
            app:tabGravity="start"
            app:tabIndicator="@drawable/tab_indicator"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="1dp"
            app:tabMode="scrollable"
            app:tabPaddingEnd="2dp"
            app:tabPaddingStart="2dp"
            app:tabRippleColor="@color/colorAccent"
            app:tabSelectedTextColor="@color/colorAccent"
            app:tabTextAppearance="@style/Widget.AppCompat.Light.ActionBar.TabBar"
            app:tabTextColor="@color/colorPrimary"
            tools:layout_conversion_absoluteHeight="48dp"
            tools:layout_conversion_absoluteWidth="0dp" />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

The layout renders with a little artifact on the bottom right and bottom left of the toolbar, where the background is curved and clipped:

Toolbar

I've previously gotten rid of this by using a ConstraintLayout instead of an AppBar, but that does not work with the animations of a CoordinatorLayout, which we are keen to use.

Please note: due to corporate considerations I have had to change colours in the above code sample.



from Why is an AppBar adding squares to the background of my app bar

Async python smooth latency

I'm using grpc.aio.server and I stuck with problem that if I try to make load test on my service it will have some requests lagged for 10 seconds, but requests are similar, load is stable (200rps) and latency of almost requests is almost same. I'm ok with more high, but stable latency, I tried to google something like async task priority, in my mind it means that something wrong with priority of tasks which wait very long time, but they're finished or whole request task is waiting to start long time.

e.g 1000 requests were sent to gRPC service, they have the same logic to execute, the same db instance, the same query to this db, the same time to get results from db, etc, everything is the same. I see that e.g. 10th request latency is 10 seconds, but 13th request latency is 5 seconds. Also I can see in logs that db query have almost the same execution time.

Any suggestions? maybe I understand something wrong



from Async python smooth latency

ngx-cookie gets deleted when link is opened in new tab in Angular Application

I have used a PC to serve the website via LAN connection and use the IPv4 address instead of localhost for hosting the website on LAN.

I'm using the npm package http-server for serving the dist compiled angular application.

I'm using ngx-cookie-service for manipulating cookies in the angular application. However, the cookies get deleted when I open a page via router.navigate(). Also, the cookies don't always get deleted as well.

So I'm kinda confused as to what is happening with the cookies. What can be the reason?

P.S. I have not implemented session in the backend though.

Angular version : 10+ ngx-cookie-service: 11.0.2 browser: chrome 86



from ngx-cookie gets deleted when link is opened in new tab in Angular Application

Compiling gettext locales with PyInstaller in Python 3.x

I was compiling a gettext localized (English and French, but probably more in the future) Python script with pyinstaller --onefile palc.py and it works perfectly — except when I try to run it it attempts to use the locales stored in the locales directory (meaning it can't find them if I don't distribute the package with the locales directory). As you can imagine, this is a major drawback and pretty much ruins the point of PyInstaller — in order to distribute it, I have to give a directory along with the package in order for it to work — though, as I'm going to show you, it doesn't work even with that.

Here is the main question:

Is it possible (preferably not too difficult or something that would require heavy rewriting) to make PyInstaller compile the Python script WITH the gettext locales?

EDIT: I tried editing my palc.spec, here is the new version:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['palc.py'],
             pathex=['~/python-text-calculator'],
             binaries=[],
             datas=[('~/python-text-calculator/locales/*', 'locales')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='palc',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='palc')

And here is the output of the compiled package:

                                   Type 'help' to know the ton of Elive features available...
>>> ./palc
--------------------------------------------------------------------------
                          Language Selection
--------------------------------------------------------------------------
1 - English // Anglais
2 - Francais // French
Type: 1
Traceback (most recent call last):
  File "/Users/computer/python-text-calculator/palc.py", line 30, in <module>
    l_translations = gettext.translation('base', localedir='locales', languages=["en"])
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/gettext.py", line 514, in translation
    raise OSError(ENOENT, 'No translation file found for domain', domain)
FileNotFoundError: [Errno 2] No translation file found for domain: 'base'
[19393] Failed to execute script palc

This is the exact same output as it was without editing the palc.spec. Plus, it made the compiled package a directory (I ran ./palc inside the palc directory in dist), so I would still have to distribute a directory. What I need is a SINGLE FILE like the ones found here.

Can anyone help? Thanks! :D



from Compiling gettext locales with PyInstaller in Python 3.x

TransactionTooLargeException when opening pick image intent (Kotlin, Android)

I'm currently working on my app, and I have a fragment where the user can pick an image to insert into a database. This works, but when I go on my edit fragment and try to use the same code again to pick an image, I get an TransactionTooLargeException.

Error

2020-12-26 23:23:45.919 3837-3837/com.google.gradient.red E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.google.gradient.red, PID: 3837
    java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 7007624 bytes
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: android.os.TransactionTooLargeException: data parcel size 7007624 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(BinderProxy.java:526)
        at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4561)
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
        at android.os.Handler.handleCallback(Handler.java:888) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8178) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 

What I've tried

I saw some solutions saying to put something like this into my code that triggers with the image intent, but I still get the same error. I might be doing something wrong, but I'm not sure.

override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.clear()
    }

Code

Here is my OnCreateView where I set the setOnClickListener:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_update, container, false)

        // Set menu
        setHasOptionsMenu(true)

        // Get bitmap
        bitmap = args.currentItem.image

        view.current_title_et.setText(args.currentItem.title)
        view.current_description_et.setText(args.currentItem.description)
        view.current_mood_spinner.setSelection(mSharedViewModel.parseMood(args.currentItem.mood))
        view.current_mood_spinner.onItemSelectedListener = mSharedViewModel.listener

        // Opens gallery when image button clicked, gets image
        view.current_image_et.setOnClickListener {
            readStorageTask()
            //Intent to pick image
            val intent = Intent(Intent.ACTION_PICK)
            intent.type = "image/*"
            startActivityForResult(intent, 1001)
        }

        return view
    }

I'm trying to make it so that users can edit images in my app and don't know how I would solve this bug, so any help would be greatly appreciated!



from TransactionTooLargeException when opening pick image intent (Kotlin, Android)

Call Dart method from JS in Flutter Web

For a Webview relate feature I had used Method channels to open Webview through Native Android & iOS code and I am opening a website in it, I was getting callback from JS to native code in mobile platforms. For Android I was providing a class whose method is getting called from JS, It looks something like this:

webView.settings.javaScriptEnabled = true
webView.addJavascriptInterface(WebAppInterface(this), "nativeCommunicator")

webView.loadUrl("SOME_URL")

…

class WebAppInterface(private val mContext: Activity) {
  @JavascriptInterface
  fun postMessage(text: String) {
    println("WebAppInterface.message($text)")
    //send back to flutter
  }
}

Which seems quite straightforward way to get callback from js to my code.

Now I am trying to do this in Flutter Web I opened the website by calling

import 'package:js/js.dart';
...
js.context.callMethod('open', 'SOME_URL', '_self');

Which works fine, Now I am trying to get callback by creating this class

@JS()
library native_communicator;

import 'package:js/js.dart';
@JS('nativeCommunicator')
class NativeCommunicator{

 @JS('postMessage')
 external static set _postMessage(void Function(String text) f);


 @JS()
 external static void postMessage();

}

void setJSCallbackFunction(void Function(String text) postMessageFunction) {
 NativeCommunicator._postMessage = allowInterop(postMessageFunction);
}

I am calling setJSCallbackFunction and passing my function as param, but it keeps giving me runtime error that It can not find ‘postMessage’, I tried some other way which leads to can not find ‘nativeCommunicator’ error, Can anyone point out how to do this right? I need to call a dart method from js.



from Call Dart method from JS in Flutter Web

Poincare embeddings: building transitive closures from WordNet

I'd like to replicate Figure 2 in Poincaré Embeddings for Learning Hierarchical Representations, namely: Poincare embeddings from the "mammal" subtree of WordNet.

First, I construct the transitive closure needed to represent the graph. Following these docs and this SO answer, I do the following to construct the relations:

from   nltk.corpus import wordnet as wn

root    = wn.synset('mammal.n.01')
words   = list(set([w for s in root.closure(hyponyms) for w in s.lemma_names()]))
rname   = root.name().split('.')[0]
closure = [(word, rname) for word in words]

Then I am using Gensim's Poincare model to compute the embeddings. Given the example relations in Gensim's documentation, e.g.

relations = [('kangaroo', 'marsupial'), ('kangaroo', 'mammal'), ('gib', 'cat')]

I infer that the hypernym needs to be to the right. Here is the model fitting code:


from   gensim.models.poincare import PoincareModel
from   gensim.viz.poincare import poincare_2d_visualization

model = PoincareModel(relations, size=2, negative=0)
model.train(epochs=50)

fig = poincare_2d_visualization(model, relations, 'WordNet Poincare embeddings')
fig.show()

However, the result is obviously not correct in that it looks nothing like the paper. What am I doing wrong?

Poincare embeddings



from Poincare embeddings: building transitive closures from WordNet

embed plotly in excel

i'm trying to embed an interactive plotly (or bokeh) plot into excel.

To do this I've tried:

  1. embed a Microsoft Web Browser UserForm into excel, following:

How do I embed a browser in an Excel VBA form?

This works and enables both online and offline html to be loaded

  1. creating a plotly html

'''

import plotly
import plotly.graph_objects as go


x = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0]
y = [i**2 for i in x]

fig = go.Figure()

fig.add_trace(go.Scatter(x=x, y=x, mode='markers', name="y=x", marker=dict(color='royalblue', size=8)))
fig.add_trace(go.Scatter(x=x, y=y, name="y=x^2", line=dict(width=3)))

plotly.offline.plot(fig, filename='C:/Users/.../pythonProject/test1.html')
  1. repointing the webbrowser object in excel using .Navigate to the local plotly.html. Banner pops up with

".... restricted this file from showing active content that could access your computer"

clicking on the banner, i run into this error:

enter image description here

The same HTML can be opened in a browser.

Anyway to show interactive plots in excel?

Many thanks,

dusio



from embed plotly in excel

Inheritance method "virtual" in extend schema mongoose doesn't work

I have below method in model UserSchema:

userSchema.virtual('password')
    .set(function(password) {
            this._password = password;
            this.salt = this.makeSalt();
            this.hashed_password = this.encryptPassword(password);
    })
    .get(function() {
            return this._password;
    }
);

but when I create new teacher I have error hashed_password is required. So I think that method virtual from userSchema isn't inheritance in teacherSchema. How can I set that teacherSchema inheritances method like virtual from userSchema?

model

const mongoose = require('mongoose');
extend = require('mongoose-extend-schema');
const Schema = mongoose.Schema;

const userSchema = new Schema({
    name: {
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    },
    surname: {
        type: String,
        trim: true,
        required: true,
        maxLength: 32
    },
    email: {
        type: String,
        unique: true,
        trim: true,
        required: true,
        lowercase: true
    },
hashed_password: {
        type: String,
        required: true
    },
    initials: String
});

userSchema.virtual('password')
    .set(function(password) {
            this._password = password;
            this.salt = this.makeSalt();
            this.hashed_password = this.encryptPassword(password);
    })
    .get(function() {
            return this._password;
    }
);



const studentSchema = extend(userSchema, {
    teachers: []   
});

const teacherSchema = extend(userSchema, {
    isActiveTutor: {
        type: Boolean,
        default: false
    }
})
  


const User =  mongoose.model('User', userSchema);
const Student = mongoose.model('Student', studentSchema);
const Teacher = mongoose.model('Teacher', teacherSchema);

module.exports = {
    User,
    Student,
    Teacher
}


from Inheritance method "virtual" in extend schema mongoose doesn't work

Getting wrong window height in chrome

Hi I'm trying to get the correct width and height of the browser window in Chrome. The size is correct in Firefox, I have not tried any other browsers.

I have set the doctype to "!DOCTYPE html", and have tried $( window ).height(), $( window ).width() and window.innerHeight, window.innerWidth and all give the wrong values.

I'm running openSuse tumbleweed and the version of chrome is "Version 86.0.4240.75 (Official Build) (64-bit)"

Updated chrome to "Version 87.0.4280.88 (Official Build) (64-bit)" this did not help either, still the wrong size.

I have included a couple in images that show this, NOTICE: the DOMRect values are not correct in chrome but are correct in firefox. Sorry, you may need to zoom in to see the values. Below is what I'm logging in the two images.

console.log(
    $('#primary')[0].getBoundingClientRect(),
    $( window ).width(),
    window.innerWidth,
    $( window ).height(),
    window.innerHeight
)

I need precise sizes as to have things line up correctly. You can see in the firefox image below the background color "red" just above the horizontal scrollbar. I need to know the correct dimensions of the window so I can do the math and make things line up.

Is a another way more accurate to do this?

 Wrong size in chrome.

Right size in firefox

The image below is what I got from the jsfiddle suggested in the comment below form "FSDford". You can see in the console the size 877 in red "need to zoom in to see it". The ruler says it's width is 856 though! Once again Firefox has the correct width.

JSFiddle wron size

JSFiddle right size



from Getting wrong window height in chrome

android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): cancelRequest:473: Camera device no longer alive

Running my app, I can see the perfect Camera View, now I open the inbuilt Camera app and again switch to my own app (here, I am getting only black screen)

Log

2020-12-26 16:57:45.696 16445-16768/app.video I/CameraManagerGlobal: Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_IDLE for client app.video API Level 2
2020-12-26 16:57:45.883 16445-16768/app.video I/CameraManagerGlobal: Camera 0 facing CAMERA_FACING_BACK state now CAMERA_STATE_CLOSED for client app.video API Level 2
2020-12-26 16:57:45.886 16445-16596/app.video E/CameraCaptureSession: Session 0: Exception while stopping repeating: 
    android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): cancelRequest:473: Camera device no longer alive
        at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:1127)
        at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:97)
        at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1151)
        at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:526)
        at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:737)
        at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:244)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.os.HandlerThread.run(HandlerThread.java:67)
     Caused by: android.os.ServiceSpecificException: cancelRequest:473: Camera device no longer alive (code 4)
        at android.os.Parcel.createException(Parcel.java:2102)
        at android.os.Parcel.readException(Parcel.java:2056)
        at android.os.Parcel.readException(Parcel.java:2004)
        at android.hardware.camera2.ICameraDeviceUser$Stub$Proxy.cancelRequest(ICameraDeviceUser.java:676)
        at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:95)
        at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1151) 
        at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:526) 
        at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:737) 
        at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:244) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.os.HandlerThread.run(HandlerThread.java:67) 
2020-12-26 16:57:45.886 16445-16596/app.video I/org.webrtc.Logging: Camera2Session: Stop internal
2020-12-26 16:57:45.887 16445-16596/app.video I/org.webrtc.Logging: SurfaceTextureHelper: stopListening()
2020-12-26 16:57:45.887 16445-16468/app.video W/app.video: Long monitor contention with owner CaptureThread (16596) at boolean android.os.BinderProxy.transactNative(int, android.os.Parcel, android.os.Parcel, int)(BinderProxy.java:-2) waiters=0 in void android.hardware.camera2.impl.CameraDeviceImpl$CameraDeviceCallbacks.onCaptureStarted(android.hardware.camera2.impl.CaptureResultExtras, long) for 381ms
2020-12-26 16:57:45.890 16445-16596/app.video I/org.webrtc.Logging: Camera2Session: Stop done
2020-12-26 16:57:45.890 16445-16596/app.video I/org.webrtc.Logging: CameraCapturer: Stop capture
2020-12-26 16:57:45.890 16445-16596/app.video I/org.webrtc.Logging: CameraCapturer: Stop capture: Nulling session
2020-12-26 16:57:45.891 16445-16596/app.video I/org.webrtc.Logging: CameraCapturer: Stop capture done
2020-12-26 16:57:45.891 16445-16596/app.video I/org.webrtc.Logging: Camera2Session: Camera device closed.
2020-12-26 16:57:45.892 16445-16596/app.video I/org.webrtc.Logging: Camera2Session: Stop camera2 session on camera 0
2020-12-26 16:57:45.892 16445-16768/app.video I/CameraManagerGlobal: Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_OPEN for client com.sec.android.app.camera API Level 1
2020-12-26 16:57:46.274 16445-16839/app.video I/CameraManagerGlobal: Camera 1 facing CAMERA_FACING_FRONT state now CAMERA_STATE_ACTIVE for client com.sec.android.app.camera API Level 1

XML

<com.src.webrtc.android.VideoView
     android:id="@+id/main_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />

CODE

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    viewModel = ViewModelProvider(
        this,
        ViewModelFactory.getInstance(application)
    ).get(MainViewModel::class.java)

    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    binding.viewModel = this.viewModel
    binding.lifecycleOwner = this

    binding.apply {
        peerViews.add(mainView)
        peerViews.add(subView1)
        for (i in viewRenderers.indices) {
            viewRenderers[i].setTarget(peerViews[i])
        }
    }

    viewModel.room.observe(this, Observer {
        Log.d(TAG, "observe room $it")
        if (it != null) {
            binding.mainView.apply {
                init(null)
                setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT)
                setEnableHardwareScaler(false)
            }
            initSubView(it, binding.subView1)
        } else {
            binding.mainView.clearImage()
            binding.subView1.clearImage()
            Handler().postDelayed({
                binding.mainView.release()
                binding.subView1.release()
            }, 100)
        }
    })

}


from android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): cancelRequest:473: Camera device no longer alive

Tuesday, 29 December 2020

android oreo get incoming call number in service

I am implementing code to get incoming call number in android Oreo. For that I am using broadcast receiver in service. Here is the code that I have done so far.

public class CallService extends Service {
private static final int ID_SERVICE = 101;
BroadcastReceiver brSms;

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                System.out.println("incomingNumber : "+incomingNumber);
                Toast.makeText(context,""+incomingNumber,Toast.LENGTH_LONG).show();
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();



    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.PHONE_STATE");
    brSms = new MyReceiver();
    registerReceiver(brSms, filter);




    // Create the Foreground Service
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String channelId = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? createNotificationChannel(notificationManager) : "";
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(PRIORITY_MIN)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .build();

    startForeground(ID_SERVICE, notification);
}

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(NotificationManager notificationManager){
    String channelId = "my_service_channelid";
    String channelName = "My Foreground Service";
    NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
    channel.setImportance(NotificationManager.IMPORTANCE_NONE);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    notificationManager.createNotificationChannel(channel);
    return channelId;
}
@Override
public void onDestroy() {
    unregisterReceiver(brSms);

 }
}

The code is not working. I tried various options given in stack overflow. I have added permission for foreground service in my manifest file. How can I get incoming call number even if the app is closed or killed by user?



from android oreo get incoming call number in service

D3.js v3 Dot Plot Histogram Duplicating Values

I am building a dot plot histogram with d3.js v3 and I have pretty much finished everything up - except for whatever reason some of my data points are duplicating (certain circles repeating themselves - not all of them, just some). I tried tweaking the axis parameters, as well as the data itself [deleted rows with null values, etc]- however sadly to no avail.

Any help would be immensely appreciated.

Here's my relevant code:

<div id="dotHappy"></div>

var data = d3.csv('happy_dot_modified.csv', function(data) {

 data.forEach(function(d) {
    d["city"] = d["city"];
    d["Happy"] = +d["Happy"];
    d["thc"] = +d["thc"];
 });

 var margin = {
    top: 30,
    right: 20,
    bottom: 30,
    left: 50
},
width = 1560 - margin.left - margin.right,
height = 1260 - margin.top - margin.bottom;

I tried this coder block but it wasn't working. (Not sure if this is even what's giving me the issue anyways - perhaps not).

// var x = d3.scale.linear()
//     .range([0, width]);

So I went with this:

var x = d3.scale.ordinal()
.rangePoints([0, width])

var y = d3.scale.linear()
.range([height, 0]);

var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");

var yAxis = d3.svg.axis()
.scale(y)
.orient("left");

var svg = d3.select("#dotHappy")
 .append("svg")
 .attr("width", width + margin.left + margin.right)
 .attr("height", height + margin.top + margin.bottom)
 .append("g")
 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var chart = svg.append("g")
 .attr("id", "chart");

Also tried tweaking this, which may or may not even be part of the problem.

x.domain(data.map(d => d.Happy));
y.domain([5, 33]);
// y.domain(data.map(d => d.city));

svg.append("g")
 .attr("class", "x axis")
 .attr("transform", "translate(0," + height + ")")
 .call(xAxis)
 // .append("text")
 .attr("class", "label")
 .attr("x", width)
 .attr("y", -6)
 .style("text-anchor", "end")
 .text("Happy");

svg.append("g")
 .attr("class", "y axis")
 // .attr("transform", "translate(0," + width + ")")
 .call(yAxis)
 // .append("text")
 .attr("class", "label")
 .attr("transform", "rotate(-90)")
 .attr("y", 6)
 .attr("dy", ".71em")
 .style("text-anchor", "end")
 .text("THC");

var groups = svg.selectAll(".groups")
 .data(data)
 .enter()
 .append("g")
 .attr("transform", function(d) {
    return "translate(" + x(d.Happy) + ".0)";
 });

var dots = groups.selectAll("circle")
.data(function(d) {
    return d3.range(1, +d.thc + 1)
    // return d3.range(d.thc)
})
.enter().append("circle")
.transition().duration(1000)
.attr("class", "dot")
.attr("r", 10)
.attr("cy", function(d) {
    return y(d)
})
.style("fill", "blue")
.style("opacity", 1);

})

Here is a snapshot of my csv file:

city.  |.   Happy.    | thc
Boston.       37.        23
NYC.          22.        30
Chicago.      88.        5

Following is a screenshot of what it currently looks like. So in this case, the tooltip displaying the text box 'The Sister' should be only for one circle (because it should only be one data point), however if you hover over the other 10 orange circles below it, it's all the same - indicating it has been repeated 11 times total:

repeated circles in visualization



from D3.js v3 Dot Plot Histogram Duplicating Values

Connecting to Mi Smart Band through Mi Fit application

I want to connect to Mi Fit application in a same way like Notify for Mi Band does. In this application there are two options to connect to Mi Band. The first one which connects to it needs auth token from freemyband and possible this also needs rooted phone/custom Mi Fit application installed. I understand this method, but there is an option to connect to Mi Band through the Mi Fit application, which needs running instance of it and it does not need rooted phone.

My question is how can I connect to the Smart Band through the Mi Fit application?



from Connecting to Mi Smart Band through Mi Fit application

How to show a pdf file from storage using AndroidPdfViewer library?

I want to load a pdf file from external storage (Download/Pdfs/myfile.pdf) using AndroidPdfViewer but it shows blank screen without any error. I tried lots of ways but it's not working.

public class PdfViewActivity2 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
        PDFView pdfView = findViewById(R.id.pdfView);
        pdfView.fromFile(path).load();

I have a pdf file in my "Download/Pdfs/myfile.pdf" and i used the above code to load the file but it's not working. I have given storage permission manually from settings. Can anyone please correct me where i am making a mistake.



from How to show a pdf file from storage using AndroidPdfViewer library?

How to apply gaussian process regression on series problems?

I have been working on a problem as follows, that I wish to perform regression on using Gaussian Process Regressor (GPR):
Input (X): [list1, list2, list3, ....] # All the lists (or arrays) may not be of the same size
Output(y): [value1, value2, value3, ....] # Equal and corresponding to the number of input lists

Code:

X = np.atleast_2d(X)
# X = X.reshape(124, 50, 3)   # 3D representation does not work for GPR
y = np.atleast_2d(y).reshape(-1,1)

# Gaussian process regressor
kernel = RBF(0.1, (10,10))
gp = gpr(kernel=kernel, n_restarts_optimizer=100, alpha = 0.04)
gp.fit(X,y)

But this code only works for one value of one list (within X) at a time, and not for one whole list as an input.

I have tried various representations and have also been able to achieve the representation of multiple features too, but this problem has been bothering me for a couple of weeks. I only need to know how to overcome the couple of hurdles below. I cannot perform the following operations:

  1. Enter 3D shaped data for GPR
  2. (Because of the above) Enter a time series as input and a value as the output

I have been able to implement the same using deep-learning neural networks, but due to the small amount of data that I have, I wanted to check the same for machine learning models. If it is not possible to use GPR, any suggestions on using other ML models like Support and Relevance Vector Machines would also be extremely helpful.



from How to apply gaussian process regression on series problems?

Passing form data to API custom route with WordPress

Getting the following 500 code response when making my API call: PUT https://my-site.com/wp/wp-json/contact/v1/send 500

In functions.php my WP custom route is defined thus:

function sendContactMail(WP_REST_Request $request) {
}

add_action('rest_api_init', function () {
  register_rest_route( 'contact/v1', 'send', array(
    'methods' => 'PUT',
    'callback' => 'sendContactMail'
  ));
});

Here's how I'm making my API call:

formData.append('contact_name', this.contactName)
formData.append('contact_email', this.contactEmail)
formData.append('contact_message', this.contactMessage)

this.$axios.$put('https://my-site.com/wp/wp-json/contact/v1/send', formData)
  .then((res) => {
    this.ApiResponse = res
  })
  .catch((err) => {
    this.$toast.error(err.response)
  })

Why am I getting a 500 error?



from Passing form data to API custom route with WordPress

How can I use the same SHA1 for an app that has different package-name, as another one?

Background

I work on 2 different apps with common shared code, both used on the same project (using productFlavors in gradle file), but with different package names.

The problem

The apps are supposed to be able to login to Google account and fetch some information from it.

The first one works fine, but the second has issues logging-in, especially on release-variant.

Both are already published on the Play Store and have Firebase being used, so I can't perform operations that might damage how the apps work.

What I've found

I made the app write to logs to show the issue:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
        if (result?.isSuccess == true) {
          ...
        }
        else {
          Log.e("AppLog", "onActivityResult failure:${result?.status}")
        }

And indeed this is what I got:

onActivityResult failure:Status{statusCode=DEVELOPER_ERROR, resolution=null}

Searching the Internet and here on StackOverflow, it showed that I need to add the SHA-1 to the project, of both release and debug:

https://console.firebase.google.com/u/0/project/.../settings/general/...

Adding the SHA-1 of debug-variant works fine (though for some reason during login it had multiple steps instead of just one or two), but when I try to add the SHA-1 of release-variant, it seems identical to the SHA-1 of the other app (which has a different package). It shows me this message (via "project settings"->"general"):

An OAuth2 client already exists for this package name and SHA-1 in another project. You can omit the SHA-1 for now and read more about this situation and how to resolve it.

enter image description here

So I followed the link, and I tried to do as was written there:

First, find your existing project's OAuth 2.0 client ID. To do this:

  1. Go to the Credentials page of the Google Cloud console. If the project containing the OAuth 2.0 client ID doesn't open automatically, select it from the drop down menu in the upper right corner of the page.
  2. Under the OAuth 2.0 client IDs section, locate the client name containing the SHA-1 and package name you used for your Firebase project. If you're unsure which one is correct, click the name of the client to see the details.
  3. When you have located the correct client name, copy the full value in the Client ID column.

Next, whitelist this client ID for Google as a sign in provider. To do this:

  1. Go to the Firebase console and select your project.
  2. Select Auth from the menu on the left.
  3. Select the Sign in method tab.
  4. On the Sign in method page, click on Google in the Sign in providers card.
  5. Expand the Whitelist client IDs from external projects option.
  6. Paste your client ID from the Cloud console into the text field and click Add.

So I pasted each of them (from "https://ift.tt/1sExpU1..." into "https://ift.tt/2WSPoH8" ) that I thought that should be there, and as it didn't work, I pasted even more, including of both apps.

enter image description here

Still didn't work.

I also tried to add SHA-256 instead, and even though it allowed me to do it, it didn't help either.

I tried to search for solutions on StackOverflow and on other places, but the questions don't seem to be related to the exact scenario I have, as here it's 2 different package names already (so there shouldn't be a problem), and the apps are already published (so I can't remove stuff from the websites).

The questions

The most important question is the first one. The rest are optional and only so that I could learn what's wrong and what's going on. I would really appreciate it to understand for next time how to handle it properly:

  1. How can I solve it for the second app, without causing any issue to either apps? I don't want to remove the account/project on the website of any of those apps.

  2. How come I can't add SHA-1 of an app of a different package name?

  3. It said to copy the "client ID" on the instructions, but it didn't say of which app. I guess it means of the app that works fine, right?

  4. Some solutions said that I could re-create the SHA-1 to have a new key, but I couldn't find how. How do I do this? Would it help? Wouldn't it affect the app that works fine, and I will have the same issue of same SHA-1 being used for both, again?



from How can I use the same SHA1 for an app that has different package-name, as another one?

Passing a JS ArrayBuffer or TypedArray to Emscripten w/o Copying

I have a very large ArrayBuffer (or TypedArray) in JavaScript that I want to pass to an emscriptened function. I'd like to pass the raw bytes without incurring a copy.

If my C/C++ function takes an std::string as in:

void processBuffer(std::string const& buffer)

I can get the data, but IIUC, the conversion to std::string will incur a copy of the buffer.

Is there a way to pass the raw buffer without a copy?
My access is strictly read-only.

I tried:

void processBuffer(const char* str, size_t size);

with setting allow_raw_pointers() in the EMSCRIPTEN_BINDINGS, but this does not seem to work.
What am I missing?



from Passing a JS ArrayBuffer or TypedArray to Emscripten w/o Copying

Getting KeyStoreException and GeneralSecurityException by using EncryptedSharedPreferences, how can I solve those?

Background

On one of the apps I work on, I store important stuff (tokens) into EncryptedSharedPreferences (taken from here and here):

/** a hardware-encrypted based shared preference (for the values).
 * Note that it is a bit slow, so it's better to always use it in a background thread.
 * Also, avoid having it being backed-up in the manifest, as it's hardware based and will become useless: https://stackoverflow.com/a/63795282/878126*/
object SecuredSharedPreferences {
    private var cachedDefaultSharedPreferences: SharedPreferences? = null

    /**warning: using this function can take some time (249 ms on Pixel 4, for example). Very recommended to avoid calling it on UI thread */
    @WorkerThread
    fun getDefaultSecuredSharedPreferences(context: Context): SharedPreferences {
        if (cachedDefaultSharedPreferences != null)
            return cachedDefaultSharedPreferences!!
        synchronized(this) {
            if (cachedDefaultSharedPreferences != null)
                return cachedDefaultSharedPreferences!!
            cachedDefaultSharedPreferences = getSecuredSharedPreferences(context, context.packageName + "_secured_preferences")
        }
        return cachedDefaultSharedPreferences!!
    }

    @WorkerThread
    private fun getSecuredSharedPreferences(context: Context, fileName: String): SharedPreferences {
        val masterKey = MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build()
        return EncryptedSharedPreferences.create(context, fileName, masterKey,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
        )
    }
}

gradle:

implementation 'androidx.security:security-crypto:1.1.0-alpha03'

The problem

I've noticed 2 bugs being reported via Crashlytics when using this code (reported here):

  1. First one is of the MasterKey.Builder line of GeneralSecurityException :
Fatal Exception: java.security.GeneralSecurityException: Keystore operation failed
       at androidx.security.crypto.MasterKeys.generateKey(MasterKeys.java:146)
       at androidx.security.crypto.MasterKeys.getOrCreate(MasterKeys.java:97)
       at androidx.security.crypto.MasterKey$Builder.buildOnM(MasterKey.java:357)
       at androidx.security.crypto.MasterKey$Builder.build(MasterKey.java:314)
...
Caused by java.security.ProviderException: Keystore operation failed
       at android.security.keystore.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:372)
       at javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:612)
       at androidx.security.crypto.MasterKeys.generateKey(MasterKeys.java:142)
       at androidx.security.crypto.MasterKeys.getOrCreate(MasterKeys.java:97)
       at androidx.security.crypto.MasterKey$Builder.buildOnM(MasterKey.java:357)
       at androidx.security.crypto.MasterKey$Builder.build(MasterKey.java:314)
  1. Second one is on EncryptedSharedPreferences.create line of KeyStoreException, and occurs more often and for more users :
Fatal Exception: java.security.KeyStoreException: the master key android-keystore://_androidx_security_master_key_ exists but is unusable
       at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewMasterKey(AndroidKeysetManager.java:275)
       at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:236)
       at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:155)
       at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
...
Caused by java.security.UnrecoverableKeyException: Failed to obtain information about key
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreSecretKeyFromKeystore(AndroidKeyStoreProvider.java:282)
       at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:98)
       at java.security.KeyStore.getKey(KeyStore.java:825)
       at com.google.crypto.tink.integration.android.AndroidKeystoreAesGcm.<init>(AndroidKeystoreAesGcm.java:58)
       at com.google.crypto.tink.integration.android.AndroidKeystoreKmsClient.getAead(AndroidKeystoreKmsClient.java:164)
       at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewMasterKey(AndroidKeysetManager.java:267)
       at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:236)
       at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:155)
       at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)

What I've tried

Searching the Internet, I've found clues only for the first exception (GeneralSecurityException), that it might be that it occurs for custom ROMs, as they might not implement well the hardware keys for encryption.

And indeed, looking at the devices on Crashlytics, and looking at the Android version of each, I've found that they are ahead of what I see about the latest version that was supported for them.

For the second exception, sadly, I couldn't find any explanation and no solution either. I think it might be related to recovery of apps, but it's weird as it occurs quite often. On reddit (here), someone wrote that in case of such an exception, he chose to wrap the initialization of EncryptedSharedPreferences with "clear all data if fails" and bite the bullet. Also suggested it might be related to having android:allowBackup being disabled (and indeed it is).

The question

Why do these exceptions occur? What can I do against them?

Is clear-data the only thing that can be done? I'm not even sure it really helps, because if I choose to have it, it means the crash report will be gone each time it's about to happen...

Is it related to android:allowBackup being disabled?



from Getting KeyStoreException and GeneralSecurityException by using EncryptedSharedPreferences, how can I solve those?