Closed YeewahChan closed 1 week ago
@microsoft-github-policy-service agree
Could you please fix the CI errors? @YeewahChan
Could you please fix the CI errors? @YeewahChan
The CI errors were due to pylint formatting issues, but I've fixed them now.
Thanks for your great efforts!
Title: Fix TSDataSampler Slicing Bug #1716
Description
I have optimized the mechanism for reading data from
data_arr
in the__getitem__
method:self.nan_idx
tolen(self.data_arr) - 1
instead of-1
.Motivation and Context
Related Issue Link The
__getitem__
function of TSDataSampler generally operates without issue, yet in certain scenarios, particularly whenstep_len
is significant andnan
must be added prior tostart
, it may inadvertently sample an empty list, which is erroneous. Consider the following code snippet as an example:The expected outcome should be an array with![Empty List Image](https://github.com/microsoft/qlib/assets/37462254/a2dfb40f-b79a-4ae1-b423-fd53ffa948a3)
nan
as the initial element followed by numerical values. However, the actual result is an empty list, as depicted in the following image:Mechanism of the Error
How did this issue arise? It stems from the fact that the
These
Therefore, the error occurs when there are several -1s at the beginning, causing the slice to become ![Error Image](https://github.com/microsoft/qlib/assets/37462254/279440b1-d120-449c-a12d-4c0ea7e48d1c)
dataframe
data has been stored in anndarray
(variable name:data_arr
) in the order of 'instrument, time', and thus a set ofindicies
is generated during the fetch. The logical error occurs whenstep_len
is large, and the count from the current processing position back tostart
requiresnan
supplementation (as shown in the following image):nan
values are then converted to -1 during subsequent processing (as shown in the following image) becauseself.nan_idx
is hardcoded as -1:[-1, index of the last value to be fetched]
. In cases requiring padding, this index is typically small, leading to the retrieval of an empty list, which is the error reported in the issue.My Modification Approach
Since the error always occurs at the beginning with
nan
, theindicies[0]
must beself.nan_idx
, which is hardcoded to-1
. For this scenario, it causes issues with the retrievedslices
. After examining, I found thatself.nan_idx
is only called once in this context. Therefore, following the original author's intention, I changedself.nan_idx
tolen(self.data_arr) - 1
. This solution not only addresses the problem but also avoids reducing code readability.How Has This Been Tested?
pytest qlib/tests/test_all_pipeline.py
under the upper directory ofqlib
.My test scripts are located in
tests/data_mid_layer_tests/test_dataset.py
.TestTSDataSampler
includes two test contents:nan
padding and is directly fetched, the test result should be the direct retrieval of the first value.Screenshots of Test Results (if appropriate):
Pipeline test:![Pipeline Test Image](https://github.com/microsoft/qlib/assets/37462254/e2ec295b-81ab-47d7-9184-ea4ebab16090)
Your own tests:![My Own Test Image](https://github.com/microsoft/qlib/assets/37462254/fb38736f-5cda-421e-a7ee-973f963820e7)
Types of changes