How to create a field in django model using values of other fields of same model?

Question: I wants to create a field name:total which have the total price of all the products*quqantity. What I want is how can I do total = price*quantity in django model. As you can see there can be more than one products.
I have join the OderItem model with Order through tabularinline.

class OrderItemInline(admin.TabularInline):
    model = OrderItem
    raw_id_fields = ['product']

class OrderAdmin(admin.ModelAdmin):
    list_display = ['id','name', 'mobile_no','address','status', 'created']
    list_editable = ['status']
    list_per_page = 15
    list_filter = ['status']
    search_fields = ('id',)
    inlines = [OrderItemInline]

admin.site.register(Order, OrderAdmin)
class Order(models.Model):
    name = models.CharField(max_length=60)
    email = models.EmailField(max_length=60,default=None, blank=True)
    mobile_no = models.CharField(max_length=13, default=None)
    address = models.CharField(max_length=150)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status_choices = (
        ('In Queue', 'In Queue'),
        ('Processing', 'Processing'),
        ('Ready', 'Ready'),
        ('Delivered', 'Delivered'),
        ('Paid', 'Paid'),
        ('Cancelled', 'Cancelled'),
    )
    status = models.CharField(max_length=15,       choices=status_choices, default=status_choices[0][0])

    class Meta:
        ordering = ('created', )

    def __str__(self):
        return 'Order {}'.format(self.id)

    def get_total_cost(self):
        return sum(item.get_cost() for item in self.items.all())

class OrderItem(models.Model):
    order = models.ForeignKey(Order, related_name='items',
    on_delete=models.CASCADE)
    product = models.ForeignKey(MenuVariant, related_name='order_items',
    on_delete=models.CASCADE, default=None)
    size = models.CharField(max_length=20, default=None)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    quantity = models.PositiveIntegerField(default=1)
    # total = models.DecimalField(max_digits=10, decimal_places=2,default=0)

    def __str__(self):
        return '{}'.format(self.id)

    def get_cost(self):
        return self.price * self.quantity
