Hydra - Cannot override value in instantiate - fb-hydra

I am developing a project based on the pytorch lightning + hydra template found here https://github.com/ashleve/lightning-hydra-template. I am trying to instantiate a Pytorch dataset object using hydra instantiate, overriding the default cfg value with the DictConfig object. Specifically, I have this config file:
...
training_dataset:
_target_: src.datamodules.components.baseline_datasets.FSD50K
cfg: omegaconf.dictconfig.DictConfig(content={})
mode: "training"
...
While the pytorch lightning datamodule does the following:
class AudioTagDataModule(LightningDataModule):
def __init__(self, cfg: DictConfig):
super().__init__()
self.cfg = cfg
self.save_hyperparameters(logger=False)
def setup(self, stage: Optional[str] = None):
self.trainset = instantiate(self.cfg.datamodule.training_dataset, cfg=self.cfg)
...
The rest of the code is pretty much unmodified from the template. However, when the pytorch dataset is instantiated, I get an error due to the config being empty. Checking in debug, I see that the config value is not being overridden, despite having the same name that was specified in the configs. Could someone provide some guidance on why the override is not working and how to correctly proceed? Thank!

Looking at the AudioTagDataModule, I see that the setup method passes a cfg=self.cfg keyword argument to the instantiate function. This is why the cfg setting from your training_dataset config is not showing up in your instantiated dataset; the keyword argument is taking precedence. Based on the code you posted, it would probably make sense to pass an empty DictConfig to AudioTagDataModule.__init__ instead of defining a cfg key/value in training_dataset.
Another thing: in your yaml file, you'd probably want cfg: {} instead of cfg: omegaconf.dictconfig.DictConfig(content={}), as the latter will result in the python string "omegaconf.dictconfig.DictConfig(content={})" and the former will result in an empty DictConfig.

Related

Can't infer proper type myself in Kotlin when using JavaFX

I just started to study Kotlin and JavaFX by following tutorial.
I could see blank JavaFX windows, and I proceed next step that using FXML.
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Scene
import javafx.stage.Stage
class AppMain : Application() {
override fun start(primaryStage: Stage) {
primaryStage.title = "Try JavaFX"
val fxml = javaClass.getResource("fxml/Main.fxml")
val root = FXMLLoader.load(fxml) // ERRORS here! `load`
val scene = Scene(root)
primaryStage.scene = scene
primaryStage.show()
}
}
However, I couldn't figure out that how to avoid the type inferencing error like:
Error:(12, 31) Kotlin: Type inference failed: Not enough information to infer parameter T in fun <T : Any!> load(p0: URL!): T! Please specify it explicitly.
From the message, I understand that I have to write the type of variable fxml explicitly.
But I have no idea that what type should be labeled to fxml.
I tried to read the document about JavaFX, but I couldn't figure it out.(I'm not familiar with Java and Kotlin)
I tried to type like URL but it does not make sense.
Many JavaFX & Kotlin example codes that I could find from google does not seems to have the problem like this. (Are the example codes written in previous version?)
What type should I put for the variable?
Or did I miss something other?
Environment and Codes
Environment
JDK 11
JavaFX 11
Kotlin 1.2.71
My complete trial code
https://github.com/QuietJoon/StudyKotlin-JavaFX/tree/fxml
The problem isn't the parameter to the FXMLLoader.load function (which is a java.net.URL object, as returned by javaClass.getResource). It's that this function returns a generic type:
public static <T> T load(URL location)
The Kotlin compiler needs to know what type your root variable will be (as you've not explicitly defined it), but it can't know that as there's nothing in the code that will allow it to infer this.
A quick Google returned this example which has this code in it (in Java):
Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));
As you can see here, the root variable is of type Parent. So what you need to do is provide this type (i.e. what you expect the load function to return) in some way. Here are two different ways you could do this:
Specify the type explicitly when declaring the variable: val root: Parent = FXMLLoader.load(fxml)
Specify the generic type when calling the method: val root = FXMLLoader.load<Parent>(fxml)
Note also that in your build.gradle file in your github repo there's a mistake that means the code didn't immediately compile when I fetched it:
compile "org.openjfx.javafx.fxml:11:$platform" should be compile "org.openjfx:javafx-fxml:11:$platform" (one of the dots should be a colon).

Getting missing positional argument error when trying to define middleware in Django

