sentinel-hub/eo-learn

[BUG] ImportError: cannot import name 'PointSamplingTask' from 'eolearn.geometry'

peanutbutter-memory opened this issue · 5 comments

Describe the bug

Attempting to run https://github.com/sentinel-hub/eo-learn-examples/custom-script/machine-learning-evalscript.ipynb.

Encounter ImportError: cannot import name PointSamplingTask from eolearn.geometry.

To Reproduce

Steps to reproduce the behavior:

  1. pip install eo-learn
  2. git clone eo-learn-examples
  3. open machine-learning-evalscript.ipynb and run first code block

Expected behavior

Expecting all imports to be found.

Environment

Running WSL2 (Ubuntu 20.04) on Windows 10. Python v3.8.10 in venv.

eo-learn is v1.2.1.

Additional context

Cloned eo-learn, opened git repo viewer looking for changes to PointSamplingTask() as it is no longer in eo-learn.geometry. Only entry is from 2018-09-07. Stumped as I can not trace history of PointSamplingTask().
image

Closest I can find is eo-learn.ml_tools.sampling::sample_by_values() but the signature is not identical to ipynb code.

Would love to update the ipynb for the Machine Learning (ML) custom-script for water detection.

Thanks so much:)

The examples in the https://github.com/sentinel-hub/eo-learn-examples are outdated.

The PointSamplingTask was removed in eo-learn 1.0, where it was replaced with FractionSamplingTask (controls sampling via mask) and BlockSamplingTask (can also sample larger blocks from array). From a quick glance FractionSamplingTask is what you're looking for, though the interface is slightly different.

Thanks:)

I updated signature to below but am not be assigning correct labels back to EOPatches ... this is more my fault at this point as I am not very familiar with the library and plan to dig into basics first.

Just wondering if eo-learn-examples will be updated in future?

sampling_task = FractionSamplingTask(
    fraction=0.10,
    sampling_feature=(FeatureType.MASK_TIMELESS, LABELS[1]),
    features_to_sample=[(FeatureType.DATA, "FEATURES", "FEATURES_SAMPLED")]
)
batic commented

Just wondering if eo-learn-examples will be updated in future?

We have plenty of ideas, and even longer wishlist, but not enough time for all of them... I hope that yes, we will update the examples repo, but until then, we'd appreciate help. Pull requests welcome!

I tried running the notebook to see how to aid you. Since there is no inherent filtering done in sampling, it is better to use BlockSamplingTask (otherwise some things break further in the notebook since not all eopatches have the same number of samples)

sampling_task = BlockSamplingTask(
    amount=100,
    features_to_sample=[
        (FeatureType.DATA, "FEATURES", "FEATURES_SAMPLED"),
        (FeatureType.MASK, "IS_DATA", "IS_DATA_SAMPLED"),
        (FeatureType.MASK_TIMELESS, "water_label", "water_label_SAMPLED"),
    ],  # tag fields to sample
)

The new tasks don't rename automatically so I had to pass them renaming schemas.

The next cell is also outdated, but I tried fixing it with minimal adjustments. It requires you to load linearly_connect_tasks and EOWorkflow.

# Define the workflow
eonodes = linearly_connect_tasks(load_task, extract_task, ndwi_task, ndmi_task, merge_task, sampling_task, save_task)
load_node, _, _, _, _, sampling_node, save_node = eonodes
workflow = EOWorkflow(eonodes)

We need the nodes because execution arguments are now linked to nodes, not tasks. Also since the EOPatches are old, you'll get a ton of warnings, but it's safe to ignore them.

%%time
import warnings

# Create list of arguments to be passed at run-time
execution_args = []
for idx, eop_name in enumerate(eopatch_names):
    execution_args.append(
            {load_node: {"eopatch_folder": eop_name}, sampling_node: {"seed": idx}, save_node: {"eopatch_folder": eop_name}}
        )

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    # Execute workflow in parallel, e.g. each EOPatch is process in parallel in dedicated processes
    executor = EOExecutor(workflow, execution_args, save_logs=True)
    executor.run(workers=N_WORKERS, multiprocess=True)

# Make report to check possible failures
executor.make_report()

And the rest worked for me.

Another note, sometime next week a new eo-learn version will release. If you wish to run this example stay on your current version, because the new one will no longer load the outdated eopatches used here.

Example has since been updated.