Comment: What do you mean you want "to create a field total"? You want to display the total price in the admin? Or you want to have access to the total in your code?
Comment: wants to do both
Comment: But you already have it in your code: `order.get_total_cost()`.
Comment: it only show the total price of one product e.g price * quantity
Comment: huh? it's the sum of the cost for all items, not just one product. Your code is correct.
Comment: @dirkgroten i want to create a field which take  the price of first product and its quantity and then multiply price and quantity and store it in total and then it take the price of second product and save its price in total= total+price and so on
Comment: why store something in the database that can be calculated from the other columns (that's bad practice)? and store it where? you want to add a field `total` to which model?
Comment: @dirkgroten in order model
Answer: You can get the total price for an Order directly with a database query, so there's no need to save it in the database:
from django.db.models import Sum, F, FloatField
orders = Order.objects.annotate(total=Sum(F('items__quantity')*F('items__price'), output_field=FloatField()))

This adds a total field to each result of the query. So then:
price_of_first_order = orders.first().total

And if you want to display the total for an order in the admin, subclass the ModelAdmin and add get_total_cost to the readonly_fields, as explained here:

A read-only field can not only display data from a model's field, it can also display the output of a model's method or a method of the ModelAdmin class itself. This is very similar to the way ModelAdmin.list_display behaves.
Comment: i add this code in order model or order item model?
Comment: as u said that get_total_cost function is doing what im asking then how can i do total= get_total_cost in model
Comment: hey can u help me out?
Comment: See the answer below of @amazonic if you really want to save it. But as I said, it's bad practice to do that, because it's redundant and will almost certainly at some stage lead to inconsistent data in your database. You need to always keep one source of truth, saving this to the database creates a second source for the same truth.
Comment: im confuse how to use the code that u provided i copy past the code in Order model but it is not working  .im new to django and im doing this first time.
Comment: My answer contains two things: First an explanation why you should not save the total in the model (because it's easy to get the total anywhere you need it in your code by performing the query I showed). Second how to display the total in the admin. Just add `get_total_cost` as readonly field to your ModelAdmin and you'll see the total for each order appear in the Order change view.
Comment: when I run ur code it give this output "
, , , , , , ]>"
Comment: when i add get_total to readonly fields it shows an empty field in admin
Answer: While I, like dirkgroten don't quite see the point of saving the value in a model field, you could add a field total=models.IntegerField(null=True, blank=True) to your Order model and use a post_save to update the value.
So, since you already have a get_total_cost() we can do something like:
def order_pre_save(sender, instance, **kwargs):
    instance.total_cost = instance.get_total_cost()

pre_save.connect(order_pre_save, sender=Order)

EDIT: As dirkgroten points out you'll need the post_save on the OrderItem:
def order_item_pre_save(sender, instance, **kwargs):
    instance.order.total_cost = instance.order.get_total_cost()
    instance.order.save()

post_save.connect(order_item_pre_save, sender=OrderItem)

And you'd also need to update the value on delete:
def order_item_pre_delete(sender, instance, **kwargs):
    instance.order.total_cost = instance.order.get_total_cost() - instance.get_cost()
    instance.order.save()

post_delete.connect(order_item_pre_delete, sender=OrderItem)

Comment: this would work, but you'd want to connect saving `OrderItem`, not `Order`, to update the field on `Order`. So it would be `instance.order.total_cost = instance.order.get_total_cost(); instance.order.save()`
Comment: the problem is that you should also update it when an OrderItem is deleted. Also these signals are not called in certain circumstances, e.g. when a bulk update or bulk create occurs.
Comment: when i run the order_item_pre_save func it gives error OrderItem is not defined
Comment: Have you defined your new function in your `models.py`? Put your post/pre-saves at the bottom of the `models.py` where your model is.


jQuery simple fade functions not running smooth in IE

Question: I've got a correctly working website, which runs very smooth in FF and Chrome, but IE shows the fadeIn and fadeOut not fluently, but it flickers...
Is there a solution for this?
I have a bgstretcher on the background, maybe this is making it too heavy for IE?
Thanks in advance,
Wouter
Comment: As far as I remember is there no opacity-support in IE... Therefore it just show()s the blocks without fading... Due to the animation the flickering happens...
Comment: [http://www.google.com/search?q=reasons+to+use+Internet+Explorer](http://www.google.com/search?q=reasons+to+use+Internet+Explorer)
Answer: IE Doesnt support opacity. thats a common problem, its uses filters. there is no way to run this smoothly in IE.
or in its real name. Internet Exploader.
Comment: Thanks for your answer, lets wait for IE 9 and pray
Comment: sorry, i meant that some animations, for instance also the slideDown one, are not going smoothly. The fadeIn was just an example. I knew that IE doesn't support opacity, but I thought I had that one covered using a transparant .png file. Apparantly this is helping a bit, but not as nice as in FF and Chrome/Safari. Thanks for your advice anyway!
Comment: i'm lucky, enlisting for 3 years to the army. when I'm back.. i'm going back to a world that has IE9!!! :-)
Answer: Kind of a kludge, but it gets around the problem:
<code>if (jQuery.support.opacity) {
    x.fadeIn(100);
}
else {
    x.show();
}
</code>
Comment: Hi Dave, thanks for your answer. I can't test it now though, working on a different project. Is the .show() animation better for IE than the .fadeIn() animation? Bye, Wouter
Comment: show() is not an animation. It just changes the display from none to block. With this code, browsers without opacity will fade in and others will just pop in. Since IE9 will be better behaved, I'm now spending no time on <IE9 work-arounds.


no matching constructor for initialization of std::lock_guard

Question: This is my c++ template class:
template <class Type, class Key = ColumnKey>
class Column {
protected:
  std::shared_ptr<Type> doGet() const {
    std::lock_guard<std::mutex> lock(mutex_);
    return std::make_shared<Type>(value_);
  }
  void doSet(const std::shared_ptr<Type> &value) {
    std::lock_guard<std::mutex> lock(mutex_);
    value_ = *value;
  }
private:
  Type value_;
  std::mutex mutex_;
};

template<class... Columns>
class Table : private Columns... {
public:
  template<class Type, class Key = ColumnKey>
  std::shared_ptr<Type> get() {
    return Column<Type, Key>::doGet();
  }

  template<class Type, class Key = ColumnKey>
  void set(const std::shared_ptr<Type> &value) {
    Column<Type, Key>::doSet(value);
  }
};

I can compile properly if I only call the set methods:
int main() {
  Table3 table3;
  table3.set<int>(std::make_shared<int>(1));
  table3.set<std::string, Key1>(std::make_shared<std::string>("hello_3a"));
  table3.set<std::string, Key2>(std::make_shared<std::string>("hello_3b"));
}

where,
struct Key1;
struct Key2;
using Table3 = Table<Column<int>, Column<std::string, Key1>, Column<std::string, Key2>>;

When I call the get method:
std::cout<<*table3.get<int>()<<std::endl;
std::cout<<*table3.get<std::string, Key1>()<<std::endl;

I get compilation error saying:

error: no matching constructor for initialization of
  'std::lock_guard'
      std::lock_guard lock(mutex_);

But both doGet and doSet I have called using same method. Why am I getting a compilation error only when doGet runs? Is there a mistake the way I am using this?
I am compiling on MacOS 10.13.1 using:
g++ file.cpp -std=c++11 -o file
Comment: The difference is that `doGet` is const and `lock_guard` might modify the mutex.
Comment: @SergeyA: I will add multi-threading to it.
Comment: @BoPersson: yes, now could compile. My bad. Thanks!
Comment: make std::mutex mutex_;
mutable std::mutex mutex_;
Answer: doGet() is a const function.
Declare mutex as below:
mutable std::mutex mutex_;

Suggestion:
Use std::scoped_lock instead of std::lock_guard.
Happy Coding!!!
Cheers!!!


How can I make classes easily configurable without run-time overhead?

Question: I recently started playing with Arduinos, and, coming from the Java world, I am struggling to contend with the constraints of microcontroller programming. I am slipping ever closer to the Arduino 2-kilobyte RAM limit.
A puzzle I face constantly is how to make code more reusable and reconfigurable, without increasing its compiled size, especially when it is used in only one particular configuration in a particular build.
For example, a generic driver class for 7-segment number displays will need, at minimum, configuration for the I/O pin number for each LED segment, to make the class usable with different circuits:
class SevenSeg {
private:
    byte pinA; // top
    byte pinB; // upper right
    byte pinC; // lower right
    byte pinD; // bottom
    byte pinE; // lower left
    byte pinF; // upper left
    byte pinG; // middle
    byte pinDP; // decimal point

public:
    void setSegmentPins(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte dp) {
        /* ... init fields ... */
    }

    ...
};

SevenSeg display;
display.setSegmentPins(12, 10, 7, 6, 5, 9, 8, 13);
...

The price I'm paying for flexibility here is 8 extra RAM bytes for extra fields, and more code bytes and overhead every time the class accesses those fields. But during any particular compilation of this class on any particular circuit, this class is only instantiated with one set of values, and those values are initialized before ever being read. They are effectively constant, as if I had written:
class SevenSeg {
private:
    static const byte pinA = 12;
    static const byte pinB = 10;
    static const byte pinC = 7;
    static const byte pinD = 6;
    static const byte pinE = 5;
    static const byte pinF = 9;
    static const byte pinG = 8;
    static const byte pinDP = 13;

    ...
};

Unfortunately, GCC does not share this understanding.
I considered using a "template":
template <byte pinA, byte pinB, byte pinC, byte pinD, byte pinE, byte pinF, byte pinG, byte pinDP> class SevenSeg {
    ...
};

SevenSeg<12, 10, 7, 6, 5, 9, 8, 13> display;

For this reduced example, where the particular parameters are homogeneous, and always specified, this is not too cumbersome. But I want more parameters: For example I also need to be able to configure the numbers of the common pins for the display's digits (for a configurable amount of digits), and configure the LED polarity: common anode or common cathode. And maybe more options in the future. It will get ugly cramming that into the template initialization line. And this problem is not limited to this one class: I am falling into this rift everywhere.
I want to make my code configurable, reusable, beautiful, but every time I add configurable fields to something, it eats up more RAM bytes just to get back to the same level of functionality.
Watching the free memory number creep down feels like being punished for writing code, and that's not fun.
I feel like I'm missing some tricks.

I've added a bounty to this question because although I quite like the template config struct thing shown by @alterigel, I don't like that it forces respecification of the precise types of each field, which is verbose and feels brittle. It's particularly icky with arrays (compounded by some Arduino limitations, such as not supporting constexpr inline or std::array, apparently).
The config struct ends up consisting almost entirely of structural boilerplate, rather than what I would ideally like: just a concise description of keys and values.
I must be missing some alternatives due to not knowing C++. More templates? Macros? Inheritance? Inlining tricks? To avoid this question becoming too broad, I'm specifically interested in ways of doing this that have zero run-time overhead.

EDIT: I have removed the rest of the example code from here. I included it to avoid getting shut down by the "too broad" police, but it seemed to be distracting people. My question has nothing to do with 7-segment displays, or even Arduinos necessarily. I just want to know the ways in C++ to configure class behavior at compile time that have zero run-time overhead.
Comment: You could probably store the data as an array in flash memory (which you probably have more of). For example, the ATMega168 has 16k flash vs. 1k sram. See: [PROGMEM](https://www.arduino.cc/reference/en/language/variables/utilities/progmem/)
Comment: Welcome to tiny systems.  It is quite a bit harder than most modern environments that  essentially unlimited computing resources.  To achieve reliability you might have to compromise on beauty.  In embedded work, reliability under ANY possible software and electrical condition is really all that matters, and everything -- everything else falls after that.  The answer below is a good one, but as an embedded patent holder, (firmware a hardware), I advise that reliability is all that matters in the end.
Comment: Maybe I'm missing something, but a 7-segment display has 8 values including the decimal point. Why are you using whole bytes for each value? You could just store the whole thing in a `std::uint8_t` and using bit twiddling get the on/off state, saving you 7 bytes.
Comment: @Boann: I'm not sure I understand 100% what your needs are but I feel that a policy based design may help you. You'd create a template class where each parameter would specify a certain policy, for instance how many segments you'd need, what is the LED polarity, etc. https://en.wikipedia.org/wiki/Modern_C%2B%2B_Design
Comment: @Casey For the LED state, I do do that (`byte chars[numDigits];`). The whole-byte config values are for defining which pin numbers the LED segments are connected to on the chip. Anyway, 7-segment displays are just an example. I'm looking for a general approach to class configuration.
Comment: Maybe you want to be more explicit about what you still want to achieve. It is not really clear to me what you would like to have improved.
Comment: @Boann: `byte chars[numDigits]` still uses 8 bytes.  Casey's suggestion is that you store all 8 values in 1 byte, a `std::uint8_t`.
Comment: @MooingDuck `byte chars[numDigits]` uses 1 bit per LED and is 100.0% memory efficient. And please I just want ideas for the configuration problem.
Answer: You can use a single struct to encapsulate these constants as named static constants, rather than as individual template parameters. You can then pass this struct type as a single template parameter, and the template can expect to find each constant by name. For example:
struct YesterdaysConfig {
    static const byte pinA = 3;
    static const byte pinB = 4;
    static const byte pinC = 5;
    static const byte pinD = 6;
    static const byte pinE = 7;
    static const byte pinF = 8;
    static const byte pinG = 9;
    static const byte pinDP = 10;
};

struct TodaysConfig {
    static const byte pinA = 12;
    static const byte pinB = 10;
    static const byte pinC = 7;
    static const byte pinD = 6;
    static const byte pinE = 5;
    static const byte pinF = 9;
    static const byte pinG = 8;
    static const byte pinDP = 13;

    // Easy to extend:
    static const byte extraData = 0xFF;
    using customType = double;
};

Your template can expect any type which provides the required fields as named static variables within the struct's scope.
An example template implementation:
template<typename ConfigT>
class SevenSeg {
public:
    SevenSeg() {
        theHardware.setSegmentPins(
            ConfigT::pinA,
            ConfigT::pinB,
            ConfigT::pinC,
            ConfigT::pinD,
            ConfigT::pinE,
            ConfigT::pinF,
            ConfigT::pinG,
            ConfigT::pinDP
        );
    }
};

And an example usage:
auto display = SevenSeg<TodaysConfig>{};

Live Example
Comment: @Boann: I'm a big fan of using `static_assert` very aggressively, especially with anything involving templates.
Comment: Oooh! I like this trick! Unlimited flexibility with no RAM cost, no ROM cost, and no performance lost. And it prevents users of the class skipping a vital setter call. I wish Java could do this.
Comment: I'm wondering, is there any way to enforce more structure on the config struct supplied to the class? It's currently quite free-form. For example, if a field type were specified differently, it could cause different behavior in the class, but with no compile-time error.
Comment: You could consider using [C++20 constraints](https://en.cppreference.com/w/cpp/language/constraints), though I'm not very familiar with them
Answer: If I understand your situation correctly, whenever you compile your program, you target a single, specific architecture/device with one specific setting. There is never a case where you program would deal with multiple settings at the same time, is that right?
I also assume that your whole project is ultimately relatively small.
If that is the case, I would probably forgo any fancy templates or objects. Instead, for every device you desire to compile for, create a separate header file with all settings given as global constexpr constants or enums. If you change your target, you need to supply a different config header file and recompile the whole program.
The only missing component is how to make your program include appropriate config header? That can be solved with the preprocessor: Depending on the desired device, you can pass a different command line -D<setting_identification_macro> when invoking the compiler. Then, create a header file which acts as a selector. In there you list all supported devices in a form of
#ifdef setting_identification_macro
#include "corresponding_config.h"
#endif

You might cringe at this "hacky" solution, but it has many advantages:

No run-time overhead as you desired
Absolutely no boilerplate code. No structs to pass around or template arguments.
No change in code required when switching between settings. You just change the command line parameter when invoking the compiler.
Can be done in old/limited C++ or plain C
Comment: Thank you for understanding the question. Yes, I do cringe at it, but macros do seem to be the done thing. Other Arduino libraries encourage you to #define some values just before including their header, or even say to straight-up edit the library header. I just assumed that if I knew C++ better, there would be some clean, structured, modern way to do this type of thing. Templates come close, but incur a lot of grammatical scaffolding.
Comment: @Boann If you want to be able to change the code you generate by passing a different parameter at the command line (or make, or cmake, etc...) then at some point you do need the preprocessor. And having that ability, in contrast of editing the source, is actually quite clean in my opinion.
Comment: Note that even if you use a template solution, as soon as you want to compile code for some different hardware (with the constraints you set),  you'll probably need some `#define`'s.
Answer: This does nothing for the whole problem, but improves the pgm_read:
template<class T = type>
auto pgm_read(const T *p) {
    if constexpr (std::is_same<T, float>::value) {
        return pgm_read_float(p);
    } else if constexpr (sizeof(T) == 1) {
        return pgm_read_byte(p);
    } else if constexpr (sizeof(T) == 2) {
        return pgm_read_word(p);
    } else if constexpr (sizeof(T) == 4) {
        return pgm_read_dword(p);
    }
}

This has to be a template for the if constexpr to work correctly.
Comment: Ah, the `is_same_v`-issue can be worked around by using `is_same<...>::value`. `if constexpr` needs c++17 and it seems like you can activate that in the compiler (it is only a warning).
Comment: This is more elegant, and will maybe work in the future, but Arduino's current stable (1.8.13) compiler complains `error: 'is_same_v' is not a member of 'std'` and `warning: 'if constexpr' only available with -std=c++1z or -std=gnu++1z`.


Unable to identify source of java.lang.ClassNotFoundException BaseDexClassLoader

Question: I have got lot of crash report in my android app from users as I see in the developer console. The stack trace that I see is:
java.lang.RuntimeException:

  at android.app.ActivityThread.handleReceiver (ActivityThread.java:2884)

  at android.app.ActivityThread.-wrap14 (ActivityThread.java)

  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1565)

  at android.os.Handler.dispatchMessage (Handler.java:111)

  at android.os.Looper.loop (Looper.java:207)

  at android.app.ActivityThread.main (ActivityThread.java:5728)

  at java.lang.reflect.Method.invoke (Native Method)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:789)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)
Caused by: java.lang.ClassNotFoundException:
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass (ClassLoader.java:511)
at java.lang.ClassLoader.loadClass (ClassLoader.java:469)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:2879)

There is no reference to my app's package name. I am using Eclipse for my development. Is it something related to multidex. Do I need to switch to Android Studio to solve this. I am unable to understand the basic reason behind the stack trace so that I can look for a solution for it.
Note: I have google play services, IAP and OneSignal Sdk added to my app
Comment: Any more input you can give on the issue? Have you been able to reproduce the error? How does it occur? etc.
Comment: You should be using android studio.
Comment: You should have some version info for us..
Comment: I have this error in increased number after I included a third party library.
Comment: may be this will help you dude https://github.com/casidiablo/multidex#whats-it-for-again
Answer:
I would personally say that Android Studio has the edge over the
  two.Android studio is becoming more popular day by day among the
  android development community and also the open source community. Android Studio or Eclipse?

Read Migrate to Android Studio .

Migrating your projects to Android Studio requires adapting to a new
  project structure, build system, and IDE functionality. If you are
  migrating an Android project from Eclipse, Android Studio provides an
  import tool so you can quickly move your existing code into Android
  Studio projects and Gradle-based build files.

Logcat Throws
Caused by: java.lang.ClassNotFoundException:
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)

BaseDexClassLoader Base class for common functionality between various dex-based ClassLoader implementations.

In order to configure your app project to use a MultiDex
  configuration, you must make the following modifications, depending on
  the minimum Android version that your app supports.