I am trying to come up with my first middleware of my own in a Django 1.10 project. And currently running into the following error
TypeError: init() missing 1 required positional argument:
'get_response'
I have defined the middleware in a middleware.py like this:
from zeon.utils import RequestLogThread
class StartRestEndPointMiddleWare(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
request.request_log_id = RequestLogThread('send_contact', request.data).start()
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
I am lost trying to figure out what's wrong, since everything seems to be in accordance with the doc. I would really appreciate any hint.
UPDATE:
I place it into middle_classes:
MIDDLEWARE_CLASSES = [
'zeon.middleware.StartRestEndPointMiddleWare',
]
Django changed the way middleware classes worked in 1.10. New-style middleware classes belong in the MIDDLEWARE list, like this:
MIDDLEWARE = [
'zeon.middleware.StartRestEndPointMiddleWare',
]
When you put your middleware into MIDDLEWARE_CLASSES, it's being treated as an old-style middleware, which works differently. A better error message might have made this more clear.

pyramid.util.Request source code

I'm new to Python and I am studying Pyramid to learn how to write good code. I wrote this root factory code:
class RootFactory(object):
def __acl__(self):
return [
(Allow, 'group:alfa', 'alfa'),
]
def __init__(self, request):
print(type(request))
pass
and the result shows that "request" is <class 'pyramid.util.Request'> but if I open the module pyramid.util.py, to see the source code, I do not found the class Request. I think that this class is in the module pyramid.request.py
Could someone explain how this is working?
It's an implementation detail of using config.add_request_method. When adding request methods, a new class object is created using the InstancePropertyHelper which is located in the pyramid.util module. This could probably be fixed, but at the end of the day the actual class we care about is pyramid.request.Request so you should look there for issues.

How to make Flow understand code written for Node.js?

I'm just getting started with Flow, trying to introduce it into an existing Node codebase.
Here are two lines Flow complains about:
import Module from 'module';
const nodeVersion = Number(process.versions.node.split('.')[0]);
The warnings about these lines are, respectively:
module. Required module not found
call of method `split`. Method cannot be called on possibly null value
So it seems like Flow isn't aware of some things that are standard in a Node environment (e.g. process.versions.node is guaranteed to be a string, and there is definitely a Node builtin called module).
But then again, Flow's configuration docs suggest it's Node-aware by default. And I have plenty of other stuff like import fs from 'fs'; which does not cause any warning. So what am I doing wrong?
Module fs works as expected because Flow comes with built-in definitions for it, see declare module "fs" here: https://github.com/facebook/flow/blob/master/lib/node.js#L624
Regarding process.versions.node, you can see in the same file that the versions key is typed as a map of nullable strings, with no mention of the specific node property: versions : { [key: string] : ?string };. So you'll need to either make a PR to improve this definition, or adjust your code for the possibility of that value being null.
I guess the answer about module "module" is obvious now – there are no built-in definitions for that module in Flow in lib/node.js. You could write your own definitions, and optionally send a PR with them to the Flow team. You can also try searching github for these, someone might have done the work already.
That lib directory is very useful by the way, it has Flow definitions for DOM and other stuff as well.

What does a bare line on its own do?

In a .sbt file, I often have copy-pasted lines from readmes, of which I have no idea what I'm actually doing. An example is, after adding sbt-revolver to plugins.sbt, writing the line
Revolver.settings
My current understanding of what this does is magically adding re-start and re-stop commands to sbt. I have been led to understand that a line in an .sbt file does not, in fact, perform magic, but rather creates a key and associates a value with it.
What keys does such a line set, and to what value? What would be the equivalent statement in a .scala build definition?
*.sbt files can take bare DslEntry which include Setting[T] and Seq[Setting[T]].
An expression like someString := "a" or someSeq += "b" is a Setting for a specific T type.
These settings are values though, they define transformation (change, add, append, etc) of different parts of the build, which get folded into the build state and structure.
In your example Revolver.settings is Seq[Setting[_]] which defines default setup of using sbt-revolver.
If setting it up in a project/*.scala you need to assign it to the root project, which is either:
the sole project in your build
the project that aggregates all other (sub) projects.
Therefore it would look something like:
import sbt._, Keys._
object Build extends Build {
val bippy = project in file(".") settings Revolver.settings
}

Resources