Binding WPF Grid to a model

Question: I have a MVVM application which has a WPF Grid which contains other embedded WPF Grids and at the same time, each of them contain some fields (WPF TextBlocks).
Very simplified example - View:
<Grid>

   <Grid>
       // Row definitions
       // Colum definitions
       <TextBlock Grid.Row="3" Grid.Column="0"
                  Text="{Binding Path=SomeField1}" />
   <Grid>

   <Grid>
       // Row definitions
       // Colum definitions
       <TextBlock Grid.Row="0" Grid.Column="1"
                  Text="{Binding Path=SomeField2}" />
   <Grid>

</Grid>

Each of these TextBlocks are bound to a string properties defined in view model.
View model (It implements INotifyPropertyChanged):
private string _someField1;
public string SomeField1
{
   get return _someField1;
   set
   {
       if (_someField1 == value) return;
       _someField1 = value;
       OnPropertyChanged("SomeField1");
   }
}

private string _someField2;
public string SomeField2
{
   get return _someField2;
   set
   {
       if (_someField2 == value) return;
       _someField2 = value;
       OnPropertyChanged("SomeField2");
   }
}

Then I have a model, I mean, a class with some public properties that is filled in by one process once data is obtained from a device. This class contains exactly the same properties as those defined in the view model.
Model:
public class MyModel
{
    private string _someField1;
    public string SomeField1
    {
       get return _someField1;
       set
       {
           if (_someField1 == value) return;
           _someField1 = value;
       }
    }

    private string _someField2;
    public string SomeField2
    {
       get return _someField2;
       set
       {
           if (_someField2 == value) return;
           _someField2 = value;
       }
    }
}

Later from view model I extract the data from this class (model), and I assign the values of those properties to the matching properties in view model. Finally, since view is bound to these properties, then view is correctly updated with values as below example.
View model method which extracts data received:
private void DataReceived(MyModel data)
{
    this.SomeField1= data.SomeField1;
    this.SomeField2= data.SomeField2;
}

The problem is that I have to define twice the properties, in view model and model. So I want to avoid this, I would like to bind Textblocks directly to properties in model and not defined the properties in view model to avoid redundant code. Or for example, is there any easy way to bind my model (MyModel) to the outer main grid and then textboxes bound to the properties in the view model (similar when bound itemsource in datagrid)?
Comment: I don't consider it redundant code. It appears to be because your model is very small. 'MyModel' could actually be a file or a database. At some point you have to pass the data across the view model/ model bounds usually via an interface for decoupling. When binding directly to the model you will have all the problems that the MVVM pattern was meant to solve. Now your business logic would be coupled to the view. If this isn't a problem for you (maybe your application is very small and you don't bother the extra work on changes) you can bind directly to the model. But than it's no longer MVVM.
Comment: Instead of setting the properties directly you could define a model interface that exposes some set and get methods. If you need to persist the data you would handle it in the model.
Comment: If model implements `INotifyPropertyChanged`, then things may get simpler: you don't need to create new properties in view model, rather single property with  instance of model and then bind to model properties via it: `propertyWithModelInstance.ModelProperty`. Sometimes you can't change model, then for the sake of MVVM you need to provide notification and thus - create new properties. Often model properties are only read by view, for those you can use same techinque - sigle property returning instance of model. And don't call property with word *"field"* in its name ;)
Comment: There are dozens of questions which comply about such "typing overload" in MVVM and for sure in other patterns. Then there are either specific libraries to solve the issue (afaik Prism has simplified syntax for view model properties, I could be wrong) or common solutions, like code generation. E.g. using T4 templates to generate viewmodel from model (`partial class` with properties).
Comment: I read that the creator of the MVVM pattern himself considered this pattern overkill for small applications. So like always, it's your choice. But when following the pattern don't introduce a dependency between the view and the model. This is mandatory for this pattern and not an option. Once you understand the pattern and especially the purpose than it would make a perfect sense to you. Then you would've never had the idea to couple those two layers.
Comment: @Sinatr Please don't recommend coupling the view to the model and talk about MVVM at the same time. That's wrong. Please read about the purpose of those patterns before thinking about the implementation details.
Comment: @Sinatr Introducing INtoifyPropertyChanged and thus bindings to your model is a plain anti-pattern. Bindings are in fact the mechanism that introduces the dependency between view and it's data source. Bindings are declared in the view and allow the dependency to be unidirectional while pointing at the data source. But they introduce a dependency.
Comment: @BionicCode, it saved me a lot of time and unless you have something more severe than your opinion - I wouldn't stop ;) Oh, and btw, I am using quite some code-behind in the views, and will continue doing dirty-mvvm and using model directly and implementing `INotifyPropertyChanged` in model for [KISS](https://en.wikipedia.org/wiki/KISS_principle). Simply because I am lazy and don't like typing only for reason of something to be *pure*.
Comment: @Sinatr It's ok to do what you like to do. You just should know the trade-offs. But don't recommend anti-pattern to somebody who tries to follow a pattern. If this would be a big application that belongs to your company you would bring a lot of trouble to your team and maybe the company. Time is money. You have to make the distinction. Just using a command is not MVVM. Just using bindings is not MVVM. You can bind directly to your data base but then please don't call it MVVM, MVP or what ever because that's wrong and would only proof your misconception.
Comment: @Sinatr Bindings and commands for example are just tools or methods to help you to write code that complies with the pattern. The pattern itself (MVVM, MVP, MVC, ...) is pure structural. So that's why it matters where to put certain logic or code.
Comment: @BionicCode When you talk about an interface in your first comment, do you refer to create an interface for the model and then passing it to the view model contructor as a parameter? If so, would it be anti-pattern or not?
Comment: @user1624552 Let's see. MVVM dependency tree is View --> View Model --> Model. You don't have to use interfaces to decouple the model. But when doing so, e.g. to access the data model you won't depend on concrete models. When passing that interface to the view model would make the view model depend on the model or abstract model to be precise. This means View Model --> Model. Looks fine to me. Please explain yourself. What you describe here sounds like dependency injection to me.
Comment: @user1624552 In fact in an advanced scenario you would introduce dependency injection and therefore would have to define interfaces.
Comment: @BionicCode I was thinking in doing something similar to the answer proposed below by c0d3b34n but instead using a generic view model, passing it the interface. But after thinking.. the solution he proposes  is the same as creating a Model Property in view model and then once I receive the data in DataReceived method to set directly the Model property and in its set method do an OnPropertyChanged("Model"); Is it ok? it seems simpler.
Comment: @BionicCode See my proposed solution below.
Comment: @BionicCode So.... if you use EF and have say 192 POCOs that represent your database, you'd rather leave the 192 POCOs as is and "re-invent the wheel" by creating another 192 INPC objects that just wrap the POCOs? Keeping in mind that each of those 192 POCOs average 10 columns? My, my, you're going to be busy creating 192 x 10 wrapper properties. Brb while I click a checkbox to have EF generate the POCOs with INPC support out of the box. Ok. Done. That was easy :). That being said, you shouldn't introduce V specific stuff in the model, of course. But binding directly to the model? Not AP at al
Comment: @SledgeHammer Sorry, I think I didn't get you. I feel like your polemic is inappropriate. Are you telling me to drop the 'VM' in 'MVVM' and bind the view directly to the model instead? In general or for this particular exception? It's not clear. What do you do if you have to add some logic or helpers to your POCOs? I wrote that there is a reason why one would like to have the model separated from the view. You must acknowledge this because this is why this patterns were invented. For example to solve problems of extensibility and testing.
Comment: @SledgeHammer If you don't need this separation than you can access the database directly from your view. Why bother with models and layers or architecture in general. You have requirements so satisfy them. Nobody forces you to use any pattern at all. Except when working on a professional level with a code base of  multiple million lines. Here you instantly know what you get from this patterns. By the way as a smart developer you can automate a lot of things. Also file generation. And again 'MVVM' without the 'VM' is 'MV' but not 'MVVM' anymore.
Comment: @BionicCode You were too hasty in assuming polemic to someone who disagrees with you to actually read and understand my response I guess. Oh well. I'll explain it to you again. No, I didn't say don't use VMs. Obviously. I said wrapping 192 x 10 EF classes which already support INPC to add nothing more then double INPC support is not architecture. I would tell you what it is, but you'd just get all polemicy on me :). A VMs job is to package data in a way for the view to consume it. It's not to arbitrarly wrap things while adding zero value.
Comment: @BionicCode In the EF example, you'd still have a VM obviously since views don't connect to data directly. The VM would expose the EF classes, not re-wrap them for no reason. But to say you can't use model classes directly is a wrong assumption. The correct answer is "it depends". And again, the important point here is that Views ALWAYS bind to VMs. You told the guy he can't expose model classes in his VM which is just plain wrong. Sorry. #NoPolemic.
Comment: @BionicCode In the case of a very large database that changes often, yes, I would expose the model classes in my VM. You are not decoupling them by using an adapter, you are just hiding the dependency :). Also, you are introducing a maintenance nightmare: 192 x 10 = 1920 properties. Ok, guess what? I just changed the structure of 7 of the tables and 3 of the FK relationships and 36 properties. Good luck re-syncing the wrapper classes, cuz I'm not giving you a list of what I changed. And I'm not saying that to be rude, I mean it like in the real world...
Comment: @BionicCode I mean, if you ask the guy changing the DB to keep a strict list of every little thing he changes on a daily basis so you can keep the wrapper classes in sync, he's going to tell you to go pound sand :). He's got better things to do. Now, I'm *very* strict by the book MVVM in my code, but in the particular case of EF, I'd re-use the model classes.
Answer: I would suggest a generic view model:
public class BaseViewModel<TModel>
{
    public TModel Model
    {
        get;
        private set;
    }

    public BaseViewModel(TModel model)
    {
        this.Model = model;
    }
}

Then you can bind to it:
<TextBlock Grid.Row="3" Grid.Column="0" Text="{Binding Path=Model.SomeField1}" />

Comment: And how do you notify to the view the changes in Model property?
Comment: Missing OnPropertyChanged("Model"); within private set method in Model property?
Comment: I wonder if it is not the same as doing the following instead of passing it to the view model constructor: once I receive the data in DataReceived set directly the Model property you have created in your answer and in its set method, do a OnPropertyChanged("Model"); Is it not the same and simpler?
Comment: When receiving new data, set the Model property to the new Instance and raise PropertyChanged on Model Property in ViewModel should do it: public void DataReceived(MyModel data) { this.Model = data; }
Comment: See my proposed solution.
Comment: As BionicCode commented on my proposed solution (same as yours but without using a generic view model), this is anti-pattern. It does not follow and respect MVVM pattern. When you bind to a view model property that is the Model is the same as binding to the model directly. This cause the view to depend on view model and also model. As view depends on model it breaks MVVM pattern.
Answer: I was thinking if below it is ok and respects MVVM pattern. I have thought it after seeing solution proposed by c0d3b34n. I think it is simpler and no need to do interfaces and generic view model. I have checked and it works:
Declare a property in view model:
private MyModel _model;
public MyModel Model
{
    get { return _model; }
    set
    {
         _model = value;
         OnPropertyChanged("Model");
    }
}