set multiDexEnabled true to your build.gradle module-level file , as shown below:
    android {
    compileSdkVersion //
    buildToolsVersion //

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion //
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

You can check DexIndexOverflowException .
Comment: Will try this today and let you know how it goes
Answer: It seems like you are using pro guard and need to exclude some classes.
Make sure you have excluded any library used in your project.


how to make eclipse issue warnings when using methods added in api levels higher than minSdkVersion?

Question: I am using SDK level 17 and have set
   <uses-sdk android:minSdkVersion="8"...

in my manifest.xml. On the other hand, in View class we have:
    // ADDED IN API LEVEL 16
    setBackground(...)

but when I use this method in my code, no warning is issued and the app crashes on devices running APIs lower than 16.
Answer: This can be solved via Lint.
The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. You can run lint from the command-line or from the Eclipse environment.

Configuring lint in Eclipse
You can configure global, project-specific, and file-specific settings for lint from the Eclipse user interface.
Global preferences

Open Window > Preferences > Android > Lint Error Checking.
Specify your preferences and click OK.

These settings are applied by default when you run lint on your Android projects in Eclipse.
For more details and Project and file-specific preferences, read Lint documentation.


system logging in linux

Question: Which one is better? syslog-ng or rsyslog or dsyslog?  Please help me
Comment: This question is off-topic for SO.
Comment: Better how?  Do you have any specific requirements?
Comment: rsyslog :-) just adding to the options list
Comment: This is off topic for serverfault too...
Answer: Use the one that came preinstalled, unless you know that you require specific advanced features.
If you require advanced features (automatic rotation, different backends, dynamically-named logfiles per remote logging host), syslog-ng will be a good starting point.

system logging in linux


UIButton deallocated issue

Question: I received this message from the debugger console :

-[UIButton release]: message sent to deallocated instance 0x1836b0

But I don't create UIButton programmatically, all my buttons have created in Interface Builder. Each of them are linked to a function, like this :

-(IBAction)theFunction:(UIButton *)sender;

