dimanche 3 mai 2015

Get enum number in R

I am having trouble with R. I have column with some text in cell which have description and after space is parenthesis with enum number with them.

The problem is to remove everything except enum number from this cells which have enum pattern.

I know I need to use gsub but I don't know how to make proper pattern.

I will be really grateful for any help!

Design heuristics for writing Python classes that interact with `scipy.integrate.odeint`?

Introduction

scipy.integrate.odeint requires as its first argument, a function that computes the derivatives of the variables we want to integrate over (which I'll refer to as d_func, for "derivative function" from now on).

d_func has to be written by the user, in Python code. A great way to get a boost of performance using Numba is to @jit the d_func (because d_func is called many times during integration).

I have questions about how to write performant code when d_func is complicated enough that it needs a Python class object behind it.

Code setup

Here is a "cartoon" of my code:

  • there is a module called DynamicBox.py
  • inside this module is a Python class, DynamicBox
  • DynamicBox has a bunch of attributes
  • some of these attributes are "phase variables" -- that is, they are the quantities I am interested in integrating
  • some of these attributes are "parameters" -- that is, I use them to calculate the derivatives of the phase variables

I will have a bunch of functions that will take DynamixBox phase variable or parameter attributes, in order to calculate relevant terms in the derivatives. That is:

  • I will have a d_func
  • d_func itself will call lots of little helper functions to calculate relevant terms in the derivative, using DynamixBox attributes

Design choices

I have to make a choice, with the following options:

  1. either I can make d_func and all its helper functions methods of DynamicBox;
  2. or I can make only d_func a method of DynamicBox, and all of its helper functions are in the same module as DynamicBox, but not methods of DynamicBox;
  3. or only the helper functions are methods of DynamicBox, but d_func is just in the same module (DynamicBox.py), and not a method of DynamicBox;
  4. or neither the helper functions, nor d_func, are methods of DynamicBox.

Question

I do not know enough about Python to figure out which choice is best. The following questions I think would need answering.

  • Is it expensive to make instance attribute calls to get attributes or is it expensive only if you are in a function that is not a method of the class?

  • What if Numba is in play? For instance, will Numba like it better if I am @jit-ting normal functions instead of class methods?

Expressions Calcullator

I've got this following class structure :

public abstract class AbstractExpression {...}

public class ConstExpression : AbstractExpression {...}
public class VarExpression : AbstractExpression {...}

public class AndExpression : AbstractExpression {...}
public class OrExpression : AbstractExpression {...}
public class XorExpression : AbstractExpression {...}

public class NotExpression : AbstractExpression {...}

And what I want to do now is split these expressions into two groups : Binary and Unary Expressions. I consider to make two abstract class inheriting the AbstractExpression class. I am looking for the best solution to convenient using this code in accordance with "interpreter" (behavioral design battern).

EXTRACT FASTA SEQUENCE FROM ORIGINAL FASTA FILE DEPENDING ON ANOTHER SEQUENCE HEDAER FILE

I have two files: File 1: contain many FASTA sequences

84C2_Locus_14_Transcript_1/3_Confidence_0.571_Length_1244 AAACTAGTCAATAGAGAAAATCCAAAGTGGATGAAATTGAAGTGATTGTATGGCACAAGT...so on

84C2_Locus_14_Transcript_2/3_Confidence_0.857_Length_1961 AAACTAGTCAATAGAGAAAATCCAAAGTGGATGAAATTGAAGTGATTGTATGGCACAAGT...so on

84C2|Locus_15_Transcript_1/9_Confidence_0.190_Length_757 ATTTGCTCGGAAAAACACTTCTCGTGGAACTTGTTAGCGCTGAGCTTGATCCCAAGACGA.....so on

84C2_Locus_15_Transcript_5/9_Confidence_0.333_Length_1841 ATTTGCTCGGAAAAACACTTCTCGTGGAACTTGTTAGCGCTGAGCTTGATCCCAAGACGA....so on

File2: only the sequence headers so many 84C2_Locus_14_Transcript_1/3_Confidence_0.571_Length_1244 84C2_Locus_14_Transcript_2/3_Confidence_0.857_Length_1961 84C2_Locus_14_Transcript_3/3_Confidence_0.571_Length_1248 84C2_Locus_15_Transcript_1/9_Confidence_0.190_Length_757 ........ many more

my output file should be contain the sequence associated with header. i.e. matching the sequence header file (file2) header portion with original fasta sequence file (file1) and those headers match the fasta sequence header in file 1 only those sequences store in another output file containing header with sequence.Just like this:

Original output file should like this:

84C2_Locus_15_Transcript_5/9_Confidence_0.333_Length_1841 ATTTGCTCGGAAAAACACTTCTCGTGGAACTTGTTAGCGCTGAGCTTGATCCCAAGACGA......so on

Please suggest me the way in perl which is applicable for my problem.

Simple rectangle pattern with graphics in java

So basically when I try to increase the number of the variable which increases the number of rectangles, it starts to overlap. I'm sure it has an easy fix. I have a feeling that i need to add something to the coordinates but i'm not sure what exactly. Heres the code

public class Pattern extends JComponent implements ActionListener{

JPanel panel = new JPanel(new GridLayout(4, 4)); 
int width = 100;
int height = 100;
int number = 1;
JTextField tf = new JTextField(String.valueOf(width));
JTextField kl = new JTextField(String.valueOf(height));
JColorChooser color = new JColorChooser();
JCheckBox selection = new JCheckBox();
JTextField howMany =new JTextField(String.valueOf(number));

public Pattern(){

    setLayout(new BorderLayout());

    paneel.add(new Label("Width:"));
    paneel.add(tf);
    paneel.add(new Label("Height:"));
    paneel.add(kl);
    paneel.add(new Label("Filled in:"));
    paneel.add(selection);
    paneel.add(new Label("Number:"));
    paneel.add(howMany);

    tf.addActionListener(this);
    kl.addActionListener(this);
    selection.addActionListener(this);
    howMany.addActionListener(this);

    add(panel, BorderLayout.SOUTH);
    add(color, BorderLayout.EAST);
    color.setPreviewPanel(new JPanel());

}


public void paintComponent(Graphics g){
    g.setColor(color.getColor());
    for(int i=0; i<number; i++){
        if(selection.isSelected()){

            g.fillRect((width)*i, 10, width, height);
            g.drawRect((width)*i, 110, width, height);
            g.drawRect((width)*i+width, 10, width, height);
            g.fillRect((width)*i+width, 110, width, height);
        } else {
            g.drawRect((width)*i, 10, width, height);
            g.fillRect((width)*i, 110, width, height);
            g.fillRect((width)*i+width, 10, width, height);
            g.drawRect((width)*i+width, 110, width, height);
        }
    }




}

public static void main(String[] args){
    JFrame window = new JFrame("Window");
    window.setSize(1000, 1000);
    window.getContentPane().add(new Pattern());
    window.setVisible(true);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println(tf.getText());
    System.out.println(kl.getText());
    width = Integer.parseInt(tf.getText());
    height = Integer.parseInt(kl.getText());
    number =Integer.parseInt(howMany.getText());

    repaint();
}


}

samedi 2 mai 2015

Convert a class to a possible interface

I have two Interfaces in different projects (these projects are not referenced to each other) which have the same methods like this:

In first project we have:

public interface IInterfaceA
{
     IAViewModel Do();
}
public interface IAViewModel
{
     int Id { get; set; }
}
public class ServiceA : IInterfaceB
{
     public IBViewModel Do()
     {
         return null;
     }
}

In second project we have:

public interface IInterfaceB
{
    IBViewModel Do();
}
public interface IBViewModel
{
    int Id { get; set; }
}

I can create an instance of ServiceA at runtime, using some Reflection, from second project.

The question is that how can I convert that service to IInterfaceB. I mean I want to access it's methods when someone is developing by referring as IInterfaceB .

How to automatize overriding methods?

I am wondering if there is any tricky way to override all class methods in the same manner. For instance, how to wisely implement composite pattern in large classes? When i get something like this for instance:

        class Foo{
            virtual void work() = 0;
...
        };

        class FooLeaf : public Foo{
            virtual void work() override{}
...
        };

        class FooComposite : public Foo{
            std::vector<Foo *> foos;
            virtual void work() override{
                for (auto foo : foos){
                    foo->work();
                }
            }
...
        };

It is not a problem to reimplement one method. But when the number of methods grows the code get tremendously WET. I mean, I don't feel good copy-pasting the foreach loop for, let's say, 10 methods.