MonkeyPatch

What? How? When?

Current work 13.07.2022

Almut Lütge

Structure

Concept:  Definition

"Monkey patching is a technique to add, modify, or suppress the default behavior of a piece of code at runtime without changing its original source code."

Charlie Parker @ StackOverflow

Concept:  Objects in Python

Python treats everything as objects, including modules and classes:

Object attributes can be updated on runtime

class MyClass:
    a = 1
    b = '2'

    def get_value(self):
        return self.a
      
var1 = MyClass()

print(var1.get_value())
# 1

def get_new_value(cls):
    return cls.b
  
MyClass.get_value = get_new_value

print(var1.get_value())
# 2
      

Concept:  Modules in Python

Python treats everything as objects, including modules and classes:

## module.py

def print_variable(var):
    print(var)
      
## script.py

import module

var1 = 1

module.print_variable(var1)
# 1
def print_plus_one(var):
    print(var+1)

module.print_variable = print_plus_one
module.print_variable(var1)
# 2

Concept:  Modules in Python

## module.py

def print_variable(var):
    print(var)
      
## script.py
import module
import module2

var1 = 1
module.print_variable(var1)
# 1

def print_plus_one(var):
    print(var+1)

module.print_variable = print_plus_one
module.print_variable(var1)
# 2
module2.another_print(var1)
# 3
## module2.py
import module

def another_print(var):
    module.print_variable(var+1)
      

Examples:  Testing

Calling renku:

      - git commit

      - generate triplets and send them to the KG

      - ...

Testing the omnibenchmark python module:

    - test our code not renku-python

    - try code without changing KG triplets and git history

Example:  Testing

# Find dataset by match of property
def query_datasets_by_property(
    string: str,
    url: str = "https://renkulab.io/knowledge-graph/datasets?query=",
    property_name: str = "keywords",
) -> List[Mapping[Any, Any]]:
    all_data_json = query_datasets_by_string(string=string, url=url)
    matched_dataset: List = []
    for dataset in all_data_json:
        data_prop = dataset[property_name]
        if any(prop_val.lower() == string.lower() for prop_val in data_prop):
            matched_dataset.append(dataset)
    return matched_dataset

Example:  Testing

@pytest.fixture
def mock_dataset_json():
    return [
        {
            "_links": [{"rel": "details", "href": "https://this_is_a_mo.ck"}],
            "description": "this does not exoist in real",
            "identifier": "XXXXXX",
            "title": "A mock dataset",
            "name": "mock_dataset",
            "published": {
                "creator": [{"email": "mock.test@mail.com", "name": "th Mock"}]
            },
            "date": "2021-06-10T07:17:51.817557Z",
            "projectsCount": 0,
            "keywords": ["mock"],
            "images": [],
        }
    ]

Example:  Testing

def test_query_datasets_by_property(mock_dataset_json, monkeypatch):
    class MockResponse:
        @staticmethod
        def json():
            return mock_dataset_json

    def mock_get(*args, **kwargs):
        return MockResponse()

    monkeypatch.setattr(requests, "get", mock_get)

    res = data_commands.query_datasets_by_property("something")
    assert res == []

pitfalls

"As a general rule, the best is not to monkey patch."

Hard to understand code

Not reproducible?!

Unexpected behavior

"monkeypatching" in R

Dynamic programming language

utils package:
assignInNamespace
fixInNamespace
## Interactive ComplexHeatmaps vignette

#  To replace the internally use of pheatmap::pheatmap with 
#  ComplexHeatmap::pheatmap, 
#  we can use assignInNamespace() to directly change the value 
#  of pheatmap in pheatmap namespace. 

assignInNamespace("pheatmap", ComplexHeatmap::pheatmap, ns = "pheatmap")

Open Science:

the very idea - Frank Miedema, 2022

Current work 19.04.2023

Almut Lütge

Disclaimer

The author

Born: 1954 (age 69 years)

Education: University of Groningen

- PhD in Immunology

- Professor of Immunology of AIDS (University of Amsterdam)

- Head of the Immunology department at UMC Utrecht

- Initiator of "science in transition"

- Minor in the philosophy of science

Motivation

"I believe that the obsolete philosophy and public image of science are the major cause of many problems in the practice of science [...] I went all out to present the major different arguments for the lack of foundation for the methods of empirical positivism, and its analytical foundational philosophy. "

Relationship of Science and Society

Phase I (1945-1960):

autonomy, building on the successes of the natural sciences and engineering in World War II

Phase II (1960-1980):

government and the public lost trust and saw the downside of science and technology

Phase III (1980-2010):

science and technology bring economic growth, which should make nations internationally competitive, strong relations with government and the private sector --> knowledge economy