In lot of this functions, I don't use the variable sender. I don't even try to release it. So I don't understand why my application try to release my buttons.
Do I do something in Interface Builder to release or not my UIButton? Is it about the picture I put in the UIButton? If I use the variable (UIButton *)sender, do I need to release it?
This problem stucks me because of it my application crashes.
Edit:
<code>- (IBAction)showPopoverOverview:(UIButton *)sender {
    TouchPlanePopover *content = [[TouchPlanePopover alloc] init];
    [content setTheAlbum:@"Overview"];

    // Setup the popover for use in the detail view.
    detailViewPopover = [[UIPopoverController alloc] initWithContentViewController:content];
    detailViewPopover.popoverContentSize = CGSizeMake(600., 400.);
    detailViewPopover.delegate = self;  // Set the sender to a UIButton.

    // Present the popover from the button that was tapped in the detail view.
    [detailViewPopover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    // Set the last button tapped to the current button that was tapped.
    lastTappedButton = sender;
    [content release];
}
</code>
detailViewPopover is create in .h like this : @property (nonatomic, retain) UIPopoverController *detailViewPopover;
Thans for your help
Let me know if you need more information, I will edit the post
Comment: Do you synthesize the buttons?
Comment: I need to synthesize my buttons created in Interface Builder ?
Comment: No, only if you want send messages to them.
Comment: I don't want send message to them, I just want to launch a function when I press a Button.
Comment: where does the crash happen? can you post a stacktrace?
Comment: Use debugger to find out where your app crashesh, post that code or line
Comment: what part of stacktrace do you want ? Because it is pretty long...

My app crashes when it receive the message "-[UIButton release]: message sent to deallocated instance 0x1836b0"
Answer: Change
<code>-(IBAction)theFunction:(UIButton *)sender
</code>
to
<code>-(IBAction)theFunction:(id)sender
</code>
Just try...
Comment: In some function, I need to put UIButton
Answer: I am working on this and its work properly
So may be its useful for you.
myController.h
#import <\UIKit/UIKit.h>
@class myController;
@interface myController : UIViewController {
}
- (IBAction)onButtonClick:(UIButton *)button;
@end
myController.m
@implementation myController
- (IBAction)onButtonClick:(UIButton *)button {
NSLog(@"work properly");
}
Comment: My functions works, when I press a button it displays what I want

UIButton deallocated issue


Logistic regression - how good is my model?

Question: I am a beginner in ML so apologize in advance if this sounds silly.
I did a logistic regression on a real data set and I am having problems measuring how well my model fits. I still don't understand how to apply the F1 score in my case.
After performing the error analysis on the cross validation set I got the following values:
Precision:  0.8642534
Recall: 0.8488889
Accuracy:   0.8222222
F1 score:   0.8565022
Are those good values? What do I compare them with? Is this a sign of a strong relationship between my predictor variables and the response variable?
The classes on the response variable are not skewed (I am predicting a gender).
Any help ... I would much appreciate.
Thanks a lot
Comment: I edited your tags.  The question has nothing to do with machine learning.
Comment: My question is taken from Prof Andrew Ng course on machine learning.
Comment: Logistic regression is a competitor of machine learning and is a precursor to some ML methods but is not ML when used the standard way.  And I hope Prof Ng's notes discussed proper scoring rules.
Answer: Logistic regression is not a classifier.  It is a probability estimator.  Any classification that you do is completely outside the scope of logistic modeling.  Some good ways to judge the quality of predictions from logistic regression include high-resolution nonparametric calibration plots, Brier score, and $c$-index (concordance probability; ROC area).  The R rms package's lrm, calibrate, validate functions make these easy to do, and calibrate and validate correct for overfitting (bias/optimism) using the bootstrap (by default) or cross-validation.
Comment: In case you are the author, thanks for writing/creating the library.
Comment: That is a good pure discrimination measure - a simple translation of the $c$-index (corcordance probability; AUROC).  The Brier score on measures that are functions of the log-likelihood are the most sensitive ones.  A smooth overfitting-corrected nonparametric calibration curve can be important to construct.
Comment: How about using Somers' D to judge the quality of predictions?
Comment: Hello Dr. Frank Harrell, are you perchance the author of above mentioned package? It helps to add it as a disclaimer, in case of bias: https://cran.r-project.org/web/packages/rms/index.html
Answer: Logistic regression is a binary classifier (edit: can be used as a binary classifier), and therefore you can use the standard metrics for classifiers. The metrics you use are the standard ones, F1 being the most complete (though the less intuitive in terms of meaning).
Accuracy, for instance, is the percentage of points that have been correctly classified.
Your model seems not bad at all. A random classifier (a monkey doing random guesses) would get an accuracy of a 50%. Still, it depends on the dataset. In an easy dataset many basic models would be able to get similar (or best) values than yours.
You can compare your model to others to see whether your model gets remarkable results or it is just that the classification task was easy.
To complete the evaluation you can also plot a confusion matrix to see whether your model tends to do a good job in classifying one class but not as good in detecting members of the other class. In a confusion matrix, you want most of the elements to fall in the diagonal (true positive and true negatives). The other two blocks in a confusion matrix of a binary classifier are the false negative and false positives.
Besides, you should do all this in a test set, a subset of your data that your model has never seen (that is, that you didn't use to tune the coefficients of your regression).
Comment: (-1) Logistic regression is *not* a binary classifier. See my answer here. http://stats.stackexchange.com/questions/127042/why-isnt-logistic-regression-called-logistic-classification/127044#127044
Comment: No.  These are improper scoring rules.
Comment: Touché. Still, "it can be used as" and therefore the above metrics are valid.
Comment: @Frank, even for out-of-sample tests, specially when you want to compare it with a completely different model (trees, SVMs...)?
Comment: An improper accuracy scoring rule fails for both in-sample and out-of-sample application.


Cleaning up magento.stackexchange.com: Join #magestackday!

Question: It's time to clean up our loved Magento Stackexchange site!
On 7. + 8. November we will do an online "hackathon" (more a magestackoverflow-athon, official on twitter: #magestackday) dedicated to answering, closing and cleaning up questions on magento.stackexchange.com. The goal is to raise the answered questions rate up by at least a few percent and work on those (currently) 2163 unanswered questions
Whoever is willing to help us, please sign up via the #magestackday website:
https://sites.google.com/site/magestackday/
If you're planning to host a local event to socialize and answer/work together, please tick the checkbox in the registration form and we will contact you with detailed information.
Suggestions? Questions?
Please contact Sander Mangel or me.
Looking forward to your participation!
Comment: @jQueryAngryBird some questions are of very low quality and are not really adding anything to the forum. Those will be deleted
Comment: I have some doubts about this megaevent. I have posted a question for this here http://meta.magento.stackexchange.com/questions/394/what-are-the-special-plans-on-magestackday . Have a look and please give us an answer :-)
Comment: Why are you deleting all questions ? In future some people will surely answer these questions ! :( please dont delete Questions .
Comment: can you at least let us edit questions or something? Those of us looking for assistance probably need help, regardless of how tidy you wish your site to look.
Answer: Update:
Thanks everyone for your participation!
Please follow this link for the recap:
MageStackDay recap

I will sum up all arising questions here. Updates to come.
Is there any personal interactions possible (via skype or whatever)?
We will have some online sessions on Skype/Google Hangout/Slack/whatever. More information to come.
What about people who cannot physically available in particular location (I am from india. I know I would be alone when participate in this event. :-( But having an online support is better right ?)
It's not about going somewhere physically. You can also join from home, your office, wherever. That's what an online-event is all about :-) You can work remote, join the online sessions, join a local event or not (or host one yourself) to gather with people around you.
Do questions divide into different category so that people who are more interested to a particular category can answer that section?
We don't want to overmanage the event. Use your favourite tags on magento stackexchange. If not everyone starts answering on page "1" we should be able to work on several questions at once. And I guess it will naturally happen that people prefer different questions to answer.
Is there any special plans to make this event a great success?
The key to success is the participation of YOU and the magento stackexchange community!
Comment: Yes got it. Thanks for your time :)
Comment: @programmer_rkt : Why are you deleting all questions ? In future some people will surely answer these questions ! :( please dont delete Questions .
Comment: @jQueryAngryBird : I didnt get you ? Where did I delete a question ??!!!!
Comment: @programmer_rkt : read title : `Cleaning up magento.stackexchange.com: ` You are also going to delete ,close questions correct?   :(
Comment: @jQueryAngryBird : oh no.. it is not at all like that. we are organizing a program called "magestackday" in which community members gather together and answer all unanswered questions in order to increase the answer rate in this community. Event is going to held on nov 7,8 . "clean" is used in a good way here, not like you guessed. Please go through the content. You are most welcome to the program :-)
Comment: @programmer_rkt : is it for free ?
Comment: @jQueryAngryBird : of course yes. it is an online event. you need to dedicate your two days or at least one day or the time that you have for this event on Nov 7 & 8. Let us make this event a great success by work together.
Answer: During the tea time, I think about this event. For the great success of this event I have some suggestions to put forward. I don't know how much of this possible, but it may make much difference if we care about this aspects.

In order to make this event a great success, we need to spread this news ASAP. We all need to take this as our own responsibility. I am so much excited when I hear about this news. It was my dream to be a part of one of magento hackathon/magento meet. This is somewhat a dream come true. I am sure there are lot of developers who are just like me to do anything for this community. So we need to catch them, atleast they should know about this event before the D-DAY.
As like gathering all community active members, it is very much important that, we need to inform this event to new comers or people who came here rarely. So that they will came to know about this event and may promote this program. For this, one of my suggestion is that, we need to "proclaim" about this event in magento community page(May be in right section, just like an ad). This way all people came to know about this event.
I am  a regurlar user of this community. I will go through almost 60% of questions that are raised in a day. From that experience, I can say that most of the questions posted here are "less-descriptive" now a days. Means we cannot understand what the questioner actually need. It may be due to they are not providing necessary codes, due to lack of awareness etc. However I am sure that during the event, we all will come to a conclusion that about 50% (at least) of unanswered questions cannot be answered due to its poor content !!!!! So in order to avoid this, my suggestion is that we need to do a pre-review section of unanswered questions. That is we need to go through old-unanswered-questions and ping the questioner about this event and request them to edit their own question in better way so that we can answer them.

I know there may be some practical difficulties to do this. But for the success of this maga event, I think these filtering is necessary.
Answer: I just wanted to add to this, every member on here can help generate awareness to the cause, to both people to participate and for users of the site to appreciate that answers need to be marked as accepted.
To do my bit, I've changed my profile to support the cause, I think others in turn should follow suit,

Change your username to MageStackDay Needs You ...
Change/prefix your profile description to:

#Stack Exchange Needs You!
If you've found magento.stackexchange.com to be helpful, then you need to help the site back. It is at risk from being decommissioned due to the question:answer ratio and answer rate.
Whether you are a genius, or someone that needs help, you can still contribute.
- Geniuses can help by marking answering the unanswered questions
  - Others can help simply by marking good answers as accepted, or upvoting other     answers they see value in
For more information see, [https://magento.meta.stackexchange.com/questions/393/cleaning-up-magento-stackexchange-com-join-magestackday](Cleaning up magento.stackexchange.com: Join #magestackday!)
---

Add a little badge to your site avatar to promote the cause (download here https://i.stack.imgur.com/1Mye6.png). I've even taken the liberty of making the avatars for the top contributors here, so they can focus on writing top quality answers :) (Sorry if I didn't make your avatar! Post a comment here, or re-tweet https://twitter.com/sonassi/status/529953245444444160 and I'll do it)
Comment: This is awesome, thank you!
Comment: Thank *you* for starting the campaign.
Comment: Awesome! thanks


Result Set not always being shown in the JTable

Question: I am creating an application in Java which retrieves some information from a MySQL database and then displays it in a JTable on the screen.
The code I am using to get and put the information into a DefaultListModel<> is shown below. The DefaultListModel<> is then fed to the JTable which displays the information onto the screen.
SwingWorker<Void, Void> worker = new SwingWorker< Void, Void>() {

        @Override
        public Void doInBackground() {

            String SQL = "SELECT * FROM AddABill";

            try {
                Statement stmt = GlobalVars.conn.createStatement();

                ResultSet rs = stmt.executeQuery(SQL);

                menuOptions.clear();

                while(rs.next()){

                    menuOptions.addElement(rs.getString(1) + " - " + rs.getString(2));
                }

                rs.close();
                stmt.close();
            }

            catch (SQLException e) {
                System.out.println("An Error Was Detected! :/");
            }

            return null;
        }

        @Override
        public void done(){
            loadingGif.setVisible(false);
        }

    };

    worker.execute();

The issue I am having is that sometimes the information is displayed onto the screen and sometimes it isn't. Since there is no change in the query used I am assuming it is something to do with the MySQL connection.
The JTable and MenuOptions are setup like this
menuOptions = new DefaultListModel<>();
menuOptions.addElement("");

menuList = new JList<>(menuOptions);
menuList.setFont(new Font("Myriad Hebrew", Font.PLAIN, 32));
menuList.setBackground(Color.decode("#005952"));
menuList.setForeground(Color.decode("#BEC423"));
menuList.setSelectionBackground(Color.decode("#660033"));
menuList.setSelectionForeground(Color.decode("#FFFFFF"));

So the question I am wondering is how can I make sure that the information is always displayed on the screen. A picture of when it is displayed and not displayed is shown below.
Displayed:

Not Displayed:

On a final note to get the information to show I have to keep on re-running the code until it is eventually shown.
Comment: 1. everything inside `doInBackground()` is out of `EDT`, you have to notify GUI inside `publish()` ---> `process()`, `setProcess()` or `done()`, 2. `JTable` != `DefaultListModel` 3. to reset `DefaultListModel`, run `SwingWorker`, where inside `publish() ---> process()`, `setProcess()` or `done()` to add a new `Item` to `DefaultListModel`, nothing esle (clear for me) to your code or description
Comment: It may work _sometimes_, but it is not reliable; more [here](http://stackoverflow.com/a/7158505/230513) and [here](http://stackoverflow.com/a/38499763/230513).
Comment: @mKorbel 1) For the rest of the application changes to the GUI from `doInBackground()` have worked. 2) I know the `DefaultListMenu` is what supplies the `JTable` with the information.

If the problem is because I'm changing the code in the `doInBackground()` function then I'm confused as to why sometimes it works and sometimes it doesn't.
Answer: I have solved the issue by adding the information into the table from the done() function. The code is below
SwingWorker<Void, Void> worker = new SwingWorker< Void, Void>() {

        String[] billItems = null;

        @Override
        public Void doInBackground() {

            String SQL = "SELECT * FROM AddABill";

            try {
                Statement stmt = GlobalVars.conn.createStatement();

                ResultSet rs = stmt.executeQuery(SQL);

                //Set The Cursor To Last Position
                rs.last();

                int totalRows = rs.getRow();

                billItems = new String[totalRows];

                //Set The Cursor Back To The Start
                rs.beforeFirst();

                while(rs.next()){
                    int rowNum = rs.getRow() - 1;
                    billItems[rowNum] = rs.getString(1) + " - " + rs.getString(2);
                }

                rs.close();
                stmt.close();
            }

            catch (SQLException e) {
                System.out.println("An Error Was Detected! :/");
            }

            return null;
        }

        @Override
        public void done(){
            menuOptions.clear();

            for(int i = 0; i < billItems.length; i++){
                menuOptions.addElement(billItems[i]);
            }

            loadingGif.setVisible(false);
        }

    };


What does the term "ex tempore" mean?

Question: I was going to give an answer and in this quotation about Werckmeister I encountered the term ex tempore.
enter link description here
I first thought this means memorizing and playing by heart.
A pianist was therefore in the original sense of the word a composer, the
Handicraft dominated, independently a piece of music from different claves
together. His ability is to know "like the claves together sound. That is why he distinguishes himself by having the "nature of the piano.
From this it follows that the familiar separation between composer and interpreter belongs to the music thinking at the time Werckmeister was alien. It becomes clear that the ability of a Clavier-player, to master "piano and composition", is identical to the artisan ability of improvisation ("ex tempore"). Also on the ability of the transposition reveals "whether an organist has his piano in his head". The term "virtuoso" in the Baroque period is diametrically opposed to today's term. A virtuoso was after Kuhnau a musician who could "compose" ex tempore a piece of music itself. The one who could not improvise, was the beginning of the 18th
Century as a "musical quack".
Now I suppose this rather means what is called in German: aus dem "Stegreif".
Comment: I'd put it, as in the text, as extemporisation, also known as improvisation.
Comment: Can we change that external link to something that might still be there next year, or something we can read without having to go off-site?
Comment: we can just take it away if you trust me that I know the source. The link is also added in the German wiki (but not in English)
https://de.wikipedia.org/wiki/Andreas_Werckmeister and here:http://www.felbick.de/diverses
Answer: As the quotation about Werckmeister suggests ex tempore also means improvisation.
(The German wiki site is more extended than the English:)
In music Extempore since the 16th century refers to the free variation of a melody to a given voice (usually the bass part, see Generalbass), but also of ornaments that can delay the process. Et al in jazz, on the other hand, one speaks of improvisations.
https://de.wikipedia.org/wiki/Extempore
But this is not all: It is not only improvisation in a solo or accompaniment of  well known tune, it means also to find the correct harmonization of a song we don't know and haven't heard before. Like in improvisation theater we don't know what the other part will be answering or where the journey is going.  So extempore I would compare with a snowboarder the falls free style downhill above rocks and avelanches without knowing where it will end, but it goes downwards.
While improvisation:
Usually a jazzer, thorough bass player, a soloist has a concept of a song in his mind. It can be compared with a snowboarder that runs down hill on a prepared piste.
Now here is a longer description found in  Dictionary of Music and Musicians
EXTEMPORE PLAYING. The art of playing without [1]premeditation, the conception of the music and its rendering being simultaneous. The power of playing extempore evinces a very high degree of musical cultivation, as well as the possession of great natural gifts. Not only must the faculty of musical invention be present, but there must also be a perfect mastery over all mechanical difficulties, that the fingers may be able to render instantaneously what the mind conceives, as well as a thorough knowledge of the rules of harmony, counterpoint, and musical form, that the result may be symmetrical and complete.
https://en.wikisource.org/wiki/A_Dictionary_of_Music_and_Musicians/Extempore_Playing

What does the term "ex tempore" mean?


Forçar um header 200 no Ajax enquanto a função PHP fica em sleep

Question: Estou criando uma função de chat, porém, o servidor ficar aguardando uma interação do BD durante um período de 10 segundos.
O problema é que, enquanto a conexão Ajax fica aberta esperando uma resposta dentro dos 10 segundos, o navegador não aceita trocar de página.
Notei que o Facebook envia um header 200 e mantém a conexão aberta, isso faz com que esse tipo de problema não aconteça.

Alguém sabe como posso forçar uma resposta header?
Comment: @RodrigoCoelho o PHP do FB é gerenciado por um engine especial, e além disso nao é só o parser que conta, é o modo como você configura a estrutura. Não tem nada a ver com o Apache + PHP que se instala por padrão nas máquinas "por aí". Só pra você ter uma idéia, veja uma das tecnologias: https://www.facebook.com/note.php?note_id=10150415177928920 - E mesmo assim eu ainda acho que você resolveria bem com WebSockets ou AJAX "normal".
Comment: E assim você está travando uma thread preciosa do servidor web por 10 segundos, o que significa que com poucas pessoas no chat seu servidor web vai travar inteiro. Você não deve segurar script PHP rodando por nada. PHP tem que terminar o script o mais rápido possível. Se quer fazer long polling, melhor escolher outra tecnologia. Quanto ao seu chat, procure estudar websockets, ou use o AJAX da forma que todo mundo usa: requisição e resposta rápida. Se tiver algum timer, que seja no lado do JS.
Comment: @RodrigoCoelho o SO usa WebSockets. Mas para você não ter que aprender uma tecnologia nova, pode usar AJAX retornando um JSON com as conversas novas a cada N segundos (requisições demais são ruins também, mas ainda é melhor que long polling neste caso. Quando o server "apertar", migre pra websockets).
Comment: De qualquer forma, agora que você entendeu a idéia, segue um caminho pro que foi perguntado originalmente: Depois do header você pode mandar alguma sequencia de bytes inicial, e ir usando isso aqui: http://php.net/manual/pt_BR/function.flush.php só que lembre-se dos riscos envolvidos. Os threads do webserver tem quantidade limitada de atendimentos simultâneos.
Comment: O Stack Overflow usa websockets para isso.
Comment: Seu código tem alguma linha como essa? `header("HTTP/1.1 200 OK");`
Comment: Sim, mas não adianta, pois ele só retorna o header quando todo o script foi executado.
Comment: Qual tecnologia acredita que o facebook esta utilizando? Eu acompanhei o relatório e ele esta fazendo um long polling de 50 segundos... a diferença é que ele consegue dar uma resposta para o HTTP antes de concluir o curso dos 50 segundos. Após esse prazo ele fecha e abre uma nova chamada.
Comment: Certo.. para que eu não fique girando em torno do mesmo problema, o que sugere? Um exemplo pode ser as ações do stackoverflow. Sem eu sair da tela, ele já exibe que tive respostas. Alguma sugestão?
Comment: Ok, vlw pelas dicas.
Answer: O chat do facebook utiliza um framework web assíncrono feito em Python, o Tornado.
O conceito de servidores assícronos é diferente, já que todo o processo é feito por conexões de stream. Esse conceito é utilizado também em servidores Node.js (já pela natureza assícrona do javascript).
Se seu objetivo é fazer um chat, é possível utilizar o port do Socket.io para o PHP Elephant.io. Não utilizei o port para PHP, somente o original (do Node.js), mas ele é realmente bom no que faz! ;)


Detecting a lego baseplate in an image

Question: I want my code to find the corners of a square lego plate in an image like the one attached.
I also want to find its dimensions, i.e. the number of "blops" in both dimensions (48x48 in the attached image).
I am currently looking at detecting the individual "blops", and the result so far is pretty good: a combination of blur, adaptiveThreshold, findContours and selection based on area finds the contours rendered in the second attached image (coloring is random).
I'm now looking for an algorithm to find the "grid" losely represented by these contours (or their mid-points), but I lack the google fu. Any ideas?
(Suggestions for different approaches are also very welcome.)
(The sample image shows bricks placed in the corners - an algorithm could expect this, if it helps.)
(The sample image has a rather wild background. I'd prefer to cope with that, if possible.)
Update 8 July 2016: I'm trying to write an algorithm that looks for streaks of adjacent contours forming lines. The algo should be able to find a number of these and, from that, deduce the form of the whole plate, even with perspective. Will update if it works...
Update December 2017: The above algorithm sort of worked, although it was a bit too unpredictable. Also I got problems with perspective (adding a "thick" lego brick changes the surface) and color recognition (shadows, camera peculiarities etc). This endeavor is on hold for now. If I resume it I will try with fixed camera positions immediately above the plate and consistent lights.
Comment: This should give you all the pointers: http://sudokugrab.blogspot.de/2009/07/how-does-it-all-work.html
Comment: ..and...another update if you succeeded?
Comment: @ZF007 I added a possible solution
Comment: Thanks! I looked at this one and it's really good - it's what showed me how to find contours etc. Problem is the lego plate is harder to detect (contrast is lower and I don't know the color of the background etc). It's possible that I can tweak to make it better, but so far no good...
Comment: Cool question - how about a few more sample images?
Comment: Will add more images as soon as I get some quality time in front of the computer. Also I wrote an algorithm that finds "runs" or "streaks" of nearby contours, will try to attach an image of that. I hope to use the output to find the plate and all its "blops", even when perspective is bad. (First attempt failed due to perspective, the thing isn't square!)
Comment: FYI no luck yet, this project was put on hold.
Comment: Volley.. if you have still some code that produced the second image or code that enabled you to get to the second update I would recommend you to add that to the questions. This because the bots are validating a lot of questions for having code in their questions and if not they are send to low-quality post review for examination. Nathancy formulated a partial answer thus-far. See my comment at answer.
Answer: Here's a potential approach using color thresholding. The idea is to convert the image to HSV format then color threshold using a lower and upper bound with the assumption that the baseplate is in gray. This will give us a mask image. From here we morph open to remove noise, find contours, and sort for the largest contour. Next we obtain the rotated bounding box coordinates and draw this onto a new blank mask. Finally we bitwise-and the mask with the input image to get our result. To find the corner coordinates, we can use cv2.goodFeaturesToTrack() to find the points on the mask. Take a look at how to find accurate corner positions of a distorted rectangle from blurry image in python? and Shi-Tomasi Corner Detector & Good Features to Track for more details

Here's a visualization of each step:
We load the image, convert to HSV format, define a lower/upper bound, and perform color thresholding using cv2.inRange()

import cv2
import numpy as np

# Load image, convert to HSV, and color threshold
image = cv2.imread('1.png')
blank_mask = np.zeros(image.shape, dtype=np.uint8)
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 109])
upper = np.array([179, 36, 255])
mask = cv2.inRange(hsv, lower, upper)

Next we create a rectangular kernel using cv2.getStructuringElement() and perform morphological operations using cv2.morphologyEx(). This step removes small particles of noise.

# Morph open to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)

From here we find contours on the mask using cv2.findContours() and filter using contour area to obtain the largest contour. We then obtain the rotated boding box coordinates using cv2.minAreaRect() and cv2.boxPoints() then draw this onto a new blank mask with cv2.fillPoly(). This step gives us a "perfect" outer contour of the baseplate. Here's the detected outer contour highlighted in green and the resulting mask.

# Find contours and sort for largest contour
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[0]

# Obtain rotated bounding box and draw onto a blank mask
rect = cv2.minAreaRect(cnts)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image,[box],0,(36,255,12),3)
cv2.fillPoly(blank_mask, [box], (255,255,255))

Finally we bitwise-and the mask with our original input image to obtain our result. Depending on what you need, you can change the background to black or white.

# Bitwise-and mask with input image
blank_mask = cv2.cvtColor(blank_mask, cv2.COLOR_BGR2GRAY)
result = cv2.bitwise_and(original, original, mask=blank_mask)
# result[blank_mask==0] = (255,255,255) # Color background white

To detect the corner coordinates, we can use cv2.goodFeaturesToTrack(). Here's the detected corners highlighted in purple:

Coordinates:
(91.0, 525.0)
(463.0, 497.0)
(64.0, 152.0)
(436.0, 125.0)

# Detect corners
corners = cv2.goodFeaturesToTrack(blank_mask, maxCorners=4, qualityLevel=0.5, minDistance=150)
for corner in corners:
    x,y = corner.ravel()
    cv2.circle(image,(x,y),8,(155,20,255),-1)
    print("({}, {})".format(x,y))

Full Code
import cv2
import numpy as np

# Load image, convert to HSV, and color threshold
image = cv2.imread('1.png')
blank_mask = np.zeros(image.shape, dtype=np.uint8)
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 109])
upper = np.array([179, 36, 255])
mask = cv2.inRange(hsv, lower, upper)

# Morph open to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1)

# Find contours and sort for largest contour
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[0]

# Obtain rotated bounding box and draw onto a blank mask
rect = cv2.minAreaRect(cnts)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image,[box],0,(36,255,12),3)
cv2.fillPoly(blank_mask, [box], (255,255,255))

# Bitwise-and mask with input image
blank_mask = cv2.cvtColor(blank_mask, cv2.COLOR_BGR2GRAY)
result = cv2.bitwise_and(original, original, mask=blank_mask)
result[blank_mask==0] = (255,255,255) # Color background white

# Detect corners
corners = cv2.goodFeaturesToTrack(blank_mask, maxCorners=4, qualityLevel=0.5, minDistance=150)
for corner in corners:
    x,y = corner.ravel()
    cv2.circle(image,(x,y),8,(155,20,255),-1)
    print("({}, {})".format(x,y))

cv2.imwrite('mask.png', mask)
cv2.imwrite('opening.png', opening)
cv2.imwrite('blank_mask.png', blank_mask)
cv2.imwrite('image.png', image)
cv2.imwrite('result.png', result)
cv2.waitKey()

Comment: .. agree with Mark but the answer is not fully covering "Lego base plate" qualifications yet. OP has in second image partial circle detection. Lego has at fixed positions circularized knobs and base plate color is either grey or green. If these criteria are met within the algorithm then in 99.9 percent of the cases its like to be a "Lego base-plate" IMHO. Agree? Hough circle detection and measure size (you have the corner coords) by counting knobs x/y-axis gives which type of base-plate it is. Almost there ;-)
Comment: Nice! Could probably use this going forward (assuming I pick it up), end goal is to detect the color of the lego brick, if any, at each "grid position" on the plate. Cheers!