Then in the view:
<TextBlock Grid.Row="3" Grid.Column="0" Text="{Binding Path=Model.SomeField1}" />

... and the same for the rest of TextBlocks.
Finally:
 private void DataReceived(MyModel data)
 {
       this.Model = data;
 }

But as said by BionicCode in comments, this solution breaks MVVM pattern.
Comment: Because you asked me I will give you my opinion on this approach. Binding to a property on your view model that is the model is the same as binding to the model directly. Your view model is exposing the model as a public property to the view. To make this work your model would need to implement INotifyPropertyChanged. This should make your alarm bells ring loud. INotifyPropertyChanged is to be implememnted by the view models only as this enables binding in the WPF framework. You can do this, but it's not MVVM conform.
Comment: Why? The view has explicit  access to the model via binding no matter if using XAML or C#. As soon as you specify the model as a binding source you created a dependency. Your example creates two dependencies for the view.: View --> Model and View --> ViewModel.
Comment: I highly recommend reading some article on the web like this: https://www.codeproject.com/articles/278901//Articles/278901/MVVM-Pattern-Made-Simple .After you read this you can come back with questions to help you to fully understand the pattern. This is not meant to be rude. It will really help you. Afterwards you can make that kind of design decisions by yourself. I'll promise you.
Comment: All patterns have a purpose. You have to understand this purpose before you can make right decisions and choices. Just google benefit of MVVM or MVP to learn what those pattern can do for you. Than decide if you need this patterns. Sometimes you have to leave your IDE and go back to school to gain and improve knowledge. Knowledge and understanding is very important but doesn't come by itself. Please read the link I provided to you.
Comment: When you want to use MVVM then you can't get around to bind to your view models and only the view models. Please read the link to understand why, if you didn't understood my explanation.
Comment: @BionicCode  ok, thanks. I will read the article. Anyway, when you say I should implement Inotifypropertychanged in Model it is not necessay since my view model already implements it. So by  assigning it in Datareceived method with the one passed as parameter and then doing an Onpropertychanged in the set method of view model property works. Now I understand you, since view is bound to the view model property and this property is the Model, then it makes view to depend on view model and also model and hence it breaks MVVM pattern since view should only depend on view model.
Comment: @BionicCode So solution proposed by c0d3b34n which makes use of a generic view model breaks MVVM pattern as well because in fact he bind view to a property in view model that is the Model.
Comment: Correct. If he would do it like you did in your question than the model would be hidden from the view. Only the view model would know that there is a model and how to interact with it. But he made it a public property. To make this binding work the model would need to implement INotifyPropertyChanged (which is wrong) otherwise there would be no working binding. I am repeating myself. Please read the link above. First image on top of the page is showing the dependency graph of this pattern. Scroll to bottom of the page to see the advantages of the pattern. Read everything in-between.
Comment: Your goal must be to be able to explain to this guy why his approach is wrong in terms of MVVM.
Comment: That makes sense ... thank you for explaining, @BionicCode!

Binding WPF Grid to a model


unblock ip address after failed login attempts?

Question: I have a custom login page that I'm testing the javascript on. The events I'm testing happen after a failed login.
After running a couple tests, I've managed to get my ip blocked because of numerous failed logins!
I have access to the database. How can I unblock my ip so I can continue to test my features without interruption?
Answer: I presume you are using Drupal 7. Did you check the {blocked_ips} table?
Comment: Nope, I hadn't :)


Create native Excel XLS and XLSX files from scratch and export directly to a PDF file

Question: We are considering migrating a Cold Fusion application to Drupal and I am wondering if the below 2 capabilities that are current Cold Fusion application is doing can be done within Drupal 7.

The ability to create native Excel XLS and XLSX files (NOT HTML tables that Excel has to convert each time it opens) from scratch. This includes the ability to use native Excel functions, formatting, and multiple tabs/sheets. CFSPREADSHEET makes this a relatively simple task for us.
The ability to export directly to a PDF file. CFPDF and CFCONTENT makes this almost as simple as wrapping the desired output in a set of tags and it's done in many cases.
Answer:
PHPExcel. It uses PHPExcel library and supports native Excel export, import. We are using it in our project and it does it well.

The PHPExcel module allows developers to export/import data to/from real Excel files.

Print. It allows you to generate the printer-friendly, PDF, EPUB and Email versions of any node. It supports following libraries:

dompdf
mPDF (only in the 7.x-2.x branch)
TCPDF
wkhtmltopdf

I'd recommend using the wkhtmltopdf library, since I'm using it an it creates a way better results than the others.
Answer: I've made a loads of PDF's using PHP and if you need to do anything complicated (particularly with tables) dompdf just can't handle it so I've stopped using it all together.
I've had great success with fpdf however I know a colleague has been using tcpdf for some really complicated ones (tables with dynamic content that may or may not break over multiple pages).
I agree with Елин that PHPExcel is the best choice for excel files.


Unable to wrap text within JFace TableViewerColumn

Question: I am unable to wrap the text within a table cell when the width exceeds a specified limit.
Code:
public static void main(String[] args) {

    List<TableData> list = Collections.synchronizedList(new ArrayList<TableData>());
    list.add(new TableData("Jack Hodgins", 24) );
    list.add(new TableData("This is just aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            + "\n" + "hugeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
            + "\n" + "messsageeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
            + "\n" + "to checkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"
            + "\n" + "wrap functionalityyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", 50));

    Display display = new Display();
    Shell shell = new Shell(display);

    shell.setText("Table Viewer Column");
    shell.setSize(800, 1500);

    TableViewer viewer = new TableViewer(shell, SWT.FILL | SWT.MULTI |  SWT.H_SCROLL |  SWT.V_SCROLL |  SWT.FULL_SELECTION | SWT.BORDER);

    Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setSize(1000, 1500);
    table.setLayout(new TableLayout());

    /** Column for Name */
    TableViewerColumn columnName = new TableViewerColumn(viewer, SWT.MULTI | SWT.WRAP);
    columnName.getColumn().setWidth(250);
    columnName.getColumn().setText("Name");
    columnName.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public void update(ViewerCell cell)
        {
            Object element = cell.getElement();
            if (element instanceof TableData)
            {
                TableData obj = (TableData) element;
                cell.setText(obj.getName());

            }
        }
    });

    /** Column for Age */
    TableViewerColumn columnAge = new TableViewerColumn(viewer, SWT.NONE);
    columnAge.getColumn().setWidth(100);
    columnAge.getColumn().setText("Age");
    columnAge.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public void update(ViewerCell cell)
        {
            Object element = cell.getElement();
            if (element instanceof TableData)
            {
                TableData obj = (TableData) element;
                cell.setText(String.valueOf(obj.getAge()) );

            }
        }
    });

    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setInput(list);
    viewer.refresh();

    table.pack();
    shell.pack();
    shell.open();
    shell.setVisible(true);
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();

}

public static class TableData
{
    String name;
    int age;

    public TableData(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

}

I will know the width of my table cell, and if the text exceeds that limit I want the remaining text to be in a new line.
I have looked at other links from stackoverflow -
multiline feature or wrap text feature in jface tableviewer
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet231.java
http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet006TableMultiLineCells.java
Still unable to find a solution to this.
Kindly help.
Comment: You could use `OwnerDrawLabelProvider` but this means drawing the cells yourself. However Table always shows all rows with the same height so all the rows will have the height of the multiline row.
Comment: Hi greg.. So is it not possible that the cell height is increased in the table only when text size is more and for the others it remains as a singleline row?
Also can you please explain how to use OwnerDrawLabelProvider. I tried setting the LabelProvider as OwnerDrawLabelProvider and couldn't get the functionality required.
Answer: This owner draw label provider set as the label provider for columnName shows multiple lines split at the newlines. But all rows have the size of the deepest row.
  private static class NameLabelProvider extends OwnerDrawLabelProvider
  {
    private static final int TEXT_MARGIN = 3;

    @Override
    protected void erase(final Event event, final Object element)
    {
      event.detail &= ~SWT.FOREGROUND;
    }

    @Override
    protected void measure(final Event event, final Object element)
    {
      final TableData data = (TableData)element;
      final String text = data.getName();
      final Point size = event.gc.textExtent(text);
      event.width = size.x + 2 * TEXT_MARGIN;
      event.height = Math.max(event.height, size.y + TEXT_MARGIN);
    }

