Wednesday, 1 January 2020

Static html generation with prismjs - how to enable line-numbers?

I'm using node.js to generate html files from code, formatting them with prismjs.

const Prism = require('prismjs');
const loadLanguages = require('prismjs/components/');
loadLanguages(['csharp']);
const code = '<a bunch of C# code>';
const html = Prism.highlight(code, Prism.languages.csharp, 'csharp');

This works great. But I want to use the line-numbers plugin and don't see how to make it work. My <pre> has the line-numbers class, and I get a bigger left margin, but no line numbers.



from Static html generation with prismjs - how to enable line-numbers?

Global fitting using scipy.curve_fit

I had a quick question regarding global fitting using scipy_curvefit. From my understanding, the only difference in setting up the script between local fitting versus global fitting, is the difference in concatenating your functions. Take the script below for example:

input_data = [protein, ligand]
titration_data=input('Load titration data')

def fun(_, kd):
    a = protein
    b = protein + ligand
    c = ligand
    return np.array((b + kd - np.sqrt(((b + kd)**2) - 4*a*c))/(2*a))

kD=[]
for values in titration_data:
    intensity=[values]
    intensity_array=np.array(intensity)
    x = ligand
    y = intensity_array.flatten()
    popt, pcov = curve_fit(fun, x, y)

Input data is a 6x2 matrix, and titration data is a 8x6 matrix as well. Each row of titration data will be fit to the model individually, and a kd value will be obtained. This is a local fit, now I want to change it to a global fit. I have attempted the script below based on my understanding of what a global fit is:

input_data = [protein, ligand]
titration_data=input('Load titration data')

glob=[]
for values in titration_data:
    def fun(_, kd):
        a = protein
        b = protein + ligand
        c = ligand
        return np.array((b + kd - np.sqrt(((b + kd)**2) - 4*a*c))/(2*a))
        print (fun)
    glob.append(fun)

def glob_fun(_,kd):
  return np.array(glob).flatten()

x = ligand
y = titration_data
popt, pcov = curve_fit(glob_fun, x, y)

From my understanding, this should give me a singular kd output now, from fitting all of the data simultameously. However, I have come across an error message trying to implement this:

popt, pcov = curve_fit(glob_fun, x, y)
return func(xdata, *params) - ydata
TypeError: unsupported operand type(s) for -: 'function' and 'float'

The issue here is glob_fun is actually an array of functions (which, from my understanding, for global fitting it should be). However, it seems rather than use the output of that function (based on whatever it chose for kD), to minimize it to ydata, it's using one of functions from the array itself. Hence the error you cannot subtract a function (or at least, this is my understanding of the error).

Edit: I have added the data so the error and functions are reproducible.

import numpy as np
from scipy.optimize import curve_fit

concentration= np.array([[0.6 , 0.59642147, 0.5859375 , 0.56603774, 0.53003534,0.41899441],
[0.06 , 0.11928429, 0.29296875, 0.62264151, 1.21908127,3.05865922]])
protein = concentration[0,:]
ligand = concentration[1,:]

input_data = [protein, ligand]
titration_data=np.array([[0, 0, 0.29888413, 0.45540198, 0.72436899,1],
 [0,0,0.11930228, 0.35815982, 0.59396978, 1],
 [0,0,0.30214337, 0.46685577, 0.79007708, 1],
 [0,0,0.27204954, 0.56702549, 0.84013344, 1],
 [0,0,0.266836,   0.43993175, 0.74044123, 1],
 [0,0,0.28179148, 0.42406587, 0.77048624, 1],
 [0,0,0.2281092,  0.50336244, 0.79089151, 0.87029517],
 [0,0,0.18317694, 0.55478412, 0.78448465, 1]]).flatten()

glob=[]
for values in titration_data:
    def fun(_, kd):
        a = protein
        b = protein + ligand
        c = ligand
        return np.array((b + kd - np.sqrt(((b + kd)**2) - 4*a*c))/(2*a))
        print (fun)
    glob.append(fun)

def glob_fun(_,kd):
  return np.array(glob).flatten()

x = ligand
y = titration_data
popt, pcov = curve_fit(glob_fun, x, y)


from Global fitting using scipy.curve_fit

How to write tests for django multi tenant applications?

I have multi-tenant applications in django with postgresql schemas. I tried to write test code inside a tenant folder "t002" as:

from django.test import TestCase
from path.customers.models import Client
# TestClass to test tenant setup
class TestTenantSetup(TestCase):
    def setUp(self):
        Client.objects.create(
            name='t002',
            schema_name='t002', 
            domain_url='t002.cmpny.com'
            )

    def test_tenant_exists(self):
        client = Client.objects.get(name='t002')
        self.assertEquals(client.schema_name, "t002")

But when I ran the test with python manage.py test path/t002/tests it was creating a test database for my actual database. So let's say I would follow this SO answer (django unit tests without a db) and avoid creating database, but I am not sure if I'm following the right path to test a multi-tenant django project. When I looked into the django-tenant-schemas docs (https://django-tenant-schemas.readthedocs.io/en/latest/test.html) I couldn't get my hands on it easily. Can anybody tell how start doing test for django multi-tenant applications ? And please do review the above code too whether it is right or wrong.

Specs - python==2.7, django==1.11, postgres==9.6



from How to write tests for django multi tenant applications?