Detecting a lego baseplate in an image


Is it dangerous to sing an entire song in falsetto?

Question: Be it short-term or long-term. Is it true that if you use your falsetto on songs for more than those typical "uuhhhhs" and "oohhhhs" to give it a more sentimental/breathy touch, you could damage your voice permanently?
I'm not talking about singing something extremely loud or high like a song by Whitney Houston or the Opera No.2 by Vitas... More like Stevie Wonder or Adele or Bruno Mars.
Comment: No. Of course not. There are singers who sing in falsetto for their *entire careers*. These include classical/operatic counter-tenors, and heavy metal singers (but that's a different matter).
Comment: I reckon Barry Gibb would say no. Reports that outlasting the rest of his brothers has anything to do with singing Falsetto are unconfirmed.
Comment: Counter-tenor is a style of singing that is all falsetto, and there are schools where you can be trained to sing in that style. Counter-tenors are men who have a natural baritone or tenor range but learn to sing operatic-style alto and mezzo-soprano in falsetto. Now centuries ago there was also a type of adult male singer called a *castrato* (look it up) who could sing alto or soprano *without* falsetto. But the surgical procedure in young boys used to enable that kind of singing in adults has been illegal for a long time, thank goodness, and there have been no castrati for centuries.
Comment: @Widor I heard that Barry Gibb did it so much he got stuck that way, and wasn't able to sing/speak any other way ... ;-) (joke)
Answer: The actual mechanics of the act of singing in a falsetto voice should be understood for starters.  This article delves into the specifics:
http://en.m.wikipedia.org/wiki/Falsetto
A reputable voice teacher would the wisest choice to develop a falsetto voice while minimizing strain or damage to the vocal chords.  To better understand vocal chord structure and vulnerabilities, I've found this article to be very informative:
http://www.drugs.com/health-guide/vocal-cord-disorders.html
I don't know if you are asking in general or for yourself but I hope this information helps answer your question.
Answer: It greatly depends on one's technique and body.
If you ask about doing permanent damage, then I believe it can happen, even if you don't strain your voice so much.
On the other hand, I know a few people singing soprano after voice change (falsetto) for many years in boys choir (today they're >20 years old, and still their voices are not any worse). But they were trained to sing the way they sing.
Please note, that this very question regards health - and one should never rely on internet experts when it comes to health advice, but on professionals - singing teachers or phoniatrists/laryngologists.
Comment: So your throat got sore from singing falsetto and you permanently damaged your voice?  Sorry, when it comes to health personal experience doesn't cut it for me.
Comment: @MatthewRead You are very right. I've restated my answer as the previous one was more like anecdotal evidence, that just confirms that "people who sing falsetto and don't hurt their voice exist", and not that "one can't damage his/her voice by using falsetto".

BTW - my voice is fine :)
Comment: IMO, NO ONE has damaged theor voice because of falsetto.
If it does, most likely it's not becaise of falsetto. It could be due to other health reasons which will affect other registers too.
Answer: I am a countertenor (male who sings alto and soprano entirely in falsetto), and since I am, I can safely say that there is nothing at all dangerous about falsetto. Look up Philippe Jaroussky, and see for yourself!
Answer: Not at all! Bee-gees i.e.! well mm that's not the best example, they've totally lost their voices for doing it wrong. :S


Why does this code not give expected output?

Question: I was experimenting with database operations. For example,
begin
   db_con = PG.connect :dbname => 'sureshkumar', :user => 'sureshkumar'
   db_con.prepare 'get_result', "SELECT * from users;"
   users_name = db_con.exec_prepared 'get_result'
   if users_name != nil
       puts users_name.values
   else
       puts "There is no users available in the table users"
   end
rescue PG::Error => e
        puts e.message
ensure
        db_con.close if db_con
end

The table users contains only two columns. They are,
user_id
user_name

If the table does not contain any row, the exec_prepared statement does not return any row. In this case, why does the if condition not become false? How do I check if the exec_prepared method doesn't return any row?
Comment: don't you need a password to connect? What happens if you write simply `if users_name`, omitting the `!=nil` part?
Comment: I did not set the password for the database. So, I don't need password to connect.
Answer: users_name = db_con.exec_prepared 'get_result' will never return nil to users_name.
According to the documentation of PG::Connection#exec_prepared it will return PG::Result object. So, users_name object can never be nil. It will always be an instance of PG::Result.
So, to check whether you have any rows returned from the query execution you need to check if this PG::Result object has any values in it. In your case, users_name variable contains the PG::Result. So you will have to call ntuples on it so see how many rows are returned from the query.
so change your if clause to something like:
if users_name.ntuples > 0
  puts "There are users"
else
  puts "There is no users available in the table users"
end

See PG::Result's documentation to know what other methods you can call on it to access the result.


How to create a line bar in Streamlit progress column?

Question: I have a DataFrame in Streamlit where I'm showing the last closing stock price relative to the 52 week low and 52 week high.
I'm using ProgressColumn (Streamlit docs) to show the column but the issue is column_config has only one min and one max:
st.data_editor(
            df,
            column_config={
                "ClosingPrice": st.column_config.ProgressColumn(
                    "ClosingPrice",
                    min_value=df["52WeekLow"].min(),
                    max_value=df["52WeekHigh"].max(),
                    format="$%.2f",
                ),
            },
        )

This doesn't work because the min_value and max_value is absolute for the whole DataFrame and not for each row.
Is there a way to display each row's min and max?  Maybe I can use a different tool to show this column in the DataFrame?
Answer: IIUC, a (hacky?) workaround would be to use pandas Styler and incorporate its own progress bar :
s = (
    df.style.hide(axis=0)
        .format({"ClosingPrice": "${:.2f}"}, precision=2)
        .set_properties(subset=["ClosingPrice"], **{"text-align": "right"})
)

for r, (l, h) in enumerate(zip(df["52WeekLow"], df["52WeekHigh"])):
    s = s.bar(
        subset=pd.IndexSlice[r, "ClosingPrice"],
        vmin=l, vmax=h,
        color="lightgreen"
    )

st.markdown(s.to_html(), unsafe_allow_html=True)

Output (in Streamlit) :

Used input :
df = pd.DataFrame(
    {
        "52WeekLow": [1.21, 0.2006, 0.161, 0.1255, 1.07, 1.46, 1.16, 1.12, 1.82, 0.27],
        "ClosingPrice": [3.15, 0.21, 0.19, 0.13, 1.73, 3.0, 1.5, 2.98, 2.09, 1.07],
        "52WeekHigh": [21.0, 13.25, 1.75, 3.45, 5.4, 7.84, 8.7, 71.5, 6.064, 1.75],
    }
)

Comment: Very clever solution!  I'm learning new things :)  Does the Styler object automatically append the rows?  I'm talking about `s = s.bar(...)` .   I see it works without having to append.


Shorten link before sending to Twitter

Question: I have a PHP loop that displays different headlines and their links. I'd like to send those links to Twitter with an auto-generated short link via Bitly. This is how the link is structured:
Share on Twitter

How can I create a shortened link after the loop has created the actual permalink?
Answer: You need to register at bit.ly and get the API key.
function make_bitly_url($url, $login, $apiKey, $format = 'xml',$version = '2.0.1', $history=1 ) {

    if(substr($url, 0, 7) != 'http://')
        $url = 'http://'.$url;

    $bitly = 'http://api.bit.ly/shorten';
    $param = 'version='.$version.'&longUrl='.urlencode($url).'&login='
    .$login.'&apiKey='.$apiKey.'&format='.$format.'&history='.$history;
    //get the url
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $bitly . '?' . $param);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    //parse depending on desired format
    if(strtolower($format) == 'json') {
    $json = @json_decode($response,true);
    return $json['results'][$url]['shortUrl'];
    } else {
    $xml = simplexml_load_string($response);
    return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
    }
} // end: function