    @Override
    protected void paint(final Event event, final Object element)
    {
      final TableData data = (TableData)element;
      final String text = data.getName();
      event.gc.drawText(text, event.x + TEXT_MARGIN, event.y, true);
    }
  }

Comment: Thanks greg. Able to add multiline feature now. Just a question.. Is there any framework that I could use to create table which would provide automatic text wrapping dynamically based on table cell width and also only based on need will increase the size not for all the rows ?
Comment: I have never really looked for one as I don't need this. In principle it could be written in SWT, but different row heights make everything more complex.


Veins 4.7 Omnet++ 5.3 build error in windows 7

Question: I am new to vein and omnet++. I followed this tutorial http://veins.car2x.org/tutorial/ everything works up to step 3, for some reason veins 4.7 doesn't build in omnet++ 5 (IDE) and generates the following errors.
veins/modules/utility/TimerManager.cc
In file included from veins/modules/utility/TimerManager.cc:20:
.\veins/modules/utility/TimerManager.h:137:26: error: expected identifier
  enum class StartMode { RELATIVE, ABSOLUTE, IMMEDIATE };
                         ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1434:18: note: expanded from macro 'RELATIVE'
#define RELATIVE 2
                 ^
In file included from veins/modules/utility/TimerManager.cc:20:
.\veins/modules/utility/TimerManager.h:137:36: error: expected identifier
  enum class StartMode { RELATIVE, ABSOLUTE, IMMEDIATE };
                                   ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
In file included from veins/modules/utility/TimerManager.cc:20:
.\veins/modules/utility/TimerManager.h:138:24: error: expected identifier
  enum class EndMode { RELATIVE, ABSOLUTE, REPITITION, OPEN };
                       ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1434:18: note: expanded from macro 'RELATIVE'
#define RELATIVE 2
                 ^
In file included from veins/modules/utility/TimerManager.cc:20:
.\veins/modules/utility/TimerManager.h:138:34: error: expected identifier
  enum class EndMode { RELATIVE, ABSOLUTE, REPITITION, OPEN };
                                 ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:42:28: error: expected unqualified-id
  start_mode_ = StartMode::RELATIVE;
                           ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1434:18: note: expanded from macro 'RELATIVE'
#define RELATIVE 2
                 ^
veins/modules/utility/TimerManager.cc:48:28: error: expected unqualified-id
  start_mode_ = StartMode::ABSOLUTE;
                           ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:54:24: error: expected unqualified-id
  end_mode_ = EndMode::RELATIVE;
                       ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1434:18: note: expanded from macro 'RELATIVE'
#define RELATIVE 2
                 ^
veins/modules/utility/TimerManager.cc:60:24: error: expected unqualified-id
  end_mode_ = EndMode::ABSOLUTE;
                       ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:86:19: error: expected unqualified-id
  case StartMode::RELATIVE:
                  ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1434:18: note: expanded from macro 'RELATIVE'
#define RELATIVE 2
                 ^
veins/modules/utility/TimerManager.cc:88:30: error: expected unqualified-id
    start_mode_ = StartMode::ABSOLUTE;
                             ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:90:19: error: expected unqualified-id
  case StartMode::ABSOLUTE:
                  ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:98:17: error: expected unqualified-id
  case EndMode::RELATIVE:
                ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1434:18: note: expanded from macro 'RELATIVE'
#define RELATIVE 2
                 ^
veins/modules/utility/TimerManager.cc:100:26: error: expected unqualified-id
    end_mode_ = EndMode::ABSOLUTE;
                         ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:102:17: error: expected unqualified-id
  case EndMode::ABSOLUTE:
                ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
veins/modules/utility/TimerManager.cc:106:26: error: expected unqualified-id
    end_mode_ = EndMode::ABSOLUTE;
                         ^
C:\Users\Abu\src\omnetpp-5.3\tools\win64\mingw64\x86_64-w64-mingw32\include\wingdi.h:1433:18: note: expanded from macro 'ABSOLUTE'
#define ABSOLUTE 1
                 ^
15 errors generated.
make[1]: *** [Makefile:205: ../out/clang-debug/src/veins/modules/utility/TimerManager.o] Error 1
make[1]: Leaving directory '/c/Users/Abu/src/veins-4.7/src'
make: *** [Makefile:12: all] Error 2

I am also using sumo_win64_0.32.0. I am not sure what causes these errors.
Any help is greatly appreciated.
Comment: Seems like Veins 4.7 is not compatible with Windows. Would you mind trying Veins 4.6 instead?
Comment: Yup. This version of Veins needs an older version of OMNeT++. Sorry about that!
Comment: Fix merged into Veins 4.7.1
Comment: @ChristophSommer - Thanks for a response. Veins 4.6 builds with no errors. Thank you. :)
Comment: @ChristophSommer - But now for the last step of the tutorial I get following error mentioned in this link https://stackoverflow.com/questions/49967052/error-while-running-example-of-the-veins-in-the-last-step-of-the-installation
Comment: Okay, I'll try that. Thanks again.
Comment: @ChristophSommer - Thank you, everything works now.
Answer: The issue has been fixed in this pull request and a new minor revision of Veins (i.e. Veins 4.7.1) has been released. Please download the new version to get the fix and also some additional improvements.
Answer: I just encountered that error as well and here is what happens:
The compiler cannot resolve the enum class definitions in TimerManager.h as there is a name ambiguity with the macros RELATIVE and ABSOLUTE within wingdi.h
enum class StartMode { RELATIVE, ABSOLUTE, IMMEDIATE };

enum class EndMode { RELATIVE, ABSOLUTE, REPITITION, OPEN };

Quick fix is to rename the occurrences in TimerManager.h and TimerManager.cc to something else. E.g. Replace all occurrences of RELATIVE with RELATIVEINS and ABSOLUTE with ABSOLUTEVEINS etc... (or whatever you like). Worked fine for me with 4.7 and OMNet++ 5.2.1.
Comment: An alternative to editing the code is passing `NOGDI` to the preprocessor, which prevents the conflicting defines to be executed. See https://github.com/sommer/veins/issues/68.
Answer: A patch is required to make Veins 4.7 compile with OMNeT++ 5.3 for Windows. The cleanest way may be something like on https://pastebin.com/ymzqMpEF
- enum class StartMode { RELATIVE, ABSOLUTE, IMMEDIATE }
+ enum class StartMode { relative, absolute, immediate }

that is, renaming the all-uppercase enum class constants to lower case ones (something recommended by many coding styles as well).


What sound formats can the .NET library handle?

Question: I'm thinking of writing my own media player, just something basic to suit my own needs. I'm just wondering what formats the .NET libs can handle? All I can find in the libraries is WAV support, which is no surprise. Are there any other libraries that abstract other formats or what? Or can I do it better in another language?
Answer: Solution 1
.NET Framework handles wav files only, so you should use other decoders for various formats
For example for MP3 files you can use LAME
.NET Lame MP3
For other formats you can use FFDSHOW
FFDSHOWAPI
Solution 2
If you can do, you can develop your application with C++ so you won't need any wrappers
Solution 3
That's not the answer of your question but if you don't want to start from scratch. You can use
Windows Media Player COM Object
or
VLC COM Object
and embed Windows Media Player or VLC Player into your .NET application in a much more simpler way, so you don't have to deal with decoders or file formats.
Answer: If you are using WPF, then the MediaElement can be used to play pretty much anything that Windows can play.
Windows comes with various APIs for decoding and encoding audio, and to use them with .NET you'll need interop wrappers. I've written about how you can make use of the interop wrappers that come with NAudio to convert between any format that Windows supports here.


Anything slider to go back to first slide after initial loop

Question: like the title says, I'd like to go back to the first slide after looping through 4 slides.
I went through the options in jquery.anythingslider.js, but I couldn't figure how to implement this functionality.
Any help is greatly appreciated.
Answer: I'm not sure if this is exactly what you're asking for, but check out the FAQ page, listed under "How do I get the slider to play through my slides, then return to the first one and stop?", basically it needs this initialization code:
<code>$('#slider').anythingSlider({
  stopAtEnd  : true,  // If true & the slideshow is active, the slideshow will stop on the last page.
  onShowStop : function(e, slider){
    setTimeout(function(){
      // only go to the first page if the last page is visible
      if (slider.currentPage === slider.pages) { slider.gotoPage(1); }
    }, slider.options.delay);
  }
});
</code>
Please note: this code will always return the slider to the first panel, when on the last panel and the slide show stops.