--> since 2010 growing frustration within science

Relationship of Science and Society

  • Science fails to contribute to the quality of life
  • robust and significant results are secondary in an internal credit system for academic career advancement at the individual level.
  •  focused on positions on international ranking lists which drive highly competitive social systems

Problems

"Republic of science"

Reasoning:

  • there is a scientific method to identify the "truth",
  • science itself is and should be value-free
  • to guarantee neutrality science need to be independent of politics and society

Social Contract for Science:

- science is governed by scientists

- science spends public money without influence by government.

"Republic of science"

Aim:

reveal  ‘a hidden reality for the sake of intellectual satisfaction’

Objectives:

“Guiding the progress of science into socially beneficent channels’ is ‘nonsensical’ and ‘guiding scientific research towards a purpose other than its own, will deflect the advancement of science’

Polanyi, 1962

The standard model and the legend

The scientist (Robert Merton, 1930-70):

  • scientists are altruistically looking for the truth
  • open community, characterized by skeptical debates
  • scientists are fair and honest
  • scientists are not driven by personal or intellectual interests

credit for work --> reputation --> competition/stratification

The "scientific method"

Hypotheses

Evidence

Knowledge

testing

falsification/support

derive

challenge

logical, empirical --> objective, value-free

natural science >> social science and humanities

The standard model and the legend

The Matthew effect

accumulative advantage of elites

Merton: scientific norms will prevent scientists from taking advantage

There is no one formal scientific method that leads us to the truth

There is no God-given or timeless, universal foundation for such a method to build on

Knowledge is arrived at, not by individuals in isolation ‘talking to nature’

There are many ways (methodologies) to do good research

Research is guided by our common cognitive and cultural values, when tested in experiments and discussions with peers constrained by natural and social reality

Knowledge is tested in interventions and (social) actions in practice

It is this communitive open, independent and transparent process that is unique to science which has produced knowledge which has been proven to be reliable over the past centuries.

What is the problem with an idealized model?

  • effect on scientific agenda setting at national and institutional level
  • causes the production of flawed results and science waste
  • is a major obstacle for translation of research to societal impact in real world

(justification for low work protection? limited inclusion? slow adaptation?)

What is the problem with an idealized model?

How does this relate to open science

"I believe that the obsolete philosophy and public image of science are the major cause of many problems in the practice of science [...]"

Obviously, a correct self-understanding of science is also of particular importance in debates where proper reflection on the status, the higher purpose and the position of science in society is required (Habermas, 1971)

--> Demystification of the legend

The standard model 1960 onwards

There is no one formal scientific method that leads us to the truth

There is no God-given or timeless, universal foundation for such a method to build on

Knowledge is arrived at, not by individuals in isolation ‘talking to nature’

There are many ways (methodologies) to do good research

In sharing ideas and experimental results and methods, for debate and scrutiny in a rigorous and communitive process by the community of inquirers

Inquiry is a social process producing reliable knowledge that produced objective (intersubjective) knowledge

Research is guided by our common cognitive and cultural values, when tested in experiments and discussions with peers constrained by natural and social reality

Knowledge is tested in interventions and (social) actions in practice

It is then either rejected, improved or it is accepted for the time being

Knowledge claims are fallible, absolute and always up to scrutiny and tests

It is this communitive open, independent and transparent process that is unique to science which has produced knowledge which has been proven to be reliable over the past centuries.

Images of science a reality check

"How this ‘logico-inductive’ metaphysics of Science can be correct, when few scientists are interested in (it) or understand it, and no one ever uses it explicitly in his work? But if Science is not distinguished from other intellectual disciplines neither by a particular style or argument nor by a definable subject matter, what is it? [..] The scientific enterprise is corporate. It is never one individual that goes through all steps of the logico-inductive chain; it is a group of individuals, dividing their labour but continuously and jealously checking each other’s contributions" Ziman, 1968

Because of enormous increase in scale, loss of social and ethical control, the system will increasingly face poor quality ‘shoddy’ research because of the lack of shared value of individual researchers with the scientific community. On the other hand, he is deeply concerned about the external influences on the research agenda by powerful private parties, multi-nationals, but also the military and governments. Ravetz, 1977

The pragmatist view on science

"Science does not rest upon solid bedrock. The bold structure of its theories rises, as it were, above a swamp. It is like a building erected on piles. The piles are driven down from above into the swamp, but not down any natural or ‘given’ base; and if we stop driv-ing the piles deeper, it is not because we have reached firm ground. We simply stop when we are satisfied that the piles are firm enough to carry the structure, at least for the time being" (Popper, 1959)

Minimal

By Almut Luetge

Minimal

  • 74