Now the $login variable is your login, the $apeKey your apiKey and $url is the long url, you want to be short and the function outputs the short bit.ly address.
More at: http://code.google.com/p/bitly-api/wiki/ApiDocumentation
Comment: Thanks for your help, Mike. Not quite sure how to integrate the code you provided into my actual page though. Would you be willing to help me with a full example?
Comment: The integration is pretty simple. You just add this function to your code, somewhere at the top and then when you want to use it, just use it like this: make_bitly_url($url, $login, $apiKey);

Shorten link before sending to Twitter


Select com operador IN em coluna do tipo array no Postgres

Question: Boa noite pessoal!
Eu tenho uma coluna (ids_autores) que é um array de inteiros em uma tabela do Postgres. Eu to tentando fazer um select com o operador IN nessa coluna, porém está dando erro. To tentando fazer a seguinte consulta:
SELECT
titulo,
ids_autores
FROM tb_livros
WHERE ids_autores IN (10,15)

Alguém sabe como posso fazer uma consulta desse tipo?
Answer: Assumindo que sua tabela seja algo como:
CREATE TABLE tb_livros
(
  id SERIAL PRIMARY KEY,
  titulo TEXT,
  ids_autores INT[]
);

Contendo os seguintes dados:
INSERT INTO tb_livros ( titulo, ids_autores ) VALUES ( 'Fooz', ARRAY[5] );
INSERT INTO tb_livros ( titulo, ids_autores ) VALUES ( 'Foo', ARRAY[10] );
INSERT INTO tb_livros ( titulo, ids_autores ) VALUES ( 'Bar', ARRAY[15] );
INSERT INTO tb_livros ( titulo, ids_autores ) VALUES ( 'FooBar', ARRAY[10,15,20] );
INSERT INTO tb_livros ( titulo, ids_autores ) VALUES ( 'Baz', ARRAY[10,20,30] );

Como o campo ids_autores se trata de um tipo ARRAY, você deve usar o operador @> para verificar se sua lista de ids contém os ids que você deseja filtrar.
Solução 1: Recupera todos os livros que tenham como autores os identificadores 10 e 15:
SELECT
  id,
  titulo,
  ids_autores
FROM
  tb_livros
WHERE
  ids_autores @> ARRAY[10,15];

Saída:
| id | titulo | ids_autores |
|----|--------|-------------|
|  4 | FooBar |    10,15,20 |

Solução 2: Recupera todos os livros que tenham os autores com os identificadores 10 ou 15:
SELECT
  id,
  titulo,
  ids_autores
FROM
  tb_livros
WHERE
  ids_autores @> ARRAY[10] OR
  ids_autores @> ARRAY[15];

Saída:
| id | titulo | ids_autores |
|----|--------|-------------|
|  2 |    Foo |          10 |
|  3 |    Bar |          15 |
|  4 | FooBar |    10,15,20 |
|  5 |    Baz |    10,20,30 |

Veja funcionando no SQLFiddle
Answer: Utilize o operador de arrays adequado, no caso o OVERLAPS:
SELECT titulo, ids_autores
    FROM tb_livros
    WHERE ids_autores && ARRAY[10,15];

Se um dos elementos, 10 ou 15, existir no array.


Notation: What Octave is This?

Question: In a piece I am learning the sheet music suggests I should play in one octave, when the piece is performed in a different octave. I need help figuring out the notation.
Consider the following piece of sheet music:

From listening to the piece, I know that bars 4 and 5 repeat bars 2 and 3, but an octave higher. However it is non-obvious to me that this should be the case from the sheet. Bar 4 switches to treble in the left hand, so that is clearly a higher octave. But the right hand notation doesn't change from bars 2 and 3 to bars 4 and 5.
Am I missing something? Or is the sheet ambiguous?
Answer: Looks like bars 4 and 5 are missing the 8va sign applicable to the treble clef. Without it, both hands will be fighting for room.
Comment: @psmears - I am indeed. Based mainly on the treble and bass clefs bracketed. if 'twere on organ, there would more likely be one treble and a bass clef, with another bass clef for pedals, I think.
Comment: You're assuming (probably correctly, to be fair) that it's to be played on piano - but that's not the only possibility (e.g. it could be played, without the hands fighting, on an organ with multiple manuals... though I don't know enough about how organ music is typically notated to know if that's likely!)

Notation: What Octave is This?


Taylor series of an integral