How can i refactor my async code in javascript (I'm using Promise)

Question: dbConnect(res).then((conn)=>{
query(conn,res,
  `SELECT * FROM programs WHERE user_id = ?`,
  [
    user_id
  ]
).then((programList)=>{

  new Promise((resolved,rejected)=>{
    for(let i = 0; i<programList.length;i++){
      query(conn,res,`
        SELECT * FROM open_programs WHERE program_id = ?`,[programList[i].program_id])
      .then((opList)=>{
        Object.assign(programList[i],{openList : opList});
        console.log(programList[i]);
        if(i == (programList.length-1)){
          resolved(programList)
        }
      })
    }
  }).then((result)=>{
    conn.release();
    res.json(toRes(SUCCESS,
      { data : result }
    ));
  })
});
});

'dbConnect', 'query' is my custom method that consist of 'Promise'
In this code, first i get firstResults(schema=programs)
after i get nextsqlResults using firstResults.program_id in 'for loop'
and Object assign firstresult and nextResults
after assigning, I response the combined data
How can i develop this code?
Answer: Promise.all and Array.map will help you here. Untested:
dbConnect(res).then(conn => {
    return query(conn, res, `SELECT * FROM programs WHERE user_id = ?`, [
        user_id,
    ]);
}).then(programList => {
    return Promise.all(programList.map( program =>
        query(
            conn,
            res,
            `SELECT * FROM open_programs WHERE program_id = ?`,
            [program.program_id],
        ).then(opList => {
            console.log(program);
            return Object.assign(program, { openList: opList });
        });
}).then(result => {
    conn.release();
    res.json(toRes(SUCCESS, { data: result }));
});

You should verify that result is the data structure you're looking for after this code.
Cleaned up a little more with the babel-plugin-transform-object-rest-spread plugin:
dbConnect(res).then(conn =>
    query(conn, res, `SELECT * FROM programs WHERE user_id = ?`, [
        user_id,
    ])
).then(programList =>
    Promise.all(programList.map(program =>
        query(
            conn,
            res,
            `SELECT * FROM open_programs WHERE program_id = ?`,
            [program.program_id],
        ).then(opList => ({
            ...program,
            openList: opList
        })
    ))
).then(result => {
    conn.release();
    res.json(toRes(SUCCESS, { data: result }));
});

Comment: dbConnect()'s Promise return conn (connection), So this code I can't use conn when Promise.all(). How can i refactor?
Answer: @Andy Ray provided a nice solution. I just want to add some additional improvement.
In other to make thing readable, by extracting your existing code into smaller function you can organize your code like this:
connectDb(res)
    .then(selectPrograms)
    .then(populateOpenPrograms)
    .then(handleResult);

And if you want to improve it more, consider using async / await


Terminology question about parellelized morphisms

Question: Given $f:A\to B$ and $g:C\to D$, what is the correct name for the morphism $h: A \times C \to B \times D$ defined as
$$
h:(a,b) \mapsto (f(a),g(b))?
$$
Comment: It's also called *split* or *pair*, and in Haskell is denoted `(&&&)`. Your *parallelized* intuition is also mentioned on the first page here: https://github.com/alhassy/CatsCheatSheet/blob/master/CheatSheet.pdf ;-)
Answer: It's called the product morphism of $f$ and $g$.


Como passar values de activity para util?

Question: Gostava de trabalhar com o objecto item mas preciso de passar valores da minha actividade para  o util alguem  me consegue explicar?
    item[0] = new ColorClothes();
    item[0].record = 0;
    item[0].color = "blue";
    item[0].clothes = "shoes";

Por exemplo onde tem "blue" gostaria de estar a receber um valor da minha actividade como seria possível?
import java.util.*;

    public class ColorClothes
    {
        public int record;
        public String color;
        public String clothes;

        public static void main(String[] args)
        {
            Initialize();
        }

        public static void Initialize()
        {
            ColorClothes item[] = new ColorClothes[4];

            item[0] = new ColorClothes();
            item[0].record = 0;
            item[0].color = "blue";
            item[0].clothes = "shoes";

            item[1] = new ColorClothes();
            item[1].record = 1;
            item[1].color = "yellow";
            item[1].clothes = "pants";

            item[2] = new ColorClothes();
            item[2].record = 2;
            item[2].color = "red";
            item[2].clothes = "boots";

            item[3] = new ColorClothes();
            item[3].record = 3;
            item[3].color = "black";
            item[3].clothes = "coat";

            System.out.println("Unsorted");

            for(int i = 0; i < item.length; i++)
            {
                System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
            }

            System.out.println("\nSorted By Color\n");

            Arrays.sort(item, new ColorComparator());

            for(int i = 0; i < item.length; i++)
            {
                System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
            }

            System.out.println("\nSorted By Clothes\n");

            Arrays.sort(item, new ClothesComparator());

            for(int i = 0; i < item.length; i++)
            {
                System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
            }

        }

    }

Comment: Não entendi direito o que você deseja, você tem um Objeto, com 3 atributos, depois você criar um array desse mesmo objeto e usa ele dentro desse objeto.
Comment: Por exemplo se eu tiver noutra actividade posso usar esse objecto ?
Comment: Bom, essa classe está um pouquinho mal formada para mim, sua intenção com o ´Initialize()` é retornar um array com 4 objetos pré inicializados ?
Comment: sim é isso mesmo
Answer: Bom Tiago,
Não vejo um bom motivo para tentar instanciar uma classe dentro dela mesma, acredito que o ideal seria você dividir essa classe em duas, sendo uma somente com o Objeto que terá as informações, e uma para fazer essa inicialização, arranjo e etc..
Desta forma, cada "item" terá uma instância separada de dados.
Então, eu faria da seguinte forma:
ColorClothes:
public class ColorClothes
{

    public ColorClothes() // <------ método construtor
    {
       // O ideal é usar esse método somente para trazer parâmetros iniciais para a instância
    }

    public Clothes[] Initialize(Clothes[] item)
    {
        Clothes item[] = new Clothes[4]; // <----------- utilize o objeto com os dados

        System.out.println("Unsorted");

        for(int i = 0; i < item.length; i++)
        {
            System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
        }

        System.out.println("\nSorted By Color\n");

        Arrays.sort(item, new ColorComparator());

        for(int i = 0; i < item.length; i++)
        {
            System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
        }

        System.out.println("\nSorted By Clothes\n");

        Arrays.sort(item, new ClothesComparator());

        for(int i = 0; i < item.length; i++)
        {
            System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
        }

 return item;

    }

    public class Clothes{
        public int record;
        public String color;
        public String clothes;
   }

    }

Na sua Activity:
Clothes[] listaClothes = new Clothes[4];
            listaClothes [0] = new Clothes();
            listaClothes [0].record = 0;
            listaClothes [0].color = "blue";
            listaClothes [0].clothes = "shoes";
        listaClothes [1] = new Clothes();
        listaClothes [1].record = 1;
        listaClothes [1].color = "yellow";
        listaClothes [1].clothes = "pants";

        listaClothes [2] = new Clothes();
        listaClothes [2].record = 2;
        listaClothes [2].color = "red";
        listaClothes [2].clothes = "boots";

        listaClothes [3] = new Clothes();
        listaClothes [3].record = 3;
        listaClothes [3].color = "black";
        listaClothes [3].clothes = "coat";

ColorClothes colorClothes = new ColorClothes();
Clothes[] listaOrganizada= colorClothes.Initialize(listaCloth);

Pronto, você terá na sua activity o Objeto que era o "item", contendo os 4 objetos.
Comment: O objectivo era o inverso era colocar lá os objectos em vex de retirar eu não sei como manipular o código para isso
Comment: Você quer colocar 4 objetos e retornar eles organizados é isso ?
Comment: Mas o valores estão fixados no Initialize
Comment: Sim desde já obrigado
Comment: É a minha primeira vez usando util se achar que initialize não é necessário para o pretendido pode retirar estou tentanto aprender a sortear multiplos arrays de uma actividade que sao enviados para ai para o util
Comment: o Util é a biblioteca que organiza, não tem a ver com o modo de construir a classe, posso alterar para um método em  que você receba um array e retorne ele no final organizado, os dados normalmente são mutáveis, então você tem que atribuir os valores na Activity, que é o mais usual
Comment: É exactamente isso que procuro e obrigado pela explicação


how to make the dropdown appear in default structure

Question: hello friends i have a drop down menu ,which appears and disappears on alternate clicks.
But when I click the  list  third time ,the entire list appears (multiple drops appears unclosed). How can I make the list appear back in default structure?
Here is my code. Please help.

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

Multi-Level Dropdowns

In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).

Note that we have added jQuery to open the multi-level dropdown on click (see script section below).

      <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials
</button>

HTML

CSS

          New dropdown

2nd level dropdown

2nd level dropdown

              Another dropdown

3rd level dropdown

3rd level dropdown

Answer: You need to hide the sub-dropdowns when the main dropdown is closing.
You can do this by listening to the hide.bs.dropdown event.

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

Multi-Level Dropdowns

In this example, we have created a .dropdown-submenu class for multi-level dropdowns (see style section above).

Note that we have added jQuery to open the multi-level dropdown on click (see script section below).

      <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials
</button>

HTML

CSS

          New dropdown

2nd level dropdown

2nd level dropdown

              Another dropdown

3rd level dropdown

3rd level dropdown


android fragment running in the background

Question: i have the following chain of call

in a fragment i call zxing integrator to scan qr code which returns the result in
 onActivityResult(int requestCode,int resultCode,Intent data)

of the fragment
The onActivityResult calls an Asynctask,
new getStaffIdTask(choosen_schema_for_scanning,userid).execute((Void)null);

whose onPostExecute(final Boolean success) calls the fragments listener as..
 if(mListener!=null)
            mListener.onScannedStaff(tableName,Integer.parseInt(id),Integer.parseInt(userid));

Back on the host activity the onScannedStaff function is called and in it replaces a fragment
@Override

public void onScannedStaff(String tableName, int staffid,int staffUid)
{
    getSupportActionBar().setTitle("Staff Profile");
Fragment fragment= StaffProfileFragment.newInstance(tableName,staffid,staffUid);
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.home_boss_base,fragment,"scannedstaff");
transaction.addToBackStack(null);
transaction.commit();

}

The problem is that this fragment runs in the background and its not showing any UI at all. I cant figure out how to show it
Answer: I have also faced the same issue. Use LinearLayout in the fragment. Background color should be white and it should be clickable.
And don't call UI element from OnPostExecute().that will give you some major memory leaks or errors.


Expected number of rounds

Question: Assume that we have a bucket containing $n$ different balls. At each round we choose at random a subset of the remaining balls, and remove them from the bucket. We keep doing this until the bucket gets empty. What is the expected number of rounds?
I tried to find the distribution of the number of rounds $m$:
for $m = 1$ the probability is $1/2^{n}$.
for $m = 2$ the probability is $1/2^{n} - 1/2^{2n}$.
But for bigger number of rounds it gets complicated. Is there other way to find the expected number of rounds or at least estimate it?
Comment: Equivalent model: pick each ball $i$ after a random time $T_i$ and assume that $(T_i)$ is i.i.d. geometric with parameter $\frac12$, thus $P(T_i\geqslant t)=2^{1-t}$ for every positive integer $t$. The expected number of rounds is $T=\max T_i$ hence $$E(T)=\sum_{t\geqslant1}P(T\geqslant t)=\sum_{t\geqslant1}(1-P(T<t))=\sum_{t\geqslant1}(1-P(T_1<t)^n)={}{}{}{}\sum_{t\geqslant1}(1-(1-2^{-t})^n).$$ From here one can expand each $(1-2^{-t})^n$ into powers of $2^{-t}$, leading to $E(T)$ as a sum of $n$ terms of alternating signs, namely, $$E(T)=\sum_{k=1}^n{n\choose k}\frac{(-1)^{k-1}}{2^k-1}.$$
Answer: When you choose a random subset of the balls in the tub, each ball has $\frac 12$ chance to be in that subset.
Answer: Completely revised.
Let $E_n$ be the expected number of rounds when starting with $n$ balls. Clearly $E_0=0$. For $n>0$ we must have
$$\begin{align*}
E_n&=\frac1{2^n}+\frac1{2^n}\sum_{k=1}^n\binom{n}k(E_k+1)\\
&=1+\frac1{2^n}\sum_{k=1}^n\binom{n}kE_k
\end{align*}$$
and hence
$$(2^n-1)E_n=2^n+\sum_{k=1}^{n-1}\binom{n}kE_k\;.$$
Thus, by straightforward calculation we find that $E_1=2$, $E_2=\frac83$, $E_3=\frac{22}7$, and $E_4=\frac{368}{105}$.
Checking OEIS for the sequence $\langle 2,8,22,368\rangle$ of numerators, we get one hit, OEIS A158466. This is described as:

Numerators of $EH(n)$, the expected value of the height of a probabilistic skip list with $n$ elements and $p=1/2$

This doesn't convey anything to me, but the following comment by Geoffrey Critzer does:

$n$ fair coins are flipped in a single toss. Those that show tails are collected and reflipped in another single toss. The process is repeated until all the coins show heads. $H(n)$ is the discrete random variable that denotes the number of tosses required. $\Bbb P\big(H(n)\le k\big) = \left(1-(1/2)^k\right)^n$.

This is clearly the balls and bucket problem in a slightly different guise. The entry goes on to note that
$$\begin{align*}
EH(n)&=\sum_{k>0}k\left(\left(1-\left(\frac12\right)^k\right)^n-\left(1-\left(\frac12\right)^{k-1}\right)^n\right)\\
&=-\sum_{k=1}^n(-1)^k\frac{\binom{n}k}{1-\left(\frac12\right)^k}
\end{align*}$$
but gives no closed form. As a quick check, the last formula gives
$$E_3=\frac3{\frac12}-\frac3{\frac34}+\frac1{\frac78}=6-4+\frac87=\frac{22}7\;.$$
Comment: But the number of turns for removing all balls isn't the sum of the number of turns for removing the individual balls -- so there's no linearity.
Answer: Premise revised
If the choice of the balls to extract is done on a yes/no basis, than it corresponds to the "coins re-flipping" mechanism introduced in the other reply. Then the number of balls extracted is not uniformly distributed.
If the choice instead is just on the number of balls to extract, a uniform random value from 1 to the number of balls remaining, then:

The number of different runs that make the box empty clearly corresponds to the No. of Compositions of $n$ into exactly $m$ parts, which is $\left( \matrix{
  n - 1 \cr  m - 1 \cr}  \right)$, and when summed over $m$ gives the total of $2^{\,n - 1} $.
Re.: https://en.wikipedia.org/wiki/Composition_%28combinatorics%29
So the probability of a $m$-run is $$
\left( \matrix{
  n - 1 \cr
  m - 1 \cr}  \right)/2^{\,n - 1}
$$
and the expected value of m is therefore:
$$
\eqalign{
  & E(m)\quad \left| {\;1 \le n} \right.\quad  = {1 \over {2^{\,n - 1} }}\sum\limits_{1\; \le \,m\, \le \,n} {m\left( \matrix{
  n - 1 \cr
  m - 1 \cr}  \right)}  =   \cr
  &  = {1 \over {2^{\,n - 1} }}\sum\limits_{1\; \le \,m\, \le \,n} {\left( {\left( {m - 1} \right)\left( \matrix{
  n - 1 \cr
  m - 1 \cr}  \right) + \left( \matrix{
  n - 1 \cr
  m - 1 \cr}  \right)} \right)}  =   \cr
  &  = {1 \over {2^{\,n - 1} }}\left( {2^{\,n - 1}  + \left( {n - 1} \right)\sum\limits_{2\; \le \,m\, \le \,n} {\left( \matrix{
  n - 2 \cr
  m - 2 \cr}  \right)} } \right) =   \cr
  &  = {1 \over {2^{\,n - 1} }}\left( {2^{\,n - 1}  + \left( {n - 1} \right)2^{\,n - 2} } \right) = 1 + {{n - 1} \over 2} \cr}
$$
Answer: I am using Did's and Ross' interpretation.
For $m=2$, the probability is $$\left(\frac34\right)^n-\left(\frac12\right)^n$$
The first term is the probability that all $n$ coins have been chosen, and the second term is probability that $m\leq1$.  In the same way, the probability that $m=3$ is $$\left(\frac78\right)^n-\left(\frac34\right)^n$$

Expected number of rounds


Is there a branch of statistics that tries to explain "why" the dataset has certain statistical properties?

Question: Suppose I have a big dataset and I compute some statistical summary of it - e.g., the correlation of one dimension with another.
I think a reasonable question to ask would be "what data points explain this result" - e.g., perhaps, is it because there were two huge outliers that explain the whole correlation? Or are all the points approximately equally important?
Or, say, I'm computing the mean of a response variable, measured at two different values of a parameter. I find that the means are approximately the same. Why is it the same: is it because the distributions of the variable are the same, or is it because the mean in both cases is determined by a big cluster that determines the whole mean and obscures the differences in the rest of the distribution?
I guess, in general, I'm interested in computing sensitivity of a statistical summary (not necessarily a single number) w.r.t. the data points and parameters involved in the computation.
This notion of sensitivity would help me, on one hand, avoid meaningless results (which are entirely explained by abnormal measurements), and on the other hand, it would guide my further exploration.
So: is there a branch of statistics that studies this kind of sensitivity? If yes, what are some useful methods from that branch / what further reading would you recommend? E.g., I imagine, it might be useful for data visualization methods - not just draw the data, but colorize each point according to the sensitivity of some metric w.r.t. that point?
I tried googling for things like "explanatory statistics", "sensitivity of statistical summaries" etc., but did not find a lot. I found Uncertainty and sensitivity analysis but this is not quite what I'm looking for - I'm not interested in sensitivity of one variable of the dataset w.r.t. the other, I'm interested in making deeper answers to questions like "do these two variables correlate".
P.S. Some more googling yielded the keyword "input importance" and some data visualization methods, e.g. http://vis.cs.ucdavis.edu/papers/TVCG_Chan_GSS.pdf . But I crave for advice from experts :)
Comment: For a classic, systematic approach to such questions in a least squares regression context, please see [*Regression Diagnostics* (Belsley, Kuh, & Welsch, 1980)](http://books.google.com/books/about/Regression_Diagnostics.html?id=GECBEUJVNe0C).
Answer: I am not sure if there is a pithy title for this entire topic but it is certainly an important issue. Maybe "robust statistics" would be a good place to start?
The aptly-named empirical influence function describes how an estimator (e.g., the mean or median) depends on the value of one of the points in its sample. It can also be generalized into the "influence" or sensitivity function, which asks how the estimator's value changes as the distribution of the data shifts.
You could also consider an estimator's breakdown point, which is essentially the proportion of "bogus" values (e.g., arbitrarily large) that an estimator can tolerate. The mean, for example, has a breakdown point of zero because you can completely change its value by subsituting an arbitrarily large positive or negative value for a single point in the data set. On the other hand, the median is very resistent to this sort of 'attack;.
In regression contexts, DDFITS is a diagnostic which asks "how does the prediction for this point change if that is included/excluded in the analysis?" Cook's Distance (or Cook's D) is a similar quantity (they're calculated differently, but are inter-convertible). Leverage is a related quantity but is only affected by the independent variables' values, rather than both the independent and dependent ones.


Python os.mkdir, arquivo existe

Question: Estou tentando baixar vídeos, mas quando vai para o próximo, retorna o erro dizendo que a pasta já existe. Não tem como salvar mais de um arquivo?

def json_decoder(file):
    file = open(file, 'r')
    file = file.read().replace(chr(92) * 2, chr(92))
    file = codecs.decode(file, 'unicode_escape')
    lst = file.split('\n')
    tofile = []
    for i in lst:
        if not i:
            continue
        j = i.index(':') + 2
        k = i.index('"', 50)
        tofile.append(i[j:k])
    try:
        os.remove('output.txt')
    except OSError:
        pass
    with open('output.txt', 'w') as f:
        for item in tofile:
            print(item)
            f.write(f'{item}\n')

def get_files(data):
    file = open(data, 'r')
    lst = file.read().split('\n')
    for i in lst:
        if not i:
            continue
        video_name = i.split('=')[-1]
        print('Downloading ' + video_name)
        r = requests.get(i)
        if os.path.isdir("./" + video_name.split('-')[0]) is not True:
            os.mkdir(video_name[:3])
        with open(video_name[:3] + "/" + video_name, 'wb') as f:
            f.write(r.content)

Estou usando python 3.7.1
Comment: Pode salvar mais de um arquivo, o que não pode é criar uma pasta já criada.
Answer: Tente usar o os.makedirs(), ele possui um parâmetro exist_ok que quando True não dá erro se a pasta já existir.
os.makedirs(video_name[:3], exist_ok=True)

Comment: Perfeito! Era isso, valeu! Deu certíssimo! Obrigado

Python os.mkdir, arquivo existe


Automatically creating an organic group for each new user

Question: I would like to automatically create an organic group for each newly registered user. I checked all its settings, no such option with organic groups module, then I checked the rules module but no luck there too.
Comment: why would anyone give this post negative reputation.
Answer: The Rules module will allow you create new OG node after saving a new user account. Just do the following:

Create your rule and add an "After saving new user account" event to it
In the action section, select create a new entity
In the value section, select node
In the Content type, select the "your group"
Fill the title as you need; you can do this using the token
Fill the author

Hope this will help.
Answer: Ok so here is another way to do it, if you dont want to do it with rules;
<code>    <?php
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Alters the user registration form by adding a custom submit handler.
 */
function custom_user_insert(&$edit, $account, $category) {
  if (isset($account->is_new) && !empty($account->is_new)) {
        //print '<pre>';print_r($account);die;
        $groupnode = new stdClass();
        $groupnode->type = 'group'; // Group node type
        $groupnode->status = 1;
        //$groupnode->language ='de';
        $groupnode->uid =  $account->uid;
        $groupnode->title = 'group name comes here';
        //here I save the uid from the activated user
                $groupnode->field_group_user['und'][0]['value'] = $account->uid;
        //$groupnode->{OG_GROUP_FIELD}['und'][0]['value'] = '1';

        node_save($groupnode);
        $nid = $groupnode->nid;
        og_group('node',$nid, array(
        //"entity type"       => 'user', // defaults to user
        "entity"        => $account->uid,
        'field_name' => 'og_user_node',
        'state' => 1,//OG_STATE_ACTIVE
        'force_reload' => TRUE
        ));
        //drupal_set_message('Do it!');
        // Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member)
        og_role_grant('node',$nid, $account->uid, 12);

        $assign_grp_userid=loadusers_by_role('coaching expert');

         og_group('node',$nid, array(
        //"entity type"       => 'user', // defaults to user
        "entity"        => $assign_grp_userid,
        'field_name' => 'og_user_node',
        'state' => 1,//OG_STATE_ACTIVE
        'force_reload' => TRUE
        ));
        //drupal_set_message('Do it!');
        // Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member)
        og_role_grant('node',$nid, $account->uid, 11);
  }
}
</code>


How to show that $\exists_f(M)\le N$ iff $M\le f^*(N)$ for two subobjects $M$ and $N$?

Question: I have read a lecture note by Jaap van Oosten and I get stuck from the exercise 69 b). The exercise asks how to prove $\exists_f(M)\le N$ implies $M\le f^*(N)$ and vice versa. Here $M$ is a subobject of $X$ represented by a map $m:E\to X$ (I guess we can identify $M$ to $m$.), $N$ is a subobject represented by $n:F\to Y$, $f:M\to N$ and $f^*(N)$ is a suboject from the map $f^*(n):X\times_YF\to X$, where $f^*(n)$ given by the following pullback diagram:
$$\begin{matrix}
X\times_YF&\longrightarrow&F\\
\sideset{f^*(n)}{}\downarrow\qquad&&\quad\sideset{}{n}\downarrow\\
X&\underset{f}{\longrightarrow}&Y
\end{matrix}$$
In addition, $\exists_f(M)$ is the subobject corresponeded to the map given by the image factorization of $fm:E\to Y$. Moreover, for two subobject $A$, $B$ of $X$ representated by maps $a:A_0\to X$ and $b:B_0\to X$ respectively, $A\le B$ if and only if there is a map $i:A_0\to B_0$ such that $bi=a$.
To repeat, I have tried to show that $\exists_f(M)\le N$ iff $M\le f^*(N)$.
From $M\le f^*(N)$ I have found a representative candidate of $\exists_f(M)$, namely $f\circ f^*(n)$. However I can't prove that it gives a pullback diagram.
Thanks for any help.
Answer: I'm gonna call the subobjects $m : M \to A,n : N \to B$ and so on. There is a morphism $a : f^*(N)\to N$ with $n\circ a = f\circ f^*(n)$ ($X\times_Y F = f^*(N))$
Suppose $m\leq f^*(n) : f^*(N) \to A$. That means there is a morphism $b : M \to f^*(N)$ with $f^*(n)\circ b = m$. Then there is an $e$ with:
$$f\circ m = \exists_f(m) \circ e = n\circ (a\circ b)$$
By the universal property of images there is a (unique) morphism $u$ with $n\circ u = \exists_f(m)$, i.e. $\exists_f(m) \leq n$.
For the other direction assume $\exists_f(m) = n\circ a$. Then $n\circ (e\circ a) = f\circ m$, so by the universal property of the pullback there is a (unique) morphism $u$ such that $f^*(n)\circ u = m$ meaning $m\leq f^*(n)$.
Answer: Suppose first that $\exists_f(M)\leq N$, so that there exist some morphism $\sigma : I\to F$ such that $n\circ \sigma=\hat m $, where $E\xrightarrow{q} I\xrightarrow{\hat m} Y$ is the image factorization of $f\circ m$. Then $n\circ \sigma\circ q =\hat m\circ q=f\circ m$, so there exist a unique $t:E\to X\times_Y F$ such that $f^*(n)\circ t=m$, which proves that $M\leq f^*(N)$.
For the converse, assume that $f^*(n)\circ t=m$ for some arrow $t$; then $f\circ m=f\circ f^*(n)t=n\circ g\circ t$ (where $g$ is the pull-back projection $X\times_Y N$). Now because $n$ is a mono, if you take the image factorisation of $g\circ t$ to be $\hat t\circ p$, then the image of $f\circ m$ will be given by $n\circ \hat t$; and this proves that $\exists_f(M)\leq N$.


Redirecionar p/ nova página ao ocorrer erro 404 usando Laravel 5.6

Question: Qual a forma correta do Laravel 5.6 redirecionar outra rota caso seja erro 404?
Criei a pasta resources/views/errors/ e o arquivo 404.blade.php,
e funcionou certinho, mas
não quero exibir o erro, quero que seja redirecionado para outra página.
Answer: Você pode criar um script dentro da pagina 404.blade.php para fazer este redirecionamento, desta forma:

Ou alterar o arquivo app/Exceptions/Handler.php do Laravel, na linha onde contem o seguinte trecho de código:
return response()->view('core.layout::error', ['code' => 404, ...

Onde você pode colocar a rota que você quiser no lugar de 'core.layout::error'.
Comment: Não encontrei essa linha, acho que na versão 5.6 não existe mais. Vou ficar com o JS mesmo. Valeu Wictor!
Comment: @LuisAlberto e Wictor, talvez isto ajude a esclarecer mais um pouco: https://pt.stackoverflow.com/questions/232812/por-que-minhas-p%c3%a1ginas-de-erro-personalizada-n%c3%a3o-est%c3%a3o-sendo-chamadas/232822#232822 . É dentro desse ficheiro que o Wictor falou que podes colocar esse JS
Comment: Miguel, foi exatamente isso que fiz, mas acho que ficaria melhor usando o sistema do Laravel. Valeu!


asp.net API post values received are null from postmaster

Question: I'm writing a very basic asp .net api with a simple post method. The post parameters are returning null. I've tried various ways to get the method to return the object I passed in. I created a data transfer object and I've verified that the method is getting called. What else can I check ?
post master settings ----
<code>     url: /api/values
     params: incidentID 4
     params: incidentTitle 'this is some text'
     Content-Type: application/json

    // POST api/values
    [Route("api/values")]
    [HttpPost]
    public HttpResponseMessage Post([FromBody]incidentDTO incid)
    {
        return Request.CreateResponse(incid);

    }
</code>
Comment: Could you add the client logic to post?
Comment: from postmaster? I'm not using any js frameworks as yet.
Comment: How does this incidentDTO class look like? I think the binder can't turn these params into your incidentDTO object.
Answer: I assume by postmaster you meant Postman.
If you are using Postman, use the following steps:
Under Header type Content-Type with a value of application/json
Select the raw tab and enter your data as follows:
<code>{
    "incidentId":4,
    "incidentTitle":"this is some text"
}
</code>
Click on Send.
As the post data is being read from the body, there in no need to enter any values in URL params.
Comment: aha, it's returning an object now. I was passing my parameters as form data.


What is the difference between , , and ?

Question: It seems there isn't much difference between  and <button> from this other question, but is it also fine to use
 ?
Comment: It depends on the case, but generally not, since without further changes, it won't be focusable/activatable with a keyboard and accessibility tools like screen readers won't call it out as a button. Better to restyle a real `` if you want a button. (So, please specify in the question if you want a button.)
Comment: onclick on all tags are same
Answer: Roughly,
 by default has a click handler that parses the URL and decides on the right sort of action; for HTTP(S) URLs, it will dispatch a HTTP(S) request and replace the window.location with the new URL, and also correctly handle History. It also has other handlers associated with other events, not just click.
<button type="submit"> by default has a click handler that finds out where a form is supposed to be submitted to, constructs the URL in case of a GET method or a request body in case of a POST method, then does the same thing as above.
<... onclick="..."> will invoke the JavaScript code in the onclick attribute if the element is dispatched a click event. This code can do whatever it wants, including (but not limited to) simulating any of the above's response to a click — but you will have to write it yourself. If you would want to simulate everything the above ones do, it would be a lot of work, and the devil is in details. (Also, writing JavaScript code inside HTML attributes is icky; one would do well to bind event handlers from JavaScript code using addEventListener.)
Answer: someText is going to only make the text within the anchor clickable to take you to some URL that you specify
<button (click)='someFunction()'> is different in a couple of ways. In your UI it is going to create a clearly clickable button rather than some underlined text. Clicking the button will then perform some sort of function that you specify that button to perform whether thats a JavaScript/TypeScript function or something else.

 is similar to button onClick in that it will perform some sort of function that you write, but it makes the whole division a clickable area rather than simply a button.


function inequality $f(x+y)+y \leq f(f(f(x)))$

Question: $f(x+y)+y \leq f(f(f(x)))$
find all possible solution for $ f: \mathbb {R} \rightarrow \mathbb {R}$
Comment: Please add some context. Where did you find this problem? And have you tried anything yourself?
Comment: I would start with the fact that $f(y)+y\le f(f(f(0)))$ so $f(x)\le -x+c$ where $c=f(f(f(0)))$.  $f(x)\le f(f(f(x)))$, too (but it's a lot harder to apply this).
Comment: I found this problem from my country's training
Answer: Let $y=f(f(x))-x$.  Then $f(f(f(x)))+f(f(x))-x\le f(f(f(x)))$ or $f(f(x))\le x$.  If we substitute, we obtain $f(f(f(u)))\le f(u)$.  If $y=0$, though, we know $f(x)\le f(f(f(x)))$ so we have $f(x)=f(f(f(x)))$.
Now consider $f(x)=f(f(f(x)))\ge a-x+f(x+a-x)$ and so $f(x)+x\ge f(a)+a$ for all $x,a\in\Bbb R$.  This also implies $$ f(a)+a\ge f(x)+x\ge f(a)+a\\\Longrightarrow f(x)+x=f(a)+a $$
Therefore $f(x)=f(0)-x$.  $$f(x+y)+y=f(0)-x-y+y=f(0)-x=f(x)=f(f(f(x)))$$
So this works for all $f(0)$.


When are Monte Carlo methods preferred over temporal difference ones?

Question: I've been doing a lot of research about Reinforcement Learning lately. I followed Sutton & Barto's Reinforcement Learning: An Introduction for most of this.
I know what Markov Decision Processes are and how Dynamic Programming (DP), Monte Carlo and Temporal Difference (DP) learning can be used to solve them. The problem I'm having is that I don't see when Monte Carlo would be the better option over TD-learning.
The main difference between them is that TD-learning uses bootstrapping to approximate the action-value function and Monte Carlo uses an average to accomplish this. I just can't really think of a scenario when this is the better way to go.
My guess is that it might have something to do with performance but I can't find any sources that can proof this.
Am I missing something or is TD-learning generally the better option?
Answer: The main problem with TD learning and DP is that their step updates are biased on the initial conditions of the learning parameters. The bootstrapping process typically updates a function or lookup Q(s,a) on a successor value Q(s',a') using whatever the current estimates are in the latter. Clearly at the very start of learning these estimates contain no information from any real rewards or state transitions.
If learning works as intended, then the bias will reduce asymptotically over multiple iterations. However, the bias can cause significant problems, especially for off-policy methods (e.g. Q Learning) and when using function approximators. That combination is so likely to fail to converge that it is called the deadly triad in Sutton & Barto.
Monte Carlo control methods do not suffer from this bias, as each update is made using a true sample of what Q(s,a) should be. However, Monte Carlo methods can suffer from high variance, which means more samples are required to achieve the same degree of learning compared to TD.
In practice, TD learning appears to learn more efficiently if the problems with the deadly triad can be overcome. Recent results using experience replay and staged "frozen" copies of estimators provide work-arounds that address problems - e.g. that is how DQN learner for Atari games was built.
There is also a middle ground between TD and Monte Carlo. It is possible to construct a generalised method that combines trajectories of different lengths - from single-step TD to complete episode runs in Monte Carlo - and combine them. The most common variant of this is TD($\lambda$) learning, where $\lambda$ is a parameter from $0$ (effectively single-step TD learning) to $1$ (effectively Monte Carlo learning, but with a nice feature that it can be used in continuous problems). Typically, a value between $0$ and $1$ makes the most efficient learning agent - although like many hyperparameters, the best value to use depends on the problem.
If you are using a value-based method (as opposed to a policy-based one), then TD learning is generally used more in practice, or a TD/MC combination method such as TD(λ) can be even better.
In terms of "practical advantage" for MC? Monte Carlo learning is conceptually simple, robust and easy to implement, albeit often slower than TD. I would generally not use it for a learning controller engine (unless in a hurry to implement something for a simple environment), but I would seriously consider it for policy evaluation in order to compare multiple agents for instance - that is due to it being an unbiased measure, which is important for testing.
Comment: @Anne-dirk If you are using a value-based method (as opposed to a policy-based one), then TD learning *is* generally used more in practice, or a TD/MC combination method such as TD(λ) can be even better.  I am not sure what you mean by "practical advantage"? Monte Carlo learning is conceptually simple, robust and easy to implement. I would generally not use it for a learning controller engine (unless in a hurry to implement something for a simple environment), but I would seriously consider it for policy evaluation in order to compare multiple agents for instance.
Comment: First off, thanks for the answer. I see how in theorie an unbiased algorithm could be preferred over a biased one. But considering the high variance Monte Carlo can give at the start of training, I don't see how this really matters.
Both Monte Carlo and TD will start with inaccurate approximations and from what I've read, TD will converge much faster.
I just can't really come up with a practical advantage of using Monte Carlo. (Amusing the _deadly triad_ can be avoided)
Comment: @Neul Slater Aaaah I see... That's the kind of answer I was looking for :) Thanks for your help!
Answer: Essentially it depends on your environment.
TD exploits the Markov property, i.e. the future states of a process rely only upon the current state, and therefore it's usually more efficient to use TD in Markov environments.
MC does not exploit the Markov property as it bases rewards on the entire learning process,  which lends itself to non-Markov environments.
Comment: To be clear, I was referring to efficiency. If you can exploit the Markov property then TD is advantageous because you can start in any given state, take and action and the result will always be the same, so you can compute the TD error with high levels of certainty.

With non-MDP if you get a state that is partially observed then TD may not be very efficient.

That's not to say you can't use TD in non-MDPs, you can, but it might be inefficient and may get better success with TD lambda rather than TD(1).
Comment: It literally has everything to do with it being Markov; if you're in a Markov environment then once you establish taking action a in state a it will lead will state a' with reward x - that will *always* be the case in a markov environment, so you don't need to evaluate it over and over - you can take bigger steps and TD enables you to exploit that.  But it won't be the case in a POMDP because you can have exactly the same state, take same action, but end up in completely different states and rewards.
Comment: Yes!  MDPs *are* deterministic by definition. POMDPs are stochastic, that's exactly what I've been saying. A MDP is not stochastic - it literally cannot be deterministic and stochastic.  But look, if you want to take it up with David Silver, feel free to do so:

https://youtu.be/PnHCvfgC_ZA?t=3811

Burn!
Comment: Oh look, the baby has marked down my answer because he was shown to be wrong. Pathetic. I didn't say MC methods *don't* work in Markov environments! I was saying efficiency gains can be found by using the correct approach depending on the environment.  See, you're now even needing to make things up, and you're coming across as quite pedantic now.
Comment: And no, an MDP is not "just a way of representing the environment and its dynamics", it literally means that if you are state S then all events that occurred before are defined by that state, and if you take action a, then you will always get the same State S' and reward r.  That is literally the definition of an MPD, and why it is deterministic. That's why a MDP can be guaranteed to converge to an optimal policy.  I get the impression you don't know what you're talking about.
Comment: Which it does. That's the definition of an MDP. If you're in a state, take the same action but end up in different states and get different rewards then it ceases to be an MDP.  It may be some other form of an MDP, but it ceases to be a true MDP.
Comment: That's *precisely* why using TD in markov environments is more efficient; because TD exploits the Markov property. In a POMDP you cannot rely on the state you have because in all likelihood it's aliased with other states, which means if you try to use TD, certainly over more than single steps, then you get more and more variance which makes it more stochastic.
Comment:  Exactly, which is why you may not get good results using TD, and MC may be a better option, which is what I've been saying from the outset...


Can not disable ViewState

Question: I have made a simple Web Form where I try to get rid of the ViewState. When I run it and press Go the label gets the value of whatever I put in the texbox. So far all good. But the textbox maintains the value I filled in. As far as I understand it this is done by the ViewState mechanism. What do I miss?
<%@ Page Language="C#" AutoEventWireup="true" Inherits="SportsPlay.Sida1" ViewStateMode="Disabled" EnableViewState="false" %>
<!DOCTYPE html>

    <title></title>

    <form id="form1" runat="server" enableviewstate="false">

            <asp:TextBox runat="server" ID="txtTest" ></asp:TextBox>
            <asp:Button runat="server" ID="btnGo" Text="Go" OnCommand="btnGo_Command" />
            <asp:Label runat="server" ID="lblResult"></asp:Label>

    </form>

Answer: The ViewState manages the information of the current page. It is utilized by the HTML pages by ASP.NET applications to maintain the state of the web form controls. By this you can save a lot of coding by maintaining the ViewState of the objects in your Web Form.
You do not need ViewState in the following situations:

The control is repopulated on every postback. If you ignore old data, and if you repopulate the server control each time the page is refreshed then you do not need ViewState.
When you complete a web page, review the controls in the page and consider what information is being passed in the view state and whether you really need all that information to be maintained. To optimize web page size, consider disabling view state in these cases.
The control is an input control and it changes only from user actions.
The control never changes.

By default, ViewState is enabled for all server controls. ViewState can be enabled and disabled in any of the following ways:

Control Level
Page Level
Application Level
Machine Level

To disable ViewState
To disable ViewState for a single control on a page, set the EnableViewState property of the control to false, as in the following:
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="false" />

To disable ViewState for a page
To disable ViewState for a single page, set the EnableViewState attribute in the @ Page directive to false, as in the following:
<%@ Page Language="C#" EnableViewState="false" AutoEventWireup="true" CodeFile="URLRouting.aspx.cs" Inherits="URL_Rewriting" %>

To disable a page's View State, add the code below in the Page class of the page.
public void DisableViewState()
{
    this.Init += new EventHandler(Page_Init);
}

private void Page_Init(object sender, System.EventArgs e)
{
    this.EnableViewState = false;
}

To disable ViewState for a specific application
To disable ViewState for a specific application, use the following element in the Web.config file of the application:
<configuration>
  <system.web>
    <pages enableViewState="false" />
  </system.web>
</configuration>

To disable ViewState for all applications on a Web server
To disable ViewState for all applications on a Web server, configure the  element in the Machine.config file as follows:
<Machine.config >
    <system.web>
       <pages enableViewState="true" />
    </system.web>
 </Machine.config>

Comment: So why does the textbox show the text after postback? I have tried most of what you suggest with the same result.
Answer: Aside from the previous answer you can explicitly clear it on click TextBox1.Text = ""; Or Redirect to the same page should clear everything out.
Comment: But why is the textbox filled after postback?
Comment: because it looks like you are setting it on click/postback:  lblResult.Text =txtTest.Text ;
Comment: I am setting the label (lblResult), not the textbox (txtTest). I set the label just to confirm that a postback has occurred. What I understand when I read up on ViewState is that the textbox should not maintain its value when EnableViewState="false". I have tried to disable it in the textbox, in the form and on the project level too with the same result.
Comment: https://codewala.net/2011/02/07/why-textbox-persists-data-during-postback-even-if-view-state-set-to-off/

Can not disable ViewState


Problem regarding visualizing high dimensional data using PCA

Question: I have 10000 samples, each of which has 100 features. To visualize this high dimensional sample, I use 2 component PCA. Here is a the result:

Here, results are colored based on a target value for each sample. Then, I tried to draw a new sample, and plot it on top of the previous visualization using
<code>from sklearn.decomposition import PCA

pca = PCA(n_components=2)
feature_space_2d = pca.fit_transform(np.vstack((sample, new_draw)))
</code>
When I tried to compare these two, I realized that the visualization of original 10000 samples have essentially been rotated . You can see newly drawn sample at top left (please discard other three black points).

I understand that adding a point would change the PCA output, but it seems so drastically to me. Would some one be able to shed some light on it?
Is there a way to apply the same PCA transform as we did on the original samples to any drawn samples?

Thanks!
Comment: I'm not sure I understand your plots correctly. What does the color represent? A continuous value, and you are trying to solve a regression problem?
Comment: no, Its a training set, and I want to visualize it. I have 10000 training data, each has 100 dimension, and we have a target value. Eventually I will use it for something but my question at this point only deals with the training set itself. So, colors just represent the target value.
Answer: Is it rotated or flipped (like in a mirror)? The latter happens when you use eigen analysis to do PCA. You're essentially plotting the eigenvectors, and they are invariant to the sign. The sign can flip arbitrarily when you run eigenvalue routines, and it doesn't matter but can produce "flipped" plots
Here's the horizontally flipped second image, compare it to the first picture:
Comment: You mean statsmodels PCA? I think it uses SVD inside, and does happen to have this "issue." I don't know if it's possible to do through API. I simply standardize the sign. For your plotting you could simply use the sign of the first element of the PCA component to normalize the signs in two plots. Look at your plots, they seem to be flipped over the horizontal axis.
Comment: Teşekkürler arkadaşım! Do you happen to know how to prevent this in python?


Interpretation of strings from PHP's serialization

Question: Thanks to Stack Overflow, I just knew what serialize() and unserialize() does in PHP.
I came across this code example in PHP.net Manual:
class obj implements Serializable {
 private $data;
 public function __construct() {
    $this->data = "My private data";
  }
 public function serialize() {
    return serialize($this->data);
 }
 public function unserialize($data) {
    $this->data = unserialize($data);
 }
 public function getData() {
    return $this->data;
 }
}

$obj = new obj;
$ser = serialize($obj);

var_dump($ser);

$newobj = unserialize($ser);

var_dump($newobj->getData());

The code produces the below result:
string(38) "C:3:"obj":23:{s:15:"My private data";}"
string(15) "My private data"

I want to know what "C" means in this string and why it equals to "3" and "obj" and why it equals to "23."
I know "s" means string and it contains 15 characters, but I can't understand C and obj and their results.
Answer: PHP's serialisation format is based a lot on declaring what something is and then declaring how many of the following characters belong to it. So C means a class, 3 means that the following three characters will tell you what class it is, obj is those three characters telling you the class name, 23 tells you that the following 23 characters describe the object's data, and the rest is self explanatory.
Comment: Specifically, `C` means it is a class that implements the `Serializable` interface.  Other classes will be serialized with `O` instead.
Answer: C = class
3 - "obj" has 3 characters
23 - because {s:15:"My private data";} has 23 characters


Magento changing the language selector

Question: In my project I have to display the webshop in two languages.
By default you can choose languages with the following code in:
app/design/frontend/base/default/template/page/switch/language.phtml
<?php if(count($this->getStores())>1): ?>

<?php echo $this->__('Your Language:') ?>
<select id="select-language" title="<?php echo $this->__('Your Language') ?>" onchange="window.location.href=this.value">
<?php foreach ($this->getStores() as $_lang): ?>
    <?php $_selected = ($_lang->getId() == $this->getCurrentStoreId()) ? ' selected="selected"' : '' ?>
    <option value="<?php echo $_lang->getCurrentUrl() ?>"<?php echo $_selected ?>><?php      echo $this->htmlEscape($_lang->getName()) ?></option>
<?php endforeach; ?>
</select>

<?php endif; ?>

This of course displaya a select box with the options being all the languages.
I however want to change this so that it become seperate links.
I just don't really know how to do this.
This is what I have now.
<?php if(count($this->getStores())>1): ?>

<?php foreach ($this->getStores() as $_lang):?>
    <?php echo $this->htmlEscape($_lang->getName()) ?>
<?php endforeach;?>

<?php endif; ?>

PS: I did not change this in the default magento code. I am working in app/design/frontend/default/projectname/template/page/switch/language.phtml.
So I managed to get this working by myself with this code:
<?php if(count($this->getStores())>1): ?>

<?php foreach ($this->getStores() as $_lang):?>
    <a href="<?php echo Mage::getUrl() . '?___store=' . $_lang->getId()?>" title=""><?php echo $this->htmlEscape($_lang->getName()) ?>
<?php endforeach;?>

<?php endif; ?>

But now when I switch language. It redirects to the home page.
I found I should use:
$_lang->getCurrentUrl()

But I have no idea where to place this in my code.
Comment: In my version of magento (newest Enterprise) it already is a list with separate links. I'm not sure from which version this changed, maybe you could take a newer magento version?
Comment: Hmm i have the newest..
Weird :P
Answer: You're very close, you just need to include the URL!
<?php if(count($this->getStores())>1): ?>

<?php foreach ($this->getStores() as $_lang):?>
    <a href="<?php echo $_lang->getCurrentUrl() ?>" title="<?php echo $this->htmlEscape($_lang->getName()) ?>"><?php echo $this->htmlEscape($_lang->getName()) ?>
<?php endforeach;?>

<?php endif; ?>

Comment: Ah thank you :)
Somehow i thought that i needed the Mage::getUrl() and i didn't even think about what it did..
Answer: This seems to be different in Enterprise compared to Community. This is what the code is like in Magento Enterprise v1.12. Maybe it is of use, or maybe it works as well.
<?php if(count($this->getStores())>1): ?>

    <?php echo $this->__('Language') ?>:

        <strong class="current language-<?php echo $this->htmlEscape(Mage::app()->getStore()->getCode()) ?>">
            <?php echo $this->htmlEscape(Mage::app()->getStore()->getName()) ?>

        (<?php echo $this->__('Change')?>)

            <li class="current language-<?php echo $this->htmlEscape(Mage::app()->getStore()->getCode()) ?>">
                <?php echo $this->htmlEscape(Mage::app()->getStore()->getName()) ?>

            <?php foreach ($this->getStores() as $_lang): ?>
                <?php if($_lang->getId()!=$this->getCurrentStoreId()): ?>
                    <li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
                        <a href="<?php echo $_lang->getCurrentUrl() ?>"><?php echo $this->htmlEscape($_lang->getName()) ?>

                <?php endif; ?>
            <?php endforeach; ?>

<?php endif; ?>

Comment: Ah this might help people who use Enterprise who are looking for the same thing i am :)
Answer: Try this code. I tested in EE 1.12.
<?php if(count($this->getStores())>1): ?>

    <?php foreach ($this->getStores() as $_lang): ?>
      <?php if($_lang->getId()!=$this->getCurrentStoreId()): ?>
        <li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
        <a href="<?php echo $_lang->getCurrentUrl() ?>"><?php echo $this->htmlEscape($_lang->getName()) ?>

      <?php endif; ?>
     <?php endforeach; ?>

    <?php endif; ?>

Magento changing the language selector


Module not appearing in list after pushing to Acquia Cloud

Question: I have added a new module in my local code repository. After adding, I committed and pushed it to my remote repository on Acquia Cloud, but it is still not showing up in the modules list.
Can anyone suggest why this is happening?
Comment: Have you tried re-deploying from your branch on Acquia cloud? Also, have you checked you are deployed form the _correct_ branch?
Answer: Unless it's automated, you'll also need to pull to code from the remote repository onto your web server.


How to enable IAP for k8s on GCP?

Question: Aim: to enable Identity Aware Proxy (IAP) in conjunction with Kubernetes (k8s).
Methods:

Two apps were deployed
A SSL load balancer was put in front
When one navigates to domain/app1, app1 is shown
This tutorial was followed to enable IAP for k8s https://cloud.google.com/iap/docs/enabling-kubernetes-howto

Results

The Google login screen appears when one navigates to domain/app1
When the login succeeds a 'default gateway - 404' is shown instead of the app when IAP is disabled

Discussion

Should a redirect not be configured somewhere? When the authentication and authorization succeeds a URI _gcp_gatekeeper/authenticate was added and a 404 was returned, while a redirect to the app should be done right?
When an App engine is deployed, IAP is enable the IAP works out of the box. What makes this deploy different? Perhaps App Engine contains some elements that are omitted in k8s.

Current problem
When the authentication succeeds the following error is shown:
There was a problem with your request. Error code 11

Comment: Actually you can now do this natively through Ingress!
https://cloud.google.com/iap/docs/enabling-kubernetes-howto
Comment: Can you add the configuration yaml files to your question? It could help to reproduce your case and hopefully find a solution.
Comment: Did you find a solution for this use case? I'm also having trouble configuring multiple IAP-protected backends under non-default paths.
Answer: To begin with,  here's some general information:
To get started with IAP in GCP, add an App Engine app or configure Cloud Load Balancer for IAP. In case you are running Kubernetes cluster, you may have the Load Balancer configured already.
Then you should enable IAP for it here: Menu -> Security -> Identity-Aware Proxy
And finally, that is the place where redirect URI can be configured. You can get there from previous step by selecting triple dot on the right side of your App/LB and choosing Edit OAuth Client.
Menu -> APIs & Services -> Credentials

Create OAuth clientID and set Authorized redirect URIs for it.

Authorized redirect URIs
  For use with requests from a web server. This
  is the path in your application that users are redirected to after
  they have authenticated with Google. The path will be appended with
  the authorization code for access. Must have a protocol. Cannot
  contain URL fragments or relative paths. Cannot be a public IP
  address.

For App Engine apps this value is predefined, but you can adjust it according to your needs.
Comment: It should be `https:///_gcp_gatekeeper/authenticate` right? This setting does not redirect to the app, but is required to authenticate or is this incorrect? At the moment a 404 - default gateway is shown. When IAP is disabled the app appears again. What is missing?

How to enable IAP for k8s on GCP?


Filling gaps in "a proof" of Fourier inversion formula

Question: Suppose $f\in L^1(\mathbb{R})$. Define the Fourier transform of $f$ as:
$$\hat{f}:\mathbb{R}\rightarrow\mathbb{C}, \xi\mapsto\int_{\mathbb{R}}f(t)e^{-2\pi i\xi t}\operatorname{d}t.$$
Suppose that $\hat f\in L^1(\mathbb{R})$.
Under what further hypothesis on $f$ and $\hat{f}$can we make rigorous the following argument:
$$\forall x\in\mathbb{R}, \int_\mathbb{R} \hat f(\xi)e^{2\pi i\xi x}\operatorname{d}\xi \overset{(1)}{=} \lim_{\Delta L\rightarrow0^+} \sum_{n=-\infty}^{+\infty} \left(\hat{f}(n\Delta L)e^{2\pi \Delta L in x}\Delta L\right) \\ = \lim_{\Delta L\rightarrow0^+} \sum_{n=-\infty}^{+\infty} \left(\left(\int_{\mathbb{R}}f(t)e^{-2\pi\Delta L i n  t}\operatorname{d}t\right)e^{2\pi in \Delta Lx}\Delta L\right) \\ = \lim_{\Delta L\rightarrow0^+} \sum_{n=-\infty}^{+\infty} \left(\int_{\mathbb{R}}f(t)e^{2\pi\Delta L i n  (x-t)}\operatorname{d}t \Delta L\right) \\ = \lim_{\Delta L\rightarrow0^+} \sum_{n=-\infty}^{+\infty} \left(\sum_{k=-\infty}^{+\infty}\int_{\frac{1}{\Delta L}[k,k+1]}f(t)e^{2\pi\Delta L i n  (x-t)}\operatorname{d}t \Delta L\right) \\ = \lim_{\Delta L\rightarrow0^+} \sum_{n=-\infty}^{+\infty} \left(\sum_{k=-\infty}^{+\infty}\int_{\frac{1}{\Delta L}[k,k+1]}f(t)e^{2\pi\Delta L i n  \left((x+\frac{k}{\Delta L})-t\right)}\operatorname{d}t \Delta L\right) \\ \overset{(2)}{=} \lim_{\Delta L\rightarrow0^+} \sum_{k=-\infty}^{+\infty} \left(\sum_{n=-\infty}^{+\infty}\int_{\frac{1}{\Delta L}[k,k+1]}f(t)e^{2\pi\Delta L i n  \left((x+\frac{k}{\Delta L})-t\right)}\operatorname{d}t \Delta L\right) \\ = \lim_{\Delta L\rightarrow0^+} \sum_{k=-\infty}^{+\infty} \left(\sum_{n=-\infty}^{+\infty} \left(\frac{1}{\frac{1}{\Delta L}}\int_{\frac{1}{\Delta L}[k,k+1]}f(t)e^{-2\pi\Delta L i n  t}\operatorname{d}t\right)e^{2\pi \Delta L in(x+\frac{k}{\Delta L})}\right) \\ \overset{(\star)}{=} \lim_{\Delta L\rightarrow0^+} \sum_{k=-\infty}^{+\infty} f\left(x+\frac{k}{\Delta L}\right)\overset{(3)}{=}f(x) ?$$
The idea is to prove Fourier inversion formula in $\mathbb{R}$ using the Fourier inversion formula on the torus, that we used in $(\star)$.
In particular, while it seems that we can take care of $(1)$ and $(3)$ imposing some decreasing condition at $\infty$ on $f$ and $\hat{f}$, I'm having hard times in finding hypothesis on $f$ and $\hat{f}$ for which $(2)$ holds, neither I have an idea on what technique I could use to prove it.
Comment: An application of Fubini's theorem
Comment: @timur yes, and how do you prove that the hypothesis of Fubini are satisfied?
Comment: I know, if I had an answer it would not have been a comment :)
Answer: The answer provided by David C. Ulrich in this post can be adapted to answer positively this question under the hypothesis in there.


Is there a way to see when the Azure Functions Runtime was updated in Linux?

Question: For an Azure Function, how can I tell when an Azure App Service Plan's FUNCTION_EXTENSION_VERSION was updated on a Linux server?
Note this is in relation to the same exact question answered for Windows.
Comment: I checked all the log files in the physical path, and also used the monitor to monitor the behavior of the function app, but none of this information was recorded. For Linux-based function apps, you may not be able to do it, because this information is not recorded...
Answer:
For an Azure Function, how can I tell when an Azure App Service Plan's
FUNCTION_EXTENSION_VERSION was updated on a Linux server?

Linux azure function is different from windows azure function. Their design methods are different. I first tried to use ftp and connected to the physical path to view the log information recorded therein. After manually changing the runtime version, I checked all the log files, but none of them stored relevant information. Then I used Azure Monitor to monitor the behavior of the function app, and there was still no record. I think for the linux-based function app, your needs cannot be fulfilled temporarily, because the function app itself does not record it as information at all...