Question: I have the following integral
$$ 2\int_r^\infty \frac{x g(x)}{\sqrt{x^2-r^2}}\text{d}x $$
where $g$ is a probability distribution (normalized and symmetrical around its only maximum in 0). I'm trying to study its behaviour for $r\to 0$ (at least second order), but I fail to Taylor expand it. With the help of the computer, the result seems to be $1-r^2/2$ regardless of $g$.
I've tried changing variable to $x/r$ or deriving under integral sign, but without convincing results.
Thanks.
Comment: Did you check Abel transform? http://en.wikipedia.org/wiki/Abel_transform
Comment: Curiosly, it seems to be exacly the Abel transform of $g$. However, this does not help me at the moment...
Answer: Consider
$$I(r)=\int_r^\infty \frac{x g(x)}{\sqrt{x^2-r^2}}\text{d}x=\int_r^\infty \frac{ g(x)}{\sqrt{1-\frac{r^2}{x^2}}}\text{d}x$$  The next step is a Taylor expansion
$$\frac{ 1}{\sqrt{1-\frac{r^2}{x^2}}}=1+\sum_{k=1}^{\infty}\frac{(2k-1)!!}{(2k)!!}\frac{r^{2k}}{x^{2k}}$$ where $(2k)!!=2^kk!$ and $(2k-1)!!=\frac{(2k)!}{2^kk!}$ are so called double factorials. Thus
$$I(r)=\int_{r}^{\infty}g(x)dx+\sum_{k=1}^{\infty}\frac{(2k-1)!!}{(2k)!!}\int_{r}^{\infty}\frac{r^{2k}}{x^{2k}}g(x)dx$$ Now, we make a change of variables in the second integral. Namely $x=ry$. This yields
$$I(r)=\int_{r}^{\infty}g(x)dx+r\sum_{k=1}^{\infty}\frac{(2k-1)!!}{(2k)!!}\int_{1}^{\infty}\frac{g(ry)}{y^{2k}}dy$$ Next, let $M=\text{max}\;g(x)$;$\;$ $r<x<\infty$ Then, we can evaluate the integral inside the sum
$$\int_{1}^{\infty}\frac{dy}{y^{2k}}=\frac{1}{2k-1}$$ and get the following estimation
$$I(r)<\int_{r}^{\infty}g(x)dx+rM\sum_{k=1}^{\infty}\frac{1}{2k-1}\frac{(2k-1)!!}{(2k)!!}$$ Finally,for $r\to 0$, an answer could be:
$$1\leqslant I(r)< 1+rM\sum_{k=1}^{\infty}\frac{1}{2k-1}\frac{(2k-1)!!}{(2k)!!}$$ The last sum converges!

Taylor series of an integral


$PsScriptRoot null in ps 2.0

Question: While this Powershell PSScriptRoot is null (pasted below for an easy read) seems to answer the question it does not in my case.

You have to make sure that this expression is in a saved .ps1 script.
This can happened in following cases:
You use this statement in PowerShell ISE console You use this
  statement in PowerSehll console without a script file You marked only
  this expression for execution in PowerShell ISE

I am running the ps in a .ps1 file outside the ISE and still getting a null pointer.
Comment: `$PsScriptRoot` in PowerShell v2 work only inside `.psm1` script module files, but not inside `.ps1` ordinary script files.
Comment: Possible duplicate of [How can I get the file system location of a PowerShell script?](https://stackoverflow.com/questions/3667238/how-can-i-get-the-file-system-location-of-a-powershell-script)
Answer: <code>$PSScriptRoot</code> is an automatic variable introduced in PowerShell v3.

$PsScriptRoot null in ps 2.0


Standard Error in OLS Regression

Question: Assuming I have the following linear regression set-up:
$y_i = \alpha + x_i * \beta + \epsilon_i$
for $i = 1,2,..., n$.
When I run the regession, I get a $\beta$ and $\alpha$ estimates, along with their standard errors. Let $\sigma_{\alpha}$ and $\sigma_{\beta}$ be the standard error of $\alpha$ and $\beta$ respectively.
If I want to compute the standard error of the expression $\hat{\alpha} + x_i * {\hat\beta}$ for each value of $i$, would that be:
$\sqrt{\sigma^2_{\alpha} + x^2_i * \sigma^2_{\beta}}$   ???
Any help would be appreciated! Thanks!
Comment: Well $x_i$ as you said can be treated as a constant. I mean overall standard error. We have the standard error of the two terms, are they additive? Is there a correlation term?
Comment: What would be your suggestion in computing the standard deviation of that quantity?
Comment: Actually, this is not really what I am after. I am looking for the standard error of that specific quantity.
Comment: well you could calculate $var(\hat{\alpha} + x_i \hat{\beta})$ if you knew $E(\hat{\alpha}\hat{\beta})$. You can find formulas for $\hat{\alpha}$ and $\hat{\beta}$, so perhaps you can calculate this expectation analytically.
Answer: Perhaps calculating this variance will help
\begin{align}
var(\hat{\alpha} + x_i \hat{\beta})&= var(\hat{\alpha})+x_i^2 var(\hat{\beta}) + 2cov(\hat{\alpha},\hat{\beta}x_i)\\
&= var(\hat{\alpha})+x_i^2 var(\hat{\beta}) + 2x_icov(\hat{\alpha},\hat{\beta})\\
&= var(\hat{\alpha})+x_i^2 var(\hat{\beta}) - 2x_i \frac{\sigma^2\bar{x}}{S_{xx}}
\end{align}
where $\sigma^2=var(\epsilon_i)$. The last line comes from here.  If $\sigma^2$ is unknown, an unbiased estimator for it is
$$\hat{\sigma}^2 = (S_{yy} − \beta S_{xy})/(n − 2)$$
Sanity check: Note that the covariance term is negative. This makes sense. In simple linear regression the point $(\bar{x},\bar{y})$ is always on the regression line. Now imagine increasing the slope, but fixing $(\bar{x},\bar{y})$, in this case the y intercept would decrease. Therefore, it makes sense that the estimate of the slope and the estimate of the intercept would be negatively correlated.

Standard Error in OLS Regression


Nocedal & Wright — proof of theorem 3.7

Question: On page 47 of Nocedal & Wright's Numerical Optimization, the authors provide a proof for Theorem 3.7 and there is
one idea that I didn't understand. The authors prove the following inequality:
$$\|x_k+p_k-x^*\| \le \|x_k+p_k^N-x^*\| + \|p_k-p_k^N\| =
O(\|x_k - x^*\|^2) + o(\|p_k\|)$$
and say that a simple manipulation of the above inequality reveals that
$$\|p_k\|=O(\|x_k-x^*\|).$$
Question: What ideas and manipulation are needed to prove that $\|p_k\|=O(\|x_k-x^*\|)$ from the above inequality?
Thank you for taking the time to read my question. Any advice is helpful.
Comment: Since the link to the PDF file is broken, I removed it.
Answer: We have:
$\|p_k\| \le \|x_k -x^*\| + \|x_k + p_k -x^* \| $, and
so,
$\|p_k\| \le \|x_k -x^*\| + O(\|x_k-x^*\|^2) + o(\|p_k\|)$.
Choose $K$ large enough so that $o(\|p_k\|) \le {1 \over 2} \|p_k\|$, then
for $k \ge K$ you have
${1 \over 2} \|p_k\| \le \|x_k -x^*\| + O(\|x_k-x^*\|^2)$, from which the result follows.
Comment: I am not a fan of o/O calculations for proofs of this sort as it often 'loses' constants which can provide intuition.
Comment: Your answer was very helpful to me! Thank you a lot!
Comment: Glad to help! ${}{}$
Comment: @jjjjjj: I don't follow your question?
Comment: Oops, nvm, I think I was wrong, thanks!

Nocedal & Wright — proof of theorem 3.7


Can top user change their display picture?

Question: Recetenly #stackmagento gives excellent gift(Top user swag) to top 72 users. Can those user change their display picture along with t-shirt?
I think it looks really cool.
What do you think guys ?
Comment: This should be person's choice. However I wonder where is my gift !!! I think @AmitBera got his gift. Waiting for it
Comment: as soon i get will change it..
Comment: Yes, It's person choice, but I think It looks really good if we can change. Also, hope you will get your gift with in short time. :) @RajeevKTomy
Answer: This is up to each user.
Some of the users already changed their profile picture to include the shirt even before the shirts were delivered (using their awesome Photoshop skills).
If it's worth something, I will not change mine mainly because I'm lazy.
Comment: about some things I actually am.
Comment: awesome Photoshop skills   :D  :D  totally agree
Comment: lazy....? Really...........? : )
Comment: I think expect Magento...:)


Why so many apache slots marked as "busy"?

Question: During the month of May there was a spike in the amount of busy apache slots. What could have caused this spike?
If you look at Google Analytics (included below) there isn't really a change of traffic during May.
I also included the mysql graph to show that I enabled mysql caching at one point but I doubt it would cause more busy apache process.
Question

What could have caused this busy server spike?

Stats

Apache process: https://i.stack.imgur.com/1jYnO.png
Mysql: https://i.stack.imgur.com/5fifr.png
Google Analytics: https://i.stack.imgur.com/RTeMj.png
Comment: First, please increase kernel limit/tcp-ip stack for for server Then, put Apache behind reverse proxy (reverse proxy may be config for cachinh). In this case, you can try nginx like as reverse proxy with caching for performance. Absolutely optimize your apache config (disable module not use, edit parameter mpm, ....)
Comment: @tquang Thanks for the information. I'll research every term to learn more about it.
Comment: What do your access logs tell you?  Consider modifying the format to include response time.
Comment: I checked my access logs and there doesn't seem to be any increase to the amount of accesses.
Answer: Google Analytics doesn't include traffic from web spiders, which is the likely source of your additional traffic.  I'd recommend that you checkout out your crawl statistics using Google's Webmaster Tools.  It (obviously) will only show you traffic generated by GoogleBot, but might offer a bit of insight.
Comment: Thanks for the hint! Please check my note above in the comments.


What is the order of $g^r$ in terms of order of $g$?

Question:
Let $G$ be a finite group. Suppose $g \in G$ has order $o(g)$ and let $r$ be a divisor of $o(g)$, what is the order of $g^r$ ?

I'm able to find answers that give me what I think I need but its difficult for me to decipher all the notation and precisely what the answers are trying to tell me
What I know is:

Order of $g$ is the minimal $m$ such that $g^m=e$.
$r|m$ , so there exists a $k \in \Bbb Z$ st $m=kr$
I conclude that $g^{rk}=e$ with $rk=m$, thus $r=m/k$
So the order of $g^r$ is $m/k$

I just doubt every abstract Algebra thought I have, so naturally I feel that this is incorrect.
Any Thoughts? Thanks!
Answer: Call $n=o(g)$. One  standard result in algebra says that $$o(g^r)=\frac{n}{\gcd(n,r)}$$
Proof:  Let $k=\frac{n}{\gcd(n,r)}$.  $$(g^r)^k=(g^r)^{\frac{n}{\gcd(n,r)}}=(g^n)^{\frac{r}{\gcd(n,r)}}=e^{\frac{r}{\gcd(n,r)}}=e$$
Now try to prove $k$ is the least such power!
Comment: Thx ill try that


Half-normal distributed DV in generalized linear model

Question: My dependent variable is, by origin, absolute residual* left after some regression; it is distributed half-normally. Now I plan to use Generalized linear model in SPSS (GENLIN) to regress it on some predictors (totally different from those which produced the residuals). What distribution type should I use for the DV? For continuous data, GENLIN offers Gamma, Inverse Gaussian and Tweedie distributions. Which to choose to model the half-normal? Or should I apply special transforms before? And what link function would be most appropriate? What can you advice on that? Thanks.
*More precisely, I'm analysing positive residuals separately and negative residuals separately.
Comment: Why are you using an absolute residual as a dependent variable? If you're interested in how the variance depends on some predictors, there may be better ways of doing it.
Comment: Actually, I'm interested in half of the residuals at a time. I study positive residuals and negative residuals separately, as DVs on their own, with potentially different predictors and models. No, I'm not interested in variance.
Comment: If you had an unscaled normal (such as you might approximately have from raw residuals) before you split them, and we ignore that the residuals won't be exactly identically distributed nor independent, then the ***square*** of the half-normal would be scaled chi-square(1) (gamma with shape parameter $\frac12$). (In R you can specify the deviance parameter when asking for the summary table, I assume something similar is possible in SPSS). As for what link function is suitable, that really depends on your model; but note that a log-link would work as well for squared values as absolute ones.
Answer: Even though you state you are not interested in the variance your exercise is very similar to econometric models trying to estimate the variance as a function of some regressors. They typically work in a two step approach like you suggest here, estimate a model for the mean, take the residuals from this first model, and then fit a seperate model on the residuals (which may or may not have different regressors than the mean model).
If the first model is OLS and you square the residuals, a gamma model is often used as the link function (with the relationship between the residuals and a chi-square distribution the motivating factor I imagine). But in your case you have several complicating factors that make the choice of the link function less clear:

The residuals from generalized linear models are more complicated than residuals from OLS (and are not even well defined - you have several options to choose from)
You want to use the absolute value of the residual (instead of the square)
You want to seperately estimate models for negative and positive residuals (This seems like a mistake to me, I would consider stacking the equations and using dummy variables to distinguish between negative and positive residuals in whatever subsequent equation you want to examine.)

Because it is unlikely you will be able to even approximately derive a distribution of the residuals, an ad-hoc approach of simply using a distribution that will reasonably approximate the residual distribution seems called for. The distribution will look different for different types of generalized linear models (e.g. a logistic regressions residuals will look different than a Poisson regression) so it is hard to give general advice. A gamma distribution may work alright for the residuals from a Poisson regression, but I'm not sure it would for the residuals from a logistic regression (at least in the typical residual plots I have seen for these types of models). Gung gives other possibilities that aren't in the exponential family that may be considered as well.
For an example application of estimating the variance as a function of regressors (along with a brief review of the econometrics literature that has similar applications), see

Western, B., & Bloome, D. (2009). Variance function regressions for studying inequality. Sociological Methodology, 39(1), 293-326. Pre-print PDF Here
Answer: I can't tell exactly what you're doing, or why, and it seems a little strange, to be honest.  I'm guessing that you have a regression model, and believe that those who are above the line are from a different underlying (latent) group from those who are below the line, and hope to find out something about these 'groups'.  Perhaps not, but if so, I'm not sure this is a legitimate strategy.  I have difficulty imagining how the distance above a regression line can be determined by one set of explanatory variables, and the distance below the line by a different set.  There is a great deal of real noise in data and trying to explain it is more likely to lead to phantoms than knowledge.  But I don't mean to scold.
Perhaps a tobit model would be appropriate.  The tobit is based on a probit model with an underlying normal distribution, but where some proportion of the data have been censored.  Recognize that this means the model is assuming the other half of the data are a part of the picture, but just have been hidden from the model, which is true in your case.  The non-zero level of intrinsically unexplainable variance that must exist in your data necessitates that some proportion of the negative residuals really belong to the set of your positive residuals and vice versa, so the assumption the model is making is justifiable.  Nonetheless, running two tobits would allow you to try to model your residuals with different explanatory variables.
I have no idea how to conduct a tobit regression in SPSS; I'm not sure the software supports that analysis.  If you want to know a little bit more about it, and how to conduct it with other software, UCLA's website has a nice, concise, clear description (of course) of it for R.  If you want more theoretical background on it, J Scott Long's book is very accessible.  It requires calculus, but he steps through it very gently.
Good luck.
Comment: `how the distance above a regression line can be determined by one set of explanatory variables, and the distance below the line by a different set` Imagine the residuals are deviations of body weight from normal (median) body weight. Binary variable "endocrine diagnosis" (Present/Absent) will strongly positively correlate with positive residuals, extent of overweight (because of obesity cases), but will not correlate or will weakly negatively correlate with negative residuals, extent of underweight.
Comment: That helps to clarify the situation you are asking about.  But, I wonder if a simple logistic regression with "endocrine diagnosis" regressed onto BMI (the full range, not just above the median) will work.  Patients who are underweight will have a very low probability of ED, but the model can handle that just fine.  If you are particularly concerned about the symmetry of the logistic transformation, cloglog or loglog could be used instead.
Comment: Deviations (either only positive or only negative) from median body weight have to be DV, and different diagnoses will constitute set of IVs of the model. The only thing that stops me is that I don't know how to be, given that the distribution of DV is thus half-normal. I never worked with such distributed DVs before.
Comment: You could try a tobit model, then.  If you're just looking at ED vs. no ED (i.e., just 1 factor), you could try a Mann-Whitney U test--it doesn't require assumptions about the distribution.  Other than that, I don't know.
Comment: This got upvoted today, @ttnphns. I'm just seeing it again after years. I had forgotten about it. I wonder how useful this is. Would you prefer if I just deleted it?
Comment: I upvoted both answers here, but years ago. Are you feeling your answer is incorrect or irrelevant, to delete it? I don't see why you should want to.
Comment: OK, @ttnphns, just checking.


Proving that a quotient space is Hausdorff

Question: This question is influenced by Quotient space of unit sphere is Hausdorff. I have slightly modified this to make it somewhat difficult.
 Let $S^1=\{e^{2\pi it}|r\in\mathbb{R}\}$ be the unit sphere. Define $\sim$ on $S^1$ where two points are identified if $t_1-t_2=\sqrt{2}k$, for some $k\in\mathbb{Z}$. It must be shown that $S^1/\sim$ is Hausdorff.
Define $f:S^1\times S^1\rightarrow S^1$ by $f(x,y)=xy^{-1}$. Clearly $f$ is continuous. Let $R=\{(x,y)\in S^1\times S^1|x \sim y \}=\{(e^{2\pi it_1},e^{2\pi it_2})\in S^1\times S^1|t_1-t_2=\sqrt{2}k,k \in\mathbb{Z}\}$. Also, let $A=\{ e^{2\sqrt{2}k\pi i}|k\in \mathbb{Z} \}$. Then $f^{-1}(A)=R$. $A$ being a countable set implies that $A$ is closed, therefore $R$ is closed since $f$ is continuous. Thus $S/\sim$ is Hausdorff. Is this proof correct? Thank you.
Comment: The equivalence relation has each equivalence class being dense in $S^1$.  So, my first guess would be that the quotient topology is actually the trivial topology, which wouldn't be Hausdorff.
Comment: Why would countable imply closed?
Comment: Is $\Bbb{Q}$ closed in $\Bbb{R}?
Comment: And in fact, assuming the equivalence classes are dense, here's the proof the quotient topology is the trivial topology: if $A \subseteq S^1 / \sim$ is closed and nonempty, then $\pi^{-1}(A)$ is closed and contains an equivalence class, so it is all of $S^1$.  That implies $A$ is all of $S^1 / \sim$.
Comment: @DonaldEdwards It would be a corollary of the fact that $\{ m + n\sqrt{2} : m, n \in \mathbb{Z} \}$ is dense in $\mathbb{R}$.  I would imagine that fact has probably been proved before somewhere on this site.
Comment: @DanielSchepler How are the equivalence classes dense in $S^1$?
Comment: https://math.stackexchange.com/questions/1909/characterizing-dense-subgroups-of-the-reals was already a duplicate of https://math.stackexchange.com/questions/90177/subgroup-of-mathbbr-either-dense-or-has-a-least-positive-element?noredirect=1&lq=1
Answer: First, $S^1=\{e^{2\pi it}:t\in\mathbb{R}\}$ is a unit circle rather than a unit sphere.
Second, for $t_1,t_2\in\mathbb{R}$ we have $e^{2\pi it_1}=e^{2\pi it_2}$ if and only if $t_2-t_1\in\mathbb{Z}$, hence we can also write $S^1=\{e^{2\pi it}:t\in[0,1)\}$.
Third, in order to correctly define a binary relation $\sim$ on $S^1$ by identifying points $e^{2\pi it_1}$ and $e^{2\pi it_2}$ whenever $t_1,t_2$ satisfy a property $P(t_1,t_2)$, one has to ensure that the property $P$ has the same logical value for all pairs $(t_1,t_2)\in\mathbb{R}^2$ that yield the same pair of points $(e^{2\pi it_1},e^{2\pi it_2})$ on the circle.
Namely, that for any $t_1,t_2,t'_1,t'_2\in\mathbb{R}$, if $t'_1-t_1\in\mathbb{Z}$ and $t'_2-t_2\in\mathbb{Z}$, then $P(t_1,t_2)\equiv P(t'_1,t'_2)$.
Your property $P(t_1,t_2)\equiv(\exists k\in\mathbb{Z})(t_1-t_2=\sqrt{2}k)$ is not such.
Let $t_1=1+\sqrt{2}$, $t'_1=\sqrt{2}$, $t_2=t'_2=1$.
Then $t'_1-t_1$ and $t'_2-t_2$ are integers, $P(t_1,t_2)$ holds true since $t_1-t_2=\sqrt{2}$, but $P(t'_1,t'_2)$ is false since $t'_1-t'_2=\sqrt{2}-1$ and there is no $k\in\mathbb{Z}$ satisfying $\sqrt{2}k=\sqrt{2}-1$, as this would mean that $k=1-1/\sqrt{2}$. So your relation is not correctly defined and it has no sense to ask whether $S^1/\mathord{\sim}$ is Hausdorff.
Comment: @DonaldEdwards No, I say that the relation is not defined properly. Every point of $S^1$ can be described in infinitely many ways, and your definition of the relation depends on which description you take into account. By other words, $S_1$ already is a quotient of $\mathbb{R}$. Property $P$ defines an equivalence on $\mathbb{R}$, but that is not _congruent_ (or in accordance) to the equivalence used to define $S^1$.
Comment: So what you are saying is that this relation is not transitive>.
Comment: $t'_1-t'_1$, $t'_2-t'_2$ being integers is irrelevant
Comment: @DonaldEdwards And what did you mean by saying that zeros beeing integers is irrelevant?
Answer: The only way for points to be equivalent is for k=o so your equivalence relation is the identity (again ) . Try t_1-t_2 =k/2  which would identify antipodal points. Then I think  your argument will work . detail ? If t_1-t_2=2^(1/2)k then e^2(PI)i$\sqrt2$k =1 . Then $\sqrt2$k is an integer so the square root of 2 is rational ( impossible) unless k=0 So k=0 and the equivalence relation is the identity
Comment: t_1-t_2=2^(1/2)k then e^2(PI)i2–√2k =1 does not make sense